OpenGL Lighting and Shine

I need to turn on the highlight in the OpenGL image, but I don’t know how to set the spotlight correctly, as in the following figure:

Right

At the moment, I tried to use a different lighting mode, but the result is as follows: (

Wrong

I am attaching you the code that I used for my result. What's wrong?

    float specref[] = { 1.0f, 1.0f, 1.0f, 1.0f };
    // Light values and coordinates
    float ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f };
    float diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f };
    float specular[] = { 1.0f, 1.0f, 1.0f, 1.0f};
    float lightPos[] = { 0.0f, -150.0f, -150.0f, 1.0f };

    glEnable ( GL_LIGHTING ) ;

    glLightfv(GL_LIGHT0,GL_AMBIENT, ambientLight);
    glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
    glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
    glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
    glEnable(GL_LIGHT0);
    glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,specref);
    glMateriali(GL_FRONT_AND_BACK,GL_SHININESS,128);
    glEnable ( GL_COLOR_MATERIAL ) ;

    glClearColor (1.0, 1.0, 1.0, 1.0);
    glColor3f(0.0,0.0,0.0);

Edit 11/11/2011 0.39 CET

I also add a display () function called by glutDisplayFunc (display);

  void display(void)

  {

  [...]

  //Draw polygons in 3d

  glColor3f(0.0,1.0,1.0);

  glBegin(GL_QUADS);

  for(i=0;i<NVERT-1;i++) {

        for (j=0;j<NVERT-1;j++) {

              glVertex3f( (GLfloat)((sb[i*NVERT+j]).x()),

                                      (GLfloat)((sb[i*NVERT+j]).y()),

                                      (GLfloat)((sb[i*NVERT+j]).z()));


              glVertex3f( (GLfloat)((sb[i*NVERT+j+1]).x()),

                                      (GLfloat)((sb[i*NVERT+j+1]).y()),

                                      (GLfloat)((sb[i*NVERT+j+1]).z()));


              glVertex3f( (GLfloat)((sb[(i+1)*NVERT+j+1]).x()),

                                      (GLfloat)((sb[(i+1)*NVERT+j+1]).y()),

                                      (GLfloat)((sb[(i+1)*NVERT+j+1]).z()));


              glVertex3f( (GLfloat)((sb[(i+1)*NVERT+j]).x()),

                                      (GLfloat)((sb[(i+1)*NVERT+j]).y()),

                                      (GLfloat)((sb[(i+1)*NVERT+j]).z()));


        }



  }

  glEnd();
  glFlush();

  }

In practice, with the display, I write small areas that make up the 2 Β° shape presented by me (I skipped the algorithm for calculating the points of each area because it works).

My goal is to get a result similar to Figure 1 for lightning and rendering the figure, but I only got the result in Figure 2.

, , (, ).

+5
2

. (glNormal), GL_AUTO_NORMALS, glMap2 ( ). , opengl.org, , , .

Begin Function CalculateSurfaceNormal (Input Polygon) Returns Vector

   Set Vertex Normal to (0, 0, 0)

   Begin Cycle for Index in [0, Polygon.vertexNumber)

      Set Vertex Current to Polygon.verts[Index]
      Set Vertex Next    to Polygon.verts[(Index plus 1) mod Polygon.vertexNumber]

      Set Normal.x to Sum of Normal.x and (multiply (Current.y minus Next.y) by (Current.z plus Next.z))
      Set Normal.y to Sum of Normal.y and (multiply (Current.z minus Next.z) by (Current.x plus Next.x))
      Set Normal.z to Sum of Normal.z and (multiply (Current.x minus Next.x) by (Current.y plus Next.y))

   End Cycle

   Returning Normalize(Normal)

End Function

, , , ( ), .

+1

All Articles