PDA

View Full Version : texture2D always returning (0,0,0,1)



tborgers
05-05-2011, 03:35 AM
I'm trying to display a texture using a fragment shader, with standard code. The problem is that the texture2D function always returns black (0,0,0,1).

I have looked for people with the same problem, but haven't found it. Does anyone know what's wrong with the following code:

Vertex shader:


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


Fragment shader:

uniform sampler2D tex;
void main(){
vec4 color = texture2D(tex, gl_TexCoord[0].st);
gl_FragColor = color;
}


Java code:


private void setShaders(GL gl) {//on init
String[] fragment = loadShaderSrc("fragmentshader.glsl");
String[] vertex = loadShaderSrc("vertexshader.glsl");

vertexShaderProgram = gl.glCreateShader(GL.GL_VERTEX_SHADER);
fragmentShaderProgram = gl.glCreateShader(GL.GL_FRAGMENT_SHADER);
gl.glShaderSource(vertexShaderProgram, 1, vertex, null, 0);
gl.glCompileShader(vertexShaderProgram);
gl.glShaderSource(fragmentShaderProgram, 1, fragment, null, 0);
gl.glCompileShader(fragmentShaderProgram);
shaderprogram = gl.glCreateProgram();
gl.glAttachShader(shaderprogram, vertexShaderProgram);
gl.glAttachShader(shaderprogram, fragmentShaderProgram);
gl.glLinkProgram(shaderprogram);
gl.glValidateProgram(shaderprogram);
gl.glUseProgram(shaderprogram);
}



//in display()
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -6.0f);
int location = gl.glGetUniformLocation(shaderprogram, "tex");
gl.glUniform1i(location, texture.getTextureObject());
gl.glPointSize(50.0f);
gl.glBegin(GL.GL_POINTS);
...


EDIT:
The texture I'm using, is this one:
http://rc.fas.harvard.edu/sites/default/files/particle.jpg

It works fine with the standard OpenGL binding, but not here

Dan Bartlett
05-05-2011, 04:14 AM
Rather than:


gl.glUniform1i(location, texture.getTextureObject());
you need to use the texture unit that the texture is bound to for the uniform value:

gl.glActiveTexture(GL_TEXTURE0+n);
gl.glBindTexture(texture.getTextureObject());
gl.glUniform1i(location, n);
As a possible performance improvement, the location of the uniform won't change unless you re-link program, and the value of the uniform won't change unless you change it yourself, so it's possible to get rid of the glGetUniformLocation/glUniform1i calls entirely from the display(), and simply use the program, then bind the texture to the correct texture unit.

tborgers
05-05-2011, 04:44 AM
Rather than:


gl.glUniform1i(location, texture.getTextureObject());
you need to use the texture unit that the texture is bound to for the uniform value:

gl.glActiveTexture(GL_TEXTURE0+n);
gl.glBindTexture(texture.getTextureObject());
gl.glUniform1i(location, n);
As a possible performance improvement, the location of the uniform won't change unless you re-link program, and the value of the uniform won't change unless you change it yourself, so it's possible to get rid of the glGetUniformLocation/glUniform1i calls entirely from the display(), and simply use the program, then bind the texture to the correct texture unit.

Thanks a lot! I'm getting textures :D