Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
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]libdgx - Mesh -> Matrix math curious result  (Read 789 times)
0 Members and 1 Guest are viewing this topic.
Offline Phibedy

Senior Devvie


Medals: 9



« Posted 2013-01-04 00:18:21 »

Hi, after I am now able to create fragshaders, they don't even look that bad, thx for help Smiley

But now I have been wondering about the vertexshader for about 13 hours  Roll Eyes
I just don't get why vec4(aVertex,0,1)* uModelMatrix != vec4(aVertex,0,1)
In my opinion uModelMatrix is an identity matrix so it should work.
I expected a red field, the mesh coords are set weird because I just would like to see a red pixel.

What I get:
Nothing, absolutely nothing is rendered  Undecided

There's the code, you could compile and test it yourself if you want Smiley
+ downloadlink if you don't wanna grab the libs yourself: http://www.file-upload.net/download-7008284/ErrorShader.rar.html
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  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.math.Matrix4;

public class ErrorShader implements ApplicationListener {
    MeshHelper meshHelper;

    public static void main(String[] args) {
      LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
      cfg.title = "Error";
      cfg.useGL20 = true;
      cfg.width = 500;
      cfg.height = 500;
      cfg.resizable = false;
      new LwjglApplication(new ErrorShader(), cfg);
   }
   
   
    @Override
    public void create() {
        meshHelper = new MeshHelper();
        meshHelper.createMesh(new float[] { -100.0f, -100.0f,
                230.0f, 100.0f,
                100f, 156.0f });
        }
   
   

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void render() {
        Gdx.graphics.getGL20().glClearColor(0.2f, 0.2f, 0.2f, 1);
        Gdx.graphics.getGL20().glClear(GL10.GL_COLOR_BUFFER_BIT);
        Gdx.graphics.getGL20().glEnable(GL20.GL_BLEND);
        //Gdx.gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        meshHelper.drawMesh();
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
        meshHelper.dispose();
    }
}

class MeshHelper {
    private Mesh mesh;
    private ShaderProgram redShader;
   
    private OrthographicCamera cam;
    private Matrix4 modelMatrix;

    public MeshHelper() {
        createShader();
        modelMatrix = new Matrix4();
        cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
for(int i =0; i<  modelMatrix.getValues().length; i++){
           System.out.println("values "+ i+": " +  modelMatrix.getValues()[i]);
        }
    }

    public void createMesh(float[] vertices) {
       //modelMatrix.translate(translation)
       //modelMatrix.rotate(rotation)
       mesh = new Mesh(true, vertices.length, 0,
               new VertexAttribute(Usage.Position, 2, "aVertex"));
        mesh.setVertices(vertices);
    }

    public void drawMesh() {
        if (mesh == null)
            throw new IllegalStateException("drawMesh called before a mesh has been created.");
       
      //modelMatrix.translate(translation)
     // modelMatrix.rotate(250f,250f,0f, 0.5f);
      //  redShader.setUniformMatrix("uViewProjMatrix", cam.combined);
 //redShader.setUniformMatrix("uModelMatrix", modelMatrix); //Error call it inside glbegin/end
        redShader.begin();
redShader.setUniformMatrix("uModelMatrix", modelMatrix);
        mesh.render(redShader, GL20.GL_TRIANGLES);
        redShader.end();
    }

    private void createShader() {
       String vertexShader =
               "attribute vec2 aVertex;       \n"
             + "uniform mat4 uViewProjMatrix;       \n"
             + "uniform mat4 uModelMatrix;       \n"
                + "void main()                   \n"
                + "{                             \n"
               // + "  gl_Position = vec4(aVertex,0.0,1.0) * uModelMatrix* uViewProjMatrix;  \n"
                + "  gl_Position = vec4(aVertex,0,1)* uModelMatrix; \n"// * uViewProjMatrix;
                + "}                             \n";          
       
        String fragmentREDShader =
                "#ifdef GL_ES\n" //
                + "#define LOWP lowp\n" //
                + "precision mediump float;\n" //
                + "#else\n" //
                + "#define LOWP \n" //
                + "#endif\n" + //
                "\n" +
                "void main() {\n" +
                "   gl_FragColor = vec4(1.0,0.0,0.0, 1.0);\n" +
                "}";
        redShader = new ShaderProgram(vertexShader, fragmentREDShader);
        if (redShader.isCompiled() == false)
            throw new IllegalStateException(redShader.getLog());
    }
    public void dispose() {
        mesh.dispose();
        redShader.dispose();
    }
}


thx for help,
best regards
Offline pitbuller
« Reply #1 - Posted 2013-01-04 00:24:53 »

You should multiply those other way around.

This is right way:
1  
vec4 a = matrix * vector;


Offline Phibedy

Senior Devvie


Medals: 9



« Reply #2 - Posted 2013-01-04 00:30:17 »

1  
gl_Position = uModelMatrix*vec4(aVertex,0,1);

don't work either, it would be strange because the order souldn't have any effect on it.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline HeroesGraveDev

JGO Kernel


Medals: 325
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #3 - Posted 2013-01-04 00:47:36 »

1  
gl_Position = uModelMatrix*vec4(aVertex,0,1);

don't work either, it would be strange because the order souldn't have any effect on it.

In matrix math, the order does effect the outcome.

Look through these.

Offline Phibedy

Senior Devvie


Medals: 9



« Reply #4 - Posted 2013-01-04 00:49:38 »

thx, but one of these is an identity matrix so it shouldn't have any effect.
Offline Phibedy

Senior Devvie


Medals: 9



« Reply #5 - Posted 2013-01-04 02:14:16 »

Got the mistake, thx to bach in the libgdx irc.
I will edit my post, as soon as I wake up.
best regards and good night  Grin
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.

CopyableCougar4 (16 views)
2014-12-28 02:10:29

BurntPizza (17 views)
2014-12-27 22:38:51

Mr.CodeIt (13 views)
2014-12-27 04:03:04

TheDudeFromCI (17 views)
2014-12-27 02:14:49

Mr.CodeIt (25 views)
2014-12-23 03:34:11

rwatson462 (56 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (92 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (86 views)
2014-12-05 12:39:02
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

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
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!