gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 01:24:36 25 4
gpt4 key购买 nike

我在 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) 为中心的红色球体。尽管如果我修改半径,球体会改变其尺寸。

Single sphere

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

Inside the bigger sphere

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

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

Camera visual issue

无论我用相机做什么 Action (使用鼠标和键盘),球体都将保持在那个位置(在相机 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 , glVertexPointer 指定的固定函数坐标属性(分别由 glVertex 设置的顶点坐标)由当前模型 View 矩阵(GL_MODELVIEW)和当前投影矩阵(GL_PROJECTION)转换。

如果你已经设置了一个 4x 投影和(模型) View 矩阵,这个矩阵可以通过 glLoadMatrix 加载到当前矩阵。 .矩阵必须由指向 16 个连续值的指针提供,这些值用作 4x4 列主矩阵的元素。
当前矩阵(类型)可以通过glMatrixMode来选择.

在通过 glutSolidSphere 绘制几何之前加载投影和(模型) View 矩阵。例如:

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

关于c++ - 为什么 glColor3f() 和 glTranslated() 不影响对象创建?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58061683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com