1
votes

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?

1
Did you change the texture unit (using the glActiveTexture call) anywhere in your application? A texture unit can only hold a single texture (and a mipmap stack is considered a single texture), and so to use two textures simultaneously, you need to change the texture unit before uploading. - radical7
@radical7: No I haven't, but considering if I do it, still how do I make my Texture be treated as Vertex? - user2112840
@user2112840: Textures as Vertex? Seriously, you should get your terminology and OpenGL basics solid. What you must pass is the texture unit used as uniform. I strongly recommend you work through some good tutorials first, like the one of Nicol Bolas: arcsynthesis.org/gltut - datenwolf

1 Answers

0
votes

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