OpenGL - 为什么我的 Z 坐标是 2 倍比例?
OpenGL - Why are my Z coords 2x scale?
这是我的立方体?我已将所有顶点坐标设置为 -1 或 +1,因此每条边的所有距离都应为 2.0。但是,如果我将 Z 坐标值减半,它只会显示为立方体。
我错过了 MVP 代码中的错误吗?
glEnableVertexAttribArray( 0 );
glEnableVertexAttribArray( 1 );
glBindBuffer( GL_ARRAY_BUFFER, m_VBO );
// First attributes are the x,y,z coords..
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, CVertex3DC::m_Stride, 0 );
// Second attribute will be the RGBA
glVertexAttribPointer( 1, 4, GL_FLOAT, GL_FALSE, CVertex3DC::m_Stride, (void *) g_XYZFloatLen );
// PROJECTION
glm::mat4 Projection = glm::perspective( 45.0f, g_AspectRatio, 0.1f, 100.0f );
// VIEW
glm::mat4 View = glm::mat4(1.0);
dist = -5.0f;
View = glm::translate( View, glm::vec3( 0.0f, 0.0f, dist ) );
// MODEL
glm::mat4 Model = glm::mat4(1.0);
rot = 0;
Model = glm::rotate( Model, rot, glm::vec3( 0.0f, 1.0f, 1.0f ) ); // where x, y, z is axis of rotation (e.g. 0 1 0)
glm::mat4 MVP;
MVP = Projection * View * Model;
GLuint transformLoc;
transformLoc = glGetUniformLocation( g_ShaderProgram, "transform" );
glUniformMatrix4fv( transformLoc, 1, GL_FALSE, glm::value_ptr( MVP ) );
int nOffset = 0;
// Draw each face..
for( int f = 0; f < (int) m_Faces.size(); f ++ )
{
nOffset = m_Faces[ f ].Render( nOffset );
}
glDisableVertexAttribArray( 0 );
glDisableVertexAttribArray( 1 );
更新:这是顶点着色器 - 我目前没有使用 'normal'。
#version 330
layout (location = 0) in vec3 Position;
layout (location = 1) in vec4 color;
layout (location = 2) in vec3 normal;
out vec4 frag_color;
uniform mat4 transform;
void main()
{
gl_Position = transform * vec4(0.5 * Position.x, 0.5 * Position.y, Position.z, 1.0);
// Pass vertex color to fragment shader..
frag_color = color;
}
变换矩阵只说对了一半。在现代 GL 中,顶点着色器(或更准确地说:光栅化器之前的所有可编程阶段)负责将对象 space 坐标转换为剪辑 space.
所以着色器可能会使用矩阵,或者它可能完全忽略它们,或者它可能应用额外的转换。在您的情况下,VS 在应用变换矩阵之前将 x
和 y
缩放 0.5,因此您的对象在 z
.
中出现拉伸
请注意,顶点着色器甚至可能根本不使用输入属性。它可以完全无属性地工作,通过单独生成一些由 gl_VertexID
参数化的表面。因此,当只知道顶点数组中的数据和所有 uniform 的状态时,仍然不知道渲染后的外观如何(虽然在大多数情况下,数据和 uniform 会被使用,否则会很浪费指定它们的资源)。
这是我的立方体?我已将所有顶点坐标设置为 -1 或 +1,因此每条边的所有距离都应为 2.0。但是,如果我将 Z 坐标值减半,它只会显示为立方体。
我错过了 MVP 代码中的错误吗?
glEnableVertexAttribArray( 0 );
glEnableVertexAttribArray( 1 );
glBindBuffer( GL_ARRAY_BUFFER, m_VBO );
// First attributes are the x,y,z coords..
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, CVertex3DC::m_Stride, 0 );
// Second attribute will be the RGBA
glVertexAttribPointer( 1, 4, GL_FLOAT, GL_FALSE, CVertex3DC::m_Stride, (void *) g_XYZFloatLen );
// PROJECTION
glm::mat4 Projection = glm::perspective( 45.0f, g_AspectRatio, 0.1f, 100.0f );
// VIEW
glm::mat4 View = glm::mat4(1.0);
dist = -5.0f;
View = glm::translate( View, glm::vec3( 0.0f, 0.0f, dist ) );
// MODEL
glm::mat4 Model = glm::mat4(1.0);
rot = 0;
Model = glm::rotate( Model, rot, glm::vec3( 0.0f, 1.0f, 1.0f ) ); // where x, y, z is axis of rotation (e.g. 0 1 0)
glm::mat4 MVP;
MVP = Projection * View * Model;
GLuint transformLoc;
transformLoc = glGetUniformLocation( g_ShaderProgram, "transform" );
glUniformMatrix4fv( transformLoc, 1, GL_FALSE, glm::value_ptr( MVP ) );
int nOffset = 0;
// Draw each face..
for( int f = 0; f < (int) m_Faces.size(); f ++ )
{
nOffset = m_Faces[ f ].Render( nOffset );
}
glDisableVertexAttribArray( 0 );
glDisableVertexAttribArray( 1 );
更新:这是顶点着色器 - 我目前没有使用 'normal'。
#version 330
layout (location = 0) in vec3 Position;
layout (location = 1) in vec4 color;
layout (location = 2) in vec3 normal;
out vec4 frag_color;
uniform mat4 transform;
void main()
{
gl_Position = transform * vec4(0.5 * Position.x, 0.5 * Position.y, Position.z, 1.0);
// Pass vertex color to fragment shader..
frag_color = color;
}
变换矩阵只说对了一半。在现代 GL 中,顶点着色器(或更准确地说:光栅化器之前的所有可编程阶段)负责将对象 space 坐标转换为剪辑 space.
所以着色器可能会使用矩阵,或者它可能完全忽略它们,或者它可能应用额外的转换。在您的情况下,VS 在应用变换矩阵之前将 x
和 y
缩放 0.5,因此您的对象在 z
.
请注意,顶点着色器甚至可能根本不使用输入属性。它可以完全无属性地工作,通过单独生成一些由 gl_VertexID
参数化的表面。因此,当只知道顶点数组中的数据和所有 uniform 的状态时,仍然不知道渲染后的外观如何(虽然在大多数情况下,数据和 uniform 会被使用,否则会很浪费指定它们的资源)。