Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Shader Program Fatal Error  (Read 2099 times)
0 Members and 1 Guest are viewing this topic.
Offline Harris6310

Senior Newbie





« Posted 2013-05-12 17: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;
   }
}
Offline sproingie
« Reply #1 - Posted 2013-05-12 17:44:18 »

Paste the shaders too.
Offline Harris6310

Senior Newbie





« Reply #2 - Posted 2013-05-12 17: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!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie
« Reply #3 - Posted 2013-05-12 17: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 Tongue
Offline pitbuller
« Reply #4 - Posted 2013-05-12 18:06:15 »

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

Senior Newbie





« Reply #5 - Posted 2013-05-12 18:33:13 »

-snip-

There are no errors up until glValidateProgram.
Offline l3dx

Junior Member


Medals: 1


Say what?


« Reply #6 - Posted 2013-05-12 18: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.
Offline Harris6310

Senior Newbie





« Reply #7 - Posted 2013-05-12 18:41:54 »

-snip-

I have changed it and the problem still exists.
Offline davedes
« Reply #8 - Posted 2013-05-12 19: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-Utility

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

Offline l3dx

Junior Member


Medals: 1


Say what?


« Reply #9 - Posted 2013-05-12 19:13:28 »


Not according to the 1.50.09 spec
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #10 - Posted 2013-05-12 19:15:39 »

Ah, you are correct. Cool

Offline Longarmx
« Reply #11 - Posted 2013-05-12 20:05:35 »

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

Offline Harris6310

Senior Newbie





« Reply #12 - Posted 2013-05-12 20: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.
Offline davedes
« Reply #13 - Posted 2013-05-12 21: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. Grin 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

https://github.com/mattdesl/lwjgl-basics/wiki/Textures

Offline Harris6310

Senior Newbie





« Reply #14 - Posted 2013-05-12 21: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)
Offline HeroesGraveDev

JGO Kernel


Medals: 212
Projects: 11
Exp: 2 years


If it wasn't Awesome, it wasn't me.


« Reply #15 - Posted 2013-05-12 21:50:34 »

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

Or use LWJGL's BufferUtils

Offline Harris6310

Senior Newbie





« Reply #16 - Posted 2013-05-12 21:55:20 »

-snip-

Huh, I really am missing the simple errors here. Thanks, now the screen displays but the vertices are not textured. Weird.
Offline Harris6310

Senior Newbie





« Reply #17 - Posted 2013-05-13 09:45:54 »

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



Thanks again for all your help.
Offline Harris6310

Senior Newbie





« Reply #18 - Posted 2013-05-17 11: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
Offline nsigma
« Reply #19 - Posted 2013-05-17 15:15:49 »

At a guess, you haven't enabled blending?

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline Harris6310

Senior Newbie





« Reply #20 - Posted 2013-05-17 15:58:57 »

I didn't know that was needed. How is it done?
Offline matheus23

JGO Wizard


Medals: 97
Projects: 3


You think about my Avatar right now!


« Reply #21 - Posted 2013-05-17 16:54:55 »

This:
1  
glEnable(GL_BLEND);

(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 Smiley

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline davedes
« Reply #22 - Posted 2013-05-17 18:22:41 »

Also needed:
1  
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Offline Harris6310

Senior Newbie





« Reply #23 - Posted 2013-05-17 21:28:10 »

I added both of those methods to my initialisation and the texture is still rendered with a white background.
Offline pitbuller
« Reply #24 - Posted 2013-05-18 11: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.
Offline Harris6310

Senior Newbie





« Reply #25 - Posted 2013-05-18 11:34:44 »

I understand how they work, I just can't find out where this bug is coming from.
Offline nsigma
« Reply #26 - Posted 2013-05-18 12:17:24 »

https://github.com/Harris6310/Dragonshire/blob/master/src/dragonshire/graphics/Texture.java#L58 ?

Your texture doesn't appear to have an alpha channel - shouldn't that be GL_RGBA?

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (37 views)
2014-04-15 18:08:23

BurntPizza (33 views)
2014-04-15 03:46:01

UprightPath (49 views)
2014-04-14 17:39:50

UprightPath (31 views)
2014-04-14 17:35:47

Porlus (48 views)
2014-04-14 15:48:38

tom_mai78101 (69 views)
2014-04-10 04:04:31

BurntPizza (129 views)
2014-04-08 23:06:04

tom_mai78101 (229 views)
2014-04-05 13:34:39

trollwarrior1 (193 views)
2014-04-04 12:06:45

CJLetsGame (200 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!