Advertisement
microwerx

ugly applying of parameters

Dec 30th, 2017
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.00 KB | None | 0 0
  1.     void SimpleSceneGraph::Render(SimpleProgram &program)
  2.     {
  3.         SimpleUniform InverseCameraMatrix;
  4.         SimpleUniform CameraMatrix;
  5.         SimpleUniform ProjectionXMatrix;
  6.         SimpleUniform ProjectionYMatrix;
  7.  
  8.         camera.projectionMatrix.LoadIdentity();
  9.         camera.projectionMatrix.Perspective(camera.fov, camera.imageAspect, camera.imageNearZ, camera.imageFarZ);
  10.  
  11.         ProjectionXMatrix = camera.projectionMatrix;
  12.  
  13.         camera.projectionMatrix.LoadIdentity();
  14.         camera.projectionMatrix.PerspectiveY(camera.fov, camera.imageAspect, camera.imageNearZ, camera.imageFarZ);
  15.  
  16.         ProjectionYMatrix = camera.projectionMatrix;
  17.  
  18.         CameraMatrix = camera.viewMatrix;
  19.         InverseCameraMatrix = camera.viewMatrix.AsInverse();
  20.  
  21.         program.Use();
  22.         program.ApplyUniform("NewProjectionXMatrix", ProjectionXMatrix);
  23.         program.ApplyUniform("NewProjectionYMatrix", ProjectionYMatrix);
  24.         program.ApplyUniform("NewInverseCameraMatrix", InverseCameraMatrix);
  25.         program.ApplyUniform("NewCameraMatrix", CameraMatrix);
  26.  
  27.         if (debugging)
  28.             hflog.info("%s(): Starting Render", __FUNCTION__);
  29.  
  30.         GLuint objectId = 0;
  31.         GLuint groupId = 0;
  32.         GLuint mtlId = 0;
  33.         GLuint mtllibId = 0;
  34.         string objectName;
  35.         string groupName;
  36.         string mtllibName;
  37.         string mtlName;
  38.  
  39.         GLint program_loc_Ka = program.GetUniformLocation("Ka");
  40.         GLint program_loc_Kd = program.GetUniformLocation("Kd");
  41.         GLint program_loc_Ks = program.GetUniformLocation("Ks");
  42.         GLint program_loc_Ke = program.GetUniformLocation("Ke");
  43.         GLint program_loc_Tr = program.GetUniformLocation("Tr");
  44.         GLint program_loc_Tf = program.GetUniformLocation("Tf");
  45.         GLint program_loc_Ns = program.GetUniformLocation("Ns");
  46.         GLint program_loc_Ni = program.GetUniformLocation("Ni");
  47.         GLint program_loc_map_Ka = program.GetUniformLocation("map_Ka");
  48.         GLint program_loc_map_Kd = program.GetUniformLocation("map_Kd");
  49.         GLint program_loc_map_Ks = program.GetUniformLocation("map_Ks");
  50.         GLint program_loc_map_Ke = program.GetUniformLocation("map_Ke");
  51.         GLint program_loc_map_Tr = program.GetUniformLocation("map_Tr");
  52.         GLint program_loc_map_Tf = program.GetUniformLocation("map_Tf");
  53.         GLint program_loc_map_Ni = program.GetUniformLocation("map_Ni");
  54.         GLint program_loc_map_Ns = program.GetUniformLocation("map_Ns");
  55.         GLint program_loc_map_bump = program.GetUniformLocation("map_bump");
  56.         GLint program_loc_map_normal = program.GetUniformLocation("map_normal");
  57.         GLint program_loc_map_Ka_mix = program.GetUniformLocation("map_Ka_mix");
  58.         GLint program_loc_map_Kd_mix = program.GetUniformLocation("map_Kd_mix");
  59.         GLint program_loc_map_Ks_mix = program.GetUniformLocation("map_Ks_mix");
  60.         GLint program_loc_map_Ke_mix = program.GetUniformLocation("map_Ke_mix");
  61.         GLint program_loc_map_Tf_mix = program.GetUniformLocation("map_Tf_mix");
  62.         GLint program_loc_map_Tr_mix = program.GetUniformLocation("map_Tr_mix");
  63.         GLint program_loc_map_Ni_mix = program.GetUniformLocation("map_Ni_mix");
  64.         GLint program_loc_map_Ns_mix = program.GetUniformLocation("map_Ns_mix");
  65.  
  66.         GLint program_loc_sphere_array = program.GetUniformLocation("Spheres");
  67.         GLint program_loc_sphere_count = program.GetUniformLocation("SpheresCount");
  68.         GLint program_loc_sphere_Ke = program.GetUniformLocation("SpheresKe");
  69.  
  70.         vector<float> spherePositions;
  71.         vector<float> sphereKe;
  72.         for (auto sphIt = spheres.begin(); sphIt != spheres.end(); sphIt++)
  73.         {
  74.             if (spherePositions.size() > 8)
  75.                 break;
  76.             Vector4f pos(0, 0, 0, 1);
  77.             Vector4f radius(1, 0, 0, 1);
  78.             pos = sphIt->second.transform * pos;
  79.             radius = sphIt->second.transform * radius;
  80.             radius = radius - pos;
  81.             float length = radius.length();
  82.             pos.w = length;
  83.             spherePositions.push_back(pos.x);
  84.             spherePositions.push_back(pos.y);
  85.             spherePositions.push_back(pos.z);
  86.             spherePositions.push_back(pos.w);
  87.  
  88.             Vector3f Ke = materials.SetLibraryMaterial(sphIt->second.mtllibName, sphIt->second.mtlName)->Ke;
  89.             sphereKe.push_back(Ke.r);
  90.             sphereKe.push_back(Ke.g);
  91.             sphereKe.push_back(Ke.b);
  92.             sphereKe.push_back(1.0f);
  93.         }
  94.         glUniform4fv(program_loc_sphere_array, (int)spherePositions.size(), &spherePositions[0]);
  95.         glUniform4fv(program_loc_sphere_Ke, (int)spherePositions.size(), &sphereKe[0]);
  96.         glUniform1i(program_loc_sphere_count, (int)spherePositions.size());
  97.  
  98.         // apply each material separately
  99.         for (auto libIt = materials.begin(); libIt != materials.end(); libIt++)
  100.         {
  101.             SimpleMaterialLibrary &mtllib = libIt->second;
  102.             mtllibName = mtllib.name;
  103.             mtllibId = materials.GetLibraryId(mtllib.name);
  104.             materials.SetLibrary(mtllib.name);
  105.            
  106.             if (debugging) cout << "SimpleSceneGraph::Render() -- using mtllib " << mtllib.name << endl;
  107.             for (auto mtlIt = mtllib.mtls.begin(); mtlIt != mtllib.mtls.end(); mtlIt++)
  108.             {
  109.                 mtlId = mtlIt->first;
  110.                 mtlName = materials.GetMaterialName(mtlId);
  111.                 while (mtlName.back() == '\0')
  112.                     mtlName.resize(mtlName.size() - 1);
  113.                 SimpleMaterial &mtl = mtlIt->second;
  114.                 materials.SetMaterial(mtlName);
  115.                
  116.                 if (debugging) cout << "SimpleSceneGraph::Render() -- using mtl " << mtlId << endl;
  117.  
  118.                 map<string, SimpleMap*> textures;
  119.                 GLuint unit = 0;
  120.                 if (!mtl.map_Ka.empty()) textures["map_Ka"] = materials.GetTextureMap(mtl.map_Ka);
  121.                 if (!mtl.map_Kd.empty()) textures["map_Kd"] = materials.GetTextureMap(mtl.map_Kd);
  122.                 if (!mtl.map_Ks.empty()) textures["map_Ks"] = materials.GetTextureMap(mtl.map_Ks);
  123.                 if (!mtl.map_Ke.empty()) textures["map_Ke"] = materials.GetTextureMap(mtl.map_Ke);
  124.                 if (!mtl.map_Ns.empty()) textures["map_Ns"] = materials.GetTextureMap(mtl.map_Ns);
  125.                 if (!mtl.map_Tf.empty()) textures["map_Tf"] = materials.GetTextureMap(mtl.map_Tf);
  126.                 if (!mtl.map_Tr.empty()) textures["map_Tr"] = materials.GetTextureMap(mtl.map_Tr);
  127.                 if (!mtl.map_bump.empty()) textures["map_bump"] = materials.GetTextureMap(mtl.map_bump);
  128.                 if (!mtl.map_normal.empty()) textures["map_normal"] = materials.GetTextureMap(mtl.map_normal);
  129.  
  130.                 if (mtl.map_Ka.empty()) glUniform1f(program_loc_map_Ka_mix, 0.0f);
  131.                 if (mtl.map_Kd.empty()) glUniform1f(program_loc_map_Kd_mix, 0.0f);
  132.                 if (mtl.map_Ks.empty()) glUniform1f(program_loc_map_Ks_mix, 0.0f);
  133.                 if (mtl.map_Ke.empty()) glUniform1f(program_loc_map_Ke_mix, 0.0f);
  134.                 if (mtl.map_Ns.empty()) glUniform1f(program_loc_map_Ns_mix, 0.0f);
  135.                 if (mtl.map_Tf.empty()) glUniform1f(program_loc_map_Tf_mix, 0.0f);
  136.                 if (mtl.map_Tr.empty()) glUniform1f(program_loc_map_Tr_mix, 0.0f);
  137.  
  138.                 for (auto tmapIt = textures.begin(); tmapIt != textures.end(); tmapIt++)
  139.                 {
  140.                     SimpleMap *pMap = tmapIt->second;
  141.                     if (pMap) {
  142.                         pMap->unitId = 0;// unit;
  143.                         pMap->samplerId = pMap->textureObject.samplerObject.GetId();
  144.                         pMap->textureId = pMap->textureObject.GetTextureId();
  145.                         pMap->textureObject.Bind(unit, false);
  146.                         glTexParameterf(pMap->textureObject.GetTarget(), GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0);
  147.                         textures[mtlName] = pMap;
  148.                         unit++;
  149.                         GLint program_loc = program.GetUniformLocation(tmapIt->first.c_str());
  150.                         if (program_loc >= 0)
  151.                             glUniform1i(program_loc, pMap->unitId);
  152.                         string tmp = tmapIt->first + "_mix";
  153.                         program_loc = program.GetUniformLocation(tmp.c_str());
  154.                         glUniform1f(program_loc, 1.0f);
  155.                     }
  156.                 }
  157.  
  158.                 // Apply Material Uniforms to the program shader
  159.                 glUniform3fv(program_loc_Kd, 1, mtl.Kd.v);
  160.                 glUniform3fv(program_loc_Ks, 1, mtl.Ks.v);
  161.                 glUniform3fv(program_loc_Ke, 1, mtl.Ke.v);
  162.                 glUniform3fv(program_loc_Ka, 1, mtl.Ka.v);
  163.                 glUniform1fv(program_loc_Ni, 1, &mtl.Ni);
  164.                 glUniform1fv(program_loc_Ns, 1, &mtl.Ns);
  165.                 glUniform3fv(program_loc_Tr, 1, mtl.Tf.v);
  166.                 glUniform1fv(program_loc_Tf, 1, &mtl.Tr);
  167.  
  168.                 for (auto geoIt = geometry.begin(); geoIt != geometry.end(); geoIt++)
  169.                 {
  170.                     SimpleGeometryGroup &geo = geoIt->second;
  171.                     if (debugging) cout << "SimpleSceneGraph::Render() -- using OBJ " << geo.objectName << endl;
  172.                     objectId = geo.objectId;
  173.                     groupId = 0;
  174.  
  175.                     renderer.ApplyIdToMtlNames(mtlName, mtlIt->first);
  176.  
  177.                     // Apply object specific uniforms like transformation matrices
  178.                     SimpleUniform ModelViewMatrix;
  179.                     ModelViewMatrix = geo.transform;
  180.                     program.ApplyUniform("NewModelViewMatrix", ModelViewMatrix);
  181.  
  182.                     // No iterate through each object and render it with this material
  183.                     //renderer.RenderIf(objectId, groupId, mtllibId, mtlId, false);
  184.                     renderer.RenderIf(geo.objectName, "", geo.mtllibName, mtlName, false);
  185.                 }
  186.  
  187.                 // Turn off textures
  188.                 for (auto tmapIt = textures.begin(); tmapIt != textures.end(); tmapIt++)
  189.                 {
  190.                     SimpleMap *pMap = tmapIt->second;
  191.                     if (pMap) {
  192.                         glutBindTexture(pMap->unitId, pMap->textureObject.GetTarget(), 0);
  193.                        
  194.                         GLint program_loc = program.GetUniformLocation(tmapIt->first.c_str());
  195.                         if (program_loc >= 0)
  196.                             glUniform1i(program_loc, pMap->unitId);
  197.                         string tmp = tmapIt->first + "_mix";
  198.                         program_loc = program.GetUniformLocation(tmp.c_str());
  199.                         glUniform1f(program_loc, 0.0f);
  200.                     }
  201.                 }
  202.                 glutSetActiveTexture(GL_TEXTURE0);
  203.             }
  204.         }
  205.  
  206.         if (debugging) cout << "SimpleSceneGraph::Render() -- END\n";
  207.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement