Friday, August 11, 2017

LWJGL 3: Texture to GPU and back

Time Flies...

It's supposed to end with "when you're having fun" but that and a lot of other cliche sayings have all been in question lately. For one thing who ever said that "slept like a baby" was actually supposed to be mean waking up every two hours to feed and then fussing because you do not want to close your eyes. And since I'm a programmer I have fun when I am getting things done and there is time left over in the day. Not when I only get to sit down and do things for five minutes right before bed. And then you look down at the date on your computer and see that more than a month has gone by since your last post. Ridiculous!

What was accomplished?

So there is some small bit of progress... The hard part of actually doing things the way I wanted to do them... see I wanted to combine a bunch of textures as texture classes after they had been converted from BufferedImages and their contents transferred to the GPU. But how to do that was not entirely straight forward, or so I thought. 

See the BufferedImages were being loaded and in order to not be bothered with hashing out different methods of copying the images to the GPU based on different ColorFormats I simply rendered the image to a new BufferedImage whose format is ARGB but in OpenGL the complimentary image format is RGBA so yeah simple one would say just use the rotate assembly instruction and we are all set. ***BUZZER SOUND*** WRONG!!!! you cannot do that in Java. Thanks a bunch Jawa.  

So after fiddling and fiddling with it because I wanted to pull out all the pixels into an array instead of accessing them one by one through the BufferedImage object I present you with my silly idea of progress.

And then...

So once I got that to work properly and could stop fiddling with bits in Java which is not a fun process. I was able to generate a single texture from multiples and replace those multiples with SubTextures in the TextureManager. That all works now and that enabled me to externalize all my BlockTypes into a single json file which enables an editor that I will work on to add new blocks this is huge because editors can make adding content 10 times faster than editing a growing json file and 100 times faster and less error prone than adding new blocktypes in code.


And here just to make this post complete is how the textures are loaded from an image in LWJGL 3.

And there you have it now on to bigger and better things. Like I also added movement in 4 directions WASD and fixed a bunch of odd things that were incorrect about displaying the textures and generated a Font texture and a number of other things but ya know other topics for another day. TTFN

Thursday, July 6, 2017

Playing with LWJGL 3

Recap & Update

I have been very busy lately. Got a new job, moved, had a kid, ya know the usual life stuff. I must admit blogging has never been something I have been good at. As is evident by my infrequent posting and sporadic change in directions. I have ultimately discovered that my personal projects that I talk about here, work on for a little while and then abandon are my method of learning. I am not sure whether this is indicative of us as human beings or if its just me. I can never stick with any of my "fun" side projects very long. Perhaps I have not put enough effort in or none have reached a good enough point to be released creating a sense of responsibility to continue working on it.

It is probably one of the reasons some of my more useful programs stay unreleased since I do not want to have to support them and can just use them for the purpose they were created for. For instance I have made at least 3 different tilemap makers, and a small image editor that I use often. Perhaps another reason is that most of them were made with Java Swing and those apps are not particularly fun to modify after a while or pleasing to utilize.

now on to the goodies...

LWJGL 3 and Vertex Arrays

So navigating the options available to different opengl versions is almost nightmareish. So the basics are pretty simple for working with these arrays. You gen the array glGenBuffers you bind the gen'd buffer glBindBuffer. Then you fill the buffer with your data, from using JOGL for this and switching to LWJGL I must say having only to send a FloatBuffer object to the glBufferData call is kind of nice. The included screenshot is rendered from a single vertex array with a single texture array for the 3d geometry. As you can see by the render quads though that is the texture used to render the individual blocks. Yes omg another block game, whatever man its a good way to expirement and learn so sue me if I generated a 100x10x100 block world and culled 76832 blocks that are not open to the sky or surrounded by others leaving 23168 blocks to generate a buffer of 10008576 floats. 

For the geometry I am using 36 vertices per cube (108 floats) and rendering without culling (omg I know, but hey I was debugging). I am using a glsl shader with a simple projection * view * model matrix multiplication to transform the three dimensional coordinates to the camera 2D plane. Currently I am using the nice JOML library for the projection but quaternions for the camera.

Triangle Strips

Now as for the 4 quads...ooops the heading gave it away. They are rendered as triangle strips in a single vertex/texture and index buffer. In order to get them to be rendered as triangle strips I used GL_PRIMITIVE_RESTART setting the restart index glPrimitiveRestartIndex. I had to use 0xFF to set the short constant in java as well as enable the behavior using glEnable(GL_PRIMITIVE_RESTART). There are a lot of optimizations to be made here.

Next I want to get up a decent FPS counter to track the effectiveness of the optimizations. Also put in a number of performance tracking logs.

Thats all for now...