Using HelloGL to Display a Model with Assimp
-
Hey everybody,
i got a working code in MSVC 2010 to load a 3d model and display it in a glut-window. Now i try to include this code into a QtGui. Because i have barely any experience with Qt i started off with the HelloGL-Example and tried to enable assimp in this code. The problem is that hen i run this it just paints the usual blueish background but nothing more. Unless i turn the Qtlogo->draw back on. then the example runs as if i hadn't done any changes to it.
@
GLFrame cameraFrame;
GLMatrixStack modelViewMatrix;
M3DMatrix44f mCamera;
const aiScene* scene = NULL;
aiVector3D scene_min, scene_max, scene_center;
GLuint scene_list = 0;
#define aisgl_min(x,y) (x<y?x:y)
#define aisgl_max(x,y) (y>x?y:x)
static float angle = 0.f;
GLfloat ambientLight[]={0.5f, 0.5f, 0.5f, 1.0f};GLWidget::GLWidget(QWidget *parent)
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
{
qtGreen = QColor::fromCmykF(0.40, 0.0, 1.0, 0.0);
qtPurple = QColor::fromCmykF(0.39, 0.39, 0.0, 0.0);//NEU
struct aiLogStream stream;
stream = aiGetPredefinedLogStream(aiDefaultLogStream_STDOUT,NULL);
aiAttachLogStream(&stream);
stream = aiGetPredefinedLogStream(aiDefaultLogStream_FILE,"assimp_log.txt");
aiAttachLogStream(&stream);
glClearColor(0.0f,1.0f,1.0f,1.f);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
glLightfv(GL_LIGHT0,GL_DIFFUSE,ambientLight);
glEnable(GL_LIGHT0); // Uses default lighting parameters
glEnable(GL_DEPTH_TEST);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
glEnable(GL_NORMALIZE);
glEnable(GL_CULL_FACE);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_DEPTH_TEST);
}GLWidget::~GLWidget()
{
}
void GLWidget::initializeGL()
{
qglClearColor(qtPurple.dark());logo = new QtLogo(this, 64); logo->setColor(qtGreen.dark()); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glShadeModel(GL_SMOOTH); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_MULTISAMPLE); static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 }; glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
scene = aiImportFile("C:/Magen_18Kfaces.ply",aiProcessPreset_TargetRealtime_MaxQuality);//WusonOBJ.obj
if(scene){
get_bounding_box(&scene_min,&scene_max);
scene_center.x = (scene_min.x + scene_max.x) / 2.0f;
scene_center.y = (scene_min.y + scene_max.y) / 2.0f;
scene_center.z = (scene_min.z + scene_max.z) / 2.0f;
}
}void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, 0.0, -10.0);
glRotatef(xRot / 16.0, 1.0, 0.0, 0.0);
glRotatef(yRot / 16.0, 0.0, 1.0, 0.0);
glRotatef(zRot / 16.0, 0.0, 0.0, 1.0);
logo->draw();
float tmp;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();gluLookAt(0.f,0.f,1.f,0.f,0.f,-5.f,0.f,1.f,0.f); tmp = scene_max.x-scene_min.x; tmp = aisgl_max(scene_max.y - scene_min.y,tmp); tmp = aisgl_max(scene_max.z - scene_min.z,tmp); tmp = 1.f / tmp; glScalef(tmp, tmp, tmp); // center the model glTranslatef( -scene_center.x, -scene_center.y, -scene_center.z);
if(scene_list == 0) {
scene_list = glGenLists(1);
glNewList(scene_list, GL_COMPILE);
// now begin at the root node of the imported data and traverse
// the scenegraph by multiplying subsequent local transforms
// together on GL's matrix stack.
recursive_render(scene, scene->mRootNode);
glEndList();
}
glCallList(scene_list);
recursive_render(scene, scene->mRootNode);
modelViewMatrix.PopMatrix();
}
void GLWidget::resizeGL(int width, int height)
{
int side = qMin(width, height);
glViewport((width - side) / 2, (height - side) / 2, side, side);glMatrixMode(GL_PROJECTION); glLoadIdentity();
#ifdef QT_OPENGL_ES_1
glOrthof(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0);
#else
glOrtho(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0);
#endif
glMatrixMode(GL_MODELVIEW);
}// New Functions
void get_bounding_box (aiVector3D* min, aiVector3D* max)
{
//Need to shorten code because max number of charcters..
}
void get_bounding_box_for_node (const struct aiNode* nd, aiVector3D* min, aiVector3D* max, aiMatrix4x4* trafo)
{
//also shortend
}
void recursive_render (const aiScene sc, const aiNode nd)
{
unsigned int i;
unsigned int n = 0, t;
aiMatrix4x4 m = nd->mTransformation;// update transform
aiTransposeMatrix4(&m);
glPushMatrix();
glMultMatrixf((float*)&m);// draw all meshes assigned to this node
for (; n < nd->mNumMeshes; ++n) {
const aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];//apply_material(sc->mMaterials[mesh->mMaterialIndex]);
if(mesh->mNormals == NULL) {
glDisable(GL_LIGHTING);
} else {
glEnable(GL_LIGHTING);
}for (t = 0; t < mesh->mNumFaces; ++t) {
const aiFace* face = &mesh->mFaces[t];
GLenum face_mode;switch(face->mNumIndices) {
case 1: face_mode = GL_POINTS; break;
case 2: face_mode = GL_LINES; break;
case 3: face_mode = GL_TRIANGLES; break;
default: face_mode = GL_POLYGON; break;
}glBegin(face_mode);
for(i = 0; i < face->mNumIndices; i++) {
int index = face->mIndices[i];
if(mesh->mColors[0] != NULL)
glColor4fv((GLfloat*)&mesh->mColors[0][index]);
else
glColor4f(0.0,0.5,0.5,1.0);
if(mesh->mNormals != NULL)
glNormal3fv(&mesh->mNormals[index].x);
glVertex3fv(&mesh->mVertices[index].x);
}glEnd();
}}
// draw all children
for (n = 0; n < nd->mNumChildren; ++n) {
recursive_render(sc, nd->mChildren[n]);
}glPopMatrix();
}
@So this is a part of the .cpp of the widget.
Did anybody try to use assimp with qt and has some insight on my problem??
I would be so glad!Thanks in advance!!
Greetings from Munich