Shader Program Fatal Error

Hello, I was wondering if someone could help fix a fatal error I receive with my shader program. It is a run-time error that is thrown by the glValidateProgram method. Here is my code:

public class ShaderProgram
{
	int objectID;
	int vertexShaderID;
	int fragmentShaderID;
	
	public void initialise()
	{
		loadShader("shaders/vertex.shader", GL_VERTEX_SHADER);
		loadShader("shaders/fragment.shader", GL_FRAGMENT_SHADER);
		
		objectID = glCreateProgram();
		
		glAttachShader(objectID, vertexShaderID);
		glAttachShader(objectID, fragmentShaderID);
		glLinkProgram(objectID);
		glBindAttribLocation(objectID, 0, "in_Position");
		glBindAttribLocation(objectID, 1, "in_Color");
		glBindAttribLocation(objectID, 2, "in_TextureCoord");
		glValidateProgram(objectID);
	}
	
	public void terminate()
	{
		glUseProgram(0);
		glDetachShader(objectID, vertexShaderID);
		glDetachShader(objectID, fragmentShaderID);
		glDeleteShader(vertexShaderID);
		glDeleteShader(fragmentShaderID);
		glDeleteProgram(objectID);
	}
	
	public void select()
	{
		glUseProgram(objectID);
	}
	
	public void deselect()
	{
		glUseProgram(0);
	}
	
	public int loadShader(String fileLocation, int type)
	{
		StringBuilder stringBuilder = new StringBuilder();
		int shaderID = 0;
		
		try
		{
			BufferedReader bufferedReader = new BufferedReader(new FileReader(fileLocation));
			String line;
			
			while((line = bufferedReader.readLine()) != null)
			{
				stringBuilder.append(line);
				stringBuilder.append("\n");
			}
			
			bufferedReader.close();
		}
		catch(IOException exception)
		{
			exception.printStackTrace();
		}
		
		shaderID = glCreateShader(type);
		
		glShaderSource(shaderID, stringBuilder);
		glCompileShader(shaderID);
		
		return shaderID;
	}
}

Paste the shaders too.

Okay, here is the vertex shader:

#version 150 core

in vec4 in_Position;
in vec4 in_Color;
in vec2 in_TextureCoord;

out vec4 pass_Color;
out vec2 pass_TextureCoord;

void main(void)
{
	gl_Position = in_Position;
	pass_Color = in_Color;
	pass_TextureCoord = in_TextureCoord;
}

And here is the fragment shader:

#version 150 core

uniform sampler2D texture_diffuse;

in vec4 pass_Color;
in vec2 pass_TextureCoord;

out vec4 out_Color;

void main(void)
{
	out_Color = pass_Color;
	out_Color = texture2D(texture_diffuse, pass_TextureCoord);
}

Nothing jumping out at me. What’s glGetProgramInfoLog tell you? You should really have ShaderProgram checking the status after each of the compile, link, and validate steps and spewing the appropriate log out on error. It’s handy to do that for warnings too, though some compilers like to make “successfully compiled” into a warning :stuck_out_tongue:

What you try to do with pass color? Currently it does absolute nothing and will get optimized away.

[quote=“sproingie,post:4,topic:41920”]
There are no errors up until glValidateProgram.

texture2D is deprecated.

out_Color = texture2D(texture_diffuse, pass_TextureCoord);

should be

out_Color = texture(texture_diffuse, pass_TextureCoord);

…not 100% sure that causes your problem though.

I have changed it and the problem still exists.

Only as of #version 330. See here.

To the OP… Here are some suggestions to clean up your shader handling.

  1. Check to see if glCompileShader failed, and print the log if it is a non-zero string. Also do the same after glLinkProgram.

  2. Check to see if glCreateProgram returns zero. If so, shaders are not supported.

  3. You need to call glBindAttribLocation before glLinkProgram for it to have an effect.

  4. You don’t need new line characters when passing to glShaderSource. You can just read the String fully like this.

  5. You aren’t using the return values from loadShader() – therefore you are using zero as the second parameter to glAttachShader. This is probably the cause of your error.

  6. You don’t need to call glValidateProgram. Right now the call is useless since you aren’t checking the status of the validation.

  7. In the programmable pipeline there is no “default” shader program; so it would be cleaner and more OpenGL-like to get rid of your deselect() method. You can see in my shader utility, I just have a single method to select the program: use().

  8. If you plan to target GL 2.x (which you probably should for better compatibility), you should not use #version 150.

Read through my tutorial to get a better idea of how to put everything together:

Also next time you get a runtime exception, be sure to include the stack trace. :slight_smile:

Not according to the 1.50.09 spec

Ah, you are correct. 8)

Shouldn’t you be assigning your shader id’s when you load the shaders?

[quote=“davedes,post:9,topic:41920”]
Wow, thanks for the advice. And it was number 5 that caused the problem. The shader program is fixed, but I now have an issue with my texture loader.

[quote=“Harris6310,post:13,topic:41920”]

Welcome to OpenGL. ;D This is why a framework like LibGDX is often a better choice; it allows you to utilize OpenGL and learn graphics programming without writing a lot of error-prone and low-level boilerplate.

Anyways, here is another tutorial that should help you with textures. If you’re still running into problems, you need to post your code and any errors… otherwise we can’t do much to help. :wink:

[quote=“davedes,post:14,topic:41920”]
Well here is my current Texture class:

public class Texture
{
	int objectID;
	
	int width;
	int height;
	
	String resourceReference;
	
	public Texture(String resourceReference)
	{
		this.resourceReference = resourceReference;
	}
	
	public void initialise()
	{
		ByteBuffer byteBuffer = null;
		
		try
		{
			InputStream inputStream = new FileInputStream(resourceReference);
			PNGDecoder pngDecoder = new PNGDecoder(inputStream);
			
			width = pngDecoder.getWidth();
			height = pngDecoder.getHeight();
			byteBuffer = ByteBuffer.allocate(width * height * 4);
			pngDecoder.decode(byteBuffer, width * 4, Format.RGBA);
			byteBuffer.flip();
			inputStream.close();
		}
		catch(IOException exception)
		{
			exception.printStackTrace();
		}
		
		glEnable(GL_TEXTURE_2D);
		
		objectID = glGenTextures();
		
		glBindTexture(GL_TEXTURE_2D, objectID);
		glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, byteBuffer);
	}
	
	public void bind()
	{
		glBindTexture(GL_TEXTURE_2D, objectID);
	}
	
	public void terminate()
	{
		glDeleteTextures(objectID);
	}
}

And here is the console output:

Exception in thread "main" java.lang.IllegalArgumentException: ByteBuffer is not direct
	at org.lwjgl.BufferChecks.checkDirect(BufferChecks.java:115)
	at org.lwjgl.BufferChecks.checkBuffer(BufferChecks.java:231)
	at org.lwjgl.opengl.GL11.glTexImage2D(GL11.java:2845)
	at dragonshire.graphics.Texture.initialise(Texture.java:60)
	at dragonshire.graphics.Graphics.initialise(Graphics.java:26)
	at dragonshire.Game.initialise(Game.java:21)
	at dragonshire.Game.main(Game.java:14)

Change ByteBuffer.allocate() to ByteBuffer.allocateDirect()

Or use LWJGL’s BufferUtils

[quote=“HeroesGraveDev,post:16,topic:41920”]
Huh, I really am missing the simple errors here. Thanks, now the screen displays but the vertices are not textured. Weird.

I have fixed the minor texture problem. Here is the result:

http://s15.postimg.org/b388cr21n/Screenshot_from_2013_05_13_08_47_02.png

Thanks again for all your help.

Sorry to be a bother, but I have realised that my code doesn’t actually work. The textures render, but transparency isn’t working. Can someone suggest what the problem is? Here is my code: https://github.com/Harris6310/Dragonshire

At a guess, you haven’t enabled blending?