# render mesh/lighting

## Recommended Posts

arbin    100
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 lifgt sourse has to be placed at the same place. I try such a code:
   glEnable(GL_DEPTH_TEST);
//Light

GLfloat light_position[] = {0.0,
0.0,2*m_allModels.getDiam(), 1.0 };
GLfloat white_light[] = { 0.7, 0.3, 0.2, 1.0 };

glLightfv(GL_LIGHT1,
GL_POSITION, light_position);
glLightfv(GL_LIGHT1, GL_DIFFUSE, white_light);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT1);

//Projection
glMatrixMode(GL_PROJECTION);
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 view
glMatrixMode(GL_MODELVIEW);

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

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//rener here
glEnable(GL_NORMALIZE);
//........


I get a strange results seems like the light is place by side (not at the same place as camera) -- Thank you, Irena [Edited by - arbin on February 8, 2010 2:18:17 PM]

##### Share on other sites
Lord_Evil    680
First: GLfloat white_light[] = { 0.7, 0.3, 0.2, 1.0 }; this is red/brown rather than white ([smile])

Second: are your object's normals calculated correctly?

Third: Do you call this code every frame? You could move huge parts of the code you posted to an init() function if it doesn't change. Or you split it into pieces (like updateLight(), updateProjection(), ...) and call the respective function, when the data it updates has changed (i.e. light position/color, projection parameters etc.).

##### Share on other sites
arbin    100
yes ,the normals are calculated correctly.
Ill arrange it to init-just want to solve problematic issue

##### Share on other sites
swiftcoder    18437
Recall that the light position is stored in eye space - or in other words, glLightfv multiplies the position you pass in by the modelview matrix.

Thus, unless you set the modelview matrix to identity before setting the light position, your light position won't be in world space.

##### Share on other sites
Lord_Evil    680
If you are calling the code every frame and there's no glLoadIdentity(); before calling it, then what swiftcoder said would apply: in frame n you alter the modelview matrix while rendering your objects and if you don't reset it to identity in frame n+1 you'll multiply the light's position with the last value of the modelview matrix.

Thus, your light will be in the correct position in frame 0 and in the wrong position in all frames that follow. Provided that your camera and models don't move the light would stay stationary albeit in the wrong location.

##### Share on other sites
arbin    100
This is a part of function which is called only once
In its rendering section it goes in loop over all surfaces,
drawing in every iteration new surface.Is every such iteretation
considered as a new frame?

Surfaces draw code:
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();				}}`