How To Code OpenGl in Visual Studio(Part 2)

Draw basics

Mohammad Javadian Farzaneh

--

Methods of drawing objects in opengl

  • Immediate mode
  • Retained mode
  • Advanced mode

Drawing every object in OpenGL is done by defining some points and connect them based on your usage.you can make polygon, lines, triangles and etc…

check the below to see what you can do with just points in opengl.

Note: Below code are pseudo,actual codes will be at Drawing Points part in this article.

1. Immediate mode

In this method we just make points and then tell the GPU to show them on the screen.Here is the pseudo-code of this method.

for i=1:n
{
make a random point;
show the point on the screen;
}

as you can see we don’t save data(points) in the above code.

2. Retained mode

This method is similar to first method but unlike the previous method we save our data(points) in some arrays or variables and the passed the array or lists to GPU.

make list of points
for i=1:n
{
points[i]=make a random point;
}
drawallPoints();

3. Advanced Mode

Unlike previous methods in this method you prepare points and then for faster processing you send them to the GPU and allow your GPU to do the things which you like instead of telling CPU to do them.It will discuss in later articles.In this article we will discuss immediate and retained mode.

make list of points
for i=1:n
{
points[i]=make a random point;
}
send all points to gpu and do some changes if it is needed.
drawallpoints();

Draw Points

In this post we are going to draw some points by opengl.First we have to create project in visual studio and then do the first initialization of glut library.if you have forgotten how to initialize them see the first part.

Now it is time to draw our first point look, at the below code and the result.

But first let me say some notes.If you want to display anything in the screen,you must set display callback function which is responsible for handling your views.I mean if you want to show something on the screen you must do it in the callback of display function.And of course you must call glutMainloop(); to process event handling;without this non of your callbacks will trigger.creating display function is at the first part of tutorial.

Now the code

First,we change the color of screen,as mentioned in the above part,all graphic-related works are done in display callback function,so we put the code of changing color in the display callback function:

glClearColor(1, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();

The first line of code change color of screen to red and second line will clear the buffer which will be empty for the next use.it is better to do this ,but if you do not do it there is no problem and the program will run.At the last line we empty the buffer of data to

As you can see by glclearColor(1,0,0,1); we change the color of screen to red.Now let’s add some points to the screen. For adding points to screen again we write the code in the callback of display function.

NOTE: All coordinates should be in the range of [-1,1]. check this link for reason.

glClearColor(1, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glVertex2d(0.0, 0.0);
glVertex2d(0.5, 0.0);
glVertex2d(0.5, 0.5);
glVertex2d(0.0, 0.5);
glEnd();
glFlush();

First we change the color to red and then we start to draw points, First we specify that we want to draw points or polygons or triangles.Try to change GL_POINTS to GL_POLYGON and see the result,this is my result

The mentioned method was immediate now let’s look at retained method.
In this method we can first prepare points and then send them to the GPU for drawing.

NOTE: Before go to next part,we can define classes which contain x,y or x,y,z or we can define 2d or 3d arrays and pass them to the opengl with glVertex2dv(); which get the address of a pointer of array.

```
float vert[4][2] = {
{ 0.0,0.0},
{ 0.5,0.0 },
{ 0.5,0.5 },
{ 0.0,0.5 },
};

glBegin(GL_POLYGON);
glVertex2fv(vert[0]);
glVertex2fv(vert[1]);
glVertex2fv(vert[2]);
glVertex2fv(vert[3]);
glEnd();
```

you can replace the above code with the previous one.the result is same.
you can also change the color of points by or the polygon by adding glColor3f(R,G,B); .

All Above codes are done by glut library which means that we are telling the CPU to call GPU for drawing.
In next articles we will directly tell the GPU to draw points.

Have a good day.

--

--

Mohammad Javadian Farzaneh
Mohammad Javadian Farzaneh

Written by Mohammad Javadian Farzaneh

M.Sc in Artificial Inteligence, Computer Vision Enthusiast, and looking for Ph.D positions

No responses yet