gluLookAt() 在 OpenGL 中无效
gluLookAt() has no effect in OpenGL
我正在尝试使用 gluLookAt() 函数从另一侧查看正方形。
使用该功能后,没有任何变化,虽然我预计正方形的角会发生变化。
我将相机点设置在世界的最右边,然后看它的中心,也就是正方形所在的位置。
他不得不向两侧伸展。为什么什么都没有改变?
代码:
#include "includes.h"
using namespace std;
constexpr auto FPS_RATE = 60;
int windowHeight = 600, windowWidth = 600, windowDepth = 600;
void init();
void idleFunction();
void displayFunction();
double getTime();
double getTime()
{
using Duration = std::chrono::duration<double>;
return std::chrono::duration_cast<Duration>(
std::chrono::high_resolution_clock::now().time_since_epoch()
).count();
}
const double frame_delay = 1.0 / FPS_RATE;
double last_render = 0;
void init()
{
glutDisplayFunc(displayFunction);
glutIdleFunc(idleFunction);
glViewport(0, 0, windowWidth, windowHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-windowWidth / 2, windowWidth / 2, -windowHeight / 2, windowHeight / 2, -windowDepth / 2, windowDepth / 2);
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
}
void idleFunction()
{
const double current_time = getTime();
if ((current_time - last_render) > frame_delay)
{
last_render = current_time;
glutPostRedisplay();
}
}
void displayFunction()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
gluLookAt(-300, 0, 0,
0, 0, 0,
0, 1, 0);
glColor3f(1, 1, 1);
glVertex3i(-150, 150, 0);
glVertex3i(150, 150, 0);
glVertex3i(150, -150, 0);
glVertex3i(-150, -150, 0);
glEnd();
glutSwapBuffers();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(windowWidth, windowHeight);
glutInitWindowPosition((GetSystemMetrics(SM_CXSCREEN) - windowWidth) / 2, (GetSystemMetrics(SM_CYSCREEN) - windowHeight) / 2);
glutCreateWindow("Window");
init();
glutMainLoop();
return 0;
}
问题是因为 gluLookAt()
是在 glBegin
/glEnd
序列中调用的。这是不允许的。您必须在 glBegin
.
之前致电 gluLookAt
一旦图元绘制由 glBegin
it is only allowed to specify vertex coordinates (glVertex
) and change attributes (e.g. glColor
, glTexCoord
...) 开始,直到绘制结束 (glEnd
)。
所有其他指令将被忽略并导致 GL_INVALID_OPERATION
错误(错误代码 1282)。
另外请注意,glLookAt
不会设置当前矩阵。它定义一个矩阵并将当前矩阵乘以新矩阵。设置矩阵模式(glMatrixMode
) and set Identity matrix by glLoadIdentity
前gluLookAt
.
带视图矩阵
gluLookAt(-300, 0, 0, 0, 0, 0, 0, 1, 0);
你想要 "see" 任何东西,因为有了那个矩阵,视线沿着 x-axis 设置,你从侧面看二维多边形。
请注意,多边形是一个二维对象。如果从正面、侧面(然后它是一条线而不可见)或从两者之间的方向看,物体的大小会有所不同。 gluLookAt
的前 3 个参数定义了视角,接下来的 3 个参数定义了你看的点。从视点到你所注视的点的矢量就是视线。
可能你想看看 z-axis:
void displayFunction()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, -300, 0, 0, 0, 0, 1, 0);
glBegin(GL_POLYGON);
glColor3f(1, 1, 1);
glVertex3i(-150, 150, 0);
glVertex3i(150, 150, 0);
glVertex3i(150, -150, 0);
glVertex3i(-150, -150, 0);
glEnd();
glutSwapBuffers();
}
您使用Orthographic (parallel) projection. If you would use Perspective projection, then the projected size of the object would decrease, when the distance to the point of view increases. Perspective projection can be set by gluPerspective
。例如:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0, (double)windowWidth / windowHeight, 0.1, 600.0);
我正在尝试使用 gluLookAt() 函数从另一侧查看正方形。
使用该功能后,没有任何变化,虽然我预计正方形的角会发生变化。
我将相机点设置在世界的最右边,然后看它的中心,也就是正方形所在的位置。
他不得不向两侧伸展。为什么什么都没有改变?
代码:
#include "includes.h"
using namespace std;
constexpr auto FPS_RATE = 60;
int windowHeight = 600, windowWidth = 600, windowDepth = 600;
void init();
void idleFunction();
void displayFunction();
double getTime();
double getTime()
{
using Duration = std::chrono::duration<double>;
return std::chrono::duration_cast<Duration>(
std::chrono::high_resolution_clock::now().time_since_epoch()
).count();
}
const double frame_delay = 1.0 / FPS_RATE;
double last_render = 0;
void init()
{
glutDisplayFunc(displayFunction);
glutIdleFunc(idleFunction);
glViewport(0, 0, windowWidth, windowHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-windowWidth / 2, windowWidth / 2, -windowHeight / 2, windowHeight / 2, -windowDepth / 2, windowDepth / 2);
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
}
void idleFunction()
{
const double current_time = getTime();
if ((current_time - last_render) > frame_delay)
{
last_render = current_time;
glutPostRedisplay();
}
}
void displayFunction()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
gluLookAt(-300, 0, 0,
0, 0, 0,
0, 1, 0);
glColor3f(1, 1, 1);
glVertex3i(-150, 150, 0);
glVertex3i(150, 150, 0);
glVertex3i(150, -150, 0);
glVertex3i(-150, -150, 0);
glEnd();
glutSwapBuffers();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(windowWidth, windowHeight);
glutInitWindowPosition((GetSystemMetrics(SM_CXSCREEN) - windowWidth) / 2, (GetSystemMetrics(SM_CYSCREEN) - windowHeight) / 2);
glutCreateWindow("Window");
init();
glutMainLoop();
return 0;
}
问题是因为 gluLookAt()
是在 glBegin
/glEnd
序列中调用的。这是不允许的。您必须在 glBegin
.
之前致电 gluLookAt
一旦图元绘制由 glBegin
it is only allowed to specify vertex coordinates (glVertex
) and change attributes (e.g. glColor
, glTexCoord
...) 开始,直到绘制结束 (glEnd
)。
所有其他指令将被忽略并导致 GL_INVALID_OPERATION
错误(错误代码 1282)。
另外请注意,glLookAt
不会设置当前矩阵。它定义一个矩阵并将当前矩阵乘以新矩阵。设置矩阵模式(glMatrixMode
) and set Identity matrix by glLoadIdentity
前gluLookAt
.
带视图矩阵
gluLookAt(-300, 0, 0, 0, 0, 0, 0, 1, 0);
你想要 "see" 任何东西,因为有了那个矩阵,视线沿着 x-axis 设置,你从侧面看二维多边形。
请注意,多边形是一个二维对象。如果从正面、侧面(然后它是一条线而不可见)或从两者之间的方向看,物体的大小会有所不同。 gluLookAt
的前 3 个参数定义了视角,接下来的 3 个参数定义了你看的点。从视点到你所注视的点的矢量就是视线。
可能你想看看 z-axis:
void displayFunction()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, -300, 0, 0, 0, 0, 1, 0);
glBegin(GL_POLYGON);
glColor3f(1, 1, 1);
glVertex3i(-150, 150, 0);
glVertex3i(150, 150, 0);
glVertex3i(150, -150, 0);
glVertex3i(-150, -150, 0);
glEnd();
glutSwapBuffers();
}
您使用Orthographic (parallel) projection. If you would use Perspective projection, then the projected size of the object would decrease, when the distance to the point of view increases. Perspective projection can be set by gluPerspective
。例如:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0, (double)windowWidth / windowHeight, 0.1, 600.0);