Skip to main content
GameDev.net gamedev.net
🔒 Locked

problem with broken fragment improperly rendering

Started by jerrylin88 Jan 21, 2011 at 3:30 AM 3 replies 1.3k views
Original Post
jerrylin88
jerrylin88
i am currently using lib3ds library loading a vehicle model from 3ds file after i draw in on the screen it appears that there r a lot of broken fragment and cannot be rendered properly.it seems that there r some far points draw onto a nearer surface but ive no idea what s the problem really any answer would be appreciated.
attached is my code.


#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;

unsigned long total_face;

void lightup()
{
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CCW);
glCullFace(GL_BACK);
float gambient[]={0.2,0.2,0.2,1.0};
float lightpos[]={1.0,1.0,1.0,0.0};
float lambient[]={0.8,0.8,0.8,1.0};
float ldiffuse[]={1.0,1.0,1.0,1.0};
float lspecular[]={0.3,0.3,0.3,1.0};

glClearColor(0.0,0.0,0.0,0.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 display()
{
glEnable(GL_DEPTH_TEST);
glClearDepth(1.0);
glDepthFunc(GL_LEQUAL);

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
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();
glTranslatef(0.0,0.0,-1000.0);
long meshcount;
for(meshcount=0;meshcount<model->nmeshes;meshcount++)
{
face=mesh[meshcount]->faces;
Lib3dsVector* vertex_normal=new Lib3dsVector[mesh[meshcount]->nfaces*3];
Lib3dsVector* face_normal=new Lib3dsVector[mesh[meshcount]->nfaces];
lib3ds_mesh_calculate_vertex_normals(mesh[meshcount],vertex_normal);
lib3ds_mesh_calculate_face_normals(mesh[meshcount],face_normal);
for(long i=0;i<mesh[meshcount]->nfaces;i++)
{
glMaterialfv(GL_FRONT,GL_DIFFUSE,material[face.material]->diffuse);
glMaterialfv(GL_FRONT,GL_AMBIENT,material[face.material]->ambient);
glMaterialfv(GL_FRONT,GL_SPECULAR,material[face.material]->specular);
glBegin(GL_TRIANGLES);

glNormal3fv(face_normal);
glNormal3fv(vertex_normal[i*3]);
glVertex3fv(mesh[meshcount]->vertices[face.index[0]]);
glNormal3fv(vertex_normal[i*3+1]);
glVertex3fv(mesh[meshcount]->vertices[face.index[1]]);
glNormal3fv(vertex_normal[i*3+2]);
glVertex3fv(mesh[meshcount]->vertices[face.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,5.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;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BITS);

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;
}


FXACE
FXACE
Can you introduce a model (as "how it must looks like") ? for better searching of the problem...







jerrylin88
jerrylin88

Can you introduce a model (as "how it must looks like") ? for better searching of the problem...


some other tools showed what that model should be look like perfectly(i am gonna upload the pic later),since its just an earlier version of drawing models with non-texture lighting support,the model should be rendered and composed of some smooth and non-textured solid objects,when i say "smooth" i mean the deeper points should be hidden by nearer points other than nearer points hidden by deeper points(ive already clear the depth buffer b4 rendering).take a look at my newest picture.


FXACE
FXACE
I'm not sure that my notes can fix your problem... but...

(in "main" function)

GL_DEPTH_BITS is not used for glClear (it's for glGetIntegerv that returns a value of number of bits containig depth buffer of current applied GL context)...

You can use "gl" and "glu" commands only after created and applied GL context (it's initializing by glutCreateWindow(...) on a GLUT API)... otherwise it gives you an error at glGetError() (!=0)...

Comparing of two pictures. my opinion is: "Depth buffer is not initialized (not exist) or you are using invalid parameters for visualizing the scene)..."

Check out your code (in "reshape" function):

"gluPerspective(45.0,w/h,0.0,5.0);"

Znear plane must be > 0 (make a modifications with it to 1.0)...

like this:

"gluPerspective(45.0,w/h,1.0,5.0);"
jerrylin88
jerrylin88

I'm not sure that my notes can fix your problem... but...

(in "main" function)

GL_DEPTH_BITS is not used for glClear (it's for glGetIntegerv that returns a value of number of bits containig depth buffer of current applied GL context)...

You can use "gl" and "glu" commands only after created and applied GL context (it's initializing by glutCreateWindow(...) on a GLUT API)... otherwise it gives you an error at glGetError() (!=0)...

Comparing of two pictures. my opinion is: "Depth buffer is not initialized (not exist) or you are using invalid parameters for visualizing the scene)..."

Check out your code (in "reshape" function):

"gluPerspective(45.0,w/h,0.0,5.0);"

Znear plane must be > 0 (make a modifications with it to 1.0)...

like this:

"gluPerspective(45.0,w/h,1.0,5.0);"



thank you very much it works very well now as u mentioned because of sending the wrong parameter to the gluPerspective routine the depth buffer was not initialized correctly.after i correct it.the model turned dark partly due to the light,but obviously the tire hided the component behind it that`s remarkable!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.