OPENGL - TUTORIAL - LESSON #3 - DHTML (c) Artur Marques, 19 August 1999

*You are supposed to have read Lesson #1 , Lesson #2

The simplest Visual C++ OpenGL application... that actually draws something!


#1 - Introduction to the rectangle

#2 - Coding from the previous lesson

#3 - OpenGL statements

download the source code + win32 exe

#1 - Introduction to the rectangle

Lesson #3 could NOT be more simple. We are going to start _exactly_ where the previous [lesson #2] left us, and just add its view class two new small methods, enough to bring life to that "OpenGL enabled application".

Actually, I renamed the classes from the previously suggested name [CMy002...] to something that explicits that it is code from the 3rd tutorial [CMy003...]. However, you don't need such changes yourself, and you can even jump directly to the available source code and win32 executable.

This lesson's application will draw a white rectangle on a black background! Whow!- Yes, I know it does not seem too much, but it really is a great step forward. From now on you can do things as explained in any OpenGL books for UNIX.

I strongly advise "OpenGL Programming - The Official Guide to Learning OpenGL", from Addison Wesley. Today's Visual C++ OpenGL example, is the first example from the mentioned book, and further examples will be very much inspired in such a great reading. The differences from the book to my tutorials, are the ones that the port from UNIX to win32 really demand.

Most of the required changes were explained in lesson #2 . The two methods we will be adding today, are responses to the WM_PAINT and WM_SIZE windows' messages. WM_PAINT is sent to the window, everytime it needs to be repainted, as it happens when you scroll some other window over its area; WM_SIZE is sent to the window, everytime its size changes, as in the moment of creation or when you maximize / minimize it. Overriding methods that handle such messages is absolutely crucial. If we didn't override such methods, our OpenGL code, despite having a correct syntax, would perform nothing.


#2 - Coding from the previous lesson

Just use the ClassWizard to member function to the view class, that respond to WM_PAINT and WM_SIZE.

The code I suggest is:

void CMy003_openglView::OnSize(UINT nType, int cx, int cy) {

CView::OnSize(nType, cx, cy);

glViewport(0, 0, cx, cy);

};//method ends

//--------------

void CMy003_openglView::OnPaint() {

CPaintDC dc(this); //device context for painting

opengl_show1();

};//method ends

Comments are on this color, and the code you actually add is in RED. All the other code is auto-inserted by the ClassWizard.

So, as you can see, Lesson #3 only asks you to write two lines of code!! Incredible! But where are the OpenGL statements?! Right, those are still missing, although the more aware of you should have noticed that opengl_show1 is a method yet not implemented.

The opengl_show1 method codes the 100% OGL stuff that will draw the rectangle.


#3 - OpenGL statements

- Use the ClassWizard on your application's view class [CMy003_openglView on my case] and choose "add member function"; then specify opengl_show1 as the function's name, int as the return type, and public as the access. Edit it and code it as follows:

int CMy003_openglView::opengl_show1(){

float fx=0.5, fy=0.5; //will be used for the width and height of a rectangle, on a scale from -1 to 1

float f_dimx=1.0, f_dimy=1.0, f_dimd=1.0; //will be used for setting a coordinate system where to display the rectangle

glClearColor (0.0, 0.0, 0.0, 0.0); //the clear values that will be used for clearing the buffers (R, G, B, ALPHA)

glClear (GL_COLOR_BUFFER_BIT); //set that further glClear calls will refer to the buffers currently enabled for color writing

glColor3f (1.0, 1.0, 1.0); //set color to white (R, G, B), where 1 means highest intensity

glOrtho (-f_dimx, f_dimx, -f_dimy, f_dimy, -f_dimd, f_dimd); //the coordinate system (left, right, bottom, top, near, far)

glBegin (GL_POLYGON); //start delimiting the vertices of a primitive (single convex polygon)

glVertex2f (-fx, -fy); //left bottom vertice

glVertex2f (-fx, fy); //left top

glVertex2f (fx, fy); //right top

glVertex2f (fx, -fy); //right bottom

glEnd(); //end of polygon

glFlush(); //force execution of OpenGL statements in finite time (empties buffers)

return 1;

};//method ends

This function is very well commented and it contains all the 100% OpenGL exclusive code. Try to understand all the lines, and notice the extreme importance of the glViewport function, used in response to a WM_SIZE message. If it was not for such statement, you would see no white rectangle - only a black background.

These instructions will be explained in greater detail in the next lessons. For now, read their comments and search for help on the syntax of the calls.


Downloads

the source files, before adding the suggested code [ZIP - 12K]

all the final source files [ZIP - 13K]

the debug release win32 executable [ZIP - 21K]