我正在尝试使用 opengl 3.3 在四边形(2 个三角形)上显示纹理
在四边形上绘制纹理效果很好;但是,当我有一个纹理(精灵图集)但使用 2 个四边形(对象)来显示图集的不同部分时。在绘制循环中,它们最终会切换回第四个(一个消失然后再次出现,等等)在它们各自的翻译位置。
我绘制它的方式不是每个四边形(或对象)的标准 DrawElements,而是我打包所有四边形、uv、平移等将它们作为一个大块(作为“in”变量)发送到着色器:顶点着色器:
#version 330 core
// Input vertex data, different for all executions of this shader.
in vec3 vertexPosition_modelspace;
in vec3 vertexColor;
in vec2 vertexUV;
in vec3 translation;
in vec4 rotation;
in vec3 scale;
// Output data ; will be interpolated for each fragment.
out vec2 UV;
// Output data ; will be interpolated for each fragment.
out vec3 fragmentColor;
// Values that stay constant for the whole mesh.
uniform mat4 MVP;
...
void main(){
mat4 Model = mat4(1.0);
mat4 t = translationMatrix(translation);
mat4 s = scaleMatrix(scale);
mat4 r = rotationMatrix(vec3(rotation), rotation[3]);
Model *= t * r * s;
gl_Position = MVP * Model * vec4 (vertexPosition_modelspace,1); //* MVP;
// The color of each vertex will be interpolated
// to produce the color of each fragment
fragmentColor = vertexColor;
// UV of the vertex. No special space for this one.
UV = vertexUV;
}
顶点着色器是否像我认为的那样在处理大量数据时工作 - 它单独绘制每个段作为统一传递,因为它看起来不像?我的思路是否正确?
为了完整起见,这是我的片段着色器:
#version 330 core
// Interpolated values from the vertex shaders
in vec3 fragmentColor;
// Interpolated values from the vertex shaders
in vec2 UV;
// Ouput data
out vec4 color;
// Values that stay constant for the whole mesh.
uniform sampler2D myTextureSampler;
void main()
{
// Output color = color of the texture at the specified UV
color = texture2D( myTextureSampler, UV ).rgba;
}
叮当猫咪
相关分类