Primitives are basic shapes that you can easily draw. It can be a triangle, a square, or even a single point.
First, let me mention that OpenGL is a low level API, this means that it has no support for drawing complex geometrical objects. It is the programmer’s job to combine the geometrical primitives from OpenGL in complex shapes and bodies. The basic geometrical primitives that the core OpenGL profile provide to us are points, lines and triangles.
Each primitive has at least one vertex. What is a vertex exactly? Is it one of the points in a polygon or is it a stand-alone point in space? Well it can be both, depending on how you think about it. With points, vertex is just that - the point.
Note that a 3D cube cannot be considered a primitive. Generally, primitives restrict themselves to triangles. A four-sided polygon can generate a quad but that quad will still be made out of 2 polygons. Points and lines can also be considered primitives.
Here is a list of the possible modes and what they mean:
All the shapes that you can think of either are, or can be broken down into, geometric primitives. For example a pyramid can be broken down into a few triangles. All the geometry that you render in OpenGL is constructed of primitives and there are quite a few available to you. Below is a list of the OpenGL primitive types available and an example of how they can look with an example set of vertices:
The main function (and probably the most used OpenGL function) is function named glVertex. This function defines a point (or a vertex) in your 3D world and it can vary from receiving 2 up to 4 coordinates.
Let's take a look at these variations:
glVertex2f(100.0f, 150.0f); defines a point at x = 100, y = 150, z = 0; this function takes only 2 parameters, z is always 0. glVertex2f can be used in special cases and won't be used a lot unless you're working with pseudo-2D sprites or triangles and points that always have to be constrained by the depth coordinate.
glVertex3f(100.0f, 150.0f, -25.0f); defines a point at x = 100, y = 150, z = -25.0f; this function takes 3 parameters, defining a fully 3D point in your world. This function will be used a lot to define any kind of shapes you will possibly want.
glVertex4f(100.0f, 150.0f, -25.0f, 1.0f); this is the same as glVertex3f, the only difference is in the last coordinate that specifies a scaling factor. The scaling factor is set to 1.0f by default. This won't make a lot of use and I'm not going to explain this function in details. It can be used to make your 3D points look thicker than one pixel. I don't want to sound pathetic but why would you want to use that functionality?
Anyway, glVertex alone won't draw anything on the screen, it merely defines a vertex, usually of a more complex object. To really start displaying something on the screen you will have to use two additional functions.
These functions are: glBegin(int mode) and glEnd( void ).
glBegin and glEnd delimit the vertices of a primitive or a group of like primitives. What this means is that everytime you want to draw a primitive on the screen you will first have to call glBegin, specifying what kind of primitive it is that you want to draw in the mode parameter of glBegin, and then list all vertices one by one (by sequentially calling glVertex) and finally call glEnd to let OpenGL know that you're done drawing a primitive.
Now in the Render() function, before you write the glBegin() code, we will set the point size to be 10 pixels in size:
First we want a triangle to the left. So we need three vertices on the left side representing one triangle and three vertices on the right side of the window representing the second triangle. Note that you don’t need two glBegin() functions to draw two triangles. Since the mode GL_TRIANGLES is plural, it can handle more than one triangle in between one glBegin() and glEnd() function call:
Note that I wrote on the bottom – right corner of the diagram the coordinates of the vertices. Here is the code to draw these two triangles:
First, let me mention that OpenGL is a low level API, this means that it has no support for drawing complex geometrical objects. It is the programmer’s job to combine the geometrical primitives from OpenGL in complex shapes and bodies. The basic geometrical primitives that the core OpenGL profile provide to us are points, lines and triangles.
Each primitive has at least one vertex. What is a vertex exactly? Is it one of the points in a polygon or is it a stand-alone point in space? Well it can be both, depending on how you think about it. With points, vertex is just that - the point.
- A line has only 2 vertices - its starting point and its ending point.
- With polygons, there should be more than 2 vertices since polygons are surfaces defined by more or equal to 3 vertices residing on the same plane.
- A triangle is, for instance, a polygon with 3 vertices.
Note that a 3D cube cannot be considered a primitive. Generally, primitives restrict themselves to triangles. A four-sided polygon can generate a quad but that quad will still be made out of 2 polygons. Points and lines can also be considered primitives.
Immediate Mode
The easiest way to do drawing in OpenGL is using the Immediate Mode. For this, you use the glBegin() function which takes as one parameter the “mode” or type of object you want to draw.Here is a list of the possible modes and what they mean:
GL_POINTS | Draws points on screen. Every vertex specified is a point. |
---|---|
GL_LINES | Draws lines on screen. Every two vertices specified compose a line. |
GL_LINE_STRIP | Draws connected lines on screen. Every vertex specified after first two are connected. |
GL_LINE_LOOP | Draws connected lines on screen. The last vertex specified is connected to first vertex. |
GL_TRIANGLES | Draws triangles on screen. Every three vertices specified compose a triangle. |
GL_TRIANGLE_STRIP | Draws connected triangles on screen. Every vertex specified after first three vertices creates a triangle. |
GL_TRIANGLE_FAN | Draws connected triangles like GL_TRIANGLE_STRIP, except draws triangles in fan shape. |
GL_QUADS | Draws quadrilaterals (4 – sided shapes) on screen. Every four vertices specified compose a quadrilateral. |
GL_QUAD_STRIP | Draws connected quadrilaterals on screen. Every two vertices specified after first four compose a connected quadrilateral. |
GL_POLYGON | Draws a polygon on screen. Polygon can be composed of as many sides as you want. |
All the shapes that you can think of either are, or can be broken down into, geometric primitives. For example a pyramid can be broken down into a few triangles. All the geometry that you render in OpenGL is constructed of primitives and there are quite a few available to you. Below is a list of the OpenGL primitive types available and an example of how they can look with an example set of vertices:
The main function (and probably the most used OpenGL function) is function named glVertex. This function defines a point (or a vertex) in your 3D world and it can vary from receiving 2 up to 4 coordinates.
Let's take a look at these variations:
glVertex2f(100.0f, 150.0f); defines a point at x = 100, y = 150, z = 0; this function takes only 2 parameters, z is always 0. glVertex2f can be used in special cases and won't be used a lot unless you're working with pseudo-2D sprites or triangles and points that always have to be constrained by the depth coordinate.
glVertex3f(100.0f, 150.0f, -25.0f); defines a point at x = 100, y = 150, z = -25.0f; this function takes 3 parameters, defining a fully 3D point in your world. This function will be used a lot to define any kind of shapes you will possibly want.
glVertex4f(100.0f, 150.0f, -25.0f, 1.0f); this is the same as glVertex3f, the only difference is in the last coordinate that specifies a scaling factor. The scaling factor is set to 1.0f by default. This won't make a lot of use and I'm not going to explain this function in details. It can be used to make your 3D points look thicker than one pixel. I don't want to sound pathetic but why would you want to use that functionality?
Anyway, glVertex alone won't draw anything on the screen, it merely defines a vertex, usually of a more complex object. To really start displaying something on the screen you will have to use two additional functions.
These functions are: glBegin(int mode) and glEnd( void ).
glBegin and glEnd delimit the vertices of a primitive or a group of like primitives. What this means is that everytime you want to draw a primitive on the screen you will first have to call glBegin, specifying what kind of primitive it is that you want to draw in the mode parameter of glBegin, and then list all vertices one by one (by sequentially calling glVertex) and finally call glEnd to let OpenGL know that you're done drawing a primitive.
Drawing Points
Lets do an example with the simplest mode, the GL_POINTS. When drawing points using OpenGL, the default size of the points is 1 pixel wide and high. This would be very hard to see when you run the program. To edit the size of the point you want to draw, you use the glPointSize() function which takes as one parameter the size of the point you want.Now in the Render() function, before you write the glBegin() code, we will set the point size to be 10 pixels in size:
glPointSize(10.0f);
After that, any drawing of points will be drawn 10 pixels wide and high. Now write the glBegin() function with the GL_POINTS mode parameter. Then after that specify the vertices you want to use using the glVertex3f() function. For this example, we want the upper – right corner (1.0,1.0,0.0) and the lower – left corner (-1.0,-1.0,0.0) of the screen to have a point. After drawing those two points, make sure to end the drawing with the glEnd() function:
glBegin(GL_POINTS); //starts drawing of points
glVertex3f(1.0f,1.0f,0.0f);//upper-right corner
glVertex3f(-1.0f,-1.0f,0.0f);//lower-left corner
glEnd();//end drawing of points
Drawing Triangles
Triangles are composed of three vertices. For this example we are going to use the regular GL_TRIANGLES mode to draw two triangles side by side.First we want a triangle to the left. So we need three vertices on the left side representing one triangle and three vertices on the right side of the window representing the second triangle. Note that you don’t need two glBegin() functions to draw two triangles. Since the mode GL_TRIANGLES is plural, it can handle more than one triangle in between one glBegin() and glEnd() function call:
Note that I wrote on the bottom – right corner of the diagram the coordinates of the vertices. Here is the code to draw these two triangles:
glBegin(GL_TRIANGLES);//start drawing triangles
glVertex3f(-1.0f,-0.25f,0.0f);//triangle one first vertex
glVertex3f(-0.5f,-0.25f,0.0f);//triangle one second vertex
glVertex3f(-0.75f,0.25f,0.0f);//triangle one third vertex
//drawing a new triangle
glVertex3f(0.5f,-0.25f,0.0f);//triangle two first vertex
glVertex3f(1.0f,-0.25f,0.0f);//triangle two second vertex
glVertex3f(0.75f,0.25f,0.0f);//triangle two third vertex
glEnd();//end drawing of triangles
No comments:
Post a Comment