Harris6310
Senior Newbie 
|
 |
«
Posted
2013-05-12 15:37:20 » |
|
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: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| 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; } } |
|
|
|
|
sproingie
|
 |
«
Reply #1 - Posted
2013-05-12 15:44:18 » |
|
Paste the shaders too.
|
|
|
|
Harris6310
Senior Newbie 
|
 |
«
Reply #2 - Posted
2013-05-12 15:45:55 » |
|
Okay, here is the vertex shader: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #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: 1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #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); } |
|
|
|
|
Games published by our own members! Check 'em out!
|
|
sproingie
|
 |
«
Reply #3 - Posted
2013-05-12 15:59:38 » |
|
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 
|
|
|
|
pitbuller
|
 |
«
Reply #4 - Posted
2013-05-12 16:06:15 » |
|
What you try to do with pass color? Currently it does absolute nothing and will get optimized away.
|
|
|
|
Harris6310
Senior Newbie 
|
 |
«
Reply #5 - Posted
2013-05-12 16:33:13 » |
|
-snip- There are no errors up until glValidateProgram.
|
|
|
|
l3dx
Junior Devvie   Medals: 1
Say what?
|
 |
«
Reply #6 - Posted
2013-05-12 16:40:39 » |
|
texture2D is deprecated. 1
| out_Color = texture2D(texture_diffuse, pass_TextureCoord); |
should be 1
| out_Color = texture(texture_diffuse, pass_TextureCoord); |
…not 100% sure that causes your problem though.
|
|
|
|
Harris6310
Senior Newbie 
|
 |
«
Reply #7 - Posted
2013-05-12 16:41:54 » |
|
-snip-
I have changed it and the problem still exists.
|
|
|
|
davedes
|
 |
«
Reply #8 - Posted
2013-05-12 17:08:09 » |
|
texture2D is deprecated. 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: https://github.com/mattdesl/lwjgl-basics/wiki/ShaderProgram-UtilityAlso next time you get a runtime exception, be sure to include the stack trace.
|
|
|
|
l3dx
Junior Devvie   Medals: 1
Say what?
|
 |
«
Reply #9 - Posted
2013-05-12 17:13:28 » |
|
texture2D is deprecated. Only as of #version 330. See here.Not according to the 1.50.09 spec
|
|
|
|
Games published by our own members! Check 'em out!
|
|
davedes
|
 |
«
Reply #10 - Posted
2013-05-12 17:15:39 » |
|
texture2D is deprecated. Only as of #version 330. See here.Not according to the 1.50.09 spec Ah, you are correct. 
|
|
|
|
Longarmx
|
 |
«
Reply #11 - Posted
2013-05-12 18:05:35 » |
|
Shouldn't you be assigning your shader id's when you load the shaders?
|
|
|
|
Harris6310
Senior Newbie 
|
 |
«
Reply #12 - Posted
2013-05-12 18:57:51 » |
|
-snip- 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.
|
|
|
|
davedes
|
 |
«
Reply #13 - Posted
2013-05-12 19:06:40 » |
|
-snip- 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. Welcome to OpenGL.  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. https://github.com/mattdesl/lwjgl-basics/wiki/Textures
|
|
|
|
Harris6310
Senior Newbie 
|
 |
«
Reply #14 - Posted
2013-05-12 19:45:09 » |
|
-snip- Well here is my current Texture class: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| 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: 1 2 3 4 5 6 7 8
| 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) |
|
|
|
|
HeroesGraveDev
|
 |
«
Reply #15 - Posted
2013-05-12 19:50:34 » |
|
Change ByteBuffer.allocate() to ByteBuffer.allocateDirect()
Or use LWJGL's BufferUtils
|
|
|
|
Harris6310
Senior Newbie 
|
 |
«
Reply #16 - Posted
2013-05-12 19:55:20 » |
|
-snip- Huh, I really am missing the simple errors here. Thanks, now the screen displays but the vertices are not textured. Weird.
|
|
|
|
Harris6310
Senior Newbie 
|
 |
«
Reply #17 - Posted
2013-05-13 07:45:54 » |
|
I have fixed the minor texture problem. Here is the result:  Thanks again for all your help.
|
|
|
|
Harris6310
Senior Newbie 
|
 |
«
Reply #18 - Posted
2013-05-17 09:55:26 » |
|
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
|
|
|
|
nsigma
|
 |
«
Reply #19 - Posted
2013-05-17 13:15:49 » |
|
At a guess, you haven't enabled blending?
|
Praxis LIVE - hybrid visual IDE for (live) creative coding
|
|
|
Harris6310
Senior Newbie 
|
 |
«
Reply #20 - Posted
2013-05-17 13:58:57 » |
|
I didn't know that was needed. How is it done?
|
|
|
|
matheus23
|
 |
«
Reply #21 - Posted
2013-05-17 14:54:55 » |
|
This: (above needs ordered rendering (back to front, back drawn first)) or, when you only want to have the functionality to simply discard transparent pixels and have to semi-transparency (no need for ordered rendering): 1 2
| glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER, 0.1f); |
hope this helps 
|
|
|
|
davedes
|
 |
«
Reply #22 - Posted
2013-05-17 16:22:41 » |
|
Also needed: 1
| glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
|
|
|
|
Harris6310
Senior Newbie 
|
 |
«
Reply #23 - Posted
2013-05-17 19:28:10 » |
|
I added both of those methods to my initialisation and the texture is still rendered with a white background.
|
|
|
|
pitbuller
|
 |
«
Reply #24 - Posted
2013-05-18 09:14:38 » |
|
I added both of those methods to my initialisation and the texture is still rendered with a white background.
You should read this http://www.arcsynthesis.org/gltut/Understanding how stuff work make doing it lot easier.
|
|
|
|
Harris6310
Senior Newbie 
|
 |
«
Reply #25 - Posted
2013-05-18 09:34:44 » |
|
I understand how they work, I just can't find out where this bug is coming from.
|
|
|
|
nsigma
|
 |
«
Reply #26 - Posted
2013-05-18 10:17:24 » |
|
|
Praxis LIVE - hybrid visual IDE for (live) creative coding
|
|
|
|