I have written a program displaying an object in obj format. But the computer I have used during coding had a 1.5.1 version Opengl. However when I transfered my code to an another computer(which had a 2.0.5525 WinXP version Opengl) lighting direction changed somehow. Below is the array I have used for light position:
float[] lightpos = { 1.0f, 0.0f, 0.0f, 0.0f };
When I direct light towards +x axis, it displays as if light is directed towards -y axis and the inverse also works in the opposite way. However if I direct my light towards y or z axis no lighting occurs. Can this be caused by different opengl versions?
No, try this:
float[] lightpos = { 1.0f, 0.0f, 0.0f, 1.0f };
But I want directional light, if I define lightpos as {1.0f, 0.0f, 0.0f, 1.0f} then it becomes a light source
Here is the full code:
protected override void OnPaint( System.Windows.Forms.PaintEventArgs e )
{
if(DC == 0 || RC == 0)
return;
if(lastMs == 0)
lastMs = DateTime.Now.Ticks;
long currentMs = DateTime.Now.Ticks;
long milliseconds = (currentMs - lastMs) / 10000;
lastMs = currentMs; //Calculate elapsed timer
WGL.wglMakeCurrent(DC,RC);
//*****************************************************
//*** Camera Properties********************************
GL.glViewport(0, 0, this.Width, this.Height);
GLU.gluPerspective(80.0, 1.3, 0.0, 40.0);
GLU.gluLookAt(0.0, 0.5, -0.5, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0);
//GL.glMatrixMode(GL.GL_MODELVIEW);
//GL.glLoadIdentity();
//*****************************************************
//***Lighting Properties*******************************
GL.glShadeModel(GL.GL_SMOOTH);
GL.glEnable(GL.GL_LIGHTING);
GL.glEnable(GL.GL_LIGHT0);
float[] lightpos = { -1.0f, 0.0f, 0.0f, 0.0f }; //directional light
float[] lAmb = { 0.0f, 0.0f, 0.0f, 1.0f };
float[] lDif = { 0.9f, 0.9f, 0.9f, 1.0f };
float[] lSpec = { 0.2f, 0.2f, 0.2f, 1.0f };
GL.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, lightpos);
GL.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, lAmb);
GL.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, lDif);
GL.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, lSpec);
//*****************************************************
//***Render********************************************
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
GL.glLoadIdentity();
GL.glTranslatef (0.0f, 0.0f, -6.0f);
//if (AutoRotate)
//{
// Model.Rotate(2.0, 2.0, 0.0);
//}
//Render the model
Model.Render();
GL.glFlush (); // Flush The GL Rendering Pipeline
WGL.wglSwapBuffers(DC);
angle += (float)(milliseconds) / 5.0f;
}
protected override void OnPaint(PaintEventArgs e) {
// DC, RC and OS handles in general should probably be IntPtrs in C#
if (DC == IntPtr.Zero || RC == IntPtr.Zero)
return;
// Make context current
WGL.wglMakeCurrent(DC,RC);
// Setup viewport
GL.glViewport(0, 0, this.Width, this.Height);
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
// Setup projection
// Do not apply view transforms to the projection
// matrix. Lighting will not work properly, since
// lights are assumed to be in eye space
GL.glMatrixMode(GL.GL_PROJECTION);
GL.glLoadIdentity();
// zNear must be > 0!
GLU.gluPerspective(80.0, 1.3, 1.0, 40.0);
// Setup view
GL.glMatrixMode(GL.GL_MODELVIEW);
GL.glLoadIdentity();
GLU.gluLookAt(0.0, 0.5, -0.5, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0);
// Setup light(s)
// NB: Light positions are transformed by the
// current modelview matrix into eye space!
float[] lightpos = { -1.0f, 0.0f, 0.0f, 0.0f };
float[] lAmb = { 0.0f, 0.0f, 0.0f, 1.0f };
float[] lDif = { 0.9f, 0.9f, 0.9f, 1.0f };
float[] lSpec = { 0.2f, 0.2f, 0.2f, 1.0f };
GL.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, lightpos);
GL.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, lAmb);
GL.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, lDif);
GL.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, lSpec);
GL.glEnable(GL.GL_LIGHT0);
// Set render state
GL.glShadeModel(GL.GL_SMOOTH);
GL.glEnable(GL.GL_LIGHTING);
// Draw a model
// Make sure your model's normals are valid!
// Normals are transformed into eye space with the lights!
// If you want your light to move with the model, set the
// light position after the model transforms!
GL.glPushMatrix();
// Apply all model transformations here
Model.Render();
GL.glPopMatrix();
// Swap (probably no real need to flush here)
WGL.wglSwapBuffers(DC);
// !!!
// Always add code to check for OpenGL error conditions.
// You should check for errors at _least_ once a
// frame to be sure everything is running clean.
// See glGetError().
}