Original Post
Hi, I have a problem with the displaying of a 3d model in OpenGL which I loaded with the lib3ds library. The problem is that it doesn't seem to draw all faces of the model, here's what I mean:
[Program's output.] And here is the code: My guess is the problem is somehwere in the draw(void) function: But I can't find out what I did wrong or what I didn't do at all. So therefore I've come here to ask you humbly for help ([grin]). Thanks in advance.
[Program's output.] And here is the code: #include <allegro.h>
#include <alleggl.h>
#include <gl/glu.h>
#include <lib3ds/camera.h>
#include <lib3ds/file.h>
#include <lib3ds/material.h>
#include <lib3ds/mesh.h>
static Lib3dsFile* katana = NULL; //model file
int init(void) {
allegro_init();
install_allegro_gl();
install_keyboard();
install_timer();
set_color_depth(24);
if(set_gfx_mode(GFX_OPENGL_FULLSCREEN, 640, 480, 0, 0) < 0) {
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
allegro_message("Errors:\nAllegro: %s\nAllegroGL: %s", allegro_error, allegro_gl_error);
return 0;
}
//open and check if file is opened
katana = lib3ds_file_load("..\\models\\katana\\katana.3ds");
if(!katana) {
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
allegro_message("no katana");
return 0;
}
//check for meshes
if(!katana->meshes) {
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
allegro_message("no meshes");
return 0;
}
//check for cameras
if(!katana->cameras) {
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
allegro_message("no cameras");
return 0;
}
//set some opengl parameters
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
return 1;
}
void setcamera(void) {
Lib3dsCamera* camera = katana->cameras; //camera in model file
//set up viewport and perspective with camera properties
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, SCREEN_W, SCREEN_H);
gluPerspective(camera->fov, SCREEN_W / SCREEN_H, camera->near_range, camera->far_range);
//set up cameraview
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(camera->position[0], camera->position[1], camera->position[2],
camera->target[0], camera->target[1], camera->target[2],
0.0, 1.0, 0.0);
glFlush();
allegro_gl_flip();
}
void draw(void) {
Lib3dsMesh* mesh = katana->meshes; //current mesh in model file
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
while(mesh) {
for(unsigned long i = 0; i < mesh->faces; i++) {
//set material to the current face material
Lib3dsMaterial* material = lib3ds_file_material_by_name(katana, mesh->faceL.material);
glMaterialf(GL_FRONT, GL_SHININESS, material->shininess);
glMaterialfv(GL_FRONT, GL_AMBIENT, material->ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, material->diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, material->specular);
//draw face
glVertex3fv(mesh->pointL[mesh->faceL.points[0]].pos);
glVertex3fv(mesh->pointL[mesh->faceL.points[1]].pos);
glVertex3fv(mesh->pointL[mesh->faceL.points[2]].pos);
}
//go to next mesh if any
if(mesh->next) mesh = mesh->next;
else mesh = NULL;
}
glEnd();
glFlush();
allegro_gl_flip();
}
int main(void) {
if(!init()) return 0; //initialize
setcamera(); //setup the camera
do {
draw(); //draw scene
rest(1000 / 24); //rest
}while(!keypressed());
return 0;
}
END_OF_MAIN();
void draw(void) {
Lib3dsMesh* mesh = katana->meshes; //current mesh in model file
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
while(mesh) {
for(unsigned long i = 0; i < mesh->faces; i++) {
//set material to the current face material
Lib3dsMaterial* material = lib3ds_file_material_by_name(katana, mesh->faceL.material);
glMaterialf(GL_FRONT, GL_SHININESS, material->shininess);
glMaterialfv(GL_FRONT, GL_AMBIENT, material->ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, material->diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, material->specular);
//draw face
glVertex3fv(mesh->pointL[mesh->faceL.points[0]].pos);
glVertex3fv(mesh->pointL[mesh->faceL.points[1]].pos);
glVertex3fv(mesh->pointL[mesh->faceL.points[2]].pos);
}
//go to next mesh if any
if(mesh->next) mesh = mesh->next;
else mesh = NULL;
}
glEnd();
glFlush();
allegro_gl_flip();
}
