LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   openGL, glut and lights on C++ (https://www.linuxquestions.org/questions/programming-9/opengl-glut-and-lights-on-c-212645/)

poeta_boy 08-02-2004 05:21 PM

openGL, glut and lights on C++
 
Hello everyone:

I have a simple scene in openGl, in my display() I have:

Code:

void renderScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);                       
GLfloat lightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };               
GLfloat ambientPosition[] = {50,50,50,1};               
glLightfv(GL_LIGHT1, GL_AMBIENT, lightAmbient);       
glLightfv(GL_LIGHT1, GL_POSITION, ambientPosition);
glEnable(GL_LIGHT1)

glColor3f(0, 0.9, 0);                                       
glBegin(GL_QUADS);                               
glVertex3f(-100.0f, 0.0f, -100.0f);               
glVertex3f(-100.0f, 0.0f,  100.0f);
glVertex3f( 100.0f, 0.0f,  100.0f);
glVertex3f( 100.0f, 0.0f, -100.0f);
glEnd();

glPushMatrix();
glTranslatef(-40,40,-40);
drawSun();
glPopMatrix();

glPushMatrix();
glTranslatef(-40, 0, -40);
drawWindmill();               
glPopMatrix();

glPushMatrix();
glTranslatef(20, 0, 0);
drawBoy();       
walk();
glPopMatrix();
}
void drawSun()
{       
GLfloat light_position[] = {0,0,0,1};                       
GLfloat light_color[] = {1,1,1,1};
glTranslatef(10,10,10);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_color);
glEnable(GL_LIGHT0);
       
glTranslatef(-10, -10, -10);
float size = 10;
glColor3f(1,1,0);
glutSolidSphere(size, 100, 100);
}

int main(int argc, char** argv)
{
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);
       
        glShadeModel(GL_SMOOTH);
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
       
        glutInitWindowPosition(100, 10);
        glutInitWindowSize(700,700);
        glutCreateWindow("My World!");

        glutDisplayFunc(renderScene);
        glutIdleFunc(renderScene);

              glutMainLoop();
        return EXIT_SUCCESS;
}

As you can see drawSun() is just drawing an Sphere and then tries to put a light inside it. But I can't make it work, since if I only add GL_AMBIENT light (at renderScene() )I end up with a black screen, and if I add the light at the drawSun() there appears the light as intended, but everything else loses it's color, it gets only black and white (even tough I do get the light effect, shadows are black and light white).

Why doesn't the ambient light work? thanks

Poeta


All times are GMT -5. The time now is 08:06 AM.