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

  • Home
  • SEARCH
  • 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 7086695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:34:55+00:00 2026-05-28T07:34:55+00:00

My program has been working perfectly so far, but it turns out that I’ve

  • 0

My program has been working perfectly so far, but it turns out that I’ve been lucky. I began doing some cleanup of the shader, because it was full of experimental stuff, and I had the following line at the end of the fragment shader:

gl_FragColor = final_color * (texture2D(tex, gl_TexCoord[0].st)*1.0 + texture2D(tex2, gl_TexCoord[0].st)*1.0);

I attempted to clean it up and I had the following declared at the top:

uniform sampler2D tex, tex2;

Changing these lines to:

gl_FragColor = final_color * texture2D(tex, gl_TexCoord[0].st;

and

uniform sampler2D tex;

actually broke the program (black screen), even though I am doing

GLuint tex_loc = glGetUniformLocation(shader_prog_id_, "tex");
glUniform1i(tex_loc, texture_id_);

in my main code. I’m sure it’s a texture issue and not a glsl compiler error, because I can add 1.0 to the output and end up with a white silhouette of my mesh.

The strangeness begins when I change the lines in my shader to:

gl_FragColor = final_color * texture2D(tex2, gl_TexCoord[0].st;

and

uniform sampler2D tex2;

but still retrieve the location for tex. The program works as it always has, even though inspecting the value of tex_loc in the debugger indicates an error. I’m not happy doing this, and now that I’m trying to load multiple textures, it will cause bigger headaches down the line.

I’m using VBOs, in interleaved format, to render the geometry. I’m passing in the vertex position, normal and texcoord this way.

There are other questions with the “black texture” issue, but they’re using immediate mode calls and setting the wrong texture state. I tried changing the texture unit before supplying the arrays, with no success.

Here is as much relevant code as possible from the main program:

void MeshWidget::draw() {

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -4.0f + zoom_factor_);
    glRotatef(rotX, 1.0f, 0.0f, 0.0f);
    glRotatef(rotY, 0.0f, 1.0f, 0.0f);
    glRotatef(rotZ, 0.0f, 0.0f, 1.0f);

    // Auto centre mesh based on vertex bounds.
    glTranslatef(-x_mid_, -y_mid_, -z_mid_);

    glDrawElements(GL_TRIANGLES, mesh_.num_indices, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));   //The starting point of the IBO
}


void MeshWidget::openMesh(const string& filename) {

    if (mesh_filename_ != filename) {

        clearMeshData(mesh_);

        glDeleteBuffersARB(1, &VertexVBOID);
        glDeleteBuffersARB(1, &IndexVBOID);

        ReadMsh(mesh_, filename);

        // Create buffer objects here.
        glGenBuffersARB(1, &VertexVBOID);
        glBindBufferARB(GL_ARRAY_BUFFER, VertexVBOID);
        glBufferDataARB(GL_ARRAY_BUFFER, sizeof(VertexAttributes)*mesh_.num_vertices, &mesh_.vertices[0], GL_STATIC_DRAW);

        glGenBuffersARB(1, &IndexVBOID);
        glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
        glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER, sizeof(uint16_t)*mesh_.num_indices, &mesh_.indices[0], GL_STATIC_DRAW);

        glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
        glEnableClientState(GL_VERTEX_ARRAY);
        glVertexPointer(3, GL_FLOAT, sizeof(VertexAttributes), BUFFER_OFFSET(0));   //The starting point of the VBO, for the vertices
        glEnableClientState(GL_NORMAL_ARRAY);
        glNormalPointer(GL_FLOAT, sizeof(VertexAttributes), BUFFER_OFFSET(12));   //The starting point of normals, 12 bytes away
        glClientActiveTexture(GL_TEXTURE0);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        glTexCoordPointer(2, GL_FLOAT, sizeof(VertexAttributes), BUFFER_OFFSET(24));   //The starting point of texcoords, 24 bytes away

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);

        if (startup_done_) updateGL();
    }
}

void MeshWidget::openTexture(const string& filename) {

    size_t dot = filename.find_last_of('.');
    string ext(filename, dot, filename.size()); // 3rd parameter should be length of new string, but is internally clipped to end.

    glActiveTexture(GL_TEXTURE0);
    glClientActiveTexture(GL_TEXTURE0);
    glDeleteTextures(1, &texture_id_);
    glGenTextures(1, &texture_id_);
    glBindTexture(GL_TEXTURE_2D, texture_id_);

    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    if (ext == ".dds") {
        if (GLEE_EXT_texture_compression_s3tc) {

            texture_id_ = SOIL_load_OGL_texture(filename.c_str(), SOIL_LOAD_AUTO, texture_id_, SOIL_FLAG_DDS_LOAD_DIRECT);
            // SOIL takes care of calling glTexParams, glTexImage2D, etc.
            yflip_texture_ = true;

            } else {
                //std::cout << "S3TC not supported on this graphics hardware." << std::endl;
                // TODO: Error message in status bar?
            }

    } else {

        QImage tex(filename.c_str());
        tex = QGLWidget::convertToGLFormat(tex);

        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.width(), tex.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.bits());

        yflip_texture_ = false;

    }

    updateUniforms();

    if (startup_done_) updateGL();
}

void MeshWidget::updateUniforms() {

    GLuint texture_flip_uniform = glGetUniformLocation(shader_prog_id_, "yflip");
    glUniform1f(texture_flip_uniform, float(yflip_texture_ * 1.0f));

    GLuint tex_loc = glGetUniformLocation(shader_prog_id_, "tex");
    glUniform1i(tex_loc, texture_id_);
}

And my shaders (there’s still some junk in here because I was experimenting, but nothing that affects the output):

varying vec3 normal, lightDir, eyeVec;
uniform float yflip;

void main()
{   
    normal = gl_NormalMatrix * gl_Normal;

    vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex);

    lightDir = vec3(gl_LightSource[0].position.xyz - vVertex);
    eyeVec = -vVertex;

    gl_TexCoord[0].x = gl_MultiTexCoord0.x;
    gl_TexCoord[1].x = gl_MultiTexCoord1.x;

    if (yflip == 1.0) {
        gl_TexCoord[0].y = 1 - gl_MultiTexCoord0.y;
        gl_TexCoord[1].y = 1 - gl_MultiTexCoord1.y;
    } else {
        gl_TexCoord[0].y = gl_MultiTexCoord0.y;
        gl_TexCoord[1].y = gl_MultiTexCoord1.y;
    }

    gl_Position = ftransform();
}

fragment shader:

varying vec3 normal, lightDir, eyeVec;
uniform sampler2D tex2;

void main (void)
{

    vec4 texel = texture2D(tex2, gl_TexCoord[0].st);

    vec4 final_color =
(gl_FrontLightModelProduct.sceneColor * gl_FrontMaterial.ambient) + 
(gl_LightSource[0].ambient * gl_FrontMaterial.ambient);

    vec3 N = normalize(normal);
    vec3 L = normalize(lightDir);

    float lambertTerm = dot(N,L);

    if(lambertTerm > 0.0)
    {
        final_color += gl_LightSource[0].diffuse * 
                   gl_FrontMaterial.diffuse * 
                   lambertTerm; 

        vec3 E = normalize(eyeVec);
        vec3 R = reflect(-L, N);
        float specular = pow( max(dot(R, E), 0.0), 
                     gl_FrontMaterial.shininess );
        final_color += gl_LightSource[0].specular * 
                   gl_FrontMaterial.specular * 
                   specular;    
    }

    gl_FragColor = final_color * texel;
}
  • 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-28T07:34:55+00:00Added an answer on May 28, 2026 at 7:34 am

    glUniform1i(tex_loc, texture_id_);

    The second parameter should specify ID of a texture unit(hint: glActiveTexture sets the currently active texture unit), not ID of particular texture object.

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

Sidebar

Related Questions

I been working in a project, but recently I check if my program has
I have been working on a program that has 3 classes of which 2
I have a PHP program that has been written keeping in mind a single
How I can get the location of a program that has been installed on
I need to write a small program that can detect that it has been
I have a scanner that has been working smoothly up until I try the
I have an INI file that has been created by an AutoHotKey program written
I've been working with a program someone else has made and I wanted to
I have a C++ code base that has been working for a long time.
I am working on a program in Ruby with Watir that has a series

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.