Home > other >  Texture sample when the sample value setting of the problem
Texture sample when the sample value setting of the problem

Time:12-15

In view of the little red book chapter 6 texture in the source code, the init function is as follows:
Void Init ()
{
ShaderInfo si []={{GL_VERTEX_SHADER, "TextureView. Vert"}, {GL_FRAGMENT_SHADER, "TextureView. Frag"}, {GL_NONE, NULL}};
The Program=LoadShaders (si);

GlGenVertexArrays (1, & amp; Vert);
GlBindVertexArray (vert);

GlGenBuffers (1, & amp; Vbo);
GlBindBuffer (GL_ARRAY_BUFFER vbo);
GlBufferData (GL_ARRAY_BUFFER, sizeof (Verts), Verts, GL_STATIC_DRAW);
GlVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, sizeof (Vertex), BUFFER_OFFSET (0));
GlEnableVertexAttribArray (0);
GlVertexAttribPointer (1, 2, GL_FLOAT, GL_FALSE, sizeof (Vertex), BUFFER_OFFSET (sizeof (Verts - & gt; The position)));
GlEnableVertexAttribArray (1);
GlBindVertexArray (0);


Float texData []={1.0, 0.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0,
1.0, 1.0, 0.0, 1.0};
GlGenTextures (1, & amp; Tex);
GlActiveTexture (GL_TEXTURE0);
GlBindTexture (GL_TEXTURE_2D, Tex);
GlTexStorage2D (GL_TEXTURE_2D. 2, GL_RGBA8, 2, 2);
GlTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, 2, 2, GL_RGBA, GL_FLOAT, texData);
GlTexParameteri (GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER, GL_NEAREST);
GlTexParameteri (GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER, GL_NEAREST);

VglImageData image;
TexCube=vglLoadTexture ("../../media/test. The DDS, "0, & amp; Image);
E=glGetError ();

GlGenTextures (6, texPanel);
GlTextureView (texPanel [0], GL_TEXTURE_2D texCube, image. InternalFormat, 0, the image. The mipLevels, 0, 1);
E=glGetError ();
GlActiveTexture (GL_TEXTURE1);
GlBindTexture (GL_TEXTURE_2D, texPanel [0]);
GlTexParameteri (GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER, GL_NEAREST);
GlTexParameteri (GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER, GL_NEAREST);
VglUnloadImage (& amp; Image);

GlClearColor (0.2 f, f, 0.1 0.3 f, 1.0 f);
GlUseProgram (Program);
Sleep (30);
}

The display function is as follows:
Void the Display ()
{
GlClear (GL_COLOR_BUFFER_BIT);
GlBindVertexArray (vert);
Vmath: : mat4 ModelView=vmath: : mat4: : identity ();
for (int i=0; i<1; + + I)
{
For (int j=0; J & lt; 1; J++)
{
The ModelView=vmath: : translate (0.63 to 0.63 f * I, f * j, 0.0 f);
GlUniformMatrix4fv (1, 1, GL_FALSE, ModelView);
//glActiveTexture (GL_TEXTURE0);
//glBindTexture (GL_TEXTURE_2D, Tex);
GlUniform1i (glGetUniformLocation (Program, "Tex"), 0).
GlDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
}
}
GlutSwapBuffers ();
GlutPostRedisplay ();
}
In the fragment shader code is very simple, is:
# version 430 core

In vec2 TexCoord;

Layout (location=0) uniform sampler2D Tex;

The out vec4 fColor;

Void main ()
{
FColor=texture (Tex, TexCoord);
}
In fact, in the display function, no matter I will sampler2D sampler Tex through glUniform1i is set to 0, 1, or display is binding on GL_TEXTURE1 texPanel [0] images, only in the display again will Tex is bound to the GL_TEXTURE0 (two lines to write will be commented out), can correctly according to the value of glUniform1i samples, is this why?
  • Related