解决方案
问题出在我的 z 区间中,我正在考虑 z 的负值,例如 (-0.1f < z < 100f),而不是我对近/远平面 ex 使用了正区间:(0.0f < z < 100.0f),我安排了OpenGL中已弃用无效的其他东西,我在我的glm :: lookAt上安排了我的坏位置。
注意:不要将旧的渲染方式与新的渲染方式混合使用(这是一个很好的做法,但可能会出现问题),顺便说一句:您必须了解您在空间中的位置。
谢谢大家:)
我是一名初学者图形程序员,我试图使用基本的顶点/片段着色器绘制一个非常简单的三角形。但是我的投影矩阵有一个错误,我找不到它。另外,我试图做一个 reshape void 并且我希望 reshape 回调与 shader 绘制的三角形一起工作。当我在我的渲染空白中编写这些产品时,会发生这种情况:
MVP = 投影矩阵 * 模型视图矩阵;
结果 ->我不喜欢
如果我只将 ModelView 矩阵分配给 MVP,我会得到:
这些是我的重塑空隙
void OpenGL_Initialization(int w, int h){
ProjectionMatrix = glm :: ortho(0.0f,(GLfloat)w,(GLfloat)h,0.0f,0.1f, 100.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glLoadMatrixf(glm :: value_ptr(ProjectionMatrix));
glMatrixMode(GL_MODELVIEW);
}
void Reshape(int w, int h){
width = w;
height = h;
glViewport(0,0,w,h);
OpenGL_Initialization(w,h);
}
这是我的渲染回调:
void Render(){
glClearColor(0.75,0.75,0.75,1.0);
glClear(GL_COLOR_BUFFER_BIT);
glGetFloatv(GL_MODELVIEW_MATRIX,glm :: value_ptr(ModelViewMatrix));
MVP = ProjectionMatrix * ModelViewMatrix;
glUniformMatrix4fv(glGetUniformLocation(ProgramHandler,"MVP"),1,GL_FALSE,glm :: value_ptr(MVP));
glBindVertexArray(vaoHandle);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
glutSwapBuffers();
}
这些也是我的顶点和片段着色器
#version 400
layout (location = 0) in vec3 VertexPosition;
layout (location = 1) in vec3 VertexColor;
uniform mat4 MVP;
out vec3 Color;
void main(){
Color = VertexColor;
gl_Position = MVP*vec4(VertexPosition,1.0);
}
#version 400
in vec3 Color;
out vec4 FragColor;
void main(){
FragColor = vec4(Color,1.0);
}
VBO 和 VAO 初始化:
void InitVBO(GLuint programHandler){
glBindAttribLocation(programHandler,0,"VertexPosition");
glBindAttribLocation(programHandler,1,"VertexColor");
GLfloat positionData[] = {-0.5f,0.5f,0.0f,0.0f,-.5f,0.0f,0,0,0};
GLfloat colorData[] = {1.0f,0.0f,0.0f,0.0f,1.0f,0.0f,0.0f,0.0f,1.0f};
GLuint vboHandles[2];
glGenBuffers(2,vboHandles);
glBindBuffer(GL_ARRAY_BUFFER,vboHandles[0]);
glBufferData(GL_ARRAY_BUFFER,9*sizeof(GLfloat),positionData,GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER,vboHandles[1]);
glBufferData(GL_ARRAY_BUFFER,9*sizeof(GLfloat),colorData,GL_STATIC_DRAW);
glGenVertexArrays(1, &vaoHandle);
glBindVertexArray(vaoHandle);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, vboHandles[0]);
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0,(GLubyte *)NULL );
glBindBuffer(GL_ARRAY_BUFFER, vboHandles[1]);
glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 0,(GLubyte *)NULL );
}