Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Step 1: Fix the base code to be able to display a mesh from an obj file with multiple shapes. By default the older code
Step 1: Fix the base code to be able to display a mesh from an obj file with multiple "shapes. By default the older code we have worked with on displayed "shape[O]" from the obj file. The new base code is set up so that you can create a "Shape" object for each of the tiny_obj "shapes" You need to fix the code in "initGeom" in main.cpp: you will need access to the entire object, so move code to load the object from the shape class to you will need a collection of shapes, consider using a vector of shapes For every shape loaded from the obj file, create a shape, measure it and do any necessary initialization compute the bounds of the entire mesh (so that you can reposition it and display it at the origin with a reasonable scale). * void initGeom (const sta: :string& resourcebirectorY) /EXAMPLE new set up to read one shape from one obj file - convert to read several // Initialize mesh // Load geometry // Some obj files contain material information.We'11 ignore them for this assignment. vector<:shape t> TOshapes vector objMaterials: string errStr //load in the mesh and make the shape(s) bool rc= tinyobj: :LoadObj (TOshapes, objMaterials, errStr , (resourceDirectory + "/SmoothSphere.obj ").c str()); if (rc) cerr () ; sphere->createShape (TOshapes [0]) sphere->measure ) sphere-init) - //read out information stored in the shape about its size something like this... //then do something with that information. . gMin.x - sphere->min.x; gMin.y- sphere->min.y; void render () / Get current frame buffer size. int width, height glfwGetFramebufferSize (windowManager->getHandle), &width, &height) glViewport (o, 0, width, height) / Clear framebuffer. glclear (GL COLOR BUFFER BIT GL DEPTH BUFFER BIT): //Use the matrix stack for Lab 6 float aspect- width/ (float)height // Create the matrix stacks- please leave these alone for now auto Projection make_shared(); auto Model - make_shared) / Apply perspective projection. Projection->pushMatrix() Projection->perspective (45.0f, aspect, 0.lf, 100.0f) //View is identity - for now View->pushMatrix) / Draw a stack of cubes with indiviudal transforms prog->bind ) glUniformMatrix4fv (prog->getUniform("P"), 1, GL_FALSE, value_ptr (Projection->topMatrix))) glUniformMatrix4fv (prog->getniform ("V"), 1, GL_FALSE, value_ptr (View->topMatrix ())) // draw mesh Model->pushMatrix ) Model->loadIdentity) global" translate Model->translate (vec3 (0, 0, -5)) Model->pushMatrix ) Model->scale (vec3 (0.5, 0.5, 0.5)) glUniformMatrix4fv (prog->getUniform ("M"), 1, GL_FALSE, value_ptr (Model->topMatrix))) sphere->draw (prog) Model-popMatrix) Model-popMatrix) prog->unbind () // Pop matrix stacks. Projection->popMatrix) View->popMatrix) include #include #include "Shape . h" #include #include "GLSL"h" "Program . h" using namespace std Shape: : Shape ) eleBufID (O) posBufID (O), norBufID (O), texBufID (0), vaoID(O) min - glm: :vec3 (o) max - glm: :vec3 (o) Shape: :~Shape () /*copy the data from the shape to this object / void Shape: :createShape (tinyobj: :shape_t & shape,) posBufshape.mesh.positions norBufshape.mesh.normals: tex8uf shape . mesh. texcoords ; eleBufshape.mesh.indices void Shape: :measure) float minX, minY, minZ; float maxX, maxY, maxZ; //Go through all vertices to determine min and max of each dimension for (size t v = 0; v max2 ) max2 = posBuf [ 3 *v+2 ] ; min . x minx ; min. y = miny; min.z - minZ max.x-maxX; max.z -maxz; void Shape:init () // Initialize the vertex array object g1GenvertexAr rays (1, &vaoID) ; glBindVertexArray (vaoID) // Send the position array to the GPU glGenBuffers (, &posBufID) glBindBuffer (GL ARRAY BUFFER, posBufID) glBufferData (GL_ARRAY BUEFER, posBuf.size ) *sizeof (float),&posBuf [0], GL_STATIC_DRAW); // Send the normal array to the GPU if (norBuf.empty )) norBufID0 else glGenBuffers (1, &norBufID) glBindBuffer (GL ARRAY BUFFER, norBufID) glBufferData (GL ARRAY BUFFER, norBuf.size ()sizeof (float), &norBuf ro1, GL STATIC DRAW) // Send the texture array to the GPU if (texBuf.empty )) texBufID0 else glGenBuffers (, &texBufID) glBindBuffer (GL ARRAY BUFFER, texBufID) glBufferData (GL ARRAY BUFFER, texBuf.size ()sizeof (float), &texBuf ro1, GL STATIC DRAW) // Send the element array to the GPU glGenBuffers (1, &eleBufID) glBindBuffer (GL_ELEMENT_ARRAY BUFFER, eleBufID) glBufferData (GL ELEMENT ARRAY BUFFER, eleBuf.size )tsizeof (unsigned int), eleBuf T01, GL STATIC DRAN) // Unbind the arrays glBindBuffer (GL ARRAY BUFFER, 0) glBindBuffer (GL_ELEMENT_ARRAY BUFFER, 0) assert (glGetError) GL NO ERROR) Step 1: Fix the base code to be able to display a mesh from an obj file with multiple "shapes. By default the older code we have worked with on displayed "shape[O]" from the obj file. The new base code is set up so that you can create a "Shape" object for each of the tiny_obj "shapes" You need to fix the code in "initGeom" in main.cpp: you will need access to the entire object, so move code to load the object from the shape class to you will need a collection of shapes, consider using a vector of shapes For every shape loaded from the obj file, create a shape, measure it and do any necessary initialization compute the bounds of the entire mesh (so that you can reposition it and display it at the origin with a reasonable scale). * void initGeom (const sta: :string& resourcebirectorY) /EXAMPLE new set up to read one shape from one obj file - convert to read several // Initialize mesh // Load geometry // Some obj files contain material information.We'11 ignore them for this assignment. vector<:shape t> TOshapes vector objMaterials: string errStr //load in the mesh and make the shape(s) bool rc= tinyobj: :LoadObj (TOshapes, objMaterials, errStr , (resourceDirectory + "/SmoothSphere.obj ").c str()); if (rc) cerr () ; sphere->createShape (TOshapes [0]) sphere->measure ) sphere-init) - //read out information stored in the shape about its size something like this... //then do something with that information. . gMin.x - sphere->min.x; gMin.y- sphere->min.y; void render () / Get current frame buffer size. int width, height glfwGetFramebufferSize (windowManager->getHandle), &width, &height) glViewport (o, 0, width, height) / Clear framebuffer. glclear (GL COLOR BUFFER BIT GL DEPTH BUFFER BIT): //Use the matrix stack for Lab 6 float aspect- width/ (float)height // Create the matrix stacks- please leave these alone for now auto Projection make_shared(); auto Model - make_shared) / Apply perspective projection. Projection->pushMatrix() Projection->perspective (45.0f, aspect, 0.lf, 100.0f) //View is identity - for now View->pushMatrix) / Draw a stack of cubes with indiviudal transforms prog->bind ) glUniformMatrix4fv (prog->getUniform("P"), 1, GL_FALSE, value_ptr (Projection->topMatrix))) glUniformMatrix4fv (prog->getniform ("V"), 1, GL_FALSE, value_ptr (View->topMatrix ())) // draw mesh Model->pushMatrix ) Model->loadIdentity) global" translate Model->translate (vec3 (0, 0, -5)) Model->pushMatrix ) Model->scale (vec3 (0.5, 0.5, 0.5)) glUniformMatrix4fv (prog->getUniform ("M"), 1, GL_FALSE, value_ptr (Model->topMatrix))) sphere->draw (prog) Model-popMatrix) Model-popMatrix) prog->unbind () // Pop matrix stacks. Projection->popMatrix) View->popMatrix) include #include #include "Shape . h" #include #include "GLSL"h" "Program . h" using namespace std Shape: : Shape ) eleBufID (O) posBufID (O), norBufID (O), texBufID (0), vaoID(O) min - glm: :vec3 (o) max - glm: :vec3 (o) Shape: :~Shape () /*copy the data from the shape to this object / void Shape: :createShape (tinyobj: :shape_t & shape,) posBufshape.mesh.positions norBufshape.mesh.normals: tex8uf shape . mesh. texcoords ; eleBufshape.mesh.indices void Shape: :measure) float minX, minY, minZ; float maxX, maxY, maxZ; //Go through all vertices to determine min and max of each dimension for (size t v = 0; v max2 ) max2 = posBuf [ 3 *v+2 ] ; min . x minx ; min. y = miny; min.z - minZ max.x-maxX; max.z -maxz; void Shape:init () // Initialize the vertex array object g1GenvertexAr rays (1, &vaoID) ; glBindVertexArray (vaoID) // Send the position array to the GPU glGenBuffers (, &posBufID) glBindBuffer (GL ARRAY BUFFER, posBufID) glBufferData (GL_ARRAY BUEFER, posBuf.size ) *sizeof (float),&posBuf [0], GL_STATIC_DRAW); // Send the normal array to the GPU if (norBuf.empty )) norBufID0 else glGenBuffers (1, &norBufID) glBindBuffer (GL ARRAY BUFFER, norBufID) glBufferData (GL ARRAY BUFFER, norBuf.size ()sizeof (float), &norBuf ro1, GL STATIC DRAW) // Send the texture array to the GPU if (texBuf.empty )) texBufID0 else glGenBuffers (, &texBufID) glBindBuffer (GL ARRAY BUFFER, texBufID) glBufferData (GL ARRAY BUFFER, texBuf.size ()sizeof (float), &texBuf ro1, GL STATIC DRAW) // Send the element array to the GPU glGenBuffers (1, &eleBufID) glBindBuffer (GL_ELEMENT_ARRAY BUFFER, eleBufID) glBufferData (GL ELEMENT ARRAY BUFFER, eleBuf.size )tsizeof (unsigned int), eleBuf T01, GL STATIC DRAN) // Unbind the arrays glBindBuffer (GL ARRAY BUFFER, 0) glBindBuffer (GL_ELEMENT_ARRAY BUFFER, 0) assert (glGetError) GL NO ERROR)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started