gpt4 book ai didi

c++ - OpenGL 黑屏?

转载 作者:太空狗 更新时间:2023-10-29 23:00:44 26 4
gpt4 key购买 nike

我正在从书中学习 OpenGL,我完全按照书中的内容进行操作,但是当我运行它时 (Eclipse C++ IDE),我得到的只是空白屏幕。本书是“OpenGL 程序员指南”。我认为错误在 Reshape() 中,但代码来自书本。

#include <iostream>
#include <GL/glew.h>
#include <GL/glut.h>

typedef const char* String;

String TITLE = "OpenGL";
int HEIGHT = 480;
int WIDTH = HEIGHT / 9 * 12;

void Update(int time){
glutPostRedisplay();
glutTimerFunc( 10, Update, 1);
}

void Init(){
glClearColor(1.0, 1.0, 1.0, 0.0);
glShadeModel(GL_FLAT);
}

void Render(){
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0, 1.0, 0.0);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glScalef(1.0, 2.0, 1.0); // Modeling transformation
glutWireCube(1.0);

glutSwapBuffers();
}

void Reshape(int w, int h){
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow(TITLE);

glewExperimental = true;
GLenum err = glewInit();
if(err!=GLEW_OK){
exit(1);
}

Init();
Update(1);
glutDisplayFunc(Render);
glutReshapeFunc(Reshape);

glutMainLoop();
return 0;
}

最佳答案

是的,reshape函数肯定是错误的

用那个

void Reshape(GLsizei w,GLsizei h)
{
glViewport(0,0,w,h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

if (h == 0)
h = 1;

GLfloat aspectRatio;
aspectRatio = static_cast<GLfloat>(w) / static_cast<GLfloat>(h);

gluPerspective(45.0,aspectRatio,1.0,4000.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

代替

关于c++ - OpenGL 黑屏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32936016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com