Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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  
  From immediate mode to VBO mode LWJGL (basic)  (Read 1004 times)
0 Members and 1 Guest are viewing this topic.
Offline israelg99

Senior Newbie





« Posted 2013-10-14 19:00:47 »

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 :

   
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
 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,
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #1 - Posted 2013-10-14 19:34:59 »

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

Offline israelg99

Senior Newbie





« Reply #2 - Posted 2013-10-14 21:41:38 »

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 Sad
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline opiop65

JGO Kernel


Medals: 124
Projects: 7
Exp: 3 years


Team Alluminum


« Reply #3 - Posted 2013-10-14 23:23:56 »

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.

Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #4 - Posted 2013-10-14 23:28:58 »

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.

Offline israelg99

Senior Newbie





« Reply #5 - Posted 2013-10-15 06:53:28 »

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.
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.
Offline DrHalfway
« Reply #6 - Posted 2013-10-15 07:49:49 »

perhaps this post will help.

http://www.java-gaming.org/topics/simple-vertex-array-immediate-drawer/30852/view.html

take same concept and apply VBO instead of VA

Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #7 - Posted 2013-10-15 09:47:42 »

Well I kinda still wanted to do this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
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
}

Offline opiop65

JGO Kernel


Medals: 124
Projects: 7
Exp: 3 years


Team Alluminum


« Reply #8 - Posted 2013-10-15 11:35:48 »

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.

Offline israelg99

Senior Newbie





« Reply #9 - Posted 2013-10-15 13:04:19 »

Well I kinda still wanted to do this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
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
}


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!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline israelg99

Senior Newbie





« Reply #10 - Posted 2013-10-15 14:42:47 »

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.
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #11 - Posted 2013-10-15 22:15:45 »

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.

Offline israelg99

Senior Newbie





« Reply #12 - Posted 2013-10-15 23:05:34 »

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 Cheesy
Only once in a hour..
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.

xsi3rr4x (57 views)
2014-04-15 18:08:23

BurntPizza (55 views)
2014-04-15 03:46:01

UprightPath (68 views)
2014-04-14 17:39:50

UprightPath (51 views)
2014-04-14 17:35:47

Porlus (68 views)
2014-04-14 15:48:38

tom_mai78101 (92 views)
2014-04-10 04:04:31

BurntPizza (153 views)
2014-04-08 23:06:04

tom_mai78101 (249 views)
2014-04-05 13:34:39

trollwarrior1 (205 views)
2014-04-04 12:06:45

CJLetsGame (212 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!