如何通过着色器在世界中应用绘图位置?
我的顶点着色器看起来像这样:
in vec2 position;
uniform mat4x4 model;
uniform mat4x4 view;
uniform mat4x4 projection;
void main() {
gl_Position = projection * view * model * vec4(position, 0.0, 1.0);
}
其中 position
是三角形顶点的位置。
我按如下方式绑定(bind)矩阵。
查看:
glm::mat4x4 view = glm::lookAt(
glm::vec3(0.0f, 1.2f, 1.2f), // camera position
glm::vec3(0.0f, 0.0f, 0.0f), // camera target
glm::vec3(0.0f, 0.0f, 1.0f)); // camera up axis
GLint view_uniform = glGetUniformLocation(shader, "view");
glUniformMatrix4fv(view_uniform, 1, GL_FALSE, glm::value_ptr(view));
投影:
glm::mat4x4 projection = glm::perspective(80.0f, 640.0f/480.0f, 0.1f, 100.0f);
GLint projection_uniform = glGetUniformLocation(shader, "projection");
glUniformMatrix4fv(projection_uniform, 1, GL_FALSE, glm::value_ptr(projection));
模型转换:
glm::mat4x4 model;
model = glm::translate(model, glm::vec3(1.0f, 0.0f, 0.0f));
model = glm::rotate(model, static_cast<float>((glm::sin(currenttime)) * 360.0), glm::vec3(0.0, 0.0, 1.0));
GLint trans_uniform = glGetUniformLocation(shader, "model");
glUniformMatrix4fv(trans_uniform, 1, GL_FALSE, glm::value_ptr(model));
所以这样我就必须在 CPU 上计算每一帧的位置变换。这是推荐的方法还是有更好的方法?
最佳答案
So this way I have to compute the position transformation each frame on the CPU. Is this the recommended way or is there a better one?
是的。在 CPU 上为网格计算一次新变换,然后将其应用于顶点着色器内网格内的所有顶点不会是一个非常慢的操作,需要在每一帧完成。
关于c++ - 如何应用位置变换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18488826/