OpenGL 灯光不工作
我使用这段代码使用 LWJGL 绘制不同颜色的不同立方体:
GL11.glBegin(GL11.GL_QUADS);
GL11.glColor3f(rcol.x, rcol.y, rcol.z); // Color Vector
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z); // Top Right Of The Quad (Top)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z); // Top Left Of The Quad (Top)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z); // Bottom Left Of The Quad (Top)
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z); // Bottom Right Of The Quad (Top)
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y - rsiz.y, rpos.z + rsiz.z); // Top Right Of The Quad (Bottom)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y - rsiz.y, rpos.z + rsiz.z); // Top Left Of The Quad (Bottom)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y - rsiz.y, rpos.z - rsiz.z); // Bottom Left Of The Quad (Bottom)
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y - rsiz.y, rpos.z - rsiz.z); // Bottom Right Of The Quad (Bottom)
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z); // Top Right Of The Quad (Front)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z); // Top Left Of The Quad (Front)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y - rsiz.y, rpos.z + rsiz.z); // Bottom Left Of The Quad (Front)
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y - rsiz.y, rpos.z + rsiz.z); // Bottom Right Of The Quad (Front)
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y - rsiz.y, rpos.z - rsiz.z); // Bottom Left Of The Quad (Back)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y - rsiz.y, rpos.z - rsiz.z); // Bottom Right Of The Quad (Back)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z); // Top Right Of The Quad (Back)
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z); // Top Left Of The Quad (Back)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z); // Top Right Of The Quad (Left)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z); // Top Left Of The Quad (Left)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y - rsiz.y, rpos.z - rsiz.z); // Bottom Left Of The Quad (Left)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y - rsiz.y, rpos.z + rsiz.z); // Bottom Right Of The Quad (Left)
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z); // Top Right Of The Quad (Right)
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z); // Top Left Of The Quad (Right)
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y - rsiz.y, rpos.z + rsiz.z); // Bottom Left Of The Quad (Right)
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y - rsiz.y, rpos.z - rsiz.z); // Bottom Right Of The Quad (Right)
GL11.glEnd();
它绘制的立方体很好并且颜色很完美,但是一旦我向它添加光线,一切都会变成白色并且看起来很糟糕。这是我的照明代码:
GL11.glEnable(GL11.GL_LIGHTING);
GL11.glEnable(GL11.GL_LIGHT0);
float lightAmbient[] = { 0.5f, 0.5f, 0.5f, 1.0f }; // Ambient Light Values
float lightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f }; // Diffuse Light Values
float lightPosition[] = { 20.0f, 15.0f, 20.0f, 1.0f }; // Light Position
float lightSpecular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; // Light Position
ByteBuffer temp = ByteBuffer.allocateDirect(16);
temp.order(ByteOrder.nativeOrder())
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_SPECULAR, (FloatBuffer)temp.asFloatBuffer().put(lightSpecular).flip());
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_AMBIENT, (FloatBuffer)temp.asFloatBuffer().put(lightAmbient).flip()); // Setup The Ambient Light
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, (FloatBuffer)temp.asFloatBuffer().put(lightDiffuse).flip()); // Setup The Diffuse Light
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION,(FloatBuffer)temp.asFloatBuffer().put(lightPosition).flip()); // Position The Light
有谁知道我在这里做错了什么?这是示例图片:
I am using this code to draw different cubes with different colors using the LWJGL:
GL11.glBegin(GL11.GL_QUADS);
GL11.glColor3f(rcol.x, rcol.y, rcol.z); // Color Vector
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z); // Top Right Of The Quad (Top)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z); // Top Left Of The Quad (Top)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z); // Bottom Left Of The Quad (Top)
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z); // Bottom Right Of The Quad (Top)
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y - rsiz.y, rpos.z + rsiz.z); // Top Right Of The Quad (Bottom)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y - rsiz.y, rpos.z + rsiz.z); // Top Left Of The Quad (Bottom)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y - rsiz.y, rpos.z - rsiz.z); // Bottom Left Of The Quad (Bottom)
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y - rsiz.y, rpos.z - rsiz.z); // Bottom Right Of The Quad (Bottom)
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z); // Top Right Of The Quad (Front)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z); // Top Left Of The Quad (Front)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y - rsiz.y, rpos.z + rsiz.z); // Bottom Left Of The Quad (Front)
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y - rsiz.y, rpos.z + rsiz.z); // Bottom Right Of The Quad (Front)
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y - rsiz.y, rpos.z - rsiz.z); // Bottom Left Of The Quad (Back)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y - rsiz.y, rpos.z - rsiz.z); // Bottom Right Of The Quad (Back)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z); // Top Right Of The Quad (Back)
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z); // Top Left Of The Quad (Back)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z); // Top Right Of The Quad (Left)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z); // Top Left Of The Quad (Left)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y - rsiz.y, rpos.z - rsiz.z); // Bottom Left Of The Quad (Left)
GL11.glVertex3f(rpos.x - rsiz.x, rpos.y - rsiz.y, rpos.z + rsiz.z); // Bottom Right Of The Quad (Left)
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z - rsiz.z); // Top Right Of The Quad (Right)
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y + rsiz.y, rpos.z + rsiz.z); // Top Left Of The Quad (Right)
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y - rsiz.y, rpos.z + rsiz.z); // Bottom Left Of The Quad (Right)
GL11.glVertex3f(rpos.x + rsiz.x, rpos.y - rsiz.y, rpos.z - rsiz.z); // Bottom Right Of The Quad (Right)
GL11.glEnd();
It draws the cube fine and the color is perfect, but as soon as I add light to it, everything turns white and looks horrible. Here is my lighting code:
GL11.glEnable(GL11.GL_LIGHTING);
GL11.glEnable(GL11.GL_LIGHT0);
float lightAmbient[] = { 0.5f, 0.5f, 0.5f, 1.0f }; // Ambient Light Values
float lightDiffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f }; // Diffuse Light Values
float lightPosition[] = { 20.0f, 15.0f, 20.0f, 1.0f }; // Light Position
float lightSpecular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; // Light Position
ByteBuffer temp = ByteBuffer.allocateDirect(16);
temp.order(ByteOrder.nativeOrder())
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_SPECULAR, (FloatBuffer)temp.asFloatBuffer().put(lightSpecular).flip());
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_AMBIENT, (FloatBuffer)temp.asFloatBuffer().put(lightAmbient).flip()); // Setup The Ambient Light
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_DIFFUSE, (FloatBuffer)temp.asFloatBuffer().put(lightDiffuse).flip()); // Setup The Diffuse Light
GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION,(FloatBuffer)temp.asFloatBuffer().put(lightPosition).flip()); // Position The Light
Does anyone know what I am doing wrong here? Here is come example pictures:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
glNormal3f
定义四边形的法线向量。法线向量用于照明计算。法线矢量垂直于您的表面。因此,例如,对于您的第一个四边形:
y 坐标是恒定的,因此垂直于该表面的法向量可以
是
(0, 1, 0)
或(0, -1, 0)
。You need to define the normal vector to your quads with
glNormal3f
. The normal vector is used for lighting calculations.The normal vector is perpendicular to your surface. So, for example, with your first quad:
The y coordinate is constant, so a normal vector perpendicular to this surface could
be
(0, 1, 0)
or(0, -1, 0)
.