[OPENGL] render mesh/lighting
-
Hello, I need to render the mesh.It is given by set of its surfaces each surface is presented by its vertexes(x,y,z) The camera and light source has to be placed at the same place. I use this code:
//Projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();GLdouble diam = m\_allModels.getDiam(); GLdouble left = m\_allModels.getCenter()\[0\] - diam; GLdouble right = m\_allModels.getCenter()\[0\] + diam; GLdouble bottom = m\_allModels.getCenter()\[1\] - diam; GLdouble top = m\_allModels.getCenter()\[1\] + diam; glFrustum(left,right,bottom,top,diam,diam\*3.5);
//Model
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt (0.0, 0.0,2*m_allModels.getDiam(),
m_allModels.getCenter()[0],m_allModels.getCenter()[1],m_allModels.getCenter()[2],
0.0, 1.0, 0.0);//Lighting
GLfloat light_position[] = {0.0, 0.0,0.0, 1.0 };
GLfloat white_light[] = { 0.7, 0.3, 0.2, 1.0 };
glEnable(GL_DEPTH_TEST);glEnable(GL\_LIGHTING); glLightfv(GL\_LIGHT1, GL\_POSITION, light\_position); glLightfv(GL\_LIGHT1, GL\_DIFFUSE, white\_light); glEnable(GL\_LIGHT1); glShadeModel(GL\_FLAT);
//Draw
glEnable(GL_NORMALIZE);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//render here
for(unsigned int modelIdx = 0;modelIdx < m_allModels.zise();++modelIdx)
{
model curModel = m_allModels.getModel(modelIdx);
for(unsigned int modelSurfIdx = 0;modelSurfIdx <curModel.surfNum();
++modelSurfIdx)
{
surface curSurf = curModel.getSurface(modelSurfIdx);
glLineWidth(2);
glBegin( GL_POLYGON );glNormal3f(curSurf.getN\_x(),curSurf.getN\_y(),curSurf.getN\_z()); for(unsigned int vertIdx = 0;vertIdx< curSurf.size();++vertIdx) { unsigned int curVertIdx = curSurf.getSurfVertices()\[vertIdx\]; vertex curVert = curModel.getVertex(curVertIdx); glVertex3f((GLfloat)curVert.getX(),(GLfloat)curVert.getY(),(GLfloat)curVert.getZ()); } glEnd(); } }
And I get this http://img17.imageshack.us/img17/1103/64347046.png[^] And I don't understand why
-
Hello, I need to render the mesh.It is given by set of its surfaces each surface is presented by its vertexes(x,y,z) The camera and light source has to be placed at the same place. I use this code:
//Projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();GLdouble diam = m\_allModels.getDiam(); GLdouble left = m\_allModels.getCenter()\[0\] - diam; GLdouble right = m\_allModels.getCenter()\[0\] + diam; GLdouble bottom = m\_allModels.getCenter()\[1\] - diam; GLdouble top = m\_allModels.getCenter()\[1\] + diam; glFrustum(left,right,bottom,top,diam,diam\*3.5);
//Model
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt (0.0, 0.0,2*m_allModels.getDiam(),
m_allModels.getCenter()[0],m_allModels.getCenter()[1],m_allModels.getCenter()[2],
0.0, 1.0, 0.0);//Lighting
GLfloat light_position[] = {0.0, 0.0,0.0, 1.0 };
GLfloat white_light[] = { 0.7, 0.3, 0.2, 1.0 };
glEnable(GL_DEPTH_TEST);glEnable(GL\_LIGHTING); glLightfv(GL\_LIGHT1, GL\_POSITION, light\_position); glLightfv(GL\_LIGHT1, GL\_DIFFUSE, white\_light); glEnable(GL\_LIGHT1); glShadeModel(GL\_FLAT);
//Draw
glEnable(GL_NORMALIZE);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//render here
for(unsigned int modelIdx = 0;modelIdx < m_allModels.zise();++modelIdx)
{
model curModel = m_allModels.getModel(modelIdx);
for(unsigned int modelSurfIdx = 0;modelSurfIdx <curModel.surfNum();
++modelSurfIdx)
{
surface curSurf = curModel.getSurface(modelSurfIdx);
glLineWidth(2);
glBegin( GL_POLYGON );glNormal3f(curSurf.getN\_x(),curSurf.getN\_y(),curSurf.getN\_z()); for(unsigned int vertIdx = 0;vertIdx< curSurf.size();++vertIdx) { unsigned int curVertIdx = curSurf.getSurfVertices()\[vertIdx\]; vertex curVert = curModel.getVertex(curVertIdx); glVertex3f((GLfloat)curVert.getX(),(GLfloat)curVert.getY(),(GLfloat)curVert.getZ()); } glEnd(); } }
And I get this http://img17.imageshack.us/img17/1103/64347046.png[^] And I don't understand why
So what is it supposed to look like? Can't read minds, you know... I'll take a wild guess and say that, as is often the case with 3rd party models, the winding direction of the polygons is probably inconsistent. If you want to see the entire model, either enable double-sided rendering, or run the model through PoseRay (or some other model cleaner) and fix the polygon winding.
L u n a t i c F r i n g e
-
Hello, I need to render the mesh.It is given by set of its surfaces each surface is presented by its vertexes(x,y,z) The camera and light source has to be placed at the same place. I use this code:
//Projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();GLdouble diam = m\_allModels.getDiam(); GLdouble left = m\_allModels.getCenter()\[0\] - diam; GLdouble right = m\_allModels.getCenter()\[0\] + diam; GLdouble bottom = m\_allModels.getCenter()\[1\] - diam; GLdouble top = m\_allModels.getCenter()\[1\] + diam; glFrustum(left,right,bottom,top,diam,diam\*3.5);
//Model
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt (0.0, 0.0,2*m_allModels.getDiam(),
m_allModels.getCenter()[0],m_allModels.getCenter()[1],m_allModels.getCenter()[2],
0.0, 1.0, 0.0);//Lighting
GLfloat light_position[] = {0.0, 0.0,0.0, 1.0 };
GLfloat white_light[] = { 0.7, 0.3, 0.2, 1.0 };
glEnable(GL_DEPTH_TEST);glEnable(GL\_LIGHTING); glLightfv(GL\_LIGHT1, GL\_POSITION, light\_position); glLightfv(GL\_LIGHT1, GL\_DIFFUSE, white\_light); glEnable(GL\_LIGHT1); glShadeModel(GL\_FLAT);
//Draw
glEnable(GL_NORMALIZE);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//render here
for(unsigned int modelIdx = 0;modelIdx < m_allModels.zise();++modelIdx)
{
model curModel = m_allModels.getModel(modelIdx);
for(unsigned int modelSurfIdx = 0;modelSurfIdx <curModel.surfNum();
++modelSurfIdx)
{
surface curSurf = curModel.getSurface(modelSurfIdx);
glLineWidth(2);
glBegin( GL_POLYGON );glNormal3f(curSurf.getN\_x(),curSurf.getN\_y(),curSurf.getN\_z()); for(unsigned int vertIdx = 0;vertIdx< curSurf.size();++vertIdx) { unsigned int curVertIdx = curSurf.getSurfVertices()\[vertIdx\]; vertex curVert = curModel.getVertex(curVertIdx); glVertex3f((GLfloat)curVert.getX(),(GLfloat)curVert.getY(),(GLfloat)curVert.getZ()); } glEnd(); } }
And I get this http://img17.imageshack.us/img17/1103/64347046.png[^] And I don't understand why
it appears that your normals are flip flopped, at least that's what causes my some of my renders to go astray...
I'd blame it on the Brain farts.. But lets be honest, it really is more like a Methane factory between my ears some days then it is anything else...