为什么 glColor3f() 和 glTranslated() 不影响对象创建?

Why don't glColor3f() and glTranslated() affect object creation?

我在 GLFW 上下文中使用 GLUT 绘制了一个球体。我使用的代码是:

void drawSphere(std::vector<GLfloat> color, std::vector<GLdouble> position, float radius) {

    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    glColor3f(color[0], color[1], color[2]);
    glPushMatrix();
    glTranslated(position[0], position[1], position[2]);
    glutSolidSphere(radius, 50, 50);
    glPopMatrix();
}

我在 main() 函数循环中调用它,看起来像这样:

while(!glfwWindowShouldClose(window)) {
    // other stuff...
    drawSphere({(GLfloat)1.0, (GLfloat)1.0, (GLfloat)1.0}, {(GLdouble)2.0, (GLdouble)2.0, (GLdouble)2.0}, 1.0);
    // other stuff...
    glfwSwapBuffers(window);
    glfwPollEvents();
}

但是,无论我指定什么颜色或什么位置,我都会得到一个很棒的、以 (0, 0, 0) 为中心的红色球体。尽管如果我修改半径,球体会改变其尺寸。

我检查了代码很多次,几乎所有关于如何使用 GLUT 绘制实体的 Google 的结果,但找不到我的问题的答案。此外,我尝试通过两次调用该函数来绘制两个球体,给出两个不同的位置和两种不同的颜色。绘制了两个球体,但它们都是红色的并且以 (0, 0, 0) 为中心,正如您在这个屏幕截图中看到的:

因为我还使用带有顶点和片段着色器的 VAO、VBO 和 EBO 绘制其他东西,这会不会是个问题?这可能是 GLUT 和 GLFW 之间的兼容性问题吗?我该怎么做才能解决这个问题?

编辑:感谢@Rabbid76,我已经设法使用 glUseProgram(0) 解决了位置问题,而 glBindVertexArray(0) 似乎根本没有影响程序行为。不过,现在我面临另一个问题:因为我使用着色器绘制所有内容,所以我还有一个模型矩阵、一个视图矩阵和一个投影矩阵要处理。当我使用 gl 管道绘制球体时,它显示在相机视觉的一角:

无论我用相机做什么动作(使用鼠标和键盘),球体都将保持在那个位置(在相机 POV 中)。我认为这是因为我用来绘制对象的方式不同,但我不知道如何使用 gl 管道命令将球体设置到正确的位置。我能做什么?是否有某种有用的指南或教程可以让事情正常进行?

[...] I've also got a model matrix, a view matrix and a projection matrix to deal with. [...] I don't know how to set the sphere to the right position using the gl pipeline commands. [...]

Legacy OpenGL, the fixed function coordinate attribute which is specified by glVertexPointer (respectively the vertex corodinate which is set by glVertex),由当前模型视图矩阵(GL_MODELVIEW)和当前投影矩阵(GL_PROJECTION)变换。

如果您设置了 4x 投影和(模型)视图矩阵,则可以通过 glLoadMatrix 将此矩阵加载到当前矩阵。矩阵必须由指向 16 个连续值的指针提供,这些值用作 4x4 列主矩阵的元素。
当前矩阵(类型)可以通过glMatrixMode.

选择

在通过 glutSolidSphere 定义几何之前加载投影和(模型)视图矩阵。例如:

const GLfloat *projection = ...; // pointer to 4x4 matrix (16 consecutive floats)
const GLfloat *view       = ...; // pointer to 4x4 matrix (16 consecutive floats)


// load projection matrix
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(projection);

// load view matrix
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(view);

// concatenate model matrix to view matrix
glTranslated(position[0], position[1], position[2]);

// draw sphere
glutSolidSphere(radius, 50, 50);

// restore modelview matrix (the mode is still GL_MODELVIEW)
glPopMatrix();

// restore projection matrix
glMatrixMode(GL_PROJECTION);
glPopMatrix();

// set GL_MODELVIEW matrix mode (possibly this is not necessary)
glMatrixMode(GL_MODELVIEW);