Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 480/680 Part 1: Model Loading.

Similar presentations


Presentation on theme: "CS 480/680 Part 1: Model Loading."— Presentation transcript:

1 CS 480/680 Part 1: Model Loading

2 Today’s Topics What is Assimp? Installation Usage Recap Pseudocode
Resources Questions

3 What is Assimp? Assimp (written in C++) is an Open Source library that is used by developers to import 3D models in a variety of file formats. Your model loader is only capable of loading .obj files. Assimp can handle .blend, .3ds, .obj, .lwo, .lws, .x, .cob, and more.

4 What is Assimp? Assimp will load all of these file formats into the same data structure which is used for further processing. Operations such as computing normal and tangent vectors are not only available, but also optimized.

5 Installation Install assimp3, which is compatible with Ubuntu or higher. sudo apt-get install libassimp-dev Modifications to your makefile: In the LIBS section, you’ll want to add -lassimp.

6 Includes Furthermore, you’ll want to include the following in either your main.cpp file or your model loader class (recommended, develops abstraction in your code). #include <assimp/Importer.hpp> Includes the Assimp importer, which is used to read your .obj files. #include <assimp/scene.h> Includes the data structure that is used for further processing.

7 Includes More libraries to include:
#include <assimp/postprocess.h> Includes flags for post processing (calculating tangents, normals, etc.). #include <assimp/color4.h> Includes the aiColor4 object, which is used to handle colors associated with your objects.

8 Usage First off, you’ll want to declare an Assimp::Importer object and use it to read in the data from your .obj file, like so: Assimp::Importer importer; const aiScene *myScene = importer.ReadFile(yourObjFile, aiProcess_Triangulate); Triangles are a GPU’s favorite shape. Your myScene variable is a pointer to an impressively large data structure that has everything you’d ever want to know about your model.

9 Usage An aiScene object will consist of the following attributes (there’s much more within the object; we’re only covering the portion you need for your assignment): unsigned int mNumMeshes; Number of meshes in the scene (typically just one). aiMesh **mMeshes; This is an array of type aiMesh* and of size mNumMeshes, where each element of the array is pointing to a single aiMesh object, not an array of them. It’s more like a 1D array, the elements of which are pointers to single object.

10 Usage What information can we get out of an aiMesh object?
unsigned int mNumFaces; Number of primitives (can be triangles, polygons, or lines, but since we used the aiProcess_Triangulate flag, they’re triangles). aiFace *mFaces; A pointer to an array of aiFace objects (of size mNumFaces), which are the faces that make up the mesh (same faces that you encountered in PA04). aiVector3D *mVertices; An array of vertex positions. This is the information we need for our model, but we have to do some further processing.

11 Usage We also just said that mFaces is a pointer to an array of aiFace objects, but what information can we get out of an aiFace object? unsigned int *mIndices; A pointer to the indices array. These are the same indices that were in the faces you encountered in PA04. Again, since we used the aiProcess_Triangulate flag, this array will have a size of three (because triangles). unsigned int mNumIndices; This value will be three (because triangles).

12 Putting It Together There’s still one more issue to address:
In PA04, you had to use the indices in the faces to actually obtain the coordinate info regarding each vertex. We have to do something similar in Assimp. To obtain the coordinate info of each vertex, we use the indices from the mIndices array to offset into our aiMesh’s aiVector3D *mVertices attribute. In other words, we use the mIndices values to obtain the vertex coordinates from mVertices within the aiMesh object.

13 Summary Confused? We were too. To recap:
An aiScene contains (usually one) aiMesh. An aiMesh has many aiFaces. Each aiFace contains three indices (located in mIndices). An aiMesh also contains a aiVector3D *mVertices attribute. This is the vertex coordinate info that the GPU needs to render our models, but we need the mIndices values to correctly obtain this data.

14 Pseudocode Initialize an Assimp::Importer object and use it to read in your .obj file. Find out how many aiMesh objects are in your aiScene. For the .obj files we use, there’s usually only one aiMesh, so the following pseudocode will require modification if more than one aiMesh is present. What modifications would you make? Obtain the single aiMesh object from the aiScene. Iterate through each aiFace in the given aiMesh. Use the values found in the current aiFace’s mIndices array to offset into the aiMesh’s mVertices array and save these values to an array (of Vertex structs) that can be sent to the GPU. What about color?

15 Resources Assimp’s documentation: A very detailed link:
A very detailed link:

16 Vertex Shader Applications
Moving Vertices Morphing Wave Motion Fractals Lighting More realistic models Cartoon Shaders

17 Fragment Shader Applications:
Per Vertex Lighting Per Fragment Lighting

18 Fragment Shader Applications:
Texture mapping Smooth shading Environment Mapping Bump Mapping

19 GLSL OpenGL Shading Language
Vertex and Fragment shaders written in GLSL Part of OpeGL 2.0 and up As of OpenGL 3.1, application must use shaders. Example code of a vertex shader: const vec4 red = vec4(1.0, 0.0, 0.0, 1.0); out vec3 color_out; void main(void){ gl_Position = vPosition; color_out = red; }

20 Vertex & Fragment shader pipelin

21 Data Types C types: int, float, bool, double, uint Arrays/Vectors:
float vec2, vec3, vec4 Also int (ivec) and boolean (bvec) Matrices of floats or doubles: Square matrices: 2x2, 3x3, or 4x4 : mat2, mat3, mat4 Stored by columns Standard referencing m[row][col] non-square matrices: size {2,3,4}x{2,3,4}: mat3x4, dmat4x2 texture samplers sampler1D, sampler2D, sampler3D for 1, 2, 3D power-of-two textures sampler2DRect for rectangle textures samplerCube for cubemap textures

22 Data Types Variable declaration and initialization very similar to C-style float a = 1.0; vec3 point = vec3(1.0,1.0,4.0); mat2 mat = mat2(1.0,0.0,0.0,1.0); (matrices are assigned in column major order) C++ style constructors vec3 a = vec3(1.0, 2.0, 3.0) vec2 b = vec2(a)

23 Pointers There are no pointers in GLSL
We can use C structs which can be copied back from functions. Because matrices and vectors are basic types they can be passed into and be output from GLSL functions mat3 func(mat3 a)

24 Qualifiers GLSL has many of the same qualifiers (const) as C/C++
Need others due to the nature of the execution model Variables can change Once per primitive Once per vertex Once per fragment Any time in the program

25 Passing Values call by value‐return. Two possibilities
in: variables copied in out: returned values are copied back inout (deprecated) Example: vertex shader using out const vec4 red = vec4(1.0, 0.0, 0.0, 1.0); out vec3 color_out; void main(void){ gl_Position = vPosition; color_out = red; }

26 Vertex Attribute Variables
Vertex Attribute variables can change at most once per vertex Flows down the pipeline with each vertex Position, color, normal, texture cooddinates There are a few built in variables such as gl_Position but most have been deprecated User defined (in application program) Use in qualifier to get to shader (read only) in float temperature in vec3 velocity layout (location = 0) in vec3 vp;

27 Uniform parameters Variables that are set by the application but are constant for the entire primitive Transformation matrices, parameters of light sources, textures Can be changed in the application and sent to the shader Cannot be changed in the shader. uniform mat4 projectionMatrix;

28 Uniform parameters To set parameters, use glGetUniformLocation, glUniform* in application After shader is active, before rendering Example In shader declare uniform float a; In application, set a using GLuint p; //… initialize program p int i=glGetUniformLocation(p,”a”); glUniform1f(i, 1.f);

29 Some Built-In Variables
vertex shader in: int gl_VertexID (for indexed rendering) vertex shader out: vec4 gl_Position (clip space position), - Required float gl_PointSize (pixel width/height for point rendering) Note other out variables will be interpolated during rasterization (texture coordiantes, …) fragment shader in: vec4 gl_FragCoord (location in window space) Note: for user defined variables you Need to have same variable name as output (declared as out) of vertex shader fragment shader out: float gl_FragDepth (fragment depth)

30 Operators and Functions
Standard C functions Trigonometric: cos, sin, tan, etc Arithmetic: log, min, max, abs, etc Normalize, reflect, length Overloading of vector and matrix types mat4 a; vec4 b, c, d; c = b*a; // a column vector stored as a 1d array d = a*b; // a row vector stored as a 1d array

31 Swizzling and Selection
Can refer to array elements by element using [] or selection (.) operator with x, y, z, w r, g, b, a s, t, p, q vec4 a; a[2], a.b, a.z, a.p are the same Swizzling operator lets us manipulate components a.yz = vec2(1.0, 2.0);

32 Open GL Quick Reference Card
quick-reference-card.pdf GLSL starts on page 9

33 Sample Screenshots The next few slides will have screenshots from the code section of the presentation.

34

35

36 Questions?


Download ppt "CS 480/680 Part 1: Model Loading."

Similar presentations


Ads by Google