如何旋转物体但保持照明固定? (OpenGL)

发布于 2024-09-15 03:13:41 字数 1081 浏览 5 评论 0原文

我有一个要旋转的立方体。我还有一个光源 GL_LIGHT0。我想旋转立方体并将光源固定在其位置。但光源是随着我的立方体一起旋转的。我使用 OpenGL ES 1.1 这是我的代码片段,可以让我的问题更清楚。

GLfloat glfarr[] = {...} //cube points
GLubyte glubFaces[] = {...}
Vertex3D normals[] = {...} //normals to surfaces

const GLfloat light0Position[] = {0.0, 0.0, 3.0, 0.0};
glLightfv(GL_LIGHT0, GL_POSITION, light0Position);
glEnable(GL_LIGHT0);

for(i = 0; i < 8000; ++i)
{
        if (g_bDemoDone) break;
        glLoadIdentity();

        glTranslatef(0.0,0.0, -12); 
        glRotatef(rot, 0.0, 1.0,1.0);
        rot += 0.8;
        glClearColor(0, 0, 0, 1);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_NORMAL_ARRAY);

        glNormalPointer(GL_FLOAT, 0, normals);

        glVertexPointer(3, GL_FLOAT, 0, glfarr);
        glDrawElements(GL_TRIANGLES, 3*12, GL_UNSIGNED_BYTE, glubFaces);

        glDisableClientState(GL_NORMAL_ARRAY);
        glDisableClientState(GL_VERTEX_ARRAY);
        eglSwapBuffers(eglDisplay, eglSurface);
}

谢谢。

I have a cube which I want to rotate. I also have a light source GL_LIGHT0. I want to rotate the cube and leave the light source fixed in its location. But the light source is rotating together with my cube. I use OpenGL ES 1.1
Here's a snippet of my code to make my question more clear.

GLfloat glfarr[] = {...} //cube points
GLubyte glubFaces[] = {...}
Vertex3D normals[] = {...} //normals to surfaces

const GLfloat light0Position[] = {0.0, 0.0, 3.0, 0.0};
glLightfv(GL_LIGHT0, GL_POSITION, light0Position);
glEnable(GL_LIGHT0);

for(i = 0; i < 8000; ++i)
{
        if (g_bDemoDone) break;
        glLoadIdentity();

        glTranslatef(0.0,0.0, -12); 
        glRotatef(rot, 0.0, 1.0,1.0);
        rot += 0.8;
        glClearColor(0, 0, 0, 1);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_NORMAL_ARRAY);

        glNormalPointer(GL_FLOAT, 0, normals);

        glVertexPointer(3, GL_FLOAT, 0, glfarr);
        glDrawElements(GL_TRIANGLES, 3*12, GL_UNSIGNED_BYTE, glubFaces);

        glDisableClientState(GL_NORMAL_ARRAY);
        glDisableClientState(GL_VERTEX_ARRAY);
        eglSwapBuffers(eglDisplay, eglSurface);
}

Thanks.

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

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

发布评论

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

评论(2

抚你发端 2024-09-22 03:13:41

与什么相关固定?当您执行 glLightfv(GL_LIGHT0, GL_POSITION, light0Position); 时,灯光位置将由当前的 MODELVIEW 矩阵进行变换。

如果您希望它与立方体一起移动,则必须移动 glLightfv( GL_LIGHT0, GL_POSITION, light0Position); 在平移和旋转调用之后。

Fixed in relation to what? The light position is transformed by the current MODELVIEW matrix when you do glLightfv(GL_LIGHT0, GL_POSITION, light0Position);

If you want it to move with with the cube you'll have to move glLightfv(GL_LIGHT0, GL_POSITION, light0Position); to after the translation and rotation calls.

柠檬心 2024-09-22 03:13:41

问题似乎是您正在旋转模型视图矩阵,而不是立方体本身。本质上,你正在移动相机。

为了仅旋转立方体,您需要旋转构成立方体的顶点。通常,这是使用库(GLUT 或类似库)或简单的三角函数来完成的。在调用 glDrawElements 之前,您将操作存储在数组中的顶点数据。您可能/可能不需要或想要修改法线或纹理坐标,这取决于您的效果及其最终的外观。

The problem seems to be that you're rotating the modelview matrix, not the cube itself. Essentially, you're moving the camera.

In order to rotate just the cube, you'll need to rotate the vertices that make up the cube. Generally that's done using a library (GLUT or some such) or simple trig. You'll be operating on the vertex data stored in the array, before the glDrawElements call. You may/may not have to or want to modify the normals or texture coordinates, it depends on your effects and how it ends up looking.

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