OpenGL line drawing

I am practicing the exercises from my tutorial, but I could not get the results I should be.

Here's what I have:

#include <math.h>
#include <GLUT/glut.h>
#include <OpenGL/OpenGL.h>

//Initialize OpenGL 
void init(void) {
    glClearColor(0.0,0.0,0.0,0.0); 
    glMatrixMode(GL_PROJECTION); 
    gluOrtho2D(0.0,300.0,0.0,300.0);    
} 

void drawLines(void) {
    glClear(GL_COLOR_BUFFER_BIT);  
    glColor3f(0.0,0.4,0.2); 
    glPointSize(3.0);  

    glBegin(GL_LINES);
    glVertex2d(180, 15);
    glVertex2d(10, 145);
    glEnd();
} 

int main(int argc, char**argv) {
    glutInit(&argc, argv);  
    glutInitWindowPosition(10,10); 
    glutInitWindowSize(500,500); 
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 

    glutCreateWindow("Example"); 
    init(); 
    glutDisplayFunc(drawLines); 
    glutMainLoop();
}

      

When I run this piece of code, I get a completely blank white screen.

+3


source to share


1 answer


I'm not an OpenGL expert either, but the problem is that you haven't set the viewport where your scene should be projected. Your init should look something like this:

glClearColor(0, 0, 0, 0);

glViewport(0, 0, 500, 500);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glOrtho(0, 500, 0, 500, 1, -1);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

      



You also need to set glFlush (); after your drawing.

void drawLines(void) {
    ...
    glFlush();
}  

      

+8


source







All Articles