Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7026903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:10:43+00:00 2026-05-28T00:10:43+00:00

I try to access 2 textures in 1 fragment shader. For this I have

  • 0

I try to access 2 textures in 1 fragment shader.

For this I have 2 textures that I set up with following code:

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glVertexPointer(2, GL_FLOAT, 0, vbuff);
glTexCoordPointer(2, GL_FLOAT, 0, tbuff);

glActiveTextureARB(GL_TEXTURE0_ARB);
glBindTexture(GL_TEXTURE_2D, texture0);
glEnable(GL_TEXTURE_2D);

glActiveTextureARB(GL_TEXTURE1_ARB);
glBindTexture(GL_TEXTURE_2D, texture1); 
glEnable(GL_TEXTURE_2D);

And I try to combine them with following fragment shader:

uniform sampler2D tex0;
uniform sampler2D tex1;
void main()
{
    vec4 texval1 = texture2D(tex0, TextureCoordOut);
    vec4 texval2 = texture2D(tex1, TextureCoordOut);
    gl_FragColor = 0.5*(texval1 + texval2)
}

Somehow this doesn’t work and it only displays the second texture.

PS: I am developing the shaders on Windows7 but want to use the C++ code and the shaders with the Android NDK on an Android Device.

EDIT: I got it working and I post the code here, so that others might benefit from it:

vertex shader

varying vec2 TextureCoordOut;
void main()
{
    gl_Position = ftransform();
    TextureCoordOut = vec2(gl_MultiTexCoord0);
}

fragment shader

varying mediump vec2 TextureCoordOut;
uniform sampler2D tex0;
uniform sampler2D tex1;
void main()
{
    gl_FragColor = texture2D(tex0, TextureCoordOut) * texture2D(tex1, TextureCoordOut);
}

render

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glVertexPointer(2, GL_FLOAT, 0, vbuff);  //2 dimensional vertex buffer
glTexCoordPointer(2, GL_FLOAT, 0, tbuff); // 2 dimensional textures
glColor4f(1, 1, 1, 1);

glActiveTexture(GL_TEXTURE0);       glBindTexture(GL_TEXTURE_2D, mapid1);
glActiveTexture(GL_TEXTURE1);       glBindTexture(GL_TEXTURE_2D, mapid2);

glUseProgram(shader);

glUniform1i(glGetUniformLocation(gfx.prog, "tex0"), 0);   //GL_TEXTURE0
glUniform1i(glGetUniformLocation(gfx.prog, "tex1"), 1);   //GL_TEXTURE1

render....

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:10:43+00:00Added an answer on May 28, 2026 at 12:10 am

    Just binding these textures to the respective units is not enough. You also need to tell the shaders where to take the textures for it’s sampler2D uniforms from. This is done by setting the corresponding integer uniform value to the texture unit you want to use:

    //get uniform locations (either init code or render code)
    int locTex0 = glGetUniformLocation(program, "tex0");
    int locTex1 = glGetUniformLocation(program, "tex1");
    
    ...    
    
    //bind textures to texture units
    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, texture1);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture0);
    
    //bind program and specify texture units for each sampler2D uniform
    glUseProgram(program);
    glUniform1i(locTex0, 0);   //use texture bound to GL_TEXTURE0
    glUniform1i(locTex1, 1);   //use texture bound to GL_TEXTURE1
    
    ...
    draw
    

    Of course this assumes that you know how to use shaders and specify uniform values.

    Some additional remarks:

    • When using a fragment shader you don’t need to call glEnable(GL_TEXTURE_2D), since the shader decides itself if it uses a texture or not.
    • Don’t use those ARB versions of functions and constants, at least not for such old and established things like multitexturing, which is core since GL 1.2 or 1.3 (~15 years).
    • Don’t expect your code to be 1-to-1 portable to OpenGL ES 2.0, which doesn’t know about the old builtin attributes (like glVertexPointer or glTexCoordPointer), but that is a different question.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to access information from Twitter and I followed this link: http://code.google.com/p/sociallib/wiki/SocialLibGuide I
I try to access session variables stored on a different page. When I have
Every time I try to access my localhost/phpmyadmin it gives me this error --->
When I try to access info that is not presented in xml like so:
We know that if we try to access a nonexistent key of std::map with
If I try to access this system variable from the Run... dialog, Windows tells
VS 2008 / C# / MS Access 2003 / 2007 I have attached Code
I have try to access the map in jsf using C:foreach.But i can't access
I try to access a specific registry key that i know exist. I use
Why when I try to access a variable that don't exist, javascript throw an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.