PDA

View Full Version : Problem with lightening



michalo
01-03-2010, 12:04 PM
Hi,
I have big problem with standard opengl lightening model. After enabling lights every object in my scene is getting white and it totaly lose previous color. I read a few tutorials, but it don't clear up the problem. I also try to use shaders with various values of parametrs but problem ramain the same.

Have you any ideas? Thanks for help.

I post fragments of code below:


[B]
public void init(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
System.err.println("INIT GL IS: " + gl.getClass().getName());

// Enable VSync
gl.setSwapInterval(1);

// Setup the drawing area and shading mode
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glShadeModel(GL.GL_FLAT);

gl.glEnable(gl.GL_DEPTH_TEST);


float[] light_pos2={(float)0.0, (float)0.0, (float)1.0, (float)0.0};
float[] light_amb2={(float)0, (float)0, (float)0, (float)1};
float[] light_diff2={(float)1, (float)1, (float)1, (float)1};
float[] light_spec2={(float)0.4117647, (float)0.4117647, (float)0.4117647, (float)1};
float[] light_spot2={(float)0, (float)0, (float)-1, (float)0};

gl.glLightf(GL.GL_LIGHT0, GL.GL_SPOT_CUTOFF, 180.0f );
gl.glLightf(GL.GL_LIGHT0, GL.GL_SPOT_EXPONENT, 0.0f );

gl.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, light_amb2, 0);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, light_diff2, 0);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, light_spec2, 0);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPOT_DIRECTION, light_spot2, 0);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, light_pos2, 0);

float[] mat_diff = {(float)0.8, (float)0.8, (float)0.8, (float)1.0};
float[] mat_amb = {(float)0.2, (float)0.2, (float)0.2, (float)1.0};
float[] mat_spec = {(float)0.9411765, (float)0.9019608, (float)0.5490196, (float)1.0};
float[] mat_ems = {(float)0, (float)0, (float)0, (float)1.0};

gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT, mat_amb, 0);
gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_DIFFUSE, mat_diff, 0);
gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_SPECULAR, mat_spec, 0);
gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_EMISSION, mat_ems, 0);
gl.glMaterialf(GL.GL_FRONT_AND_BACK, GL.GL_SHININESS, 120.0f);

gl.glEnd();
gl.glFlush();

gl.glEnable(GL.GL_LIGHTING);
gl.glEnable(GL.GL_LIGHT0);
}


public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL gl = drawable.getGL();
GLU glu = new GLU();

if (height <= 0) { // avoid a divide by zero error!

height = 1;
}
final float h = (float) width / (float) height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, h, 1.0, 20.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}

public void display(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
// Clear the drawing area
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
// Reset the current matrix to the "identity"
gl.glLoadIdentity();

// Move the "drawing cursor" around
gl.glTranslatef(-1.5f, 0.0f, -6.0f);

// Drawing Using Triangles
gl.glBegin(GL.GL_TRIANGLES);
gl.glNormal3d(0, 0, 1);
gl.glColor3f(1.0f, 0.0f, 0.0f); // Set the current drawing color to red
gl.glVertex3f(0.0f, 1.0f, 0.0f); // Top
gl.glNormal3d(0, 0, 1);
gl.glColor3f(0.0f, 1.0f, 0.0f); // Set the current drawing color to green
gl.glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
gl.glNormal3d(0, 0, 1);
gl.glColor3f(0.0f, 0.0f, 1.0f); // Set the current drawing color to blue
gl.glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right

// Finished Drawing The Triangle
gl.glEnd();


gl.glRotatef(-10.0f, -1.0f, -1.0f, -1.0f);
// Move the "drawing cursor" to another position
gl.glTranslatef(3.0f, 0.0f, 0.0f);
// Draw A Quad
gl.glBegin(GL.GL_QUADS);
gl.glNormal3d(0, 0, 1);
gl.glColor3f(0.5f, 0.5f, 1.0f); // Set the current drawing color to light blue
gl.glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
gl.glVertex3f(1.0f, 1.0f, 0.0f); // Top Right
gl.glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right
gl.glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left
// Done Drawing The Quad
gl.glEnd();


gl.glRotatef(-10.0f, -5.0f, -1.0f, -1.0f);
gl.glTranslatef(0.0f, 0.0f, -5.0f);
gl.glBegin(GL.GL_QUADS);

gl.glNormal3d(0, 1, 0);
gl.glColor3f(0.0f,1.0f,0.0f);
gl.glVertex3f( 1.0f, 1.0f,-1.0f);
gl.glVertex3f(-1.0f, 1.0f,-1.0f);
gl.glVertex3f(-1.0f, 1.0f, 1.0f);
gl.glVertex3f( 1.0f, 1.0f, 1.0f);

gl.glNormal3d(0, -1, 0);
gl.glColor3f(1.0f,0.5f,0.0f);
gl.glVertex3f( 1.0f,-1.0f, 1.0f);
gl.glVertex3f(-1.0f,-1.0f, 1.0f);
gl.glVertex3f(-1.0f,-1.0f,-1.0f);
gl.glVertex3f( 1.0f,-1.0f,-1.0f);

gl.glNormal3d(0, 0, 1);
gl.glColor3f(1.0f,0.0f,0.0f);
gl.glVertex3f( 1.0f, 1.0f, 1.0f);
gl.glVertex3f(-1.0f, 1.0f, 1.0f);
gl.glVertex3f(-1.0f,-1.0f, 1.0f);
gl.glVertex3f( 1.0f,-1.0f, 1.0f);

gl.glNormal3d(0, 0, -1);
gl.glColor3f(1.0f,1.0f,0.0f);
gl.glVertex3f( 1.0f,-1.0f,-1.0f);
gl.glVertex3f(-1.0f,-1.0f,-1.0f);
gl.glVertex3f(-1.0f, 1.0f,-1.0f);
gl.glVertex3f( 1.0f, 1.0f,-1.0f);

gl.glNormal3d(-1, 0, 0);
gl.glColor3f(0.0f,0.0f,1.0f);
gl.glVertex3f(-1.0f, 1.0f, 1.0f);
gl.glVertex3f(-1.0f, 1.0f,-1.0f);
gl.glVertex3f(-1.0f,-1.0f,-1.0f);
gl.glVertex3f(-1.0f,-1.0f, 1.0f);

gl.glNormal3d(1, 0, 0);
gl.glColor3f(1.0f,0.0f,1.0f);
gl.glVertex3f( 1.0f, 1.0f,-1.0f);
gl.glVertex3f( 1.0f, 1.0f, 1.0f);
gl.glVertex3f( 1.0f,-1.0f, 1.0f);
gl.glVertex3f( 1.0f,-1.0f,-1.0f);

gl.glEnd();
gl.glFlush();
}

Abdallah DIB
01-03-2010, 03:39 PM
with lighting enabled the final color of the vertex will be calculated from ur light(s) parameters( ambiant,diffuse,...)and the material properties of each object. if u want to use glColor3f to change material properties u have to enable color material :
glEnable(GL_COLOR_MATERIAL);