Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
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  
  LWJGL - Pass value to "in" arguement on shader  (Read 599 times)
0 Members and 1 Guest are viewing this topic.
Offline Gremolin

Senior Newbie





« Posted 2014-04-29 21:56:44 »

I have create a triangle using a shader which successfully renders on a green background.
(This is a code example from the superbible 6 book)

There is a value on the shader which is suppose to make the triangle move.
But I am unable to assign a floatbuffer to that value.

Here is the shader.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
#version 430 core
// "offset" is an input vertex attribute
layout (location = 0) in vec4 offset;
void main(void)
{
const vec4 vertices[3] = vec4[3](vec4( 0.25, -0.25, 0.5, 1.0),
vec4(-0.25, -0.25, 0.5, 1.0),
vec4( 0.25, 0.25, 0.5, 1.0));
// Add "offset" to our hard-coded vertex position
gl_Position = vertices[gl_VertexID] + offset;
}


And here is my (failed) attempt at assigning a value to that shader.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
         FloatBuffer attrib = BufferUtils.createFloatBuffer(4);
         float[] move = new float[] { //
         (float) Math.sin(getCurrentTime()) * 0.5f, //
               (float) Math.cos(getCurrentTime()) * 0.6f, 0.0f,//
               0.0f };
         attrib.put(move);
         attrib.flip();
         GameUtils.exitOnGLError("set vertex");

         // Update the value of input attribute 0
         System.out.println(attrib.get(0));
         GL20.glVertexAttrib4f(0, attrib.get(0) * 100, attrib.get(1), 0.0f, 0.0f);

         GameUtils.exitOnGLError("set shader variable");

         // Draw triangle
         GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 3);


Here is the code to the whole object.
http://pastebin.java-gaming.org/360f7340b97
Offline SHC
« Reply #1 - Posted 2014-04-30 15:12:07 »

The better way to set offsets is to use uniforms. In the shader source, you use

1  
uniform vec4 offset;

Then you have to set the offset from the Java code, like this.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
float[] offset = new float[]
{
    (float) Math.sin(getCurrentTime()) * 0.5f,
    (float) Math.cos(getCurrentTime()) * 0.6f,
    0.0f,
    0.0f
};

int location = GL20.glGetUniformLocation(shaderProgramID, "offset");
GL20.glUniform4f(location, offset[0], offset[1], offset[2], offset[3]);

And they will be updated in the shaders. Hope this helps.

Online Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2014-04-30 15:17:03 »

What's this fascination with stuffing values in arrays/buffers, only to extract them again prior to passing them to OpenGL ?


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
+        float a = (float) Math.sin(getCurrentTime()) * 0.5f;
+        float b = (float) Math.cos(getCurrentTime()) * 0.6f;
-        FloatBuffer attrib = BufferUtils.createFloatBuffer(4);
-        float[] move = new float[] {
-        (float) Math.sin(getCurrentTime()) * 0.5f,
-              (float) Math.cos(getCurrentTime()) * 0.6f, 0.0f,
-              0.0f };
-        attrib.put(move);
-        attrib.flip();
         GameUtils.exitOnGLError("set vertex");

         // Update the value of input attribute 0

-        System.out.println(attrib.get(0));
-        GL20.glVertexAttrib4f(0, attrib.get(0) * 100, attrib.get(1), 0.0f, 0.0f);
+        GL20.glVertexAttrib4f(0, a * 100, b, 0.0f, 0.0f);

         GameUtils.exitOnGLError("set shader variable");

         // Draw triangle
         GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 3);


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
+        float a = (float) Math.sin(getCurrentTime()) * 0.5f;
+        float b = (float) Math.cos(getCurrentTime()) * 0.6f;
-        float[] offset = new float[]
-        {
-            (float) Math.sin(getCurrentTime()) * 0.5f,
-            (float) Math.cos(getCurrentTime()) * 0.6f,
-            0.0f,
-            0.0f
-        };

         int location = GL20.glGetUniformLocation(shaderProgramID, "offset");
-        GL20.glUniform4f(location, offset[0], offset[1], offset[2], offset[3]);
+        GL20.glUniform4f(location, a, b, 0, 0);


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 SHC
« Reply #3 - Posted 2014-04-30 15:20:11 »

(points up)  Shocked A great optimisation indeed. Thanks Riven for showing this.

Online Slyth2727
« Reply #4 - Posted 2014-04-30 20:59:13 »

About the uniforms @SHC, I was under the impression that they're from an older version, and that in's and out's are more up to date. Someone correct me if I'm wrong.

Edit: 500th post.

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Online Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2014-04-30 21:07:39 »

Do not confuse vertex attributes with uniforms. They are completely different things.

A uniform is like a global variable (not a constant).
A vertex attribute is a variable specified for each vertex.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Gremolin

Senior Newbie





« Reply #6 - Posted 2014-05-03 13:23:29 »

Thanks for the replies.

I managed to get pass the data to the uniform variable on the shader just fine although the movement isn't very smooth.


As for the original method I still wasn't able to get the triangle to move at all, even with directly putting the float values in.
Is their a line of code to enable 'layout' variables.

I did have a little success creating a buffer before passing it to the shader.
However, this only moved one vertex of the triangle

So I create a buffer with floats containing zeros in 'setup'
1  
2  
3  
4  
      // create vertex buffer object (VBO) for vertices
      positionBufferHandle = GL15.glGenBuffers();
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, positionBufferHandle);
      GL15.glBufferData(GL15.GL_ARRAY_BUFFER, positionBuffer, GL15.GL_STATIC_DRAW);


Then in the render loop I create a new floatbuffer with the vertex offsets.
Rebind, reEnable

1  
2  
3  
4  
5  
6  
         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, positionBufferHandle);
         GL15.glBufferData(GL15.GL_ARRAY_BUFFER, attrib, GL15.GL_STATIC_DRAW);

         // assign vertex VBO to slot 0 of VAO
         GL20.glEnableVertexAttribArray(0);
         GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);




I've update the code in the original link
http://pastebin.java-gaming.org/1c8e833189a

(If anyone is interested in the working 'uniform' version I can paste that somewhere)
Offline SHC
« Reply #7 - Posted 2014-05-03 13:37:42 »

You are not using a uniform Gremolin. You are creating another VBO every frame just for offsets, which you should not be doing.

Offline Gremolin

Senior Newbie





« Reply #8 - Posted 2014-05-03 13:59:26 »

I have two different versions of this code now.
One using the uniform which works fine, and I was attempted to get the above version working using a layout variable. (although I have just got one vertex moving.

I couldnt get the method GL20.glVertexAttrib4f(...) to work at all so I attempted using GL20.glVertexAttribPointer(...) but that would require the bufferData to be set on every render loop. Which it appears is not a good way to do things judging by your post?

Offline SHC
« Reply #9 - Posted 2014-05-03 14:04:06 »

The function
glBufferData()
recreates the datastore of the buffer object. Calling it in every frame causes recreation of the datastore every time it was called, which results in a performance loss. Instead, you can use the
glBufferSubData()
function which just replaces the data without recreating it.

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

Senior Newbie





« Reply #10 - Posted 2014-05-03 21:05:40 »

Thanks, the glBufferSubData method worked.

Only one vertices is moving on the triangle still, so I still got to figure that out.
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

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

Norakomi (43 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (66 views)
2014-10-14 00:35:47

TehJavaDev (59 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
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!