Drawing your first line in openGL

In this tutorial we will look at how to draw a simple line in openGL.

However before drawing a line there's a few things we need to understand.

First of all anything that needs to be displayed on the screen should be inside the display function.
If it's not within the display function it will not be drawn to the screen even if it is executed elsewhere.

Before drawing a line we need to know about vertexes, a vertex can be defined as a single point in space. so if i were to draw a vertex in openGL the code snippet would look something like this:

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POINT);
glVertex2d(0.0, 0.0);
glEnd();

        glutSwapBuffers();

}


what i have done here is as follows:

by using 'glClear' and calling the functions 'GL_COLOR_BUFFER_BIT' and 'GL_DEPTH_BUFFER_BIT', the memory buffer for the current set color and the memory buffer for the bit depth has been cleared.

glutSwapBuffers() -  swap the buffer of the current window to the back buffer.(this will be discussed further in upcoming tutorials)

by using 'glcolor3f' and setting the values to '1.0,0.0,0.0' the color of the next drawn pixel has been changed to red. in 'glColor3f' 3 stands for 3 variables and f stands to float type value. the function glColor3f() takes 3 values which are used in order for Red,Green and Blue values. so in this case the color is fully red.

glBegin(GL_POINT) - everything and anything that needs to be drawn into the screen should be specified within a glBegin() and glEnd() anything outside these two functions will not be drawn the screen. in this case we have specified glBegin(GL_POINT), to say that we are going to draw a point(s) within these two functions.

'glVertex2d' - This specifies that we are going to draw a vertex in 2 dimensions of which the location to be drawn can be specified in double type value.

The openGL display window can be though of as a graph with X and Y axis and 0,0 being the middle of it all.

By integrating the above code snippet into the base code we will get this code:

//////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "freeglut.h"
#include <Windows.h>
#include <iostream>

using namespace std;

void reshape(int w, int h)
{
glViewport(0, 0, w, h);
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POINT);
glVertex2d(0.0, 0.0);
glEnd();

        glutSwapBuffers();

}



int main(int argc, char* argv[]) {

// Initialize GLUT
glutInit(&argc, argv);
// Set up some memory buffers for our display
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
// Set the window size
glutInitWindowSize(800, 600);
// Create the window with the title "Hello,GL"
glutCreateWindow("Hello, GL");
// Bind the two functions (above) to respond when necessary
glutReshapeFunc(reshape);
glutDisplayFunc(display);

glutMainLoop();
return 0;
}

/////////////////////////////////////////////////////////////////////////////////


when this code is executed the following result should popup on your screen.


now since the point is hardly visible we can add glPointSize(5.0); right before glBegin();

now your output will look like this with the point being much more visible.




now we can move onto drawing a line.

to draw a line all you have to do is specify two points n which the line will be drawn from point A to point B.

now if you modify the code snippet as follow :


void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glPointSize(5.0);
glBegin(GL_LINES);
glVertex2d(0.0, 0.0);
glVertex2d(0.5,0.5);
glEnd();


glutSwapBuffers();
}


now the glBegin() specifies that we are drawing a line so rather than just drawing two points at different locations it will draw from vertex A to vertex .
Two vertexes are given to say we want the line drawn form this vertex to the other.

The result of this is as follows:

If you want to make the line thicker then you can type in glLineWidth("ur prefered value"); right  before the glBegin();

for example I set the glLineWidth(5.0); and this was the result.



In the next tutorials we will look at how to draw polygons and move onto drawing 3D shapes and eventually we'll make a simple game.

Comments