opengl只显示窗口,不显示图像,求大神帮忙!
感觉应该是投影和照相机参数设置不正确,但又不知道哪里不对,就一直卡在这儿了,求大家帮助...
// Experiment3.1.cpp: 定义控制台应用程序的入口点。
//
#include"stdafx.h"
#include<gl/glut.h>
#include<gl/gl.h>
#include <stdio.h>
#include<math.h>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include<iostream>
#include<Windows.h>
using namespace std;
void setLights() {
GLfloat light0_pos[] = { 0.0,5.0,0.0,1.0 }; //光源位置
GLfloat diffuse0[] = { 0.99,0.86,0.67,1.0 }; //土白色的漫反射光分量
GLfloat ambient0[] = { 0.99,0.86,0.67,1.0 }; //土白色的环境光分量
GLfloat specular0[] = { 1.0,1.0,1.0,1.0 }; //白色的镜面反射分量
glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular0);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
//绘制房间
void room() {
glColor3f(1.0, 0.0, 0.0);
//背面
glBindTexture(GL_TEXTURE_2D, back_wall);
glBegin(GL_POLYGON);
glVertex3f(-15.0, 20.0, -80.0);
glVertex3f(15.0, 20.0, -80.0);
glVertex3f(15.0, -20.0, -80.0);
glVertex3f(-15.0, -20.0, -80.0);
glEnd();
//屋顶
glBindTexture(GL_TEXTURE_2D, r);
glBegin(GL_POLYGON);
glVertex3f(-15.0, 20.0, -80.0);
glVertex3f(15.0, 20.0, -80.0);
glVertex3f(-15.0, 20.0, 10.0);
glVertex3f(15.0, 20.0, 10.0);
glEnd();
//地板
glBindTexture(GL_TEXTURE_2D, f);
glBegin(GL_POLYGON);
glVertex3f(15.0, -20.0, -80.0);
glVertex3f(-15.0, -20.0, -80.0);
glVertex3f(15.0, -20.0, 10.0);
glVertex3f(-15.0, -20.0, 10.0);
glEnd();
//墙壁
glBindTexture(GL_TEXTURE_2D, left_wall);
glBegin(GL_POLYGON);
glVertex3f(-15.0, 20.0, -80.0);
glVertex3f(-15.0, -20.0, -80.0);
glVertex3f(-15.0, -20.0, 10.0);
glVertex3f(-15.0, 20.0, 10.0);
glEnd();
glBindTexture(GL_TEXTURE_2D, right_wall);
glBegin(GL_POLYGON);
glVertex3f(15.0, 20.0, -80.0);
glVertex3f(15.0, -20.0, -80.0);
glVertex3f(15.0, -20.0, 10.0);
glVertex3f(15.0, 20.0, 10.0);
glEnd();
}
void myDisplay() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
//setLights();
room();
//在原点画一个立方体(测试效果)
glutSolidCube(1);
glutSwapBuffers();
glFlush();
}
void reshape(int width,int height) {
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void myInit() {
glClearColor(1.0, 0.0, 0.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.5, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(100, 50);
glutInitWindowSize(900, 600);
glutCreateWindow("Room");
glutSwapBuffers();
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
}
int main()
{
myInit();
glutDisplayFunc(myDisplay);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}