From immediate mode to VBO mode LWJGL (basic)

OK, I have this code in immediate mode… and i wish to convert it to vbo mode.
I tried lots of times and it doesnt seem to work -
Here is the code in immediate mode :

 package com.game.base;

    import static org.lwjgl.opengl.GL11.*;
    import static org.lwjgl.opengl.GL15.*;
    import static org.lwjgl.util.glu.GLU.gluPerspective;

    import java.nio.FloatBuffer;
    import java.util.Random;

    import org.lwjgl.BufferUtils;
    import org.lwjgl.input.Keyboard;

    import com.game.display.graphics.Entity;

    public class Game {

	float speed = 0.0f;
	float speedChange = 0.000001f;
	int stars = 20000;
	int starsDistance = 4000;
	float starSize = 2.0f;
	int vertices = 3;
	int vertex_size = 3; // X, Y, Z,
	int color_size = 3; // R, G, B,
	Entity[] points;
	Random random = new Random();

	public Game() {
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluPerspective((float) 60, (float) (MainClass.WIDTH / MainClass.HEIGHT), 0.001f, 100);
		glMatrixMode(GL_MODELVIEW);

		glEnable(GL_DEPTH_TEST);
		
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

		points = new Entity[stars];
		for (int i = 0; i < points.length; i++) {
			points[i] = new Entity((random.nextFloat() - 0.5f) * 100f, (random.nextFloat() - 0.5f) * 100f, random.nextInt(starsDistance) - starsDistance);
		}
	}

	public void render() {
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		glTranslatef(0, 0, speed);

		glPointSize(2.0f);
		glBegin(GL_POINTS);
		for (Entity p : points) {
			glVertex3f(p.x, p.y, p.z);
		}
		glEnd();
	}

	public void update() {

	}

	public void dispose() {

	}

	public void input() {
		if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
			speed += speed < 0 ? speedChange * 5 : speedChange;
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
			speed -= speed > 0 ? speedChange * 5 : speedChange;
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
			speed = 0f;
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_C)) {
			speed = 0;
			glLoadIdentity();
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
			MainClass.stop();
		}
	}
    }

Any ideas how to convert this into VBO?
Can someone convert this into VBO and show me how?

Thanks in Advance,

http://www.java-gaming.org/topics/lwjgl-tutorial-series-contents/30681/view.html

LOL… I told you… I can’t make it in VBO… It doesn’t work…
Can you convert what i wrote (immediate mode) to VBO? so i will see what i did wrong?
I follow the tutorial but it still doesn’t work for me…

So can someone convert / explain this in vbo?
thanks in advance,
i am new so sorry if i am too noob :frowning:

Look up theCodingUniverse on youtube. Sorry, but I doubt anyone will completely write your code for you. You wont learn if you don’t do. Like up SHC and his tutorials on here.

Spoonfeeding is generally frowned upon, as the recipient tends to copy+paste and forget about it.

If you truly wish to learn, start from scratch using the tutorials I linked.

you really don’t get it? It is from theCodingUniverse… it’s an example of something he showed.
I saw all SHC Tutorials and they are good. but i still cant figure out how to convert this immediate mode to vbo?

And spoonfeeding?! really? I am trying to convert this for 2 days already. i need help.

ohh well thanks anyways, and if someone can help, so please.

perhaps this post will help.

take same concept and apply VBO instead of VA

Well I kinda still wanted to do this:


private int vbo;

public Game() {
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective((float)60, (float)(Display.getWidth() / Display.getHeight()), 0.001f, 100);
	glMatrixMode(GL_MODELVIEW);
	
	glEnable(GL_DEPTH_TEST);
	
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	
	points = new Entity[stars];
	for(int i = 0; i < points.length; i++) {
		points[i] = new Entity((random.nextFloat() - 0.5f) * 100f, (random.nextFloat() - 0.5f) * 100f, random.nextInt(starsDistance) - starsDistance);
	}
	
	//Put all the data in one FloatBuffer
	FloatBuffer buffer = BufferUtils.createFloatBuffer(points.length * 3);
	for(Entity e : points)
		buffer.put(e.x).put(e.y).put(e.z);
	buffer.flip();
	
	
	vbo = glGenBuffers(); //Create a buffer name
	glBindBuffer(GL_ARRAY_BUFFER, vbo); //Bind the name we created and make it type ARRAY_BUFFER, as it's a buffer to hold an array of data for each vertex
	glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW); //upload to the graphics card, GL_STATIC_DRAW means we will never re-upload any data to this VBO again, it's for drawing only
	glBindBuffer(GL_ARRAY_BUFFER, 0); //Unbind the buffer name
}

public void render() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	glTranslatef(0, 0, speed);
	
	glPointSize(2.0f);
	
	glBindBuffer(GL_ARRAY_BUFFER, vbo); //Bind the buffer name
	glEnableClientState(GL_VERTEX_ARRAY); //Enable the vertex array, as the VBO contains position data
	glVertexPointer(3, GL_FLOAT, 3 * 4, 0); //Link the VBO to the GL_VERTEX_ARRAY: 3 = size, type GL_FLOAT, stride, meaning distance (in bytes) between each point, so 3 components * 4 bytes per float, and the last parameter = 0 to indicate that we are using the currently bound VBO
	
	glDrawArrays(GL_POINTS, 0, points.length); //Draw all the points with GL_POINTS, starting at 0, and drawing points.length points
	
	glDisableClientState(GL_VERTEX_ARRAY); //Disable the vertex array as we're done with it now
	glBindBuffer(GL_ARRAY_BUFFER, 0); //Unbind the buffer name
}

Good guy ra4king?

Basically what you need to know about VBOs is they’re efficient when you send large groups i data to them, just not too large. The buffer stores all the data and then whisks it away to the GPU where its rendered. Just remember that you need to fill, bind, enable, enable again, draw and disable. Its simple after you get the hang of it.

I didn’t try this out but thanks a lot man!!
Really, I don’t know how to thank you!
there isn’t any vote up? like? accept answer?

I gonna try this out, I really want to learn 3D (after i programmed for 2 years now).
Really thank you so much man. i really appreciate it!

OK, So i tried this out and it works, but it stuttering…
I get around 2500 - 3500 FPS but it still stuttering.
Any ideas why?

EDIT: OK, Its weird now it doesn’t stuttering… really strange… maybe something went wrong… because it suddenly stopped stuttering.

Only thing we got is the Appreciate button to the right of the Quote button, although I don’t think JGO newbies have it.

And hey any time, as long as you learn what I did and what you did wrong.

Thanks, i am still learning, I played a lot with the code and now i understand everything a lot better. thanks to you.

And i see the Appreciate button :smiley:
Only once in a hour…