emre nevayeshirazi
emre nevayeshirazi

Reputation: 19241

OpenGL Line Drawing

I am practicing the exercises from my textbook but I could not get the outputs that I should.

Here is 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 completely blank white screen.

Upvotes: 4

Views: 41859

Answers (2)

santosh jung basnet
santosh jung basnet

Reputation: 1

Determine and draw the output of OpenGL sub function below:

1.

glColor3f(0.0, 0.0, 0.0, 0.0);  
glBegin(Gl_LINES);  
glVertex2i (200, 200);  
glVertex2i (70, 20);  
glVertex2i (120, 150);  
glVertex2i (200, 20);  
glVertex2i (60, 100);  
glEnd();  
int p1[]={200,100};  
int p2[]={70,20};  
int p3[]={120,150};  
int p4[]={200,20};  
int p5[]={60,100};  
glBegin(GL_LINE_STRIP);  
glVertex2iv (p1);  
glVertex2iv (p2);  
glVertex2iv (p3);  
glVertex2iv (p4);  
glVertex2iv (p5);  
glEnd(); 

Upvotes: -1

Julian
Julian

Reputation: 158

i'm also not an expert on OpenGL but the problem is that you haven't set a viewport to where your scene should be projected. Your init should look somewhat 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 put a glFlush(); after your drawing.

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

Upvotes: 8

Related Questions