简体   繁体   中英

OpenGL using shaders on textures

I have two images, and with the help of the instruction here: http://en.wikibooks.org/wiki/OpenGL_Programming/Intermediate/Textures

I was able store them separately, into two separate textures, and upload them into video memory:

 gluBuild2DMipmaps(GL_TEXTURE_2D, 4, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);

Now, how would I access these textures with shaders to multiply these two textures?

For example, I found this example, about multiplication using shaders: http://www.opengl.org/wiki/Texture_Combiners

//Vertex shader
 #version 110
 attribute vec4 InVertex;
 attribute vec2 InTexCoord0;
 attribute vec2 InTexCoord1;
 uniform mat4 ProjectionModelviewMatrix;
 varying vec2 TexCoord0;
 varying vec2 TexCoord1;    //Or just use TexCoord0
 //------------------------
 void main()
 {
   gl_Position = ProjectionModelviewMatrix * InVertex;
   TexCoord0 = InTexCoord0;
   TexCoord1 = InTexCoord1;
 }
 //------------------------
 //Fragment shader
 #version 110
 uniform sampler2D Texture0;
 uniform sampler2D Texture1;
 //------------------------
 varying vec2 TexCoord0;
 varying vec2 TexCoord1;    //Or just use TexCoord0
 //------------------------
 void main()
 {
    vec4 texel = texture2D(Texture0, TexCoord0);
    texel *= texture2D(Texture1, TexCoord1);
    gl_FragColor = texel;
 }

But how would I make the textures that I've uploaded in a form of Vertex, so that I can use this Fragment shaders to accomplish this multiplication.

All I did was generated gluBuild2DMipmaps, but now I don't know how to apply Vertex/Fragment shaders to my texture?

Assume you have a quad where the first three values are vertex coord. and the last two your TexCoord.

   -1.0f,-1.0f, 1.0f,   0.0f, 0.0f,
    1.0f,-1.0f, 1.0f,   1.0f, 0.0f,
   -1.0f, 1.0f, 1.0f,   0.0f, 1.0f,
    1.0f,-1.0f, 1.0f,   1.0f, 0.0f,
    1.0f, 1.0f, 1.0f,   1.0f, 1.0f,
   -1.0f, 1.0f, 1.0f,   0.0f, 1.0f,

you have to submit your Hardware different uniforms and attributes:

first of all the (after MVP and so on.) the vertex and textcoord:

glEnableVertexAttribArray(VAA_Normal);
glVertexAttribPointer(VAA_Normal, 3, GL_FLOAT, GL_TRUE,  5*sizeof(GLfloat), (const GLvoid*)(5 * sizeof(GLfloat)));

glEnableVertexAttribArray(VAA_TexCoord);
glVertexAttribPointer(VAA_TexCoord, 2, GL_FLOAT, GL_TRUE,  5*sizeof(GLfloat), (const GLvoid*)(3 * sizeof(GLfloat)));

(VAA_Normal = glGetAttribLocation(aProgram, attribName);)

last but not least the important Texture:

   glActiveTexture(GL_TEXTURE0);
   glBindTexture(GL_TEXTURE_2D, aTexture);

dont forget: its up to you how you combine different textures

edit: sorry forgot

  glUniform1i(glGetUniformLocation(aProgramID, "TEXTURE0"), 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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