opengl只显示窗口,不显示图像,求大神帮忙!

weixin_41504803 2018-05-05 07:00:19
感觉应该是投影和照相机参数设置不正确,但又不知道哪里不对,就一直卡在这儿了,求大家帮助...
// 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;
}

...全文
916 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_41504803 2018-05-06
  • 打赏
  • 举报
回复
实验需要做一个三维场景,周三就要交了,而我还卡在最开始的地方,求大家帮忙了

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧