1

I want to render more than one mesh with each one having its own transformation. Using this tutorial to begin with I have modified few things like added a new model:

Model anotherModel( "path_to_obj_file" );

and after the "ourModel.Draw( shader )" I have added :

    glm::mat4 view = camera.GetViewMatrix( );
    glUniformMatrix4fv( glGetUniformLocation( shader.Program, "projection" ), 1, GL_FALSE, glm::value_ptr( projection ) );
    glUniformMatrix4fv( glGetUniformLocation( shader.Program, "view" ), 1, GL_FALSE, glm::value_ptr( view ) );
     glm::mat4 model2;
    model2 = glm::translate( model2, glm::vec3( 0.0f, 0.0, 0.0f ) ); // Translate it down a bit so it's at the center of the scene
    model2 = glm::scale( model2, glm::vec3( 0.1, 0.1, 0.1 ) );  // It's a bit too big for our scene, so scale it down
    model2 = glm::rotate(model2, (float)0.0, glm::vec3(1.0f, 1.0f, 1.0f));
    glUniformMatrix4fv( glGetUniformLocation( shader.Program, "model2" ), 1, GL_FALSE, glm::value_ptr( model2 ) );

and finally :

anotherModel.Draw( shader );

However I am not getting the result of individual transformation for both models.Only the first transformation persists although "anotherModel" is getting rendered.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 2
    Please show us the vertex shaders for both of your models. I highly suspect they use the same transformation matrix, so `"model2"` is never used. – hidefromkgb Jan 29 '19 at 11:24
  • 1
    You can check the return value of `glGetUniformLocation( shader.Program, "model2" )`. If it's -1 then the uniform `model2` isn't found in compiled shader program. This may even happen if it's in the source code but optimized "away" by the shader compiler (as I just recently noticed). – Scheff's Cat Jan 29 '19 at 12:11
  • I see only single `Draw()` call hope you are setting uniforms for obj1, render obj1 then set uniforms for obj2 and render obj2. If not than that is where you are wrong, if yes then `Schef`'s comment is most likely the cause ... Always check the GLSL logs so you actualy see if you link and compile correctly or not and why... – Spektre Jan 29 '19 at 12:40
  • I am new to opengl and hence dont know about shaders.Used the same shader as in the tutorial. Moreover I should mention that I am using the "glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );" and the wireframes are getting rendered – Argha Chakraborty Jan 29 '19 at 13:11
  • @hidefromkgb I am using the same shader program ...please throw some light I am very new to OpenGL. – Argha Chakraborty Jan 29 '19 at 14:10
  • 2
    @ArghaChakraborty if the shader is the same then after you draw the first model you need to alter the `view` matrix and reload the `"view"` uniform to be able to draw the second model from a different view. Don\`t forget to save the original `view` if you need it, and then restore it back after the draw call for the second model. – hidefromkgb Jan 29 '19 at 14:17
  • Thanks ..your solution works @hidefromkgb – Argha Chakraborty Jan 30 '19 at 08:46
  • 1
    @ArghaChakraborty Cool! BTW, now when it works the method can be optimized: you don\`t need to actually fiddle with the `view` matrix, its C++ name doesn\`t matter at all. What\`s important here is under which name you pass it as a uniform. So you can e.g. do `glUniformMatrix4fv(glGetUniformLocation(shader.Program, "view"), 1, GL_FALSE, glm::value_ptr(model2));` and it\`d be perfectly valid. And of course please answer your own question when you\`re done (it\`s both permitted and encouraged) so it could be recorded in SO history for the benefit of all the other people having the same question. – hidefromkgb Jan 30 '19 at 09:43
  • Thank you I'll do that.. both optimization and answering. – Argha Chakraborty Jan 30 '19 at 17:18

0 Answers0