简体   繁体   English

没有纹理图像单位的Opengl纹理

[英]Opengl Textures without Texture image Units

recently i found an Opengl example that applies a single texture without specifying texture image unit and without sending the corresponding unit integer uniform into the shader, is it possible to apply textures without using texture units?? 最近我发现一个Opengl示例,该示例应用a single纹理而不指定纹理图像单位,并且不将相应的单位整数均匀地发送到着色器中,是否可以在不使用纹理单位的情况下应用纹理? or it simply uses a default value for both active texture unit and its shader sampler value. 或者它仅对活动纹理单位及其着色器采样器值使用默认值。

my code block (texture related): 我的代码块(与纹理有关):

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

GLuint TextureID = 0;


SDL_Surface* Surface = IMG_Load("Abrams_Tank.jpg");

glGenTextures(1, &TextureID);
glBindTexture(GL_TEXTURE_2D, TextureID);

int Mode = GL_RGB;

if(Surface->format->BytesPerPixel == 4) {
    Mode = GL_RGBA;
}

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Surface->w, Surface->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, Surface->pixels);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);






GLuint vbo;
GLuint vbo_Text;
GLuint vao;



glGenBuffers(1, &vbo);
glGenBuffers(1, &vbo_Text);
glGenVertexArrays(1, &vao);






glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);

glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData) * 30,vertexData, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)(sizeof(float) * 18));
glDrawArrays(GL_TRIANGLES, 0,6);


glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glBindBuffer(0, vbo);

my fragment shader: 我的片段着色器:

#version 410

in vec2 texCoordout;

uniform sampler2D mysampler;

out vec4 Fcolor;

void main()
{
Fcolor = texture (mysampler, texCoordout);
}

This will work, and is within specs: 这将起作用,并且在规格范围内:

  • The default texture unit is GL_TEXTURE0 , so there is no need to ever call glActiveTexture() if only texture unit 0 is used. 默认纹理单位为GL_TEXTURE0 ,因此,如果仅使用纹理单位0,则无需调用glActiveTexture()
  • Uniforms have a default value of 0, which is the correct value for a sampler that references texture unit 0. 均匀度的默认值为0,这是引用纹理单元0的采样器的正确值。

I would still always set the uniform values just to make it explicit. 我仍将始终设置统一值以使其明确。 If you don't set the uniform in this case, it is also much more likely that you will miss to set it if you ever use a texture unit other than 0. 如果在这种情况下不设置均匀性,则如果使用非0的纹理单位,很可能会错过设置均匀性的可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM