iUuax
iUuax

Reputation: 167

OpenGL blank screen?

I am learning OpenGL from book, I did exactly what is in the book, but when I run it (Eclipse C++ IDE) I get just blank screen. Book is "OpenGL guide for programmers". I think the error is in Reshape(), but code is from book.

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

Upvotes: 3

Views: 215

Answers (1)

Mykola
Mykola

Reputation: 3363

Yes reshape function is definitely wrong

use that

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

instead

Upvotes: 1

Related Questions