Siddharth Taunk
Siddharth Taunk

Reputation: 165

OpenGL: Why this white scrreen

Why I am getting a full White colored window as an output of this program Expected a box

code is here

#include<Gl/glut.h>

static GLfloat vertices[] = {0.0, 0.0, 0.0,
                          0.5, 0.0, 0.0,
                          0.5, 0.5, 0.0,
                          0.0, 0.5, 0.0,
                          };

void reshape(int w, int h)
{
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 0.0);
}

void Draw()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0,1.0,1.0);

    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, 0);

    glBegin(GL_LINES);
        glArrayElement(0);
        glArrayElement(1);
        glArrayElement(2);
        glArrayElement(3);
    glEnd();
    glDisableClientState(GL_VERTEX_ARRAY);
    glFlush();
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
    glutInitWindowSize(400,400);
    glutInitWindowPosition(100,100);
    glutCreateWindow("vectors");

    glClearColor(0.0,0.0,0.0,0.0);

    glutReshapeFunc(reshape);
    glutDisplayFunc(Draw);
    glutMainLoop();
}

Corrected GL_LINES to GL_QUADS

Upvotes: 1

Views: 149

Answers (3)

genpfault
genpfault

Reputation: 52157

Multiple problems:

  • reshape() is broken; glOrtho() multiples by the current matrix and will give nonsensical results if you resize the window more than once.
  • You request a double-buffered (GLUT_DOUBLE) context but fail to swap the buffers. glFlush() is insufficient. Try glutSwapBuffers() instead.
  • You really ought to reset your projection/modelview matrices each frame. Helps prevent errors.

Give this a shot:

#include<Gl/glut.h>

static GLfloat vertices[] = 
{
    0.0, 0.0,
    0.5, 0.0,
    0.5, 0.5,
    0.0, 0.5,
};

void Draw()
{
    glClearColor(0.0,0.0,0.0,0.0);
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho( -2, 2, -2, 2, -1, 1 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    glColor3f(1.0,1.0,1.0);

    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer( 2, GL_FLOAT, 0, vertices );

    glBegin(GL_QUADS);
        glArrayElement(0);
        glArrayElement(1);
        glArrayElement(2);
        glArrayElement(3);
    glEnd();

    glDisableClientState(GL_VERTEX_ARRAY);

    glutSwapBuffers();
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
    glutInitWindowSize(400,400);
    glutInitWindowPosition(100,100);
    glutCreateWindow("vectors");

    glutDisplayFunc(Draw);
    glutMainLoop();
}

Upvotes: 2

GMasucci
GMasucci

Reputation: 2892

The main thing that pops out at me is the array: //static GLfloat vertices[] = {0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.5, 0.0, };

should be

static GLfloat vertices[] = {0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.5, 0.0 }; (note the removed comma)

Next thing is the enabling and disabling of the client state: that could be done in the main loop for example as it is expensive to enable and disable and your draw loop will suffer because of it.

Upvotes: -1

Peter Bloomfield
Peter Bloomfield

Reputation: 5766

I suspect the problem is your call to glVertexPointer(). The last parameter is supposed to be a pointer to the start of the array you're using, but at the moment you're just passing it a null pointer, so it's got nothing to work with.

Try this instead:

glVertexPointer(3, GL_FLOAT, 0, vertices);

EDIT: By the way, your code won't give you a box. If it works, I think it'll just give you two lines. Try using GL_QUADS instead of GL_LINES.

Upvotes: 1

Related Questions