我正在尝试在3D模型的顶部绘制2D叠加层,该叠加层必须出现在屏幕的左上角,但是我得到的是将要绘制的矩形放置在3D模型的中间。您可以在下面查看我的输出的屏幕截图。
我对正交矩阵的设置是这样的
2 / (right - left), 0, 0, 0,
0, 2 / (top - bottom), 0, 0,
0, 0, 2 / (far - near), 0,
-(right + left) / (right - left), -(top + bottom) / (top - bottom), -(far + near) / (far - near), 1
有关如何在3D之上绘制2D叠加层的步骤,我遵循了this的答案。同样,根据输出,矩形看起来根本不是一个叠加层。
希望你能帮我 :)
编辑:
这就是我绘制模型的方式。请注意,此代码块将用于绘制两次模型
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, cc * sizeof(GLfloat), v, GL_STATIC_DRAW);
GLuint tcbuffer;
glGenBuffers(1, &tcbuffer);
glBindBuffer(GL_ARRAY_BUFFER, tcbuffer);
glBufferData(GL_ARRAY_BUFFER, tcc * sizeof(GLfloat), vt, GL_STATIC_DRAW);
GLuint ncbuffer;
glGenBuffers(1, &ncbuffer);
glBindBuffer(GL_ARRAY_BUFFER, ncbuffer);
glBufferData(GL_ARRAY_BUFFER, ncc * sizeof(GLfloat), vn, GL_STATIC_DRAW);
glEnableVertexAttribArray(m_vert);
glBindBuffer(GL_ARRAY_BUFFER, vbufferid);
glVertexAttribPointer(m_vert, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(m_texcoord);
glBindBuffer(GL_ARRAY_BUFFER, tcbufferid);
glVertexAttribPointer(m_texcoord, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(m_color);
glBindBuffer(GL_ARRAY_BUFFER, cbufferid);
glVertexAttribPointer(m_color, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(m_vertexnormal);
glBindBuffer(GL_ARRAY_BUFFER, ncbufferid);
glVertexAttribPointer(m_vertexnormal, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, vc);
并用于设置投影。
Matrix44 fov_matrix(double fov, double aspect, double near, double far) {
var yScale = 1.0 / Math.tan(fov / 2);
var xScale = yScale / aspect;
return(Matrix44.for_values(
xScale, 0, 0, 0,
0, yScale, 0, 0,
0, 0, (far + near) / (near - far), -1,
0, 0, 2 * far * near / (near - far), 0
));
}
Matrix44 orthographic_matrix(double left, double right, double bottom, double top, double near, double far) {
return(Matrix44.for_values(
2 / (right - left), 0, 0, 0,
0, 2 / (top - bottom), 0, 0,
0, 0, 2 / (far - near), 0,
-(right + left) / (right - left), -(top + bottom) / (top - bottom), -(far + near) / (far - near), 1
));
}
var projm = fov_matrix(MathConstant.M_PI/4, get_aspect_ratio(), 1.0, 100.0);
var orthoprojm = orthographic_matrix(0.0, 800, 600, 0.0, -100.0, 1.0);
最佳答案
您的投影矩阵对我来说是正确的。看起来,用于Spanch Bob绘图的矩阵也用于叠加层。
检查:
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
glGetProgramiv(program, GL_LINK_STATUS, &status);
glGetIntegeriv(GL_CURRENT_PROGRAM, &program)
glGetUniformLocation(program, "yourMVPmatrix")
返回正确的ID。 请注意,如果未正确链接任何程序,则OpenGL不会在
glDraw*
上发生竞争错误。关于c++ - OpenGL-在3D场景顶部错误绘制2D叠加层,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32433503/