Contribute to thiagopsilva/Apostila-android development by creating an account on GitHub. Veja grátis o arquivo Apostila PyOpenGL enviado para a disciplina de Sistemas In this new from import * format, we can use OpenGL module commands. in due course. 2. Introduction OpenGL (Open Graphics Library) is a cross- platform, hardware-accelerated, apostila CPA 10 Uploaded by Marcella Costa.

Author: Gardajind Mashakar
Country: Yemen
Language: English (Spanish)
Genre: Medical
Published (Last): 28 September 2016
Pages: 467
PDF File Size: 8.71 Mb
ePub File Size: 9.67 Mb
ISBN: 468-2-29944-989-2
Downloads: 90272
Price: Free* [*Free Regsitration Required]
Uploader: Kigashakar

Model Rendering Tutorial by Brett Porter brettporter yahoo. But so that you can trust such a library, you should understand what it is doing, so this tutorial aims to help with that. The portions of PortaLib3D included here retain my copyright notices. This doesn’t mean they can’t be used by you – it means that if you cut-and-paste the code into your project, you have to give me proper credit. If you choose to read, understand, and re-implement the code yourself and it is what you are encouraged to do if you are not actually using the library.

You don’t learn anything with cut-and-paste! Let’s face it, the code is nothing special. Ok, let’s get onto something more interesting!

Mostly it came from Lesson 6, with a small modification to the loading of textures and the drawing routine. The changes will be discussed later. The reason I use this is because it is a damn fine modelling package, and it includes its file-format so it is easy to parse and understand. My next plan is to implement an Anim8or file reader because it is free and of course a 3DS reader.

However, the file format, while it will be described briefly here, is not the major concern for loading a model. You must create your own structures that are suitable to store the data, and then read the file into that. So first, let’s describe the structures required for a model. First, and most important, we need vertices: The two variables store the number of vertices and the actual vertices in a dynamic array which is allocated by the loader.

Next we need to group these vertices into triangles: This way each vertex need only be listed once, saving memory and calculations when it comes to animating later. The texture used is the one applied to this mesh which is described next. Each normal has 3 float coordinates describing the vector. The next structure we have in a model is a mesh. A mesh is a group of triangles that all have the same material applied to them. The collection of meshes make up the entire model.

The mesh structure is as follows: I’ll show you the material structure below: Here we have all the standard lighting coeffecients in the same format as OpenGL: The Code – Loading the Model Now, on to loading the model. You will notice there is a pure virtual function called loadModelData, which takes the filename of the model as an argument. What happens is we create a derived class, MilkshapeModel, which implements this function, filling in the protected data structures mentioned above.

Lets look at that function now: First, the file is opened. It is a binary file, hence the ios:: If it is not found, the function returns false to indicate an error. The above code determines the size of the file in bytes. Then the file is read into a temporary buffer in its entirety. Now, a pointer is acquired to out current position in the file, pPtr. A pointer to the header is saved, and then the pointer is advanced past the header.


You will notice several MS3D These are declared at the top of MilkshapeModel. The fields of the header are checked to make sure that this is a valid file we are reading. The above code reads each of the vertex structures in the file.

First memory is allocated in the model for the vertices, and then each is parsed from the file as the pointer is advanced. Several calls to memcpy will be used in this function, which copies the contents of the small arrays easily. As for the vertices, this part of the function stores all of the triangles in the model.

While most of it involves just copying the arrays from one structure to another, you’ll notice the difference for the vertexIndices and t arrays. In the file, the vertex indices are stores as an array of word values, but in the model they are int values for consistency and simplicity no nasty casting needed. So this just converts the 3 values to integers. The t values are all set to 1. The reason for this is that OpenGL uses a lower-left coordinate system, whereas Milkshape uses an upper-left coordinate system for its texture coordinates.

This reverses the y coordinate. The above code loads the mesh data structures also called groups in Milkshape3D. Since the number of triangles varies from mesh to mesh, there is no standard structure to read. Instead, they are taken field by field. The memory for the triangle indices is dynamically allocated within the mesh and read one at a time. Lastly, the material information is taken from the buffer. This is done in the same way as those above, copying each of the lighting coefficients into the new structure.

Also, new memory is allocated for the texture filename, and it is copied into there.

OpenGL Programming/Stencil buffer

The final call to reloadTextures is used to actually load the textures and bind them to OpenGL texture objects. That function, from the Model base class, is described later. The last fragment frees the temporary buffer now that all the data has been copied and returns successfully.

So at this point, the protected member variables of the Apostika class are filled with the model information. You’ll note also that this is the only code in MilkshapeModel because it is the only code specific to Milkshape3D. Now, before the model can be rendered, it is necessary to load the textures for each of its materials.

This is done with the following code: For apodtila material, the texture is olengl using a function from NeHe’s base code slightly modified from it’s previous version. If the texture filename was an empty string, then it is not loaded, and instead the texture object identifier is set to 0 to indicate there is no texture.

OpenGL Programming/Stencil buffer – Wikibooks, open books for an open world

The Code – Drawing the Model Now we can start the code to ipengl the model! This is not difficult at all now that we have a careful arrangement of the data opengo in memory.

This first part saves the state of texture mapping within OpenGL so that the function does not disturb it. Note however that it does not preserve the material properties in the same way. Now we loop through each of the meshes and draw them individually: Now, each mesh has its own material properties, so we set up the OpenGL states according to that. If the materialIndex of the mesh is -1 however, there is no material for this mesh and it is drawn with openl OpenGL defaults.


The material properties are set according to the values stored in the model. Note that the texture is only bound and enabled if it is greater than 0.

If it is set to 0, you’ll recall, there was no texture, so texturing is disabled. Texturing is also disabled if there was no material at all for the mesh. The above section does the rendering of the triangles for the model. It loops through each of the triangles for the mesh, and then draws each of its three vertices, including the normal and texture coordinates. Remember that each triangle in a mesh and likewise each vertex in a triangle is indexed into the total model arrays these alostila the two index variables used.

This final fragment of code sets the texture mapping state back to its original apoztila. The only other code of interest in the Model class is the constructor oppengl destructor. These are self explanatory.

The constructor initializes all members to 0 or NULL for pointersand the destructor deletes the dynamic memory for all of the model structures. You should note that if you call the loadModelData function twice for one Model object, you will get memory leaks. The final topic I will discuss here is the changes to the base code to render using the new Model class, and where I plan to go from here in a future tutorial introducing skeletal animation.

At the top of the code in Lesson It is created in WinMain: But the model doesn’t need to be reloaded, as its data remains intact. What doesn’t remain intact are the textures that were bound to texture objects when we loaded the object.

Apostila PyOpenGL

So the following line is added to InitGL: This takes the place of calling LoadGLTextures as we used to. If there was more than one model in the scene, then this function must be called for all of them.

If you get white objects all of a sudden, then your textures have been thrown away and not reloaded correctly. Finally there is a new DrawGLScene function: If you haven’t apowtila gluLookAt before, essentially it places the camera at the position of the first 3 parameters, places the center of the scene at the position of the next 3 parameters, and the last 3 parameters describe the vector that is “up”. In this case, we look from apostlla, 75, 75 to 0,0,0 – as the model is drawn about 0,0,0 unless you translate before drawing it – and the positive Y-axis is facing up.

The function must be called first, and after loading the identity to behave in this fashion.