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 7248729
In Process

The Archive Base Latest Questions

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

i’m trying to bind 2 textures for my shader. But for some reason it

  • 0

i’m trying to bind 2 textures for my shader. But for some reason it always seems to take the last image that i defined. Am i doing something wrong?

GLuint textures[2];

glEnable(GL_TEXTURE_2D);

glGenTextures(2, textures);
glBindTexture(GL_TEXTURE_2D, textures[0]);
glfwLoadTexture2D("C:\\front.tga", GLFW_BUILD_MIPMAPS_BIT);

glBindTexture(GL_TEXTURE_2D, textures[1]);
glfwLoadTexture2D("C:\\reflect.tga", GLFW_BUILD_MIPMAPS_BIT);

In this case i see ‘reflect.tga’ for both the reflection and refraction in my shader…

const vec3 Xunitvec = vec3 (1.0, 0.0, 0.0);
const vec3 Yunitvec = vec3 (0.0, 1.0, 0.0);

uniform vec3  BaseColor;
uniform float Depth;
uniform float MixRatio;

// need to scale our framebuffer - it has a fixed width/height of 2048
uniform float FrameWidth;
uniform float FrameHeight;

uniform sampler2D EnvMap;
uniform sampler2D RefractionMap;

varying vec3  Normal;
varying vec3  EyeDir;
varying vec4  EyePos;
varying float LightIntensity;

void main (void)
{
    // Compute reflection vector
    vec3 reflectDir = reflect(EyeDir, Normal);

    // Compute altitude and azimuth angles

    vec2 index;

    index.y = dot(normalize(reflectDir), Yunitvec);
    reflectDir.y = 0.0;
    index.x = dot(normalize(reflectDir), Xunitvec) * 0.5;

    // Translate index values into proper range

    if (reflectDir.z >= 0.0)
        index = (index + 1.0) * 0.5;
    else
    {
        index.t = (index.t + 1.0) * 0.5;
        index.s = (-index.s) * 0.5 + 1.0;
    }

    // if reflectDir.z >= 0.0, s will go from 0.25 to 0.75
    // if reflectDir.z <  0.0, s will go from 0.75 to 1.25, and
    // that's OK, because we've set the texture to wrap.

    // Do a lookup into the environment map.

    vec3 envColor = vec3 (texture2D(EnvMap, index));

    // calc fresnels term.  This allows a view dependant blend of reflection/refraction
    float fresnel = abs(dot(normalize(EyeDir), Normal));
    fresnel *= MixRatio;
    fresnel = clamp(fresnel, 0.1, 0.9);

   // calc refraction
   vec3 refractionDir = normalize(EyeDir) - normalize(Normal);

   // Scale the refraction so the z element is equal to depth
   float depthVal = Depth / -refractionDir.z;

   // perform the div by w
   float recipW = 1.0 / EyePos.w;
   vec2 eye = EyePos.xy * vec2(recipW);

   // calc the refraction lookup
   index.s = (eye.x + refractionDir.x * depthVal);
   index.t = (eye.y + refractionDir.y * depthVal);

   // scale and shift so we're in the range 0-1
   index.s = index.s / 2.0 + 0.5;
   index.t = index.t / 2.0 + 0.5;

   // as we're looking at the framebuffer, we want it clamping at the edge of the rendered scene, not the edge of the texture,
   // so we clamp before scaling to fit
   float recip1k = 1.0 / 2048.0;
   index.s = clamp(index.s, 0.0, 1.0 - recip1k);
   index.t = clamp(index.t, 0.0, 1.0 - recip1k);

   // scale the texture so we just see the rendered framebuffer
   index.s = index.s * FrameWidth * recip1k;
   index.t = index.t * FrameHeight * recip1k;

    vec3 RefractionColor = vec3 (texture2D(RefractionMap, index));

    // Add lighting to base color and mix
    vec3 base = LightIntensity * BaseColor;
    envColor = mix(envColor, RefractionColor, fresnel);
    envColor = mix(envColor, base, 0.2);

    gl_FragColor = vec4 (envColor, 1.0);
}
  • 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-28T22:10:43+00:00Added an answer on May 28, 2026 at 10:10 pm

    A sampler uniform does not bind a texture object but a texture unit. And texture objects are bound to texture units. So the sequence for binding a texture to a shader is

    glActiveTexture(GL_TEXTURE0 + texture_unit1);
    glBindTexture(GL_TEXTURE_..., texture_object1);
    
    glActiveTexture(GL_TEXTURE0 + texture_unit2);
    glBindTexture(GL_TEXTURE_..., texture_object2);
    
    
    glUniform1i(sampler1_location, texture_unit1);
    glUniform1i(sampler2_location, texture_unit2);
    

    Texture Units are in the range 0…GL_MAX_TEXTURE_UNITS.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.