PDA

View Full Version : OpenGL Lighting Selection



SecurityCop
11-03-2011, 03:33 PM
Hey guys, just starting off on OpenGL for the past month or so. I'm working on a teapot with lighting at the moment, and I have it working, however, I'm also working on incorperating the X, Y, and Z axes into it. My problem is that the lighting affects both the teapot AND my axes. Is there a way to get the axes to ignore the lighting, and just appear green, blue and red?
Thanks

carsten neumann
11-03-2011, 03:44 PM
I think you are looking for glDisable(GL_LIGHTING).

SecurityCop
11-03-2011, 03:51 PM
I don't think that's quite what I'm looking for. Or at least I don't know where to implement it. The teapot and the Axes are two separate objects, and I would like the lighting to affect only one object. I did try the disable lighting command, but it didn't work in the areas I put it.

SecurityCop
11-03-2011, 03:56 PM
Let me help by including my code for you guys.


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

float w, h, tip = 0, turn = 0;

float ORG[3] = {0,0,0};

float XP[3] = {10,0,0}, XN[3] = {-1,0,0},
YP[3] = {0,10,0}, YN[3] = {0,-1,0},
ZP[3] = {0,0,10}, ZN[3] = {0,0,-1};

void reshape (int nw, int nh)
{
w = nw;
h = nh;
}

void quit (unsigned char key, int x, int y)
{
switch (key)
{
case 'q':
exit(1);
break;
}
}

void turnFunc (int key, int x, int y)
{
switch (key) {
case GLUT_KEY_RIGHT:
turn += 5;
break;
case GLUT_KEY_LEFT:
turn -= 5;
break;
case GLUT_KEY_UP:
if (tip>-85)
tip -= 5;
break;
case GLUT_KEY_DOWN:
if (tip<85)
tip += 5;
break;
case 'q':
exit(1);
break;
}
}

void drawAxes (void)
{
glPushMatrix ();

glTranslatef (0, 0, -5);
glRotatef (tip , 1,0,0);
glRotatef (turn, 0,1,0);
glScalef (0.25, 0.25, 0.25);

glLineWidth (2.0);

glBegin (GL_LINES);
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
glDisable(GL_AUTO_NORMAL);
glDisable(GL_NORMALIZE);
glColor3f (1,0,0); // X axis is red.
glVertex3fv (ORG);
glVertex3fv (XP );
glColor3f (0,1,0); // Y axis is green.
glVertex3fv (ORG);
glVertex3fv (YP );
glColor3f (0,0,1); // z axis is blue.
glVertex3fv (ORG);
glVertex3fv (ZP );
glEnd();

glPopMatrix ();
}

void drawTeapot (void)
{
float ambient[] = {0.1745, 0.01175, 0.01175};
float diffuse[] = {0.61424, 0.04136, 0.04136};
float specular[] = {0.727811, 0.626959, 0.626959};
glPushMatrix ();

glTranslatef (0, 0, -5);
glRotatef (tip , 1,0,0);
glRotatef (turn, 0,1,0);

glColor3f (0, 1, 0);
glFrontFace(GL_CW);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
glMaterialf(GL_FRONT, GL_SHININESS, 0.6*128.0);
glutSolidTeapot (1.0);

glPopMatrix ();
}

void display (void)
{
glViewport (0, 0, w, h);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

drawTeapot();
drawAxes();

glutSwapBuffers ();
}

void main (void)
{
glutInitWindowSize (600, 400);
glutInitWindowPosition (400, 300);
glutInitDisplayMode (GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow ("Teapot and Axes");
glutKeyboardFunc(quit);
glutDisplayFunc (display);
glutIdleFunc (display);
glutReshapeFunc (reshape);
glutSpecialFunc (turnFunc);

glClearColor (0.3, 0.3, 0.3, 1.0);
glEnable (GL_DEPTH_TEST);
glMatrixMode (GL_PROJECTION);
gluPerspective (40.0, 1.5, 1.0, 10.0);
glMatrixMode (GL_MODELVIEW);

glutMainLoop ();
}

Cjacchus
11-03-2011, 07:12 PM
Hello!
I'm new in here, but I think I can solve this one.
As you said yourself, you have two separate objects. So, it's ok to enable lighting in one of them and don't in another.

Your program's idea is alright, but you shouldnt enable and disable stuff between glBegin() and glEnd(). I'd change the locations, like

glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
glDisable(GL_AUTO_NORMAL);
glDisable(GL_NORMALIZE);

glBegin (GL_LINES);
glColor3f (1,0,0); // X axis is red.
glVertex3fv (ORG);
glVertex3fv (XP );
glColor3f (0,1,0); // Y axis is green.
glVertex3fv (ORG);
glVertex3fv (YP );
glColor3f (0,0,1); // z axis is blue.
glVertex3fv (ORG);
glVertex3fv (ZP );
glEnd();

SecurityCop
11-04-2011, 10:35 AM
Ah perfect! Thank you, that was my issue, just having it after the start. Works perfectly!

Cjacchus
11-04-2011, 02:38 PM
Hey, I'm glad(:
I don't know exactly what you're looking for learning OpenGL, but just in case...
I started learning a few weeks ago, and after reading four or five chapters of the red book I realized most of it got deprecated. That means the functions used in there aren't in the core of the new versions of OpenGL, only in compatibility extensions.
If you want to learn how it's meant to be in the 4.0 version, you should look http://openglbook.com (it works for 3.3 too).