简体   繁体   English

gluLookAt()在OpenGL中无效

[英]gluLookAt() has no effect in OpenGL


I'm trying to look at the square from the other side using the gluLookAt() function. 我试图使用gluLookAt()函数从另一侧看正方形。
After using the function, nothing changes, although I expected that the corners of the square will change. 使用该函数后,什么都没有改变,尽管我希望正方形的角会改变。
I set the camera point to the rightmost part of the world and look at its center, where the square is located. 我将相机点设置在世界的最右边,并查看其中心,即正方形所在的位置。
He had to stretch out to the sides. 他不得不向两侧伸展。 Why hasn't anything changed? 为什么什么都没有改变?

Code: 码:

#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;
}

The issue is caused because gluLookAt() is call with in a glBegin / glEnd sequence. 造成此问题的原因是,在glBegin / glEnd序列中调用了gluLookAt() This is not allowed. 这是不允许的。 You've to call gluLookAt before glBegin . 您必须在glBegin之前调用gluLookAt
Once drawing of primitives was started by glBegin it is only allowed to specify vertex coordinates ( glVertex ) and change attributes (eg glColor , glTexCoord ...), till the drawn is ended ( glEnd ). glBegin开始绘制图元后,仅允许指定顶点坐标( glVertex )并更改属性(例如glColorglTexCoord ...),直到绘制结束( glEnd )。
All other instruction will be ignored and cause a GL_INVALID_OPERATION error (error code 1282). 所有其他指令将被忽略,并导致GL_INVALID_OPERATION错误(错误代码1282)。

Further note, that glLookAt doesn't set a the current matrix. 还要注意, glLookAt不会设置当前矩阵。 It defines a matrix and multiplies the current matrix by the new matrix. 它定义一个矩阵,然后将当前矩阵乘以新矩阵。 Set the matrix mode ( glMatrixMode ) and set Identity matrix by glLoadIdentity before gluLookAt . 设置矩阵模式( glMatrixMode ),并设置标识矩阵通过glLoadIdentity之前gluLookAt

With the view matrix 与视图矩阵

 gluLookAt(-300, 0, 0, 0, 0, 0, 0, 1, 0); 

you want "see" anything, because with that matrix the line of sight is set along the x-axis and you look at the 2 dimensional polygon from the side. 您想“看到”任何东西,因为通过该矩阵,视线沿x轴设置,并且您从侧面看了二维多边形。

Note, the polygon is a 2D object. 注意,多边形是2D对象。 The size of the object appears different if you look at it from the front, from the side (then it is a line and not visible) or from an direction in between. 如果从正面,侧面(然后是一条线且不可见)或中间的方向看,则对象的大小会有所不同。 The first 3 parameters of gluLookAt define the point of view the next 3 parameters define the point you look at. gluLookAt的前三个参数定义了观察点,接下来的3个参数定义了观察点。 The vector from the point of view to the point you look at is the line of sight. 从视点到观察点的向量是视线。

Probably yo want look along the z-axis: 您可能想沿z轴看一下:

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

You use 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 . 透视投影可以通过gluPerspective设置。 eg: 例如:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0, (double)windowWidth / windowHeight, 0.1, 600.0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM