https://www.khronos.org/opengl/wiki_opengl/api.php?action=feedcontributions&user=Dwadsworth&feedformat=atomOpenGL Wiki - User contributions [en]2022-10-02T03:26:01ZUser contributionsMediaWiki 1.35.6https://www.khronos.org/opengl/wiki_opengl/index.php?title=Tutorial3:_Rendering_3D_Objects_(C_/SDL)&diff=14443Tutorial3: Rendering 3D Objects (C /SDL)2018-11-20T05:28:45Z<p>Dwadsworth: </p>
<hr />
<div>{{tutorial c sdl}}<br />
<br />
== Overview ==<br />
<br />
This tutorial is designed to help explain the process of rendering 3D objects using the OpenGL 3.2 core profile. The requirements to compile and run this tutorial are the same as the previous tutorials.<br />
<br />
Rendering an object in a three dimensional space makes use of three different matrices to determine where rendering should occur. These are the model, view, and projection matrices. The model matrix is used to describe where an object exists in the world. The view matrix is used to describe the vantage point of our view. The view matrix can be thought of as the position and angle of a camera used to take a picture. The projection matrix is used to give our view perspective such as making close objects appear larger than distant objects. The projection matrix also provides a field of view which can be thought of as a camera lens; you can decide to use a wide-angle lens or a telephoto lens. Multiplying these three matrices together and then with a object's vertex positions will provide us with a three dimensional view of the object.<br />
<br />
If you have used the old OpenGL API, then you are probably familiar with some of the functions used to modify these matrices. These functions have been deprecated and are not available in the OpenGL 3.2 core profile. This means that we are required to perform these matrix operations using our own methods. In this tutorial I have included utils.c and utils.h files which will allow us to perform some of the matrix operations we will need. If you are programming in C++, there is an excellent library called [http://glm.g-truc.net/ glm] which will perform the necessary functionality as well.<br />
<br />
== Changes to the window setup ==<br />
<br />
In this tutorial we make a couple of changes to our window setup. The first change we make is to enable multisampling. Multisampling is a form of anti-aliasing used to make object edges less jagged. We enable 4x mutisampling using the follow code:<br />
<source lang="c"><br />
/* Enable multisampling for a nice antialiased effect */<br />
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);<br />
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);<br />
</source><br />
The previous code is executed before the window and OpenGL context are created. The other change we make is to enable depth testing which occurs after the window and OpenGL context are created. Enabling depth testing causes OpenGL to keep track of where rendered primitives exist in the world. We also tell OpenGL that we want to use the GL_LESS depth comparison function to determine which objects should be shown on top. The result of this is that objects closer to the viewpoint will obscure object further away from the viewpoint. If we didn't enable the depth testing, the last drawn polygon would always appear to be closest to the viewpoint. We perform these operations with the following code:<br />
<source lang="c"><br />
/* Enable Z depth testing so objects closest to the viewpoint are in front of objects further away */<br />
glEnable(GL_DEPTH_TEST);<br />
glDepthFunc(GL_LESS);<br />
</source><br />
<br />
== Changes to drawing the scene ==<br />
<br />
The first changes we make are declaring our projection and model matrices. You may notice that we don't declare a view matrix in this tutorial. This is because we are using the default view, which consists of what is known as the right-handled coordinate system. The right-handed coordinate system consists of the 0,0,0 coordinates being positioned in the center of the view with the negative Z axis extending in front of us. The next change we make is making vertex points describing a tetrahedron, a simple three dimensional object consisting of four triangles. Most of the code from tutorial2 is reused to setup our VAO, VBOs, shaders, and program. The next changes consist of creating a perspective for our projection matrix, loading the identity matrix for the model matrix, then rotating and translating our model matrix. We then multiply our model and projection matrices together and bind the resultant matrix as a uniform for use in our vertex shader. A uniform is simply a value or values, in our case a matrix, which does not change value during the execution of a shader. Here's the code snippet performing these operations:<br />
<source lang="c"><br />
/* Create our projection matrix with a 45 degree field of view<br />
* a width to height ratio of 1.0 and view from .1 to 100 infront of us */<br />
perspective(projectionmatrix, 45.0, 1.0, 0.1, 100.0);<br />
<br />
/* Loop our display rotating our model more each time. */<br />
for (i=0; i < 360; i++)<br />
{<br />
/* Load the identity matrix into modelmatrix. rotate the model, and move it back 5 */<br />
memcpy(modelmatrix, identitymatrix, sizeof(GLfloat) * 16);<br />
rotate(modelmatrix, (GLfloat)i * -1.0, X_AXIS);<br />
rotate(modelmatrix, (GLfloat)i * 1.0, Y_AXIS);<br />
rotate(modelmatrix, (GLfloat)i * 0.5, Z_AXIS);<br />
translate(modelmatrix, 0, 0, -5.0);<br />
<br />
/* multiply our modelmatrix and our projectionmatrix. Results are stored in modelmatrix */<br />
multiply4x4(modelmatrix, projectionmatrix);<br />
<br />
/* Bind our modelmatrix variable to be a uniform called mvpmatrix in our shaderprogram */<br />
glUniformMatrix4fv(glGetUniformLocation(shaderprogram, "mvpmatrix"), 1, GL_FALSE, modelmatrix);<br />
</source><br />
<br />
== Changes in the vertex shader ==<br />
<br />
The vertex shader is almost the same as the previous tutorial. The only differences are the declaration of the uniform model-view-projection matrix (mvpmatrix) we now have as well as multiplying the matrix to our vector.<br />
<source lang="c"><br />
// mvpmatrix is the result of multiplying the model, view, and projection matrices */<br />
uniform mat4 mvpmatrix;<br />
void main(void) {<br />
// Multiply the mvp matrix by the vertex to obtain our final vertex position<br />
gl_Position = mvpmatrix * vec4(in_Position, 1.0);<br />
</source><br />
<br />
== Full source code ==<br />
<br />
Full source code is available in a zip file [http://opengl.no-ip.org/tutorial3.zip here] (Note: broken link).<br />
<br />
Full source code of a variation of tutorial3 using a single VBO is available in a zip file [http://consultit.website.org/tutorial3_single-vbo.zip here] (Note: broken link)<br />
<br />
== Compilation ==<br />
On linux:<br />
<br />
gcc utils.c tutorial3.c -o tutorial3 -lGL $(sdl-config --cflags --libs)<br />
<br />
If you have SDL-1.2 and SDL-1.3 both installed, be sure to run the 1.3 version of sdl-config. For example if you installed SDL-1.3 in /usr/local:<br />
<br />
gcc utils.c tutorial3.c -o tutorial3 -lGL $(/usr/local/bin/sdl-config --cflags --libs)<br />
<br />
== Execution ==<br />
./tutorial3<br />
<br />
The result should be a 512x512 window centered on your display showing a rotating tetrahedron.<br />
<br />
[[Category:Tutorials]]</div>Dwadsworth