OpenGL空白屏幕

OpenGL blank screen?

本文关键字:屏幕 空白 OpenGL      更新时间:2023-10-16

我正在从书中学习OpenGL,我完全按照书中的内容做了,但是当我运行它(Eclipse c++ IDE)时,我得到的只是空白屏幕。这本书是"OpenGL程序员指南"。我认为错误是在重塑(),但代码是从书。

#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;
}

是,重塑函数肯定是错误的

使用

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();
}
相反,