Results 1 to 2 of 2

Thread: Does Light follow the glMultMatrixd rules ?

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2009
    Posts
    7

    Does Light follow the glMultMatrixd rules ?

    Hi,

    I have create a simple cube and a simple light... but when I rotate the cube (and not the light) the back face sounds "black"... but I have not rotate the light !

    In fact, the face in "front" should always be lighted, right ?

    Why this effect occur ? An idea ?

    Here is my code :

    private void Setup_CubeLighting()
    {
    float distance = (float)VIEWPORT_SPACE_SIZE * 20;

    //----
    Gl.glEnable(Gl.GL_LIGHTING);

    //---- Lights
    float[] LightAmbient = new float[] { 0f, 0f, 0f, 1.0f };
    float[] LightDiffuseSpecular;
    float[] LightPosition;

    // Front
    LightPosition = new float[] { .0f, .0f, distance, 1.0f };
    LightDiffuseSpecular = new float[] { 0.25f, 0f, 0f, 1f };
    Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_AMBIENT, LightAmbient);
    Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_DIFFUSE, LightDiffuseSpecular);
    Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_SPECULAR, LightDiffuseSpecular);
    Gl.glLightfv(Gl.GL_LIGHT0, Gl.GL_POSITION, LightPosition);
    Gl.glEnable(Gl.GL_LIGHT0);

    // Right
    //LightPosition = new float[] { distance, .0f, .0f, 1.0f };
    //LightDiffuseSpecular = new float[] { 0f, 0.25f, 0f, 1f };
    //Gl.glLightfv(Gl.GL_LIGHT1, Gl.GL_AMBIENT, LightAmbient);
    //Gl.glLightfv(Gl.GL_LIGHT1, Gl.GL_DIFFUSE, LightDiffuseSpecular);
    //Gl.glLightfv(Gl.GL_LIGHT1, Gl.GL_SPECULAR, LightDiffuseSpecular);
    //Gl.glLightfv(Gl.GL_LIGHT1, Gl.GL_POSITION, LightPosition);
    //Gl.glEnable(Gl.GL_LIGHT1);

    //// Left
    //LightPosition = new float[] { -distance, .0f, .0f, 1.0f };
    //LightDiffuseSpecular = new float[] { 0f, 0f, 0.25f, 1f };
    //Gl.glLightfv(Gl.GL_LIGHT3, Gl.GL_AMBIENT, LightAmbient);
    //Gl.glLightfv(Gl.GL_LIGHT3, Gl.GL_DIFFUSE, LightDiffuseSpecular);
    //Gl.glLightfv(Gl.GL_LIGHT3, Gl.GL_SPECULAR, LightDiffuseSpecular);
    //Gl.glLightfv(Gl.GL_LIGHT3, Gl.GL_POSITION, LightPosition);
    //Gl.glEnable(Gl.GL_LIGHT3);

    // Top
    //LightPosition = new float[] { .0f, distance, .0f, 1.0f };
    //Gl.glLightfv(Gl.GL_LIGHT4, Gl.GL_AMBIENT, LightAmbient);
    //Gl.glLightfv(Gl.GL_LIGHT4, Gl.GL_DIFFUSE, LightDiffuseSpecular);
    //Gl.glLightfv(Gl.GL_LIGHT4, Gl.GL_SPECULAR, LightDiffuseSpecular);
    //Gl.glLightfv(Gl.GL_LIGHT4, Gl.GL_POSITION, LightPosition);
    //Gl.glEnable(Gl.GL_LIGHT4);

    //// Bottom
    //LightPosition = new float[] { .0f, -distance, .0f, 1.0f };
    //Gl.glLightfv(Gl.GL_LIGHT5, Gl.GL_AMBIENT, LightAmbient);
    //Gl.glLightfv(Gl.GL_LIGHT5, Gl.GL_DIFFUSE, LightDiffuseSpecular);
    //Gl.glLightfv(Gl.GL_LIGHT5, Gl.GL_SPECULAR, LightDiffuseSpecular);
    //Gl.glLightfv(Gl.GL_LIGHT5, Gl.GL_POSITION, LightPosition);
    //Gl.glEnable(Gl.GL_LIGHT5);
    }

    private void Render_GuideCube()
    {
    //---- Viewport size
    int width = (int)VIEWPORT_SPACE_SIZE;
    int height = (int)VIEWPORT_SPACE_SIZE;
    Gl.glViewport((int)(oglImage.ActualWidth - width), (int)(oglImage.ActualHeight - height), width, height);

    #region Look at

    Gl.glMatrixMode(Gl.GL_PROJECTION);
    Gl.glLoadIdentity();

    Vector3 eye = new Vector3();
    Vector3 eyeAt = new Vector3();
    Vector3 eyeNormal = new Vector3();
    GetViewVectors(ref eye, ref eyeAt, ref eyeNormal, VIEWPORT_SPACE_SIZE);

    // Ortho is a Square because the viewport is now a square
    Gl.glOrtho(-VIEWPORT_SPACE_SIZE, VIEWPORT_SPACE_SIZE, -VIEWPORT_SPACE_SIZE, VIEWPORT_SPACE_SIZE, -500, 500);

    Glu.gluLookAt(
    eye.x, eye.y, eye.z,
    eyeAt.x, eyeAt.y, eyeAt.z,
    eyeNormal.x, eyeNormal.y, eyeNormal.z);

    Gl.glMatrixMode(Gl.GL_MODELVIEW);
    Gl.glLoadIdentity();

    #endregion

    #region Lighting

    Setup_CubeLighting();

    #endregion

    Gl.glPushMatrix();
    Gl.glMultMatrixd(_arcBall.Matrix);

    #region Draw the cube

    Gl.glColor3f(0.7f, 0.7f, 0.7f);
    Gl.glEnable(Gl.GL_COLOR_MATERIAL);

    //---- Materials
    Gl.glMaterialfv(Gl.GL_FRONT_AND_BACK, Gl.GL_AMBIENT, glfMatAmbient);
    Gl.glMaterialfv(Gl.GL_FRONT_AND_BACK, Gl.GL_DIFFUSE, glfMatDiffuse);
    Gl.glMaterialfv(Gl.GL_FRONT_AND_BACK, Gl.GL_SPECULAR, glfMatSpecular);
    Gl.glMaterialfv(Gl.GL_FRONT_AND_BACK, Gl.GL_EMISSION, glfMatEmission);
    Gl.glMaterialf(Gl.GL_FRONT_AND_BACK, Gl.GL_SHININESS, fShininess);

    //---- Textures
    InitializeTextures();

    Gl.glEnable(Gl.GL_TEXTURE_2D);

    // Front
    Gl.glBindTexture(Gl.GL_TEXTURE_2D, _frontTextureId);
    Gl.glBegin(Gl.GL_QUADS);
    if (_guideCubeSelection == GuideCubeNames.Front)
    Gl.glColor3f(1f, 0f, 0f);
    Gl.glTexCoord2f(0, 0); Gl.glVertex3d(-_cubeSize, -_cubeSize, _cubeSize);
    Gl.glTexCoord2f(1, 0); Gl.glVertex3d(_cubeSize, -_cubeSize, _cubeSize);
    Gl.glTexCoord2f(1, 1); Gl.glVertex3d(_cubeSize, _cubeSize, _cubeSize);
    Gl.glTexCoord2f(0, 1); Gl.glVertex3d(-_cubeSize, _cubeSize, _cubeSize);
    Gl.glColor3f(0.7f, 0.7f, 0.7f);
    Gl.glEnd();

    // Back
    Gl.glBindTexture(Gl.GL_TEXTURE_2D, _backTextureId);
    Gl.glBegin(Gl.GL_QUADS);
    if (_guideCubeSelection == GuideCubeNames.Back)
    Gl.glColor3f(1f, 0f, 0f);
    Gl.glTexCoord2f(0, 0); Gl.glVertex3d(_cubeSize, -_cubeSize, -_cubeSize);
    Gl.glTexCoord2f(1, 0); Gl.glVertex3d(-_cubeSize, -_cubeSize, -_cubeSize);
    Gl.glTexCoord2f(1, 1); Gl.glVertex3d(-_cubeSize, _cubeSize, -_cubeSize);
    Gl.glTexCoord2f(0, 1); Gl.glVertex3d(_cubeSize, _cubeSize, -_cubeSize);
    Gl.glColor3f(0.7f, 0.7f, 0.7f);
    Gl.glEnd();

    // Left
    Gl.glBindTexture(Gl.GL_TEXTURE_2D, _leftTextureId);
    Gl.glBegin(Gl.GL_QUADS);
    if (_guideCubeSelection == GuideCubeNames.Left)
    Gl.glColor3f(1f, 0f, 0f);
    Gl.glTexCoord2f(0, 0); Gl.glVertex3d(-_cubeSize, -_cubeSize, -_cubeSize);
    Gl.glTexCoord2f(1, 0); Gl.glVertex3d(-_cubeSize, -_cubeSize, _cubeSize);
    Gl.glTexCoord2f(1, 1); Gl.glVertex3d(-_cubeSize, _cubeSize, _cubeSize);
    Gl.glTexCoord2f(0, 1); Gl.glVertex3d(-_cubeSize, _cubeSize, -_cubeSize);
    Gl.glColor3f(0.7f, 0.7f, 0.7f);
    Gl.glEnd();

    // Right
    Gl.glBindTexture(Gl.GL_TEXTURE_2D, _rightTextureId);
    Gl.glBegin(Gl.GL_QUADS);
    if (_guideCubeSelection == GuideCubeNames.Right)
    Gl.glColor3f(1f, 0f, 0f);
    Gl.glTexCoord2f(0, 0); Gl.glVertex3d(_cubeSize, -_cubeSize, _cubeSize);
    Gl.glTexCoord2f(1, 0); Gl.glVertex3d(_cubeSize, -_cubeSize, -_cubeSize);
    Gl.glTexCoord2f(1, 1); Gl.glVertex3d(_cubeSize, _cubeSize, -_cubeSize);
    Gl.glTexCoord2f(0, 1); Gl.glVertex3d(_cubeSize, _cubeSize, _cubeSize);
    Gl.glColor3f(0.7f, 0.7f, 0.7f);
    Gl.glEnd();

    // Top
    Gl.glBindTexture(Gl.GL_TEXTURE_2D, _topTextureId);
    Gl.glBegin(Gl.GL_QUADS);
    if (_guideCubeSelection == GuideCubeNames.Top)
    Gl.glColor3f(1f, 0f, 0f);
    Gl.glTexCoord2f(0, 0); Gl.glVertex3d(-_cubeSize, _cubeSize, _cubeSize);
    Gl.glTexCoord2f(1, 0); Gl.glVertex3d(_cubeSize, _cubeSize, _cubeSize);
    Gl.glTexCoord2f(1, 1); Gl.glVertex3d(_cubeSize, _cubeSize, -_cubeSize);
    Gl.glTexCoord2f(0, 1); Gl.glVertex3d(-_cubeSize, _cubeSize, -_cubeSize);
    Gl.glColor3f(0.7f, 0.7f, 0.7f);
    Gl.glEnd();

    // Bottom
    Gl.glBindTexture(Gl.GL_TEXTURE_2D, _bottomTextureId);
    Gl.glBegin(Gl.GL_QUADS);
    if (_guideCubeSelection == GuideCubeNames.Bottom)
    Gl.glColor3f(1f, 0f, 0f);
    Gl.glTexCoord2f(0, 0); Gl.glVertex3d(-_cubeSize, -_cubeSize, -_cubeSize);
    Gl.glTexCoord2f(1, 0); Gl.glVertex3d(_cubeSize, -_cubeSize, -_cubeSize);
    Gl.glTexCoord2f(1, 1); Gl.glVertex3d(_cubeSize, -_cubeSize, _cubeSize);
    Gl.glTexCoord2f(0, 1); Gl.glVertex3d(-_cubeSize, -_cubeSize, _cubeSize);
    Gl.glColor3f(0.7f, 0.7f, 0.7f);
    Gl.glEnd();

    Gl.glDisable(Gl.GL_TEXTURE_2D);

    #endregion

    #region Draw the lines

    Gl.glLineWidth(1);
    Gl.glColor3f(1f, 1f, 1f);

    Gl.glBegin(Gl.GL_LINE_LOOP);
    Gl.glVertex3d(-_cubeSize, _cubeSize, -_cubeSize);
    Gl.glVertex3d(-_cubeSize, -_cubeSize, -_cubeSize);
    Gl.glVertex3d(_cubeSize, -_cubeSize, -_cubeSize);
    Gl.glVertex3d(_cubeSize, _cubeSize, -_cubeSize);
    Gl.glEnd();

    Gl.glBegin(Gl.GL_LINE_LOOP);
    Gl.glVertex3d(-_cubeSize, _cubeSize, _cubeSize);
    Gl.glVertex3d(-_cubeSize, -_cubeSize, _cubeSize);
    Gl.glVertex3d(_cubeSize, -_cubeSize, _cubeSize);
    Gl.glVertex3d(_cubeSize, _cubeSize, _cubeSize);
    Gl.glEnd();

    Gl.glBegin(Gl.GL_LINE_LOOP);
    Gl.glVertex3d(-_cubeSize, _cubeSize, -_cubeSize);
    Gl.glVertex3d(-_cubeSize, -_cubeSize, -_cubeSize);
    Gl.glVertex3d(-_cubeSize, -_cubeSize, _cubeSize);
    Gl.glVertex3d(-_cubeSize, _cubeSize, _cubeSize);
    Gl.glEnd();

    Gl.glBegin(Gl.GL_LINE_LOOP);
    Gl.glVertex3d(_cubeSize, _cubeSize, -_cubeSize);
    Gl.glVertex3d(_cubeSize, -_cubeSize, -_cubeSize);
    Gl.glVertex3d(_cubeSize, -_cubeSize, _cubeSize);
    Gl.glVertex3d(_cubeSize, _cubeSize, _cubeSize);
    Gl.glEnd();

    #endregion

    Gl.glPopMatrix();

    #region Reset

    ResetLight(Gl.GL_LIGHT0);

    Gl.glDisable(Gl.GL_LIGHTING);
    Gl.glDisable(Gl.GL_COLOR_MATERIAL);

    #endregion

    }

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Oct 2004
    Posts
    4,654

    Re: Does Light follow the glMultMatrixd rules ?

    Quote Originally Posted by ViewOn01
    I have create a simple cube and a simple light... but when I rotate the cube (and not the light) the back face sounds "black"... but I have not rotate the light !
    In fact, the face in "front" should always be lighted, right ?
    Why this effect occur ? An idea ?
    You're not passing any vertex normals down. Normals are necessary for proper lighting. See glNormal3f.

Similar Threads

  1. Replacing glMultMatrixd with Shaders ...
    By tmason in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 05-08-2014, 07:06 AM
  2. Some clarification about glMultMatrixd
    By jenny_wui in forum OpenGL: General
    Replies: 1
    Last Post: 08-19-2010, 08:28 AM
  3. glMultMatrixd()
    By thematrix in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 09-20-2009, 09:25 AM
  4. GLSL extensions rules
    By imported_Groovounet in forum OpenGL: GLSL
    Replies: 1
    Last Post: 01-07-2006, 12:32 PM
  5. geforce3 rules - screenshots
    By McBain in forum OpenGL: Advanced Coding
    Replies: 16
    Last Post: 02-11-2002, 05:22 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