简体   繁体   English

在OpenGL中设置MVP矩阵

[英]Setting up a MVP Matrix in OpenGL

i'm trying to learn the basics of OpenGL, but i have a Problem with setting up the transformation matrices. 我正在尝试学习OpenGL的基础知识,但我在设置转换矩阵时遇到了问题。 I made the model, view and projection matrices, but i have a problem sending them to my vertex shader. 我制作了模型,视图和投影矩阵,但是我将它们发送到顶点着色器时遇到了问题。

Here is the code: 这是代码:

//Set up MVP
glm::mat4 model = glm::mat4();
GLint uniModel = glGetUniformLocation(program, "model");
glUniformMatrix4fv(uniModel, 1, GL_FALSE, glm::value_ptr(model));

glm::mat4 view = glm::lookAt(
    glm::vec3(2.5f, 2.5f, 2.0f),
    glm::vec3(0.0f, 0.0f, 0.0f),
    glm::vec3(0.0f, 0.0f, 1.0f));
GLint uniView = glGetUniformLocation(program, "view");
glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));

glm::mat4 proj = glm::perspective(45.0f, 800.0f / 600.0f, 1.0f, 10.0f);
GLint uniProj = glGetUniformLocation(program, "proj");
glUniformMatrix4fv(uniProj, 1, GL_FALSE, glm::value_ptr(proj));

and the shader: 和着色器:

layout (location = 0) in vec3 position;

uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;

void main() {
    gl_Position = proj * view * model * vec4(position, 1.0);
}

I think i did something wrong with setting up the uniforms, because it doesn't draw anything, even if i set model, view and proj to the identity. 我觉得我在设置制服方面做错了,因为它没有画任何东西,即使我设置模型,查看和proj到身份。 Could be i just mistyped something, but i really can't find the problem. 可能我只是错误的东西,但我真的找不到问题。

Edit: Solved it, the problem was that i forgot to use glUseProgram() first. 编辑:解决了,问题是我忘了首先使用glUseProgram()

The first thing to do is check all possible return codes, verify your shader program has compiled and linked correctly, and make sure your uniforms are valid, which means the location value is >= 0 . 首先要检查所有可能的返回代码,验证您的着色器程序是否已正确编译和链接,并确保您的制服有效,这意味着位置值>= 0

A binary search with glGetError() can also be very useful in these instances when you don't know where it's going wrong. 当你不知道哪里出错时,使用glGetError()二进制搜索在这些实例中也非常有用。

After compiling the shaders, make sure you check glGetProgram(GL_LINK_STATUS,...) . 编译着色器后,请务必检查glGetProgram(GL_LINK_STATUS,...) And finally, you must call glUseProgram() to activate the shader. 最后,您必须调用glUseProgram()来激活着色器。

As @Vallentin suggests, it is much more efficient to pass in your precalculated MVP matrix, since it will not change between the app and the shader. 正如@Vallentin所说,传递预先计算的MVP矩阵要高效得多,因为它不会在应用程序和着色器之间发生变化。 This simplifies your code somewhat, to something like this for your application code: 这有点简化了您的代码,对于您的应用程序代码来说就是这样:

// Set up MVP matrices

glm::mat4 model = glm::mat4();

glm::mat4 view = glm::lookAt(
    glm::vec3(2.5f, 2.5f, 2.0f),
    glm::vec3(0.0f, 0.0f, 0.0f),
    glm::vec3(0.0f, 0.0f, 1.0f));

glm::mat4 proj = glm::perspective(45.0f, 800.0f / 600.0f, 1.0f, 10.0f);

glm::mat4 mvp = proj * view * model;

glUseProgram(prog);

GLint uniMvp = glGetUniformLocation(program, "mvp");
glUniformMatrix4fv(uniMvp, 1, GL_FALSE, glm::value_ptr(mvp));

and then the GLSL: 然后是GLSL:

// Shader code

layout (location = 0) in vec3 position;
uniform mat4 mvp;

void main() {
    gl_Position = mvp * vec4(position, 1.0);
}

Also, you could cache the uniform locations, since they won't change once compiled. 此外,您可以缓存统一位置,因为它们在编译后不会更改。 This will save a small amount per frame, rather than querying them for every redraw. 这将为每帧节省少量,而不是每次重绘时查询它们。

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

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