Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi there!

I am currently using lib3ds draw models in my opengl program.

After drawing vertex reading from the 3ds file, it appears a vehicle model on screen, but while I assign the light onto the surface I found there is some fragment displayed incorrectly.

Some far pixel appeared on the nearer surface (matter fact I enabled the depth buffer facility) that I can`t figure what`s wrong with the code.

Attached is my code:

C#
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
#include <lib3ds.h>
#include <iostream>
typedef float Lib3dsVector[3];
typedef float Lib3dsTexel[2];
using namespace std;
Lib3dsFile* model;
Lib3dsMesh** mesh;
Lib3dsCamera** camera;
Lib3dsFace* face;
Lib3dsLight** light;
Lib3dsMaterial** material;
GLuint vertexVBO,normalVBO,textureVBO;
unsigned long total_face;
void lightup()
{
    glShadeModel(GL_SMOOTH);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glFrontFace(GL_CCW);
    float gambient[]={0.2,0.2,0.2,1.0};
    float lightpos[]={1.0,1.0,1.0,0.0};
    float lambient[]={1.0,1.0,1.0,1.0};
    float ldiffuse[]={0.8,0.8,0.8,1.0};
    float lspecular[]={0.3,0.3,0.3,1.0};
    glLightfv(GL_LIGHT0,GL_DIFFUSE,ldiffuse);
    glLightfv(GL_LIGHT0,GL_AMBIENT,lambient);
    glLightfv(GL_LIGHT0,GL_SPECULAR,lspecular);
    glLightfv(GL_LIGHT0,GL_POSITION,lightpos);
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT,gambient);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
}
void getfaces()
{
    total_face=0;
    for(long meshcount=0;meshcount<model->nmeshes;meshcount++)
        total_face+=mesh[meshcount]->nfaces;
}
void display()
{
    glEnable(GL_DEPTH_TEST);
    glClearDepth(1.0);
    glDepthFunc(GL_LEQUAL);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BITS);
    glLoadIdentity();
    gluLookAt(camera[0]->position[0]+1000,camera[0]->position[1],camera[0]->position[2],camera[0]->target[0],camera[0]->target[1],camera[0]->target[2],0.0,0.0,1.0);
    glColor3f(1.0,0.0,0.0);
    glPushMatrix();
    lightup();
    glScalef(0.5,0.5,0.5);
    glTranslatef(0.0,0.0,-1000.0);
    long meshcount;
    for(meshcount=0;meshcount<model->nmeshes;meshcount++)
    {
        face=mesh[meshcount]->faces;
        for(long i=0;i<mesh[meshcount]->nfaces;i++)
        {
            glMaterialfv(GL_FRONT,GL_DIFFUSE,material[face[i].material]->diffuse);
            glMaterialfv(GL_FRONT,GL_AMBIENT,material[face[i].material]->ambient);
            glMaterialfv(GL_FRONT,GL_SPECULAR,material[face[i].material]->specular);
            glBegin(GL_TRIANGLES);
            glVertex3fv(mesh[meshcount]->vertices[face[i].index[0]]);
            glVertex3fv(mesh[meshcount]->vertices[face[i].index[1]]);
            glVertex3fv(mesh[meshcount]->vertices[face[i].index[2]]);
            glEnd();
        }
    }
    glPopMatrix();
/*
    glPushMatrix();
    glTranslatef(0.0,0.0,-2000.0);
    glColor3f(1.0,1.0,1.0);
    glBegin(GL_QUADS);
    glVertex3f(-10000.0f,-10000.0f,0.0f);
    glVertex3f(10000.0f,-10000.0f,0.0f);
    glVertex3f(10000.0f,10000.0f,0.0f);
    glVertex3f(-10000.0f,10000.0f,0.0f);
    glEnd();
    glPopMatrix();
*/
    glutSwapBuffers();
}
void keypress(unsigned char key,int x,int y)
{
    switch(key)
    {
    case 27:
        exit(0);
        break;
    }
}
void reshape(int w,int h)
{
    if(h==0) h=1;
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0,w/h,0.0,1000.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
int main(int argc,char* argv[])
{
    glutInit(&argc,argv);
    model=lib3ds_file_open("vehicle.3DS");
    if(model==NULL) cout<<"Error"<<endl; else cout<<model->nmaterials<<endl;
    mesh=model->meshes;
    material=model->materials;
    camera=model->cameras;
    light=model->lights;
    cout<<"Light Num:"<<model->nlights<<endl;

    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
    glutInitWindowSize(800,800);
    glutInitWindowPosition(100,100);
    glutCreateWindow("VC 2008 Glut");
    //glutFullScreen();
    glShadeModel(GL_FLAT);
    glutKeyboardFunc(keypress);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}


Thank you for reading, any answer would be appreciated.
Posted
Updated 19-Jan-11 21:38pm
v2
Comments
Dalek Dave 20-Jan-11 3:39am    
Edited for Grammar, Syntax and Readability.
GPUToaster™ 20-Jan-11 3:56am    
Provide a screen shot so that it will help members to identify problem and reply promptly.
revaldo666 20-Jul-12 10:18am    
how load 2 or more models to scene

Try to use glDisable(GL_CULL_FACE), if it renders properly then some problem with the model itself, hopefully the normals.

If that is the case use glNormals*(). Let me know if you hit anything.
 
Share this answer
 
v2
Comments
JerryLin88 20-Jan-11 7:53am    
thanx for your answer but glDisable(GL_CULL_FACE) doesn`t work,i guess as you mentioned perhaps the problem is caused by undefined normals of each face.after i defined the vertex normal it looks much better than before but some fragments are still broken,when i refer to the normal calculation i read some lines of the lib3ds header files.and i got 2 method for normal calculation.1 for vertex and another for face.i just used vertex normal but i have no idea how to use face normal.anyway your advise helps me alot.thank you
GPUToaster™ 20-Jan-11 8:28am    
I never used lib3ds. I can only say probable causes. So i guess you have answered your own query, thats remarkable!
And i'l try to follow what you have here.
JerryLin88 20-Jan-11 9:40am    
i am just not sure whether it will render properly after applying face normals as titled i am just a newbie to opengl and i ve no idea dealing with face normal combined with vertex normals and the normalize operation costs too much resource withing slow down my program extremely so do u have any optimization suggestion? thanx for your time looking forwards to your reply.
GPUToaster™ 20-Jan-11 13:00pm    
I'm not an optimization master...but you might want to refer to some common OpenGL pitfalls.
Here you go: http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/


Hope it helps! Best luck.
C#
for(meshcount=0;meshcount<model->nmeshes;meshcount++)
{
    face=mesh[meshcount]->faces;
    Lib3dsVector* vertex_normal=new Lib3dsVector[mesh[meshcount]->nfaces*3];
    lib3ds_mesh_calculate_vertex_normals(mesh[meshcount],vertex_normal);
    for(long i=0;i<mesh[meshcount]->nfaces;i++)
    {
        glMaterialfv(GL_FRONT,GL_DIFFUSE,material[face[i].material]->diffuse);
        glMaterialfv(GL_FRONT,GL_AMBIENT,material[face[i].material]->ambient);
        glMaterialfv(GL_FRONT,GL_SPECULAR,material[face[i].material]->specular);
        glBegin(GL_TRIANGLES);
        glNormal3fv(vertex_normal[i*3]);
        glVertex3fv(mesh[meshcount]->vertices[face[i].index[0]]);
        glNormal3fv(vertex_normal[i*3+1]);
        glVertex3fv(mesh[meshcount]->vertices[face[i].index[1]]);
        glNormal3fv(vertex_normal[i*3+2]);
        glVertex3fv(mesh[meshcount]->vertices[face[i].index[2]]);
        glEnd();
    }
}

here is my modified source code.as u can see lib3ds_mesh_calculate_vertex_normals for vertex and lib3ds_mesh_calculate_face_normals for face.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900