Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (517)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (578)
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]First Vertex Shader little help?  (Read 1434 times)
0 Members and 1 Guest are viewing this topic.
Offline Cakey

Junior Duke





« Posted 2009-03-03 18: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

Junior Duke





« Reply #1 - Posted 2009-03-03 20: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 Knight


Medals: 32



« Reply #2 - Posted 2009-03-03 20: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Cakey

Junior Duke





« Reply #3 - Posted 2009-03-03 20: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

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2009-03-03 20: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

Junior Duke





« Reply #5 - Posted 2009-03-03 20: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.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2009-03-03 20: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

Junior Duke





« Reply #7 - Posted 2009-03-03 20: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;

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2009-03-03 20: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
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2009-03-03 20: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Cakey

Junior Duke





« Reply #10 - Posted 2009-03-04 01: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.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #11 - Posted 2009-03-04 01:40:26 »

Cool.

Weird also.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
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.

DarkCart (16 views)
2014-10-31 21:44:48

DarkCart (21 views)
2014-10-31 21:43:57

TehJavaDev (40 views)
2014-10-27 03:28:38

TehJavaDev (31 views)
2014-10-27 03:27:51

DarkCart (44 views)
2014-10-26 19:37:11

Luminem (27 views)
2014-10-26 10:17:50

Luminem (30 views)
2014-10-26 10:14:04

theagentd (36 views)
2014-10-25 15:46:29

Longarmx (64 views)
2014-10-17 03:59:02

Norakomi (62 views)
2014-10-16 15:22:06
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!