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

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

我以前用过正交透视法,画的很到位。我在初始化期间使用了以下代码:

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

而这个着色器:

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

我一直在尝试改用我的着色器中的 MVP 矩阵,但我的屏幕上没有任何输出,除了我用来清除它的颜色。

我构建它:

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;

然后在删除旧代码后在初始化期间应用它:

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

这是我当前的主要绘图循环:

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

我试过移动相机,旋转它,以防对象被绘制到视图之外,但我不确定此时是否有任何东西被绘制。

"MVP" 是统一变量。要获取统一变量的位置索引,您必须使用 glGetUniformLocation rather than glGetAttribLocation

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