Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualrocklobster

Posted 17 December 2012 - 12:46 AM

Thanks for the response, but i still can't get this to work. I fixed 'Bind texture' part in my GenerateTexture() function but I don't see the way to use

GLuint id = glGetUniformLocation(programId, "gSampler");

and i put this

glUniform1i(id, 0); // 0 for GL_TEXTURE0
in my render function

the problem is that the tutorials on the link i posted use more than one shader function to load and process shaders. So I can't get programId from outside. When I put this function inside CompileShaders function that is used in tutorials I still can't make this work. Can you help?


Basically the getUniformLocation and uniform1i functions are for sending data to the shader. So to draw your textured object you need to:

- Bind the shader
- Set texture unit
- Bind texture
- Set the uniforms for the shader
- Draw the object

Make sure your function that creates your shaders returns a GLuint for the program id (this will be the return value from glCreateProgram())

then you can bind the shader, texture and set the uniforms

// loading the data
GLuint myShader = CreateShaders("vertex.vert", "fragment.frag");
GLuint texId = CreateTexture("logo.png");

// when drawing
glUseProgram(myShader);

glBindTexture(GL_TEXTURE_2D, texId);
glActiveTexture(GL_TEXTURE0);

GLuint loc = glGetUniformLocation(myShader, "gSampler");
glUniform1i(loc, 0);

// then just draw your mesh
glDrawArrays(GL_TRIANGLES, 0, mesh.count);

#1rocklobster

Posted 17 December 2012 - 12:44 AM

Thanks for the response, but i still can't get this to work. I fixed 'Bind texture' part in my GenerateTexture() function but I don't see the way to use

GLuint id = glGetUniformLocation(programId, "gSampler");

and i put this

glUniform1i(id, 0); // 0 for GL_TEXTURE0
in my render function

the problem is that the tutorials on the link i posted use more than one shader function to load and process shaders. So I can't get programId from outside. When I put this function inside CompileShaders function that is used in tutorials I still can't make this work. Can you help?


Basically the getUniformLocation and uniform1i functions are for sending data to the shader. So to draw your textured object you need to:

- Bind the shader
- Set texture unit
- Bind texture
- Set the uniforms for the shader
- Draw the object

Make sure your function that creates your shaders returns a GLuint for the program id (this will be the return value from glCreateProgram())

then you can bind the shader, texture and set the uniforms

// loading the data
GLuint myShader = CreateShaders("vertex.vert", "fragment.frag");
GLuint texId = CreateTexture("logo.png");

// when drawing
glUseProgram(myShader);


glBindTexture(GL_TEXTURE_2D, texId);
glActiveTexture(GL_TEXTURE0);

GLuint loc = glGetUniformLocation(myShader, "gSampler");
glUniform1i(loc, 0);


// then just draw your mesh
glDrawArrays(GL_TRIANGLES, 0, mesh.count);

PARTNERS