OpenGL 透视和光照不起作用?
编辑:有些人说使用 glNormal3f() 来解决我的照明问题,但我刚刚研究并实现了它,它根本没有改变任何东西。
我是个新手OpenGL,所以我想我做错了什么,但我尽可能严格地遵循了红皮书中的示例,但我无法弄清楚出了什么问题,所以我想我会尝试问你们。
我有两个问题。一是,即使我确定我正确使用了 glFrustum,它仍然会渲染正交场景,并且由于某种原因它会渲染相机前面和后面的对象。另一个是,尽管我很确定我的照明方式是正确的(因为我遵循了书中的示例),但它以统一的颜色照亮场景,没有阴影或任何东西。这是我的代码:
#include "GraphicsManager.h"
#include <GL/gl.h>
void GraphicsManager::initialize () {
fx = 0;
fy = 0;
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
glDepthFunc (GL_LEQUAL);
glEnable (GL_DEPTH_TEST);
glViewport (0, 0, 800, 600);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}
int GraphicsManager::draw (std::vector<float> buttoninfo) {
// changing light_position makes no difference
GLfloat light_position[] = { 1.0, 1.0, 1.0, 1.0 };
int i, j, k;
fx += buttoninfo[0];
fy += buttoninfo[1];
glClearColor (0.0, 0.0, 0.0, 1.0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f (0.5, 0.5, 0.8);
glLoadIdentity ();
glLightfv (GL_LIGHT0, GL_POSITION, light_position);�
glRotatef (buttoninfo[3], 1.0, 0.0, 0.0);
glRotatef (buttoninfo[2], 0.0, 1.0, 0.0);
glTranslatef (-fx, 0, -fy);
for (i = 0; i < 5; i ++) {
for (j = 0; j < 5; j ++) {
for (k = 0; k < 5; k ++) {
glPushMatrix ();
glTranslatef (i, j, k);
glBegin (GL_TRIANGLE_FAN);
glVertex3f (0.0, 0.5, 0.0);
glVertex3f (-0.5, -0.5, -0.5);
glVertex3f (-0.5, -0.5, 0.5);
glVertex3f (0.5, -0.5, 0.5);
glVertex3f (0.5, -0.5, -0.5);
glVertex3f (-0.5, -0.5, -0.5);
glEnd ();
glPopMatrix ();
}
}
}
glFlush ();
return (int)buttoninfo[5];
}
你能帮我找出问题所在吗?提前致谢。
EDIT: A few people have said to use glNormal3f() to solve my lighting problem, but I researched and implemented that just now and it didn't change anything at all.
I'm kind of new to OpenGL so I guess I'm doing something wrong, but I followed the examples in the Red Book as closely as I could and I just can't figure out what's wrong, so I figured I'd try and ask you guys.
I have two problems. One is that, even though I'm sure I'm using glFrustum correctly, it still renders an orthographic scene, and it renders objects that are both in front of and behind the camera for some reason. The other is that, even though I'm pretty sure I'm doing this lighting right (because I followed the example in the book), it's lighting the scene with a uniform color, no shading or anything. Here's my code:
#include "GraphicsManager.h"
#include <GL/gl.h>
void GraphicsManager::initialize () {
fx = 0;
fy = 0;
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
glDepthFunc (GL_LEQUAL);
glEnable (GL_DEPTH_TEST);
glViewport (0, 0, 800, 600);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}
int GraphicsManager::draw (std::vector<float> buttoninfo) {
// changing light_position makes no difference
GLfloat light_position[] = { 1.0, 1.0, 1.0, 1.0 };
int i, j, k;
fx += buttoninfo[0];
fy += buttoninfo[1];
glClearColor (0.0, 0.0, 0.0, 1.0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f (0.5, 0.5, 0.8);
glLoadIdentity ();
glLightfv (GL_LIGHT0, GL_POSITION, light_position);�
glRotatef (buttoninfo[3], 1.0, 0.0, 0.0);
glRotatef (buttoninfo[2], 0.0, 1.0, 0.0);
glTranslatef (-fx, 0, -fy);
for (i = 0; i < 5; i ++) {
for (j = 0; j < 5; j ++) {
for (k = 0; k < 5; k ++) {
glPushMatrix ();
glTranslatef (i, j, k);
glBegin (GL_TRIANGLE_FAN);
glVertex3f (0.0, 0.5, 0.0);
glVertex3f (-0.5, -0.5, -0.5);
glVertex3f (-0.5, -0.5, 0.5);
glVertex3f (0.5, -0.5, 0.5);
glVertex3f (0.5, -0.5, -0.5);
glVertex3f (-0.5, -0.5, -0.5);
glEnd ();
glPopMatrix ();
}
}
}
glFlush ();
return (int)buttoninfo[5];
}
Can you help me figure out what's wrong? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在围绕原点 (0,0,0) 绘制元素,但一开始并没有移动“相机”。所以你是从场景的原点看的,它位于你的元素内部。
尝试
修复它
,对于透视渲染,请考虑使用 gluPerspective。
http://www.opengl.org/sdk/docs/man/xhtml /gluPerspective.xml
You are drawing the elements around the origin (0,0,0) but you don't move the "camera" at the begining. so your are looking from the origin point at the scene, which is inside your elements.
try if
works to fix it
and for Perspective rendering consider using gluPerspective.
http://www.opengl.org/sdk/docs/man/xhtml/gluPerspective.xml