Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (575)
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  
  Bare-Bones Intro to Textures & Programmable Pipeline  (Read 3623 times)
0 Members and 1 Guest are viewing this topic.
Offline davedes
« Posted 2012-11-05 02:46:09 »

UPDATE:
The code is undergoing changes. It's now becoming more of a utility library (the likes of SlickUtil, but cleaner and more modernized) rather than a bare-bones set of minimal code examples. Eventually I hope to turn it into a third party utility library for LWJGL 3.0.

The original minimal examples will be hosted on the Wiki. Small guides or tutorials may be included there, as well:
https://github.com/mattdesl/lwjgl-basics/wiki

--

There are plenty of tutorials and guides on the web, but not a lot of "complete source examples" that demonstrate the bare essentials of a modern GL app in Java (i.e. no fixed function). This isn't meant to be a tutorial/guide (at least not yet), but rather a source code reference for those trying to build their own 2D shader-based engines. It covers only the basics to get you started:
  • Basic LWJGL display creation
  • A simple Texture utility class to decode PNG files and load them into OpenGL textures
  • A simple ShaderProgram utility class to load and bind shaders (inspired by ra4king's code)
  • A simple abstraction of "vertex data" -- aka for Vertex Arrays, Vertex Buffer Objects, etc. (So far only vertex arrays has been implemented.)
  • Utilities for creating an orthographic 2D projection matrix (origin top-left)
  • A simple SpriteBatch, demonstrating how to put it all together to draw potentially thousands of sprites efficiently
--

Source: https://github.com/mattdesl/lwjgl-basics (BSD license)

It's an early work in progress -- I hope to add more documentation and more examples, e.g. geometry shaders.

For now it uses VertexArrays (which are sometimes faster than VBOs). At some point I hope to add VBOs.

I didn't use GL3+ code or context since I'm running a 2.1 machine (as are many other Mac users). So while it may not truly be "modern GL," it does not use any fixed-function pipeline like glVertex2f or glTranslatef, and the concepts are more or less the same. Since we are using GLSL 120, attribute locations need to be passed to ShaderProgram, rather than defined in GLSL.

Cheers. Smiley Let me know if there are features/extensions you'd like added...

[size=5pt]P.S. Yes, you can probably tell the package structure was inspired by LibGDX...[/size]

Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-11-05 03:41:39 »

Good luck!

Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #2 - Posted 2012-11-06 17:29:31 »

Very good library.

Just some utilities.
I don't like to stick to engines like Slick2D or libGDX, I like to pick the utilities I want to have. That's why I created a Utils project, and didn't gave a complete engine for Noise and other stuff to people.

That's just what I needed, and it works fast, is just the "bare bones" I needed and solved a problem I had for months.

Good work and thank you davedes! Smiley

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #3 - Posted 2012-11-06 18:00:21 »

Thanks. I may turn it into a "mini utils" package including TextureRegion, BitmapFont, and Color.. i.e. to completely replace SlickUtil as the "standard LWJGL starter kit." Although maybe that would be too much bloat...  Yawn

Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #4 - Posted 2012-11-07 14:49:58 »

Here is a feature request, it may sound somehow abstruse, but yeah:

I'd like to have a non-textured "mode" in the SpriteBatch, which should be somehow not so easy... propably... Because if you would want to do it "right" you'd need to switch to a shader, which doesn't does a texture lookup...

And secondly, which is very easy (and I have already implemented it) I would like to be able to tint different edges.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline theagentd
« Reply #5 - Posted 2012-11-07 14:59:55 »

Here is a feature request, it may sound somehow abstruse, but yeah:

I'd like to have a non-textured "mode" in the SpriteBatch, which should be somehow not so easy... propably... Because if you would want to do it "right" you'd need to switch to a shader, which doesn't does a texture lookup...

And secondly, which is very easy (and I have already implemented it) I would like to be able to tint different edges.
Here is a feature request, it may sound somehow abstruse, but yeah:

I'd like to have a non-textured "mode" in the SpriteBatch, which should be somehow not so easy... propably... Because if you would want to do it "right" you'd need to switch to a shader, which doesn't does a texture lookup...

And secondly, which is very easy (and I have already implemented it) I would like to be able to tint different edges.
Just add a white texture and sample it. It'll just be multiplied with the color, which is just 1*color = color. The edge tint would be more difficult to implement I think.

Myomyomyo.
Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #6 - Posted 2012-11-07 15:01:55 »

Here is a feature request, it may sound somehow abstruse, but yeah:

I'd like to have a non-textured "mode" in the SpriteBatch, which should be somehow not so easy... propably... Because if you would want to do it "right" you'd need to switch to a shader, which doesn't does a texture lookup...

And secondly, which is very easy (and I have already implemented it) I would like to be able to tint different edges.
Just add a white texture and sample it. It'll just be multiplied with the color, which is just 1*color = color. The edge tint would be more difficult to implement I think.
Edge tint is easy, as you only need to change the attribute for each vertex.

But yeah, that's what I was talking about, when I said with "if you want to do it "right""...
Texturelookups are costly...

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline theagentd
« Reply #7 - Posted 2012-11-07 15:10:25 »

Not if you're sampling the same place over and over again. There's a texture cache on the GPU making the texture read no more expensive than accessing the color variable. And let's face it, you're going to be CPU limited anyway, so what you win in CPU performance by batching is a lot more valuable than what you lose in GPU performance.

Myomyomyo.
Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #8 - Posted 2012-11-07 15:27:34 »

Not if you're sampling the same place over and over again. There's a texture cache on the GPU making the texture read no more expensive than accessing the color variable. And let's face it, you're going to be CPU limited anyway, so what you win in CPU performance by batching is a lot more valuable than what you lose in GPU performance.

yeah, right... premature optimization Wink

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Danny02
« Reply #9 - Posted 2012-11-07 15:39:51 »

...] not so easy [...] Because [...] you'd need [...] a shader [...

Come on guys shaders are really really easy to use Smiley
I know they are some sort of black box in the beginning, one always hear about the magically stuff they do, but have no clue how.
Just read some mini tutorials and you are writing your first shader within a hour.
Take a look at some beginner webgl tutorials, because webgl forces you to use shaders.

here the requested shader, which results in white triangles
1  
2  
3  
4  
void main()
{
   gl_FragColor = vec4(1.);
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #10 - Posted 2012-11-07 15:41:27 »

Medal because of this awesome quote Wink

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline davedes
« Reply #11 - Posted 2012-11-07 17:33:58 »

For plain or "non-textured" rectangles you should use a texture region (aka "sprite" in your "sprite sheet") that is a 1x1 white pixel. That way your rectangles can be pushed into the same batch as the rest of your sprites, without needing to compile a new shader or create a new OpenGL texture. Smiley

Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #12 - Posted 2012-11-09 18:35:19 »

I've changed the spritebatch to include a View matrix. This works perfectly, and was tested in the VertexArrayExample.java class.

Propably you don't want it, but if you do, how do I "submit" my change?

EDIT: Also, the View matrix is optional, so the user doesn't have to fiddle around with it.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline davedes
« Reply #13 - Posted 2012-11-09 20:03:45 »

You can send pull requests which I will then accept or reject. Smiley

As the project grows, I will probably include a means of setting and getting projection and view matrices for the sprite batch. And, perhaps, some utility methods like translate/scale/rotate to easily affect the view matrix.

One issue is that it requires flushing the batch and sending a 4x4 matrix to the shader. This is why for individual sprite rotation it's often better to perform the rotation on the CPU, and send the transformed positions to GL. Ideally you should maximize batching and minimize glUniform calls.

Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #14 - Posted 2012-11-09 20:06:27 »

One issue is that it requires flushing the batch and sending a 4x4 matrix to the shader. This is why for individual sprite rotation it's often better to perform the rotation on the CPU, and send the transformed positions to GL. Ideally you should maximize batching and minimize glUniform calls.

You're right, but I'm talking about a view Matrix. Which defines how the user looks at the world. Something like a game-viewport, a camera, which you can rotate and move and things like that.

And the camera (or actually view-) matrix is only set once, since you don't want to render some objects with different views.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #15 - Posted 2012-11-09 20:14:08 »

Okey... Propably I need a second git course... or a better, a complete one...

Sending you a PM.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #16 - Posted 2012-11-09 20:58:25 »

Nevermind, got it working:
https://github.com/mattdesl/lwjgl-basics/pull/1

I would have never thought I'd manage to do that alone Tongue

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline davedes
« Reply #17 - Posted 2012-11-09 21:51:42 »

One issue is that it requires flushing the batch and sending a 4x4 matrix to the shader. This is why for individual sprite rotation it's often better to perform the rotation on the CPU, and send the transformed positions to GL. Ideally you should maximize batching and minimize glUniform calls.

You're right, but I'm talking about a view Matrix. Which defines how the user looks at the world. Something like a game-viewport, a camera, which you can rotate and move and things like that.

And the camera (or actually view-) matrix is only set once, since you don't want to render some objects with different views.
IMO a better approach would be to combine the view and projection matrices on the CPU, thus only requiring us to store/send a single 4x4 matrix. The uniform would be called "projTrans" or "projView" or something.

I don't see an advantage of having an extra uniform and multiplying the matrices for every vertex, unless that is specifically required (in which case you should be extending SpriteBatch with custom features).

Thanks for the pull request, though. Smiley I'll accept it for now and tweak the SpriteBatch a bit more over the weekend.

Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #18 - Posted 2012-11-09 21:53:56 »

One issue is that it requires flushing the batch and sending a 4x4 matrix to the shader. This is why for individual sprite rotation it's often better to perform the rotation on the CPU, and send the transformed positions to GL. Ideally you should maximize batching and minimize glUniform calls.

You're right, but I'm talking about a view Matrix. Which defines how the user looks at the world. Something like a game-viewport, a camera, which you can rotate and move and things like that.

And the camera (or actually view-) matrix is only set once, since you don't want to render some objects with different views.
IMO a better approach would be to combine the view and projection matrices on the CPU, thus only requiring us to store/send a single 4x4 matrix. The uniform would be called "projTrans" or "projView" or something.

I don't see an advantage of having an extra uniform and multiplying the matrices for every vertex, unless that is specifically required (in which case you should be extending SpriteBatch with custom features).

Thanks for the pull request, though. Smiley I'll accept it for now and tweak the SpriteBatch a bit more over the weekend.

Oh wonderful Smiley
Good suggestions, btw... I think I'll try this idea out myself too.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Sickan

Senior Duke


Medals: 9



« Reply #19 - Posted 2012-11-10 21:31:21 »

I have to say, not many projects I see on Github can boast that their latest changes were made 32 minutes ago.

Cheers, and good job! Cheesy
Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #20 - Posted 2012-11-10 21:33:57 »

I have to say, not many projects I see on Github can boast that their latest changes were made 32 minutes ago.

Cheers, and good job! Cheesy

heh... that is because the nice davedes just merged in my Pull request, which adds the new projViewMatrix to the SpriteBatch.

This means the view and projection matrix are premultiplied on the cpu and uploaded as one matrix to the gpu.

More changes were made, as you can see, if you click somewhere here...

So, basically this was by accident Smiley

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.

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

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

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

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

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

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

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

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

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

BurntPizza (74 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!