Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (426)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
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  
  [SOLVED]Opacity Shader + WORKING code  (Read 2073 times)
0 Members and 1 Guest are viewing this topic.
Offline Cakey

Junior Member





« Posted 2009-03-03 01:14:06 »

Pretty much I have realized the only way to give my game any decent edge, means I need to explore GLSL. I figured what would be better than to start off with a simple Opacity shader.

I am pretty confused on the basic principles of shaders, so I figured boo ya Orange book!

The Orange book is cool, and it makes sense and all, but it's 600 some pages of reading when I am imagining I am looking at a problem involving possibly 20 lines of code... So I took what I could from it, and read the NeHe GLSL tutorial(not much help imo).

Here's the code I have generated for my shader, in setting a textured object's opacity.
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  
private void useShader(GL gli) {
      int v = gl.glCreateShader(GL.GL_FRAGMENT_SHADER);

      int t = 6;//Total Number of lines in shader
     String fsrc[] = new String[t];
      int lengths[] = new int[t];
      //Shader Source Code to set a texture to 1/4 it's opacity.
     fsrc[0] = "uniform sampler2D BaseImage;";
      fsrc[1] = "uniform float Opacity;";
      fsrc[2] = "void main (void) {";
      fsrc[3] = "vec4 base = texture2D(BaseImage, gl_TexCoord[0].xy);";
      fsrc[4] = "gl_FragColor = base * 0.25";
      fsrc[5] = "}";

      //Load variables with shader code information
     String tmp;
      for(int i = 0; i < t;i++){
         tmp = fsrc[i];
         lengths[i] = tmp.length();
      }
      //Apply Shader Source
     gli.glShaderSource(v, t, fsrc, lengths, (int)0);
      gli.glCompileShader(v);//Compile

      int shaderprogram = gl.glCreateProgram();//Create prog
     gli.glAttachShader(shaderprogram, v);
      gli.glLinkProgram(shaderprogram);
      gli.glValidateProgram(shaderprogram);

      gli.glUseProgram(shaderprogram);
   }


Obviously it doesn't work otherwise I wouldn't be posting here haha. I think the problem is I am not passing the texture information to the GLSL program? But I guess I am just confused.  I know everything compiles and runs as normal, however a tiny bit slower.

Pretty much I just wanna know how far off I am, if I should just sit down pull my hair out and read the orange book, if anyone has found any links to a shader similar to this, or any other interesting basic shader(just to play with), or perhaps a lame-mans terms tutorials.

Offline irreversible_kev

Junior Member





« Reply #1 - Posted 2009-03-03 01:36:31 »

Can you post your entire code or is it too much?

You should try checking for shader compile errors, program link errors and OpenGL errors.

Read my two posts here for information on how to do that.
Offline Cakey

Junior Member





« Reply #2 - Posted 2009-03-03 01:39:59 »

The entire code is like 25 class files...
I think I just coded something wrong. Thanks for taking interest, Ill post if I can debug an error.
edit:
It's hard to translate what you did for that person because I am not even reading my shader from a file. I am just storing it directly to a string array...

and the way I derive my gl is like this

gl = drawable.getGL();


so I dont even know where I'd put the DebugGl()


oyy

In theory if you made(or borrowed) a project with just a quad visible and put a call to my method in the Init method shouldn't it work?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline lhkbob

JGO Knight


Medals: 32



« Reply #3 - Posted 2009-03-03 02:18:09 »

You need to bind values to the uniform variables.  Right now, those uniforms are just uninitialized (and may take default values, but I've had weird experience assuming that).  This can be done with glUniformXyz(uniform_id, value...).  The uniform id is an id assigned to each uniform in a program, which can be found with glGetUniformLocation(program_id, string name).  This call should only be done after the program has been successfully linked.  glUniformXyz() can only be called when the desired program is in use.

Hopefully this helps solve your problem, otherwise you might want to post a more detailed explanation of what's going wrong.

---

Also, I just noticed that you're missing a semi-colon on the 5th line (gl_FragColor = base * .25").  If this isn't a typo, then the shader creation will have failed and the program will not work correctly when it's bound.  You need to make sure that you're always checking the compilation status of each shader object and the link status of each program.

This can be done with:
glGetShaderiv(shader_id, GL_COMPILE_STATUS, store, 0)

and

glGetProgramiv(program_id, GL_LINK_STATUS, store, 0)

where store is an int[] with at least one element.

Each shader object and program come with an info log that can be accessed like:
glGetShaderiv(shader_id, GL_INFO_LOG_LENGTH, store, o); / get # of chars in log
ByteBuffer chars = BufferUtil.newByteBuffer(store[0]);
glGetShaderInfoLog(shader_id, store[0], null, chars);

Each byte in the chars buffer should then be an ASCII character, and it's straightforward to make it into a String.
To get the log for a program, just use glGetProgramiv(...) and glGetProgramInfoLog(...) instead.

Offline Cakey

Junior Member





« Reply #4 - Posted 2009-03-03 02:22:43 »

Wow thanks for the great information. I got a little bit more research to do apparently, and some work. Thanks I'll report back once something new happens Smiley Grin

Quick question glUniformXYZ does not exist? hmm
How do I pass a texture to this GL variable?

NVM! It works! Thanks sooo much everyone!

Offline Cakey

Junior Member





« Reply #5 - Posted 2009-03-03 02:40:59 »

False alarm. The shader now works! Which is awesome!

But this only makes the textures darker by the number, it doesn't make them transparent? Any idea how to do this? If it's really really complicated then I'll just back down from the task, but I mean DirectX simply deals with transparencies and like 99% of modern commerical games have translucent objects(HUDS atleast).

Offline cylab

JGO Knight


Medals: 34



« Reply #6 - Posted 2009-03-03 02:46:50 »

Do you have a non-black background? if not, getting a darker result might be expectable Wink If you have another object behind, make sure you are drawing back to front.

Mathias - I Know What [you] Did Last Summer!
Offline Cakey

Junior Member





« Reply #7 - Posted 2009-03-03 02:49:53 »

Cylab you've seen my code Smiley and yes I do have a black background.

would it matter if they were all set to the same z depth?or should I offset my HUDs marginally closer to the camera? Nvm. I just realized my HUDS aren't on the same level as my background haha. I'm gonna have to look into this drawing back to front.

btw, the game is coming along great thanks to your help. Would you mind being in the credits(I already have your name written down form the e-mail you sent me)?

Offline lhkbob

JGO Knight


Medals: 32



« Reply #8 - Posted 2009-03-03 03:16:06 »

Quick question glUniformXYZ does not exist? hmm
How do I pass a texture to this GL variable?

Sorry, glUniformXYZ was meant to represent the many possible glUniform functions.  There is a glUniform for the size of each variable and the type: glUniform2f, glUniform3f, glUniform1i, etc.  but they all do the same basic thing.  The 2f/3f varieties are used to bind vector values to uniforms.

Binding a texture to a sampler uses the glUniform1i command.  You pass in the texture unit that the desired texture will be bound to.  So if texture X was bound to GL_TEXTURE2, then you'd pass 2 in to glUniform1i.

But this only makes the textures darker by the number, it doesn't make them transparent?

When you multiply base by .25, you're multiplying each of the four values in it by .25 (which may be what you want).  By default, the alpha value doesn't do much in OpenGL.  It's only really useful when you start using alpha testing and enable blending.  If you need transparency, then you'll want to look into these.  Luckily, they don't have anything to do with glsl shaders.

Offline Cakey

Junior Member





« Reply #9 - Posted 2009-03-03 03:22:40 »

Once again big thanks to everyone here, I made my first shader thanks to you Smiley!

FIGURED IT OUT! Shader actually did work all I had to do was:

gl.glDepthMask(false);
//use shader program
//render object (make sure blending is enabled!)
gl.glDepthMask(true);
And the UPDATED shader code. Now it doesn't dampen all of the colors only the Alpha:)
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  
private void useShader(GL gli) {
     
      int v = gl.glCreateShader(GL.GL_FRAGMENT_SHADER);
       
      int t = 5;//Total Number of lines in shader
     String fsrc[] = new String[t];
      int lengths[] = new int[t];
     
      fsrc[0] = "uniform sampler2D BaseImage;";
      fsrc[1] = "void main (void) {";
      fsrc[2] = "vec4 base = texture2D(BaseImage, gl_TexCoord[0].xy);";
      fsrc[3] = "gl_FragColor = vec4(base.rgb, base.a * 0.15);";
      fsrc[4] = "}";
      //Load variables with shader code information
     String tmp;
      for(int i = 0; i < t;i++){
         tmp = fsrc[i];
         lengths[i] = tmp.length();
      }
      //Apply Shader Source
     gli.glShaderSource(v, t, fsrc, lengths, (int)0);
      gli.glCompileShader(v);//Compile

      int shaderprogram = gl.glCreateProgram();//Create prog
     transparencyShader = shaderprogram;
      gli.glAttachShader(shaderprogram, v);
      gli.glLinkProgram(shaderprogram);
      gli.glValidateProgram(shaderprogram);
   }

THANKS EVERYBODY!

I love JOGL. This is exactly what I needed to get my feet wet in the GLSL world.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cylab

JGO Knight


Medals: 34



« Reply #10 - Posted 2009-03-03 10:57:08 »

Would you mind being in the credits?

I would be honored Smiley

Mathias - I Know What [you] Did Last Summer!
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 (75 views)
2014-04-15 18:08:23

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

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

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

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

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

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

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

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

CJLetsGame (220 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!