我有一个简单的 OpenGL 程序并试图绘制一个存储在顶点着色器中的实例化数组。我正在使用两个跟随着色器进行渲染:
顶点着色器:
#version 330 core
uniform mat4 MVP;
const int VertexCount = 4;
const vec2 Position[VertexCount] = vec2[](
vec2(-100.0f, -100.0f),
vec2( -100.0f, 100.0f),
vec2( 100.0f, -100.0f),
vec2(100.0f, 100.0f));
void main()
{
gl_Position = MVP * vec4(Position[gl_VertexID], 0.0, 1.0);
}
片段着色器:
#version 330 core
#define FRAG_COLOR 0
layout(location = FRAG_COLOR, index = 0) out vec4 Color;
void main()
{
Color = vec4(0, 1, 0, 1); //let it will be green.
}
在我编译并验证了这些着色器之后,我创建了一个顶点数组对象并将其绘制成三角形条带:
glUseProgram(programHandle); //handle is checked and valid.
glBindVertexArray(vao);
glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, 1);
绘图的视口设置为像 glViewport(0, 0, 800, 600) 这样的窗口大小。我将一个带有休闲代码的简单正交矩阵传递给 MVP:
glUniformMatrix4fv(handle, 1, GL_FALSE, (GLfloat*)&matrix); //handle is checked and valid
初始化矩阵的位置:
Matrix::CreateOrthographicOffCenter(-200, 200, -200, 200, 1.0f, -1.0f, &matrix);
...
void Matrix::CreateOrthographicOffCenter(float left, float right, float bottom, float top, float zNearPlane, float zFarPlane, Matrix* matrix)
{
memset(matrix, 0, sizeof(Matrix));
matrix->M11 = 2.0f / (right - left);
matrix->M14 = (-right - left) / (right - left);
matrix->M22 = 2.0f / (top - bottom);
matrix->M24 = (-top - bottom) / (top - bottom);
matrix->M33 = 1.0f / (zFarPlane - zNearPlane);
matrix->M34 = (-zNearPlane) / (zFarPlane - zNearPlane);
matrix->M44 = 1.0f;
}
问题是我的屏幕上没有三角条。我试图在没有 MVP 矩阵的情况下绘制顶点 (gl_Position = vec4(Position[gl_VertexID], 0.0, 1.0)) 但也一无所获。如何检测问题出在哪里?