OpenGL照明和闪耀

发布于 2024-12-13 18:16:49 字数 2439 浏览 0 评论 0 原文

我必须在 OpenGL 图片中放置照明,但我不知道如何放置正确的点光源照明,如下图所示:

Right

现在我尝试了不同的照明模式,但结果如下:(

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

编辑 2011 年 4 月 11 日 0.39 CET

我还附加了由 glutDisplayFunc(display) 调用的 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();

  }

在显示实践中,我编写了组成我表示的 2° 图形的小区域(我)省略了计算每个区域的点的算法,因为它有效)。

我的目标是得到类似于图 1 的闪电和渲染结果,但我只得到了图 2 中的结果。

我不知道如何正确放置灯光以及如何设置我的图形以获得此渲染(如镜子)。

I have to put illumination in a OpenGL picture but i don't know how to put a correct point light illumination like in the following picture:

Right

For now i had tried different mode of illumination, but the result is the following :(

Wrong

I attach you the code that I used for my result. What is 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 04/11/2011 0.39 CET

I attach also my 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 display I write the small areas that compose the 2° figure that i represented (I omitted the algorithm for the calculation of the points of each area because is it working).

My goal is to have a result similar to figure 1 for the lightning and rendering of the figure but I only obtained the result in figure 2.

I don't know how to put correctly the light and how to setup my figure in order to have this render (like mirror).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是年少的欢喜 2024-12-20 18:16:49

你缺少法线。您需要为每个顶点提供一个法线 (glNormal) 或如果您使用GL_AUTO_NORMALS rel="nofollow">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

从您发布的结果图像来看,您似乎正在计算和绘制法线(红色箭头),只是没有应用它们。

You're missing normals. You either need to provide one normal per vertex (glNormal) or you could enable GL_AUTO_NORMALS if you use glMap2 (which you are not). There's an article on how to compute normals for a triangle or polygons on opengl.org that you probably will find useful.

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

From the image you posted of the results it seems you are calculating and drawing the normals (red arrows), just not applying them.

ヅ她的身影、若隐若现 2024-12-20 18:16:49

无法真正弄清楚您的代码发生了什么。我希望你的坐标在-1到+1之间。我建议您使用简单的图元来处理照明。曲面生成的工作。光源的位置和计算法线是最重要的点。

http://www.falloutsoftware.com/tutorials/gl/gl8.htm

http://glprogramming.com/red/chapter05.html

Can't really figure out what's happening with your code. I hope your cordinates are between -1 to +1. I'd suggest you to work with lighting with simple primitives. the work on with your surface generation. The lighting source's position and calculating normals are the most important points.

http://www.falloutsoftware.com/tutorials/gl/gl8.htm

http://glprogramming.com/red/chapter05.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文