简体   繁体   English

在着色器中从 glOrtho 切换到 MVP 矩阵后没有绘制任何内容

[英]Nothing is drawing after switching from glOrtho to MVP matrix inside shader

I've used orthographic perspective before, which drew everything properly.我以前使用过正交透视,它正确地绘制了所有内容。 I used the following code during initialization:我在初始化期间使用了以下代码:

glViewport(0, 0, WIDTH, HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WIDTH, HEIGHT, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);

And this shader:这个着色器:

vec3 actualPos = inPos + cubeVert * (inMass * scale);
gl_Position = vec4(actualPos, 1.0); 
Mass = inMass;
UV = cubeVert.xy + vec2(0.5, 0.5);

I've been trying to switch to using the MVP matrix inside my shader instead, but I get no output on my screen, other than the color I clear it with.我一直在尝试改用我的着色器中的 MVP 矩阵,但我的屏幕上没有任何输出,除了我清除它的颜色。

I build it with:我用以下方法构建它:

glm::mat4 model(1.f);

glm::mat4 projection(1.f);
projection = glm::perspective(glm::radians(30.f), (float)WIDTH / (float)HEIGHT, 0.1f, 100.f);

glm::mat4 view(1.f);
view = glm::lookAt(glm::vec3(-10.f, -10.f, -10.f), glm::vec3(0.f, 0.f, 0.f), glm::vec3(0.f, 1.f, 0.f));

glm::mat4 MVP = projection * view * model;

And then apply it during initialization, after removing the old code:然后在初始化期间应用它,删除旧代码后:

glViewport(0, 0, WIDTH, HEIGHT);
glUniformMatrix4fv(glGetAttribLocation(program, "MVP"), 1, GL_FALSE, glm::value_ptr(MVP));

And this is my current main drawing loop:这是我当前的主要绘图循环:

glClearColor(0.01f, 0.10f, 0.15f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program);

glUniformMatrix4fv(glGetAttribLocation(program, "MVP"), 1, GL_FALSE, glm::value_ptr(MVP));

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE0, texture);
glUniform1i(sampler, 0);

glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBOcube);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, (void*)0);

glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, AMOUNT * sizeof(Body), NULL, GL_STREAM_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, AMOUNT * sizeof(Body), bods);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Body), (void*)0);
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(Body), (void*)sizeof(glm::vec3));

glVertexAttribDivisor(0, 0);
glVertexAttribDivisor(1, 1);
glVertexAttribDivisor(2, 1);

glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, AMOUNT);

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);

I've tried to move the camera around, rotate it, in case the objects were being drawn outside of the view, but I'm not sure if anything is even being drawn at this point.我试图移动相机,旋转它,以防物体被绘制在视图之外,但我不确定此时是否正在绘制任何东西。

"MVP" Is a uniform variable. "MVP"是一个统一变量。 To geht the location index of a uniform variable, you've to use glGetUniformLocation rather than glGetAttribLocation .要获取统一变量的位置索引,您必须使用glGetUniformLocation而不是glGetAttribLocation

glLinkProgram(program);
GLint mvp_loc = glGetUniformLocation(program, "MVP"):
glUseProgram(program);
glUniformMatrix4fv(mvp_loc, 1, GL_FALSE, glm::value_ptr(MVP));

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

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