Vertex object doesn't show up (legacy OpenGL 1.2)

64 views Asked by At

So I got the OpenGL Redbook 3e and am currently on chapter 2 (newer OpenGL seems too difficult for beginners to graphics programming). I copied a code snippet from the book (I added the initialization code and drawing code) and I've tried each of the options for rendering vertex arrays (glDrawElements, glDrawRangeElements, and gDrawArrays). For this example I decided to go with glDrawArrays because it seemed to be the most straightforward. As far as I can tell there's nothing wrong with this code because it compiles and runs. However, no polygon is drawn. Does anyone have any idea what's going on? I feel like I'm either making an incredibly silly error or am missing something fundamental.

#include <stdio.h>
#include <GL/gl.h>
#include <GL/glext.h>
#include <GL/freeglut.h>

void init() {
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

void display() {
    static GLint vertices[] = { 25, 25,
                    100, 325, 
                    175, 25,
                    175, 325,
                    250, 25,
                    325, 325 };

    static GLfloat colors[] = { 1.0, 0.2, 0.2,
                    0.2, 0.2, 1.0,
                    0.8, 1.0, 0.2,
                    0.75, 0.75, 0.75,
                    0.35, 0.35, 0.35,
                    0.5, 0.5, 0.5 };
    
    glClear(GL_COLOR_BUFFER_BIT);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);

    glColorPointer(3, GL_FLOAT, 0, colors);
    glVertexPointer(2, GL_INT, 0, vertices);

    glDrawArrays(GL_POLYGON, 0, sizeof(vertices) / sizeof(GLint));
    
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);

    glFlush();
}

int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB);
    glutInitWindowSize(512, 512);
    glutCreateWindow(argv[0]);

    init();

    glutDisplayFunc(display);
    glutMainLoop();

    return 0;
}
1

There are 1 answers

3
Quimby On

Your vertices are so far out of the clipping planes.

glOrtho is usually setup using pixel coordinates (but that is not required) see ref.

You probably want

glOrtho(0.0, 512.0, 0.0, 512.0, -1.0, 1.0);