Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  [GLSL] Some Questions regarding matrices  (Read 366 times)
0 Members and 1 Guest are viewing this topic.
Offline Longor1996
« Posted 2013-05-29 17:30:34 »

Hello everyone.

I have two (probably very noob-ish) questions.
1. Can I change the content of a uniform multiple times without side-effects? Like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
myShader.bind();

myShader.changeUniformMat4f(someUniformPointer,object0.objMat);
object0.draw();

myShader.changeUniformMat4f(someUniformPointer,object1.objMat);
object1.draw();

myShader.changeUniformMat4f(someUniformPointer,object2.objMat);
object2.draw();

myShader.unbind();


2. I don't understand matrix-multiplication very well, so, is this GLSL code correct?
1  
2  
3  
4  
5  
6  
7  
8  
9  
void main(void)
{
    // cameraMatrix, is the matrix of my camera, containing position and rotation.
   // myObjectMatrix, is the matrix of a object in my world. It has a position and rotation.
   gl_Position = gl_ProjectionMatrix * cameraMatrix * myObjectMatrix;
   
    gl_TexCoord[0] = gl_MultiTexCoord0;
    gl_FrontColor = gl_Color;
}


I hope I explained that well and understandable.
My english isn't the best.

Please tell me about every mistake in the code and shader.
I wan't to use this for my main-game project, so it has to be as bug-free as possible.

- Longor1996

Sorry for my bad English! That's because i am from Germany.
Offline ags1

JGO Ninja


Medals: 62
Projects: 3
Exp: 5 years


Make code not war!


« Reply #1 - Posted 2013-05-29 19:24:02 »

Just adding myself to the thread as I have plenty to learn about GLSL too, especially matrices.

Offline pitbuller
« Reply #2 - Posted 2013-05-29 20:18:38 »

Matrix * matrix give you a matrix. So when you try to store matrix to gl_Position you are causing big error.

This work right.
1  
 gl_Position = projection * view * model * a_position;


But you don't want to do 3 matrix * matrix operation per vertex so you could change that code to

1  
 gl_Position = projection * (view * (model * a_position));


But then again why to do any unneeded work just combine those at cpu once and send that to shader. Less data, less registers, less math.

1  
 gl_Position = modelViewProj * a_position;


Notice how I changed some names to match better with common usage. Also notice that modelViewProj is same as (projection * view * model) naming convetions related to matrix math is reason for that.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Longor1996
« Reply #3 - Posted 2013-05-29 20:46:46 »

Okay, I think I got it now!

Render Method:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
void renderScene(Matrix4f modelViewProjectionMatrix){
    FloatBuffer matrixBuffer = BufferUtils.createFloatBuffer(16);
    Matrix4f modelViewProjectionObjectMatrix = new Matrix4f();
   
    for(GameObject gameObject : gameObjects)
    {
        Matrix4f.mul(modelViewProjectionMatrix, gameObject.matrix, modelViewProjectionObjectMatrix);
       
        // Transform the Matrix4f into FloatBuffer
       buf.clear();
        modelViewProjectionObjectMatrix.store(matrixBuffer);
        buf.flip();
       
        // Send the matrix to OpenGL
       GL20.glUniformMatrix4(modelViewProjObjMatrixLocation, false, matrixBuffer);
       
        // Render the model
       gameObject.render();
    }
}


Vertex-Shader:
1  
2  
3  
4  
5  
6  
7  
8  
uniform mat4 modelViewProjObjMatrix;

void main(void)
{
  gl_Position = modelViewProjObjMatrix * gl_Vertex;
  gl_TexCoord[0] = gl_MultiTexCoord0;
  gl_FrontColor = gl_Color;
}


Is that correct?


Edit:
And new question instantly popped into my head:
Would it be even better to run the calculations for the matrices using OpenCL?

- Longor1996

Sorry for my bad English! That's because i am from Germany.
Offline matheus23

JGO Kernel


Medals: 108
Projects: 3


You think about my Avatar right now!


« Reply #4 - Posted 2013-05-30 08:15:08 »

[just throwing in...: Changing variables without side-effect? Haha... Cheesy ]

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
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.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (41 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!