13 #include "omplapp/graphics/detail/assimpGUtil.h"
16 #include <OpenGL/gl.h>
31 void color4_to_float4(
const aiColor4D *c,
float f[4])
39 void set_float4(
float f[4],
float a,
float b,
float c,
float d)
47 void apply_material(
const aiMaterial *mtl)
56 float shininess, strength;
61 set_float4(c, 0.8f, 0.8f, 0.8f, 1.0f);
62 if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_DIFFUSE, &diffuse))
63 color4_to_float4(&diffuse, c);
64 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, c);
66 set_float4(c, 0.0f, 0.0f, 0.0f, 1.0f);
67 if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_SPECULAR, &specular))
68 color4_to_float4(&specular, c);
69 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, c);
71 set_float4(c, 0.2f, 0.2f, 0.2f, 1.0f);
72 if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_AMBIENT, &ambient))
73 color4_to_float4(&ambient, c);
74 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, c);
76 set_float4(c, 0.0f, 0.0f, 0.0f, 1.0f);
77 if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_EMISSIVE, &emission))
78 color4_to_float4(&emission, c);
79 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, c);
82 ret1 = aiGetMaterialFloatArray(mtl, AI_MATKEY_SHININESS, &shininess, &max);
84 ret2 = aiGetMaterialFloatArray(mtl, AI_MATKEY_SHININESS_STRENGTH, &strength, &max);
85 if((ret1 == AI_SUCCESS) && (ret2 == AI_SUCCESS))
86 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess * strength);
89 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 0.0f);
90 set_float4(c, 0.0f, 0.0f, 0.0f, 0.0f);
91 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, c);
95 if(AI_SUCCESS == aiGetMaterialIntegerArray(mtl, AI_MATKEY_ENABLE_WIREFRAME, &wireframe, &max))
96 fill_mode = wireframe != 0 ? GL_LINE : GL_FILL;
99 glPolygonMode(GL_FRONT_AND_BACK, fill_mode);
102 if((AI_SUCCESS == aiGetMaterialIntegerArray(mtl, AI_MATKEY_TWOSIDED, &two_sided, &max)) && (two_sided != 0))
103 glEnable(GL_CULL_FACE);
105 glDisable(GL_CULL_FACE);
109 void Color4f(
const aiColor4D *color)
111 glColor4f(color->r, color->g, color->b, color->a);
114 void recursive_render(
const aiScene *scene,
const aiNode* nd)
117 unsigned int n = 0, t;
118 aiMatrix4x4 m = nd->mTransformation;
121 aiTransposeMatrix4(&m);
123 glMultMatrixf((
float*)&m);
126 for (; n < nd->mNumMeshes; ++n) {
127 const aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];
129 apply_material(scene->mMaterials[mesh->mMaterialIndex]);
130 if(mesh->mNormals ==
nullptr)
131 glDisable(GL_LIGHTING);
133 glEnable(GL_LIGHTING);
134 if(mesh->mColors[0] ==
nullptr)
135 glDisable(GL_COLOR_MATERIAL);
137 glEnable(GL_COLOR_MATERIAL);
139 for (t = 0; t < mesh->mNumFaces; ++t)
141 const aiFace* face = &mesh->mFaces[t];
144 switch(face->mNumIndices)
146 case 1: face_mode = GL_POINTS;
break;
147 case 2: face_mode = GL_LINES;
break;
148 case 3: face_mode = GL_TRIANGLES;
break;
149 default: face_mode = GL_POLYGON;
break;
153 for(i = 0; i < (int)face->mNumIndices; i++)
155 int index = face->mIndices[i];
156 if(mesh->mColors[0] !=
nullptr)
157 Color4f(&mesh->mColors[0][index]);
158 if(mesh->mNormals !=
nullptr)
159 glNormal3fv(&mesh->mNormals[index].x);
160 glVertex3fv(&mesh->mVertices[index].x);
166 for (n = 0; n < nd->mNumChildren; ++n)
167 recursive_render(scene, nd->mChildren[n]);
171 int assimpRender(
const std::vector<const aiScene*> &scenes,
const std::vector<aiVector3D> &robotCenter)
173 int result = glGenLists(1);
176 glNewList(result, GL_COMPILE);
179 for (
unsigned int i = 0 ; i < scenes.size() ; ++i)
181 bool tr = robotCenter.size() > i;
184 aiMatrix4x4::Translation(-robotCenter[i], t);
185 aiTransposeMatrix4(&t);
187 glMultMatrixf((
float*)&t);
190 recursive_render(scenes[i], scenes[i]->mRootNode);
202 int assimpRender(
const aiScene* scene,
const aiVector3D &robotCenter)
204 std::vector<const aiScene*> scenes(1, scene);
205 std::vector<aiVector3D> centers(1, robotCenter);
206 return assimpRender(scenes, centers);
209 int assimpRender(
const std::vector<const aiScene*> &scenes)
211 std::vector<aiVector3D> empty;
212 return assimpRender(scenes, empty);
215 int assimpRender(
const aiScene* scene)
217 std::vector<const aiScene*> scenes(1, scene);
218 return assimpRender(scenes);