Can't get GLSL Shader working properly

Can anyone help me with this one? I am trying to get a basic GLSL fragment shader working. Creating and compiling the shader programs seems to work fine but when I draw a simple square with a texture, the image flickers and also the texture mapping is kind of wrong.



import com.sun.opengl.util.*;
import com.sun.opengl.util.texture.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class BasicGLSL implements GLEventListener {

	private String textureImageFile = "C:/data/nhs/images/flower.jpg";

	public int vertexShaderId, fragmentShaderId, shaderProgramId;

	private Texture texture;
	
	private float red = 1.f, green = 0.f, blue = 0.5f;
	
	

	public void init(GLAutoDrawable drawable) {
		GL gl = drawable.getGL();

		// create texture
		try {
			texture = TextureIO.newTexture(new File(textureImageFile), false);
		} catch (Exception e) {
			System.err.println("texture creation failed.");
		}

		//create and compile vertex shader

		/*
		 void main(){ 
		 gl_Position = ftransform(); 
		 }
		 */
		
		String vertexShaderSource = "void main(){ gl_Position = ftransform(); }";
		
		
		vertexShaderId = gl.glCreateShaderObjectARB(GL.GL_VERTEX_SHADER_ARB);
		gl.glShaderSourceARB(vertexShaderId, 1, new String[] { vertexShaderSource },
				new int[] { vertexShaderSource.length() }, 0);
		gl.glCompileShaderARB(vertexShaderId);

		
		// create and compile fragment shader
		
		/* 
		 uniform vec4 colour; 
		 uniform sampler2D tex;
		 void main (void) { 
		 gl_FragColor = colour*texture2D(tex,gl_TexCoord[0].st); 
		 }
		 */
		
		String fragmentShaderSource = "uniform vec4 colour;\n";
		fragmentShaderSource +="uniform sampler2D tex;\n";
		fragmentShaderSource +="void main (void){gl_FragColor = colour*texture2D(tex, gl_TexCoord[0].st);}";
		
		fragmentShaderId = gl
				.glCreateShaderObjectARB(GL.GL_FRAGMENT_SHADER_ARB);
		gl.glShaderSourceARB(fragmentShaderId, 1,
				new String[] { fragmentShaderSource }, new int[] { fragmentShaderSource
						.length() }, 0);
		gl.glCompileShaderARB(fragmentShaderId);
		shaderProgramId = gl.glCreateProgramObjectARB();

		// link shader program
		gl.glAttachObjectARB(shaderProgramId, vertexShaderId);
		gl.glAttachObjectARB(shaderProgramId, fragmentShaderId);
		gl.glLinkProgramARB(shaderProgramId);

		// 
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		gl.glClearDepth(1.0f);
		gl.setSwapInterval(1);
	}

	public void display(GLAutoDrawable drawable) {
		GL gl = drawable.getGL();

		gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

		texture.bind();
		texture.enable();

		gl.glUseProgramObjectARB(shaderProgramId);

		int colourVarNr = gl.glGetUniformLocationARB(shaderProgramId, "colour");
		gl.glUniform4fARB(colourVarNr, red, green, blue, 1);

		int texSampler = gl.glGetUniformLocationARB(shaderProgramId, "tex");
		gl.glUniform1i(texSampler, 0);

		gl.glActiveTexture(GL.GL_TEXTURE0);
		gl.glEnable(GL.GL_TEXTURE_2D);

		gl.glBegin(GL.GL_TRIANGLE_STRIP);
		gl.glTexCoord2f(0, 1);
		gl.glVertex3f(-1, 1, 0);
		gl.glTexCoord2f(0, 0);
		gl.glVertex3f(-1, -1, 0);
		gl.glTexCoord2f(1, 1);
		gl.glVertex3f(1, 1, 0);
		gl.glTexCoord2f(1, 0);
		gl.glVertex3f(1, -1, 0);
		gl.glEnd();

		gl.glUseProgramObjectARB(0);

		texture.disable();
	}

	public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
			boolean deviceChanged) {
	}

	public void reshape(GLAutoDrawable drawable, int x, int y, int width,
			int height) {
		final float h = (float) width / (float) height;
		GL gl = drawable.getGL();
		GLU glu = new GLU();
		gl.glViewport(0, 0, width, height);
		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glLoadIdentity();
		glu.gluPerspective(80, h, 0.1f, 20.f);
		gl.glMatrixMode(GL.GL_MODELVIEW);
		gl.glLoadIdentity();
		glu.gluLookAt(0, 0, 2, 0, 0, 0, 0, 1, 0);
	}

	// /////////////////////////////////////////////////////////
	// MAIN
	// /////////////////////////////////////////////////////////

	public static void main(String[] args) {
		GLCanvas canvas = new GLCanvas();
		canvas.addGLEventListener(new BasicGLSL());
		final Animator animator = new Animator(canvas);
		Frame frame = new Frame("BasicGLSL");
		frame.setSize(400, 400);
		frame.add(canvas);
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				runExit(animator);
			}
		});
		frame.setVisible(true);
		canvas.requestFocus();
		animator.start();
	}

	protected static void runExit(final Animator animator) {
		new Thread(new Runnable() {
			public void run() {
				animator.stop();
				System.exit(0);
			}
		}).start();
	}

}



You need to include the following line in your vertex shader:

gl_TexCoord[0] = gl_MultiTexCoord0;

GLSL doesn’t assign the first texCoord automatically as you’d expect… (either that, or its a quirk of the cards I’ve been testing on).

I tested adding that to your code, and it worked on my graphics card - made the texture all red.

Brilliant, that fixed it for me too.

Thank you very much. :slight_smile: