Hello,
please can anyone post a full working example of a basic lwjgl class (drawing simple graphic,loading image, loading font)
cause every time i follow an example alone it works, but when i try to use them all i always face a problem.
thank you very much
http://www.lwjgl.org/wiki/index.php?title=Space_Invaders_Example_Game
The wiki is always there, watching over us.
i saw it, it’s kinda too complicated and hard to follow :-\
am looking for a simple single class that render textures,fonts and simple graphic
thank you
I am afraid that that is about as simple as a whole game can get. Texture loading see TextureLoader and Texture. Texture rendering see Sprite.
Frankly we could probably help you more by solving the problems you have when putting it all together since, as you said, these can be hard to follow.
my main problem was here and today i tried to apply the texture example on the game am currently working on and i faced more than one problem…
Ah - I should have recognized the Pikachu - I recon the best ways to do text rendering is to use textures (or bitmaps) so get that sorted before you do anything else.
Basically for textures:
1)Load the texture (for now just use the example in LWJGL wiki)
2)Enable textures (glEnable(GL_TEXTURE_2D)) If you do any rendering without textures you must do this in render method and disable afterwords.
In your draw / render method.
2)bind the texture (glBindTexture)
3)give texture coordinates before giving vertex coords (glTexCoord2f before glVertex2f)
4)Disable textures if you intend to do some colour only rendering later.
Example
//Imagine you texture id is texId, your square's position is (sx, sy) and its size is (sizeX, sizeY)
//This draws a single textured quad.
public void draw() {
glEnable(GL_TEXTURE_2D);
glBindTexture(texId);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(sx, sy);
glTexCoord2f(1, 0);
glVertex2f(sx+sizeX, sy);
glTexCoord2f(1, 1);
glVertex2f(sx+sizeX, sy+sizeY);
glTexCoord2f(0, 1);
glVertex2f(sx, sy+sizeY);
glEnd();
glDisable(GL_TEXTURE_2D);
}
@quew8
i think i should reorder all what i know about openGL, i didn’t know that i can enable/disable during drawing (glDisable(GL_TEXTURE_2D) i thought all that must happens in the beginning once and for all and it could never be changed,
i’ll try to implement this now and tell you if this fixed my problem, thank’s a lot
Look through the minimal lwjgl-basics library:
It renders sprites using vertex arrays and a basic sprite batcher. It uses object-oriented patterns to wrap basic OpenGL and LWJGL concepts:
Display
Texture
Shader Program
Using glVertex is deprecated and part of the old fixed-function pipeline. Shaders are much more powerful to use and not too difficult to learn. The hard part is putting all the OpenGL boilerplate together – this is why most people would suggest using LibGDX or another solution.
EDIT: For example, you could learn how shaders work with a higher-level framework (lwjgl-basics, LibGDX). Then you will have a much greater understanding of the OpenGL pipeline. When it comes time that you want to create your own sprite batch (with LWJGL or LibGDX), you will know what you are doing, rather than just copy-pasting GL calls blindly.
OpenGl is a state machine (at least so I keep getting told). You can’t do anything state changing between glBegin / glEnd but any other time is fine.
BTW @davedes +1. Shaders and vbos are the way to go but I just think one step at a time. First lets get a textured quad on the screen before we destroy everything by using vbos and shaders (I kid they are very useful and fast)
Shouldn’t have chosen lwjgl if you’re not willing to do the extra work, go low level and get your hands dirty. Copy+pasting isn’t going to get you anywhere. Don’t try to implement everything at once - do it one by one. If you try to implement everything at once there could be problems with any feature you’re trying to implement. One by one, it’s easier to find the problem.
@davedes & quew8
thank you i’ll try that
@Jimmt
the goal isn’t copy-pasting and just reusing written codes by other people, i tried the one by one example and used them in other examples by my own, but the thing is am facing a problem everytime i try to use them all in the same time which is very weird or maybe that’s how it looks to a very noob lwjgl learner like me, and thanks for the advise
Hi, this may or may not help, but a lots of topics where cover in my post as I got to grips with lwjgl. On page 3 there is a link to source code. It’s not great code, but you can take bits, or compare bits, however you want to use it. I have use bitmap fonts in there, and vbo rendering.
thank you guys, i will follow those advise, for now i want to say that my problem is solved (thanx to quew8 )
what i learned (please correct me if am wrong)
-we don’t initialize openGL once for all the game life
-each rendering may or must have it’s own initialization (enable/disable)
finally here is the code of an all-in-one example (like i requested)
again thank you all and if you found any mistake or a better way please tell me to correct it
import java.awt.Font;
import java.io.IOException;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
public class AllInOneExample {
/** The texture that will hold the image details */
private Texture texture;
/** The fonts to draw to the screen */
private TrueTypeFont font;
/**the coordinates of the drawn elements */
int x = 500, y = 100, w = 50, h = 50;
/**
* Start the example
*/
public void start() {
initGL(800, 600);
init();
while (true) {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
render();
Display.update();
Display.sync(100);
if (Display.isCloseRequested()) {
Display.destroy();
System.exit(0);
}
}
}
private void initGL(int width, int height) {
try {
Display.setDisplayMode(new DisplayMode(width, height));
Display.create();
Display.setVSyncEnabled(true);
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glViewport(0, 0, width, height);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, height, 0, 1, -1);
}
/**
* Initialise resources
*/
public void init() {
try {
// load texture from PNG file
texture = TextureLoader.getTexture("PNG",
ResourceLoader.getResourceAsStream("res/cube.png"));
} catch (IOException e) {
e.printStackTrace();
}
Font awtFont = new Font("Times New Roman", Font.BOLD, 24);
font = new TrueTypeFont(awtFont, false);
}
/**
* draw a quad with the image on it
*/
public void render() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
// draw quad
x = 10;y = 10;w = 32;h = 32;
GL11.glColor3d(250, 250, 250);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(x, y); // up left corner
GL11.glVertex2f(x + w, y);// up right corner
GL11.glVertex2f(x + w, y + h);// down right corner
GL11.glVertex2f(x, y + h);// down left corner
GL11.glEnd();
// end of Quad
// Texture
x = 50;
GL11.glEnable(GL11.GL_TEXTURE_2D);
texture.bind(); // or GL11.glBind(texture.getTextureID());
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(x, y);
GL11.glTexCoord2f(1, 0);
GL11.glVertex2f(x + texture.getTextureWidth(), y);
GL11.glTexCoord2f(1, 1);
GL11.glVertex2f(x + texture.getTextureWidth(),
y + texture.getTextureHeight());
GL11.glTexCoord2f(0, 1);
GL11.glVertex2f(x, y + texture.getTextureHeight());
GL11.glEnd();
GL11.glDisable(GL11.GL_TEXTURE_2D);
// end of texture
// Font
x = 90;
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
font.drawString(x, y, "FINALLY !! ", Color.white);
GL11.glDisable(GL11.GL_BLEND);
// end of Font
}
/**
* Main Class
*/
public static void main(String[] argv) {
AllInOneExample all = new AllInOneExample();
all.start();
}
}