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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:58:55+00:00 2026-06-11T13:58:55+00:00

I am having some difficulty understanding the connection between my OpenGL application and my

  • 0

I am having some difficulty understanding the connection between my OpenGL application and my GLSL files (.vert and .frag). I know how to create a vertex or fragment shader, and there is plenty of examples on the web, but I am having difficulty actually using my shader within my application, specifically, linking (binding?) textures to my shader. My question is, what code am I missing to use the textures I have uploaded into my application with the shader?

My current application creates a window, loads 4 textures (rock, grass, stone, and a mixmap since I want to use this for texture splatting), and then draws a quad in the main loop. My entire code for the application is below:

#include <windows.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <SFML/Graphics.hpp>
#include <glew.h>
#include <gl/gl.h>
#include <gl/glu.h>

#include "textfile.h"
#include "textfile.cpp"

using namespace std;

class Scene {
public:
    void resize( int w, int h ) {
        // OpenGL Reshape
        glViewport( 0, 0, w, h );
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        gluPerspective( 120.0, (GLdouble)w/(GLdouble)h, 0.5, 500.0 );
        glMatrixMode( GL_MODELVIEW );
    }
};

///Shader
GLuint p, f, v;
void setShader() {

    char *vs,*fs;

    v = glCreateShader(GL_VERTEX_SHADER);
    f = glCreateShader(GL_FRAGMENT_SHADER);

    vs = textFileRead("shader.vert");
    fs = textFileRead("shader.frag");

    const char * vv = vs;
    const char * ff = fs;

    glShaderSource(v, 1, &vv,NULL);
    glShaderSource(f, 1, &ff,NULL);

    free(vs);free(fs);

    glCompileShader(v);
    glCompileShader(f);

    p = glCreateProgram();

    glAttachShader(p,v);
    glAttachShader(p,f);

    glLinkProgram(p);
    glUseProgram(p);
}

int main() {

    sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Test");

    ///Setup the scene, materials, lighting
    Scene scene;
    scene.resize(800,600);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glColorMaterial(GL_FRONT_AND_BACK, GL_EMISSION);
    glEnable(GL_COLOR_MATERIAL);
    glShadeModel(GL_SMOOTH);

    ///Terrain Textures
    sf::Image tex0;
    tex0.loadFromFile("mixmap.png");
    sf::Image tex1;
    tex1.loadFromFile("grass.png");
    sf::Image tex2;
    tex2.loadFromFile("rock.png");
    sf::Image tex3;
    tex3.loadFromFile("stone.png");

    GLuint mixmap;
    glGenTextures(1, &mixmap);
    glBindTexture(GL_TEXTURE_2D, mixmap);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, tex0.getSize().x, tex0.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const GLvoid*)tex0.getPixelsPtr() );

    GLuint grass;
    glGenTextures(1, &grass);
    glBindTexture(GL_TEXTURE_2D, grass);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, tex1.getSize().x, tex1.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const GLvoid*)tex1.getPixelsPtr() );

    GLuint rock;
    glGenTextures(1, &rock);
    glBindTexture(GL_TEXTURE_2D, rock);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, tex2.getSize().x, tex2.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const GLvoid*)tex2.getPixelsPtr() );

    GLuint stone;
    glGenTextures(1, &stone);
    glBindTexture(GL_TEXTURE_2D, stone);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, tex3.getSize().x, tex3.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const GLvoid*)tex3.getPixelsPtr() );

    ///I am afrain I am missing some code here that will actually link the textures to the shader itself

    ///Shader
    glewInit();
    setShader();

    ///Start loop
    while( window.isOpen() ) {
        sf::Event event;
        while( window.pollEvent( event ) ) {
            if( event.type == sf::Event::Closed )
                window.close();
        }

        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(50.0, 1.0, 1.0, 50);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(1, 0, 1, 0, 0, 0, 0, 1, 0);

        glBegin(GL_QUADS);
            glVertex3f(-0.5, -0.5, 0.0);

            glVertex3f(-0.5, 0.5, 0.0);

            glVertex3f(0.5, 0.5, 0.0);

            glVertex3f(0.5, -0.5, 0.0);
        glEnd();

        ///Reset env settings for SFML
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

        window.display();
    }
    return 1;
}

Below are my vertex and fragment shaders as well, though I am unsure if my vertex shader is complete…

Vertex:

void main()
{
 gl_TexCoord[0] = gl_MultiTexCoord0;
 gl_Position = ftransform();
} 

Fragment:

uniform sampler2D MixMap;
uniform sampler2D Grass;
uniform sampler2D Stone;
uniform sampler2D Rock;

varying vec4 texCoord;

void main(void)
{
   vec4 mixmap   = texture2D( MixMap, texCoord.xy );
   vec4 tex0    = texture2D( Grass, texCoord.xy ); 
   vec4 tex1    = texture2D( Rock,  texCoord.xy ); 
   vec4 tex2    = texture2D( Stone, texCoord.xy );

   tex0 *= mixmap.r; 
   tex1 = mix( tex0, tex1, mixmap.g ); 
   vec4 outColor = mix( tex1, tex2, mixmap.b ); 

   gl_FragColor = outColor;
}
  • 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-06-11T13:58:56+00:00Added an answer on June 11, 2026 at 1:58 pm

    First, you’ll need to bind each texture object to a texture unit in the OpenGL context. GPUs have GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS number of texture image units, each of which can have textures attached to it.

    This would look like this:

    glActiveTexture(GL_TEXTURE0 + 0);
    glBindTexture(GL_TEXTURE_2D, mixmap); //mixmap now attached to sampler 0
    
    glActiveTexture(GL_TEXTURE0 + 1);
    glBindTexture(GL_TEXTURE_2D, grass); //attached to sampler 1
    
    glActiveTexture(GL_TEXTURE0 + 2);
    glBindTexture(GL_TEXTURE_2D, rock);  //attached to sampler 2
    
    glActiveTexture(GL_TEXTURE0 + 3);
    glBindTexture(GL_TEXTURE_2D, stone); //attached to sampler 3
    

    Note the GL_TEXTURE0 + x syntax. This is done so that you don’t feel constrained by GL_TEXTUREi only going up to GL_TEXTURE32; you can have more than 32 simultaneous multi-textures (if your implementation supports it). Granted, I don’t know why you would want to, but you can.

    Now you have four textures bound to four texture image units. The next step is to assign the GLSL samplers to texture image units. After you have linked your shader, you must query the uniform location of each sampler, and then tell the program which texture image unit it should look for for the actual texture.

    compileAndLinkProgram();
    glUseProgram(program);
    
    mixmapUniformLocation = glGetUniformLocation(program, "MixMap"); //get the uniform associated with "MixMap"
    glUniform1i(mixmapUniformLocation, 0); //set MixMap uniform to 0 (sampler 0);
    
    
    grassUniformLocation = glGetUniformLocation(program, "Grass"); //get the uniform associated with "Grass"
    glUniform1i(grassUniformLocation, 1); //set Grass uniform to 1 (sampler 1);
    
    ...repeat for 4 samplers...
    

    Now when you sample a sampler in the shader, it will pull from the texture bound to that sampler.

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

Sidebar

Related Questions

I've been having some difficulty in understanding the source of a problem. Below is
I'm having some difficulty writing this code in php. I want to create a
Having some difficulty understanding the best way to implement subclasses with a generic repository
I am having some difficulty understanding how containers are implemented in C++. Specifically, how
I'm having some difficulty understanding how to validate a date (DOB) using MVC2. What
HI all, I'm having some difficulty understanding the rationale behind group by aggregation in
I'm having some difficulty in understanding the following WHERE clause in a T-SQL (SQL
I am having some difficulty understanding callbacks and program flow, synchronization issues. Lets say
I'm having some difficulty understanding the update command for SQLite. In my example I
I'm having some difficulty understanding a certain aspect of the @async annotation, and possibly

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.