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:
It works fine with the standard OpenGL binding, but not here