Results 1 to 7 of 7

Thread: OpenGL Lighting Selection

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2011
    Posts
    4

    OpenGL Lighting Selection

    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

  2. #2
    Newbie Frequent Contributor
    Join Date
    Mar 2016
    Posts
    896

    Re: OpenGL Lighting Selection

    I think you are looking for glDisable(GL_LIGHTING).

  3. #3
    Junior Member Newbie
    Join Date
    Nov 2011
    Posts
    4

    Re: OpenGL Lighting Selection

    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.

  4. #4
    Junior Member Newbie
    Join Date
    Nov 2011
    Posts
    4

    Re: OpenGL Lighting Selection

    Let me help by including my code for you guys.

    Code :
    #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 ();
    }

  5. #5
    Junior Member Newbie
    Join Date
    Nov 2011
    Posts
    11

    Re: OpenGL Lighting Selection

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

  6. #6
    Junior Member Newbie
    Join Date
    Nov 2011
    Posts
    4

    Re: OpenGL Lighting Selection

    Ah perfect! Thank you, that was my issue, just having it after the start. Works perfectly!

  7. #7
    Junior Member Newbie
    Join Date
    Nov 2011
    Posts
    11

    Re: OpenGL Lighting Selection

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

Similar Threads

  1. OpenGl - selection
    By CoolBeanJ in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 03-24-2011, 09:04 PM
  2. Help on OpenGL selection ???
    By jun wang in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 06-17-2010, 03:52 PM
  3. Lighting problem (opengl + shader lighting)
    By ox1d_47 in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 08-19-2004, 09:43 PM
  4. Is using selection buffer for selection or picking slow?
    By mohsin in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 04-11-2001, 03:56 AM
  5. About lighting (not OpenGL lighting, but general lighting)
    By Anonymous Coward in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 04-04-2001, 09:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean