View Full Version : [question] texture handling in fragment shader

04-10-2004, 11:06 AM
I make texture with glBindTexture.
How to send texture to uniform sampler2D variable?
and how to get current texel?

thanks for reading.

Java Cool Dude
04-10-2004, 07:30 PM
public static void sendUniform1i(int id, String name, int value){
int location = getUniformLocation(id, name);
if(checkGLError(location, name)== 0) return;

ARBShaderObjects.glUniform1iARB(location, value);

private static int getUniformLocation(int id, String name) {
ByteBuffer buffer = BufferUtils.createByteBuffer(name.length() + 1);

return ARBShaderObjects.glGetUniformLocationARB(id, buffer);
private static int checkGLError(int location, String name){
if(location == -1){
System.out.println("Error: couldn't loacte " + name);
return 0;
return 1;
ARBShaderObjects.glUseProgramObjectARB(appearance. getOGLSLShadersID());
GLShaders.sendUniform1i(appearance.getOGLSLShaders ID(), "Bump", 0);
GLShaders.sendUniform1i(appearance.getOGLSLShaders ID(), "Base", 1);
GLShaders.sendUniform1i(appearance.getOGLSLShaders ID(), "Cube", 2);
uniform samplerCube Cube;
uniform sampler2D Bump,
varying mat3 normalMatrix;
varying vec3 eyeVector;

void main()
vec3 bump = vec3(texture2D(Bump, vec2(gl_TexCoord[0]))* 2.0 - 1.0),
normal = normalize(normalMatrix*bump);

vec3 reflection = normalize((2.0*dot(eyeVector, normal)*normal - eyeVector));
vec4 base = texture2D(Base, vec2(gl_TexCoord[0])),
cube = textureCube(Cube, reflection);

gl_FragColor = cube*base*1.6;
}I'm using Java as main development language for my projects so I'm not sure that helps you out in any form.
PS: to get a texture fragment color you use

vec4 color = texture2D(Base, vec2(gl_TexCoord[0]));But don't forget to pass the gl_TexCoord[n] in your vertex shader using

gl_TexCoord[0] = gl_MultiTexCoord0;

04-10-2004, 08:31 PM
Thank you for your kindness. Java Cool Dude.
Have a nice day. :)