[SOLVED]Opacity Shader + WORKING code

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.


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.

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.

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?

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.

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 :slight_smile: ;D

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

NVM! It works! Thanks sooo much everyone!

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).

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.

Cylab you’ve seen my code :slight_smile: 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)?

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.

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.

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

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:)


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.

I would be honored :slight_smile: