LWJGL Forum

Programming => LWJGL Documentation => Topic started by: imnotanerd on December 12, 2010, 05:20:17

Title: Beginners Tutorials
Post by: imnotanerd on December 12, 2010, 05:20:17
I am concerned about the lack of tutorials for beginners to LWJGL. Sure you could use the demo's source code for inspiration, but to me, that's just spitting out code for you to understand. And i don't. Sure, i may know Java, but the API calls i don't. Are there plans to make tutorials from beginners to advanced for newcomers to LWJGL?
Title: Re: Beginners Tutorials
Post by: jediTofu on December 12, 2010, 06:01:43
http://lwjgl.org/wiki/index.php?title=Main_Page

The home page just needs to be updated and more tutorials are being worked on.
There will be more LWJGL-specific tutorials soon.
Title: Re: Beginners Tutorials
Post by: mstWeal on December 21, 2010, 20:04:27
What are the things missing that you would want a tutorial for? Of course, more tutorials would always be better but I think there are enough to get you started. You cannot expect a complete introduction into the OpenGL API but there are good books. Specifically good is the red book, also available online http://fly.cc.fer.hr/~unreal/theredbook/ .. or the OpenGL Suberbible.
Title: Re: Beginners Tutorials
Post by: CodeBunny on January 08, 2011, 20:59:14
One thing I would find very helpful would be a FBO tutorial. I've been able to find tutorials for creating one in OpenGL, but I haven't found any such thing for the LWJGL API.
Title: Re: Beginners Tutorials
Post by: kappa on January 08, 2011, 21:56:57
One thing I would find very helpful would be a FBO tutorial. I've been able to find tutorials for creating one in OpenGL, but I haven't found any such thing for the LWJGL API.

added to the wiki just for you http://lwjgl.org/wiki/index.php?title=Using_Frame_Buffer_Objects_(FBO) (http://lwjgl.org/wiki/index.php?title=Using_Frame_Buffer_Objects_(FBO)) :)

The tutorial is now a little old and is actually from the old lwjgl wiki that was lost when the server crashed. FBO's are no longer new (as stated in the tutorial) and are now available as part of standard OpenGL (so available as both extension and GL30). The tutorial should lay down the basics so pretty easy to switch between extension version and OpenGL version.

Feel free to update the tutorial and fix it as required.
Title: Re: Beginners Tutorials
Post by: CodeBunny on January 08, 2011, 21:58:01
Sweet, thank you very much. :)
Title: Re: Beginners Tutorials
Post by: kappa on January 11, 2011, 10:35:51
Resurrected the last useful and salvageable bits from the old wiki, the two VBO tutorials, now amalgamated into one VBO tutorial.

http://lwjgl.org/wiki/index.php?title=Using_Vertex_Buffer_Objects_(VBO) (http://lwjgl.org/wiki/index.php?title=Using_Frame_Buffer_Objects_(FBO))

Again slightly out of date (as VBO's are now also part of core opengl) but useful none the less, so if you use it, please free to update it.
Title: Re: Beginners Tutorials
Post by: CodeBunny on January 15, 2011, 17:10:24
So I've gotten a FBO working, and can render to textures I've loaded on command. However, the results of rendering to the textures are skewed and are improperly sized. I think it's because of

Code: [Select]
GL11.glPushAttrib(GL11.GL_VIEWPORT_BIT);
GL11.glViewport(0, 0, texture.getImageWidth(), texture.getImageHeight());

getImage(Height/Width) returns the dimension of the image placed on a particular texture (via slick-util).

I then draw other textures/shapes on it similarly to how the FBO tutorial illustrates.

What's going on? My game is 2D and in orthogonal mode, is this somehow interfering?
Title: Re: Beginners Tutorials
Post by: avm1979 on January 15, 2011, 21:09:41
The same orthogonal transformation you've already specified is determining how the stuff gets drawn to the FBO.  Since your new viewport has a different size/aspect ratio than the previous one, stuff gets stretched/scaled.

You'll want to set a new ortho transform, probably something like

Code: [Select]
GL11.glPushMatrix();
GL11.glOrtho(0, texture.getImageWidth(), 0, texture.getImageHeight(), -1000, 1000);
... draw stuff ...
GL11.glPopMatrix();
Title: Re: Beginners Tutorials
Post by: CodeBunny on January 15, 2011, 22:52:48
I see what you mean, but I haven't been able to make glOrtho work...

Here's my code:

Code: [Select]
public static boolean beginDrawingTo(Texture texture)
{
if(GLContext.getCapabilities().GL_EXT_framebuffer_object)
{
IntBuffer buffer = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
EXTFramebufferObject.glGenFramebuffersEXT(buffer);
frameBuffersActive.push(buffer.get());

EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, frameBuffersActive.peek());
EXTFramebufferObject.glFramebufferTexture2DEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT, GL11.GL_TEXTURE_2D, texture.getTextureID(), 0);

checkFBO(frameBuffersActive.peek()); // Allows interrupting one render to texture with another, when that one is done it returns to the previous.


// This is what has been giving me trouble.
GL11.glPushAttrib(GL11.GL_VIEWPORT_BIT);
GL11.glViewport(0, 0, texture.getImageWidth(), texture.getImageHeight());

GL11.glTranslatef(texture.getImageWidth() / 2, texture.getImageHeight() / 2, 0); //Draw from center of image

return true;
}
return false;
}

Where should I put the glOrtho call and what should I set it to?
Title: Re: Beginners Tutorials
Post by: avm1979 on January 16, 2011, 15:23:17
Quote
Where should I put the glOrtho call and what should I set it to?

Set it to what I put in the previous post (i.e., the size of the FBO).  Doesn't exactly matter where you put it, as long as you set it before rendering to the FBO, and glPopMatrix() after.  After your glViewport call is as good a place as any.
Title: Re: Beginners Tutorials
Post by: CodeBunny on January 16, 2011, 20:14:02
Okay, everything is working now. Thanks!



However, I do have one remaining question. Basically, when I begin drawing to a texture, the starting point for the draw commands is offset in the Y direction, always, and by a predictable amount.

This is my code to start rendering to a texture:

Code: [Select]
public static boolean beginDrawingTo(Texture texture)
{
if(renderToTextureEnabled())
{
IntBuffer buffer = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
EXTFramebufferObject.glGenFramebuffersEXT(buffer);
frameBuffersActive.push(buffer.get());

EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
frameBuffersActive.peek());
EXTFramebufferObject.glFramebufferTexture2DEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT, GL11.GL_TEXTURE_2D, texture.getTextureID(), 0);

checkFBO(frameBuffersActive.peek());

GL11.glPushMatrix();
GL11.glOrtho(0, texture.getImageWidth(), 0,-texture.getImageHeight(),  -1, 1);

GL11.glLoadIdentity();
GL11.glTranslated(texture.getImageWidth() / 2, texture.getImageHeight() / 2, 0);
GL11.glScalef(1f, -1f, 1f);

return true;
}
return false;
}

If I place
Code: [Select]
GL11.glTranslated(0, WindowManager.getScreenHeight() - texture.getImageHeight(), 0);right after glLoadIdentity(), everything works.

Why is this the case? I'm happy it's working, but a little confused.
Title: Re: Beginners Tutorials
Post by: avm1979 on January 16, 2011, 20:24:20
You're welcome, glad you got it working.

The reason for the behavior you're seeing is, in your glOrtho call, you're passing in a negative value for the height.  That causes it to flip the direction of the y-axis, and makes 0,0 the upper left corner of the viewport.  But glViewport's x,y parameters are the lower left corner of the viewport.

Then you're doing GL11.glScalef(1f, -1f, 1f) to flip the y-axis back to normal.

I'd suggest you remove the glScalef call and make the height param to glOrtho positive - that should fix it, unless something else is going on on top of this.
Title: Re: Beginners Tutorials
Post by: CodeBunny on January 16, 2011, 20:44:37
Well, I'm making a 2D game engine, and decided that the spatial coordinates should be similar to Java2D - y+ is down the screen. This render-to-texture code is meant to be an option for a developer, where they can use the in-place rendering methods to paint to a texture.

I guess I can leave it as it is, since it's simply a scaling difference and adjusting it would either make the API more awkward or require some heavy-duty restructuring.

Thank you for all the help! :D
Title: Re: Beginners Tutorials
Post by: avm1979 on January 16, 2011, 20:51:36
Ah, gotcha.  I guess it depends on how much code you have written, but if it's not *too* much, I'd recommend switching to the standard GL way.  I started out along the same lines (flipping the y-axis, 0,0 top left) and eventually it just got to be too painful as things got more complex and I wanted to take advantage of more GL features.  Haven't looked back since.

Best of luck to you!
Title: Re: Beginners Tutorials
Post by: CodeBunny on January 18, 2011, 11:51:20
My engine is now in excess of 60 classes, along with a few minor games written in it to test functionality. :P