Leonardo Vinsen
Leonardo Vinsen

Reputation: 89

(OpenGL) Line is drawn but shapes are not

I'm currently learning OpenGL and from what I understood, I have to call

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

before drawing lines. Then, to draw shapes I need to call

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

to draw shapes such as GL_TRIANGLES and GL_QUADS. I wrote this code with the goal of drawing a single line at top and 3 shapes, but only the line was drawn. Here is my code.

void drawScene() {
//Clear information from last draw
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective



glBegin(GL_LINES);

glColor3f(100,200,100);
glLineWidth(10.0f);
glVertex2f(-1.0f,0.8f);
glVertex2f(1.0f,0.8f);

glEnd();

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glBegin(GL_QUADS); //Begin quadrilateral coordinates

//Trapezoid
glVertex3f(-0.7f, -1.5f, -5.0f);
glVertex3f(0.7f, -1.5f, -5.0f);
glVertex3f(0.4f, -0.5f, -5.0f);
glVertex3f(-0.4f, -0.5f, -5.0f);

glEnd(); //End quadrilateral coordinates


glBegin(GL_TRIANGLES); //Begin triangle coordinates

//Pentagon
glVertex3f(0.5f, 0.5f, -5.0f);
glVertex3f(1.5f, 0.5f, -5.0f);
glVertex3f(0.5f, 1.0f, -5.0f);

glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(1.5f, 0.5f, -5.0f);
glVertex3f(1.5f, 1.0f, -5.0f);

glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(1.5f, 1.0f, -5.0f);
glVertex3f(1.0f, 1.5f, -5.0f);

//Triangle
glVertex3f(-0.5f, 0.5f, -5.0f);
glVertex3f(-1.0f, 1.5f, -5.0f);
glVertex3f(-1.5f, 0.5f, -5.0f);

glEnd(); //End triangle coordinates

glutSwapBuffers(); //Send the 3D scene to the screen
}

Could someone please explain to me how to switch between GL_PROJECTION and GL_MODELVIEW and how do they work?

Upvotes: 2

Views: 156

Answers (1)

Yakov Galka
Yakov Galka

Reputation: 72449

The Z-value of your polygons is -5.0 which is outside the default [-1, 1] range for the device coordinates, so they get discarded.

Replace all glVertex3f(x, y, z) calls with glVertex2f(x,y).

Upvotes: 3

Related Questions