[SOLVED]First Vertex Shader little help?

;D So I am back at the shading again. Thanks to everyone who helped me yesterday. I feel as if I discovered a secret OpenGL world ;D. So I cannot help but go exploring!


private void useShaderWiggle(GL gli) {
		int v = gli.glCreateShader(GL.GL_VERTEX_SHADER);
		 
		int t = 6;//Total Number of lines in shader
		String fsrc[] = new String[t];
		int lengths[] = new int[t];
		
		fsrc[0] = "uniform float time;";
		fsrc[1] = "void main (void) {";
		fsrc[2] = "vec4 v = vec4(gl_Vertex);";
		fsrc[3] = "v.x = v.x + time;";
		fsrc[4] = "gl_Position = v;";
		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 = gli.glCreateProgram();
		wiggleShader = shaderprogram;
		gli.glAttachShader(shaderprogram, v);
		gli.glLinkProgram(shaderprogram);
		gli.glValidateProgram(shaderprogram);
		
		wiggleLoc = gli.glGetUniformLocationARB(shaderprogram, "time");
		 
	}

then to use it - in my Display Method


gl.glUseProgram(wiggleShader); 
gl.glUniform1fARB(wiggleLoc, 1.00f);//pass a value of 1.00f to shader 

//render object
gl.glUseProgram(0); //Close shader

When I use this for some reason it just makes the objects I apply it to not display, rather then shift 1.0 floats over. I feel as if I am missing something here. Can anyone shed some light?

It seems like every Vertex Shader example I try isn’t working? Perhaps I am doing something wrong? Do you need a fragment shader, to get a vertex shader to work? Can there be only one GLProgram?

I just discovered… The effect I am trying to accomplish is actually a Fragment Shader operation NOT a vertex Shader Operation. DOH! Learn something new everyday! Hopefuly I got it now :slight_smile:

You need to think of each shader as replacing a part of the the fixed-function pipeline (and by replace, I mean REPLACE, almost everything that used to be done for you has to be emulated or not used). So in your case, you need to pass down color information. This can happen in many varieties, depending on the combos of shaders:
-you could just send tex colors or other variables that the fragment shader turns into a color value
-or you would need to write the gl_Color for the fragment stage to use (I think gl_Color is the write name, but something close anyway).

I’ve also heard that it is generally recommended to use shader programs that include both a vertex and a fragment shader, so that you can more fully control the pipeline.

Linkbob now it makes sense! I was wondering why all of the examples had a Frag and a Vert shader. Haha. Thanks so much I gaurentee I have this running in 20 minutes now :smiley:

For managability, I always keep both the Vertex and the Fragment Shader in the same file.



// vertex shader here

==[[separator]]==

// fragment shader here


Simply read the text file as a string and split on the delimiter.

Very convenient, especially when you’re working with varying variables.

Good tip! Right now I am just hard-coding them into String arrays, too lazy atm to code a reader.

Okay I have the vertex shader working!!! But I’m having trouble keeping the base texture in my Frag shader. heres the frag shader code.


psrc[0] = "uniform sampler2D BaseImaget;";
psrc[1] = "void main (void) {";
psrc[2] = "vec4 texel = texture2D(BaseImaget, gl_TexCoord[0].xy);";
psrc[3] = "gl_FragColor = texel;";
psrc[4] = "}";

It’s getting the first color of (0,0) of the texture image I think and just spanning that across the whole object.

Sampler2D is a bit unintuitive for the beginner. Google a bit and you’ll find how it’s used.

And do yourself a favour and put the shaders in files. It’s so clumsy to edit them in a string.

Thanks :smiley: I’m on it!

Figured it out! Thanks everyone again. Ooo GLSL is so much fun.

I was missing this line from my vertex shader -

gl_TexCoord[0] = gl_MultiTexCoord0;

To be honest, it’s a few years ago I last worked with shaders… I’m way to busy to mess around with gfx these days.

Anyway, I recall that gl_TexCoord[0] is just a variable, that is zero, regardless of what textcoords you pass to the gfxcard. That value is in: gl_MultiTexCoord0.

So you need to do this in your Vertex Sharder:
gl_TexCoord[0] = gl_MultiTexCoord0;

So that you can read the interpolated value of gl_TexCoord[0] in the Fragment Shader:
gl_TexCoord[0].st

I might be wrong, I’m too lazy to test it - you might want to read some tutorials regarding those variables.

You’re too fast solving your own problems! Darn! :wink:

Unlike some posters(not saying on here but that I’ve run into) I really really do try my hardest to solve a problem.

Thanks everyone. Here’s a quick youtube clip of what you’ve helped me accomplish :smiley:

hopefully you enjoy it as much as I do haha.

Cool.

Weird also.