OpenGL - 无法一次将所有数据传递到着色器中

我正在尝试使用 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;


}



HUWWW
浏览 144回答 2
2回答

叮当猫咪

你只有单身 sampler2D这意味着您只有一个纹理可供您使用不管你绑定了多少。如果您确实需要将数据作为单个块传递那么你应该为你得到的每个纹理添加采样器不确定你有多少对象/纹理但是通过这种数据传递方式,您受到纹理单元的 gfx hw 限制您还需要在数据中添加另一个值,告诉哪个图元使用哪个纹理单元然后在片段内部选择正确的纹理采样器......你应该添加这样的东西:// vertexin int usedtexture;out int txr;void main() { txr=usedtexture; }// fragmentuniform sampler2D myTextureSampler0;uniform sampler2D myTextureSampler1;uniform sampler2D myTextureSampler2;uniform sampler2D myTextureSampler3;in vec2 UV;in int txr;out vec4 color;void main {      if (txr==0) color = texture2D( myTextureSampler0, UV ).rgba; else if (txr==1) color = texture2D( myTextureSampler1, UV ).rgba; else if (txr==2) color = texture2D( myTextureSampler2, UV ).rgba; else if (txr==3) color = texture2D( myTextureSampler3, UV ).rgba; else color=vec4(0.0,0.0,0.0,0.0); }由于以下原因,这种传递方式并不好:使用的纹理数量仅限于硬件纹理单元限制如果您的渲染需要额外的纹理,如法线/光泽度/光照贴图那么每个对象类型需要超过 1 个纹理,并且您的限制突然除以 2,3,4 ...您需要if/switch片段内的语句,这会大大减慢速度是的,您可以少吃早午餐,但是您需要一直访问所有纹理,从而无缘无故地增加 gfx 的热应力...这种传球适合单个图像中的所有纹理(正如你提到的纹理图集)对于具有少量对象类型(或材料)但对象数量较多的场景,这种方式可以更快并且合理......
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java