Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does sampler2D store?

Tags:

opengl

glsl

I've read a texture example in OpenGL 2.1. The fragment shader looks like this:

#version 120  uniform sampler2D texture; varying vec2 texcoord;  void main(void) {     gl_FragColor = texture2D(texture, texcoord); } 

The texcoord is passed from vertex shader.

The following C++ rendering code is used:

void render() {     glActiveTexture(GL_TEXTURE0);     glBindTexture(GL_TEXTURE_2D, texture_id);     glUniform1i(unf_texture, 0); } 

I am confused about some things. I have some question:

  1. In the fragment shader, the texture is passed a zero value (by glUniform1i()). Is the value really zero? Is the value something else?

  2. Is glActiveTexture() call really need?

  3. Why do we pass a zero value in glUniform1i()?

like image 985
Bình Nguyên Avatar asked Jun 03 '12 08:06

Bình Nguyên


People also ask

What is a sampler2D?

A sampler2D is used to do lookup in a standard texture image; a samplerCube is used to do lookup in a cubemap texture (Subsection 5.3. 4). The value of a sampler variable is a reference to a texture unit. The value tells which texture unit is invoked when the sampler variable is used to do texture lookup.

What is sampler2D in Webgl?

sampler2D is just a way to get data from an array. Don't think of it as a texture. Think of it has a 2D array with special hardware to do interpolation between values in the array (LINEAR filtering) and values between mips. Set the sampling to NEAREST and they just become 2D arrays.

What is GL position?

gl_Position is the predefined variable which is available only in the vertex shader program. It contains the vertex position. In the above code, the coordinates attribute is passed in the form of a vector. As vertex shader is a per-vertex operation, the gl_position value is calculated for each vertex.

What is Gl_texcoord?

Description. glTexCoord specifies texture coordinates in one, two, three, or four dimensions. glTexCoord1 sets the current texture coordinates to s 0 0 1 ; a call to glTexCoord2 sets them to s t 0 1 .


1 Answers

The sampler2D is bound to a texture unit. The glUniform call binds it to texture unit zero. The glActiveTexture() call is only needed if you are going to use multiple texture units (because GL_TEXTURE0 is the default anyway).

like image 180
Ville Krumlinde Avatar answered Sep 23 '22 07:09

Ville Krumlinde