Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  [SOLVED]First Vertex Shader little help?  (Read 845 times)
0 Members and 2 Guests are viewing this topic.
Offline Cakey

Full Member
**

Posts: 127



« on: 2009-03-03 13:21:26 »

 Grin 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 Grin. So I cannot help but go exploring!

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  
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
1  
2  
3  
4  
5  
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?

Offline Cakey

Full Member
**

Posts: 127



« Reply #1 on: 2009-03-03 15:13:41 »

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 Smiley

Offline lhkbob

JGO Neuromancer
****

Posts: 1174
Medals: 35



« Reply #2 on: 2009-03-03 15:18:42 »

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.

Games published by our own members! Go get 'em!
Offline Cakey

Full Member
**

Posts: 127



« Reply #3 on: 2009-03-03 15:24:42 »

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 Cheesy

Online Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #4 on: 2009-03-03 15:34:50 »

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

1  
2  
3  
4  
5  
// 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.

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline Cakey

Full Member
**

Posts: 127



« Reply #5 on: 2009-03-03 15:40:06 »

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.
1  
2  
3  
4  
5  
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.

Online Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #6 on: 2009-03-03 15:46:07 »

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.

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline Cakey

Full Member
**

Posts: 127



« Reply #7 on: 2009-03-03 15:51:49 »

Thanks Cheesy 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 -
1  
gl_TexCoord[0] = gl_MultiTexCoord0;

Online Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #8 on: 2009-03-03 15:55:55 »

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.

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Online Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #9 on: 2009-03-03 15:56:37 »

You're too fast solving your own problems! Darn! Wink

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Games published by our own members! Go get 'em!
Offline Cakey

Full Member
**

Posts: 127



« Reply #10 on: 2009-03-03 20:18:54 »

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 Cheesy

http://www.youtube.com/watch?v=Z5v6wIZOGlc

hopefully you enjoy it as much as I do haha.

Online Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5870
Medals: 255


Hand over your head.


« Reply #11 on: 2009-03-03 20:40:26 »

Cool.

Weird also.

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.127 seconds with 20 queries.