OpenGL :: Fundamentals :: Material 2
It includes the following topics
- Color interpolation
- Vertices and polygons
- Functions and types
- Color interpolation
- Vertices and polygons
- Functions and types
Color interpolation
In the final screenshot of the previous chapter (colored triangle) you could admire the purity and beauty of the colors in OpenGL.To set the color of a vertex is used:Code: C + +
- Select
Values of red, green and blue are integers between 0 and 255.
We could both use glColor3f (therefore with color components between 0 and 1), but the value range [0 .. 255] is easier to understand because heavily used in software design. Based on a specific color, to know its RGB components (RGB or English) just use the Windows color palette (eg using Paint).
A call to glColor affects the current color, which will be used for vertices to be defined later. It can change color at each vertex or just occasionally.
The colors of the points which constitute a face are calculated by interpolation of vertex colors.Quote: Dictionary
This therefore gives rise to beautiful gradients between the peaks.
Change the code in the previous chapter to draw a rectangle with a gradient blue / red.
Code: C + +- Select
Gradient very easily with OpenGL
Indeed default coordinates range from -1 to 1 in both X and Y on whatever the ratio (width / height) of the window. We will see later how to take into account the ratio of the window and change the coordinates min / max. now you can have fun drawing 2D shapes in this range of coordinates. Play with colors, shapes and gradients.
- Select
1 | glColor3ub ( red , green , blue ); |
Values of red, green and blue are integers between 0 and 255.
We could both use glColor3f (therefore with color components between 0 and 1), but the value range [0 .. 255] is easier to understand because heavily used in software design. Based on a specific color, to know its RGB components (RGB or English) just use the Windows color palette (eg using Paint).
A call to glColor affects the current color, which will be used for vertices to be defined later. It can change color at each vertex or just occasionally.
And for the points which are not vertices, how color is defined?
The colors of the points which constitute a face are calculated by interpolation of vertex colors.Quote: Dictionary
Interpolation: assessing the value of a function between two known values.
This therefore gives rise to beautiful gradients between the peaks.
Exercise
Change the code in the previous chapter to draw a rectangle with a gradient blue / red.
Corrected
Code: C + +- Select
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <SDL/SDL.h> # Include # include <GL/gl.h> # include <GL/glu.h> int main ( int argc , char * argv []) { SDL_Init ( SDL_INIT_VIDEO ) SDL_WM_SetCaption ( "nice \ " square \ u00E9 \ " " , NULL ); SDL_SetVideoMode ( 640 , 480 , 32 , SDL_OPENGL ); bool continue = true ; SDL_Event event ; while ( continue ) { SDL_WaitEvent ( & event ) switch ( event . kind ) { case SDL_Quit: continue = false ; } glClear ( GL_COLOR_BUFFER_BIT ); glBegin(GL_QUADS); glColor3ub(0,0,255); glVertex2d(-0.75,-0.75); glVertex2d(-0.75,0.75); glColor3ub(255,0,0); glVertex2d(0.75,0.75); glVertex2d(0.75,-0.75); glEnd(); glFlush (); SDL_GL_SwapBuffers (); } SDL_Quit (); return 0 ; } |
Gradient very easily with OpenGL
download the required files ---->> Click Here
Ah yes but your square is not really square one!?
Indeed default coordinates range from -1 to 1 in both X and Y on whatever the ratio (width / height) of the window. We will see later how to take into account the ratio of the window and change the coordinates min / max. now you can have fun drawing 2D shapes in this range of coordinates. Play with colors, shapes and gradients.
Vertices and polygons
OpenGL is all based on polygons, the basic polygon being used as the triangle.
Even a 3D model is represented with polygons which compose the sides.
Land wireframe *
* We will see in Part II of this tutorial how to generate a terrain in OpenGL.
To draw a polygon must:
Let the 1 st line of code barbaric previous chapter:Code: C + +
- Select
With GL_TRIANGLES tells OpenGL that we must make triangles with vertices that will be reported. Listed below are ways that we can use:
Once the drawing mode is set, it must be declared with the vertices glVertex :Code: C + +-Select
We earlier, the same OpenGL function here glVertex can be called with a variable number of arguments. Here we use two (hence the 2d), so we define the X and Y from the top and the Z in question is automatically set to 0. Needless to say that the order of defining vertices is important. Must follow the contour of the polygon if you do not want to end up with aberrations. Finally we must close the open block by glBegin with glEnd :Code: C + +-Select
If you have to work with points or lines, you'll want to change the size ... OpenGL has thought of you:Code: C + +-Select
size and width are real worth 1.0 by default.
To understand the influence of the mode of rendering, here's an animation rendering vertices with six different modes:
Some methods were not used because they had little interest in this example.
Even a 3D model is represented with polygons which compose the sides.
Land wireframe *
* We will see in Part II of this tutorial how to generate a terrain in OpenGL.
To draw a polygon must:
- define all vertices that compose it;
- tell OpenGL how it should use its vertices.
Summit says vertex in English, the plural vertices. To stay close to the language and OpenGL 3D world, we will keep the English name. It is more pro, is not it?
Let the 1 st line of code barbaric previous chapter:Code: C + +
- Select
1 | glBegin ( GL_TRIANGLES ); |
With GL_TRIANGLES tells OpenGL that we must make triangles with vertices that will be reported. Listed below are ways that we can use:
mode | detail |
---|---|
GL_POINTS | each vertex is represented as a point |
GL_LINES | vertices are connected 2-2 to make lines. If we define 4 vertices, 1 st will be connected with 2 nd , 3 e 4 with e , but not the 2 e 3 will be connected to e |
GL_LINE_STRIP | vertices are connected by lines of the 1 st to the last. If we define 3 vertices, 1 st will be connected with 2 nd and 2 nd with 3 e |
GL_LINE_LOOP | same GL_LINE_STRIP , but the last vertex is connected to 1 st (it makes a loop: loop) |
GL_TRIANGLES | triples of vertices are used to form triangles. This mode is most often used when we represent complex objects based triangular faces |
GL_TRIANGLE_STRIP | Triangles touch, that is to say it takes 3 vertices to make a first triangle, and a 4 th vertex is sufficient to define another because the last 2 vertices will also be used |
GL_TRIANGLE_FAN | triangles touch and share the 1 st vertex set. If we define 4 vertices, 1 st 2 e and 3 e form a triangle, 1 st3 e and 4 e form a second triangle. |
GL_QUADS | quadruplets of vertices form quadrilaterals full |
GL_QUAD_STRIP | as previously, the quadrilaterals are connected, just two new vertices to define a new one, the last 2 previous vertices of the quadrilateral being used |
GL_POLYGON | all vertices forming a convex polygon (such as a pentagon, or any other polygon having more than four vertices) |
Once the drawing mode is set, it must be declared with the vertices glVertex :Code: C + +-Select
1 2 3 | glVertex2d ( - 0.75 , - 0.75 ) glVertex2d ( 0 , 0.75 ); glVertex2d ( 0.75 , - 0.75 ); |
We earlier, the same OpenGL function here glVertex can be called with a variable number of arguments. Here we use two (hence the 2d), so we define the X and Y from the top and the Z in question is automatically set to 0. Needless to say that the order of defining vertices is important. Must follow the contour of the polygon if you do not want to end up with aberrations. Finally we must close the open block by glBegin with glEnd :Code: C + +-Select
1 | glEnd (); |
Forget to close a block opened with glBegin by glEnd will block invalid and nothing will be drawn.
Some options
If you have to work with points or lines, you'll want to change the size ... OpenGL has thought of you:Code: C + +-Select
1 2 | glPointSize ( size ); glLineWidth ( width ); |
size and width are real worth 1.0 by default.
Comparative
To understand the influence of the mode of rendering, here's an animation rendering vertices with six different modes:
Some methods were not used because they had little interest in this example.
In the mode GL_QUADS as we have 6 vertices, the first 4 form a rectangle, but the other 2 are unused.
Functions and types
In the previous chapter we encountered our first OpenGL functions.
example we had:Code: C + +-Select
To differentiate them from other functions, the OpenGL functions have a special syntax in their name:
I suppose a little explanation of the formula barbaric above is not denial.
With this formalism you now understand that glVertex2i is a function that requires two integer parameters. In our code we will encounter on OpenGL constants. They are easily recognized because their name is fully capitalized. Ex: GL_TRUE , GL_FALSEwhich are equivalent to true and false (true and false). If you recall, we have already passed a constant without realizing it :Code: C + +-Select
We said here OpenGL to clear the display buffer by passing a parameter to glClear a constant that allows him to know how delete buffer. At any time, do not hesitate to consult the documentation for more details on the use of a function. documention is available on the site of OpenGL
example we had:Code: C + +-Select
1 2 | glBegin ( GL_TRIANGLES ) glColor3ub ( 255 , 0 , 0 ); glVertex2d ( - 0.75 , - 0.75 ); |
To differentiate them from other functions, the OpenGL functions have a special syntax in their name:
glNom [NbType] ([Settings]);
I suppose a little explanation of the formula barbaric above is not denial.
- gl or glu : common prefixes to all OpenGL functions;
- Name : this is the name of the function as Begin or Vertex ;
- Nb : for functions with variable number of parameters defines the number of parameters that follow (eg to Begin no forVertex 2);
- Type : for functions with variable type defines the type of parameters used. We will use include:
- i for integer (integer);
- f for float (real);
- d for double (real precise);
- ub for unsigned byte (integer between 0 and 255).
- Parameters : Finally if the function requires parameters they must return.
With this formalism you now understand that glVertex2i is a function that requires two integer parameters. In our code we will encounter on OpenGL constants. They are easily recognized because their name is fully capitalized. Ex: GL_TRUE , GL_FALSEwhich are equivalent to true and false (true and false). If you recall, we have already passed a constant without realizing it :Code: C + +-Select
1 | glClear ( GL_COLOR_BUFFER_BIT ); |
We said here OpenGL to clear the display buffer by passing a parameter to glClear a constant that allows him to know how delete buffer. At any time, do not hesitate to consult the documentation for more details on the use of a function. documention is available on the site of OpenGL
No comments:
Post a Comment