What does sampler2D store?

蹲街弑〆低调 提交于 2019-12-03 05:40:03

问题


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()?


回答1:


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).



来源:https://stackoverflow.com/questions/10868958/what-does-sampler2d-store

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!