PDA

View Full Version : Lighting and me dont get along



Slong
11-11-2009, 07:28 PM
Alright iv got my robot put together and now i need to add lighting to give it its 3d effect, also i need to make the robot walk in a circle around the origin... can anyone help me with either of these? iv defined the lighting already correctly i think but its not working the way i wanted it to haha, thanks!

#include <stdlib.h>
#include <GL/glut.h>

GLsizei wh = 800,ww = 800;

void init();
void reshape(GLsizei w, GLsizei h);
void Robot();
void torso();
void head();
void upperRightArm();
void upperLeftArm();
void upperRightLeg();
void upperLeftLeg();
void lowerRightArm();
void lowerLeftArm();
void lowerRightLeg();
void lowerLeftLeg();

//Define lighting variables
GLfloat light0_pos[] ={1.,2.,3.,1.};
GLfloat diffuse0[]={0.,0,0,1};
GLfloat ambient0[]={.5,.5,.5,1};
GLfloat specular0[]={1,1,1,1};

void main(int argc, char **argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(ww,wh);
glutInitWindowPosition(200,150);
glutCreateWindow("Robot");
glutReshapeFunc(reshape);
glutDisplayFunc(Robot);
init();
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}

void init()
{
glClearColor (0.0, 0.7, 1.0, 1.0);
glColor3f(0.,0.,1.);
glMatrixMode(GL_PROJECTION); //set the clipping space
glLoadIdentity();
glOrtho(-2.,2.0,-2.,2.,-2.,2.);
glMatrixMode(GL_MODELVIEW);
glutReshapeFunc(reshape);

/*
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glLightfv(GL_LIGHT0,GL_POSITION,light0_pos);
glLightfv(GL_LIGHT0,GL_AMBIENT,ambient0);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse0);
glLightfv(GL_LIGHT0,GL_SPECULAR,specular0);
*/

}

void reshape(GLsizei w, GLsizei h)
{
/* adjust clipping box */

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -300.0, 300.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

/* adjust viewport and clear */

glViewport(0,0,w,h);
glClearColor (0.0, 0.7, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();

/* set global size for use by drawing routine */

ww = w;
wh = h;

}

void Robot()
{
/*
//Lighting of the ball
GLfloat ambient[]={.0,.0,.3,1};
GLfloat diffuse[]={0,0,.3,1};
GLfloat specular[]={0,0,.3,1};
//Material
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,ambient) ;
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,diffuse) ;
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,specula r);
*/

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glPushMatrix();
glTranslatef(400,400,0.);
torso();
glPushMatrix();
head();
glPopMatrix();

/////////Attach Right Arms
glPushMatrix();
upperRightArm();
glPushMatrix();
lowerRightArm();
glPopMatrix();
glPopMatrix();

////////Attach Left Arms
glPushMatrix();
upperLeftArm();
glPushMatrix();
lowerLeftArm();
glPopMatrix();
glPopMatrix();

////////Attach Right Legs
glPushMatrix();
upperRightLeg();
glPushMatrix();
lowerRightLeg();
glPopMatrix();
glPopMatrix();

////////Attach Left Legs
glPushMatrix();
upperLeftLeg();
glPushMatrix();
lowerLeftLeg();
glPopMatrix();
glPopMatrix();
glPopMatrix();


glutSwapBuffers();
glFlush();
}

void torso()
{
glNormal3f(0,0,0);
glColor3f(1.0,0.,.0);
glutSolidCube(150);
}

void head()
{
glTranslatef(0,110,0.);

glScalef(1,1.5,2);
glColor3f(.0,1.,.0);
glutSolidCube(75);
}

void upperRightArm()
{
glTranslatef(-110,37,0);
glColor3f(.5,.5,.0);
glutSolidCube(75);
}

void upperLeftArm()
{
glTranslatef(110,37,0);
glColor3f(.5,.5,.0);
glutSolidCube(75);
}

void upperRightLeg()
{
glTranslatef(-38,-110,0);
glColor3f(.5,.5,.0);
glutSolidCube(70);
}

void upperLeftLeg()
{
glTranslatef(38,-110,0);
glColor3f(.5,.5,.0);
glutSolidCube(70);
}

void lowerRightArm()
{
glTranslatef(0,-60,0);
glScalef(-.8,2.5,1);
glColor3f(.0,.5,1.0);
glutSolidCube(50);
}

void lowerLeftArm()
{
glTranslatef(0,-60,0);
glScalef(-.8,2.5,1);
glColor3f(.0,.5,1.0);
glutSolidCube(50);
}

void lowerRightLeg()
{
glTranslatef(0,-60,0);
glScalef(1,3,1);
glColor3f(1.5,.0,1.0);
glutSolidCube(50);
}

void lowerLeftLeg()
{
glTranslatef(0,-60,0);
glScalef(1,3,1);
glColor3f(1.5,.0,1.);
glutSolidCube(50);
}

Slong
11-12-2009, 04:56 PM
can anyone help me with the lighting?

gumby
11-12-2009, 06:55 PM
Light position is transformed by the current modelview matrix. I don't know what your intent was, but try setting the light position after the viewing transformation is applied.

Dark Photon
11-12-2009, 07:02 PM
Alright iv got my robot put together and now i need to add lighting to give it its 3d effect...iv defined the lighting already correctly i think but its not working the way i wanted it to haha, thanks!
You're pretty darn close. Keep pluggin'.

A couple tips:

1) The main thing you're missing is that you probably want your vertex color to be used for your lighting. Color material is the way to do that. Read up on it. Before you draw your robot, enable it, and set it to GL_AMBIENT_AND_DIFFUSE.

2) Vertex normals are used for lighting. glutSolidCube provides its own normals, so you shouldn't have to provide anyway. But just delete that glNormal(0,0,0) you've got in there anyway -- it's not being used for anything, and it's bogus. Be advised that zero-length normals will screw up your lighting if you use them. A zero length vector is useless.

3) Customarily, when asking for help, state specifically what you're seeing and what you think is wrong with that. Don't just say I don't think it works, so here's the source -- fix it for me! ;)

4) I suggest you start with a directional light, not a point light. They're simpler. You just specify the direction vector toward the light (with w == 0), and it shines parallel rays.

And note that the MODELVIEW you have active when you specify the light source position is used to transform the light into eye space IMMEDIATELY. More on that below.

5) I also suggest you disable SPECULAR. These cubes only have 2 tris per side, so with vertex lighting any specular effect is gonna be a joke anyway.

So bottom line, make these changes:

GLfloat light0_pos[] ={5,5,2,0.};
GLfloat diffuse0[]={0.5,0.5,0.5,1};
GLfloat ambient0[]={0.5,0.5,0.5,1};
GLfloat specular0[]={0,0,0,1};

GLfloat ambient[]={1,0,0,1};
GLfloat diffuse[]={1,0,0,1};
GLfloat specular[]={0,0,0,1};

Add this after you switch back to MODELVIEW in init():

glLoadIdentity()

And add this after you glLoadIdentity into the MODELVIEW matrix in Robot():

glRotatef( 10,1,1,1 );

so you can see a slightly rotated, lit view of the robot.

And add the color material magic you look up at the top of Robot() (using GL_AMBIENT_AND_DIFFUSE color material mode). Note that this causes the material ambient and diffuse you set for glMaterialfv to become irrelevant, because they're overridden every vertex by the vertex color.

Slong
11-12-2009, 08:41 PM
alright thx for the explainations, once i get done with calculus ill start on trying what you've stated.. lighting is defently one thing i have no grasped haha ill inform once i make the changes

Slong
11-12-2009, 09:00 PM
alright i made the changes and the lighting works finally, now how can i make it look shiny? to give it a more realistic appreance, i know specular light does a reflection will would prolly help also and i have it defined but its not really doing much, or in other the words the lighting seems hardly noticable... maybe a black background would help ^_^


Edit: also i was wondering how i can turn the robot to like face the right instead of the left.. i tried messing with rotate but that didnt help : / i figured glRotatef(0,0,60,0); would rotate it vertically 60 on the y axis to basically change what direction its facing? help on this would be cool to

Slong
11-13-2009, 06:30 PM
anyone :)

Dark Photon
11-16-2009, 07:51 PM
now how can i make it look shiny?
Define shiny.

If you mean mirror-like, where you can see reflections of other parts of your scene, then trust me, you're not ready for that yet. If you mean you can see reflections of an infinitely distant "environment", then that's easier but you're still not ready. If you just mean has glossy white highlights for the light, then add some specular to the light and material, and use more verts to build your poly. You could do fragment lighting, but I doubt your ready to tackle shaders until you "get" lighting.

Reading the Lighting chapter in the OpenGL Programming Guide.