Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Java Game APIs & Engines / OpenGL Development / New to OpenGL, need some guidence on 2D Game / sprites and VBO on: 2012-11-06 10:26:57
Hello everyone!

I decided to start a new project and make a game and I want to keep it 2D sprite based only. So I started learning today how to use OpenGL
and was able to get a hang of it very qucikly.

Now I really just have few questions about using VBOs and using them for sprites

1. I know VBO is considered the fastest when it comes to rendering, but is it the best option for a 2D only game?
2. Would I be able to transform sprites when and if I need to (Rotation and scale)?
3. What about transforming sprites based on a world matrix or 2D camera?

And my last question!

I created the below code which uses a VBO to render the sprites I have in an array of sprite objects.
Is this the right way / best way of rendering quads with textures on them using a VBO? Is there a better way?

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  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
//Temp Class for sprites
  public class Sprite
   {
      public float x;
      public float y;
      public Texture texture;
   }

//Function used to "run" the game
  public void run()
   {

//Create an array of sprites and init them
     Sprite [] sprite = new Sprite[2];
      sprite[0] = new Sprite();
      sprite[0].x = 20;
      sprite[0].y = 20;
      sprite[0].texture = square; //previously loaded texture using the slick texture and texture loader
     
      sprite[1] = new Sprite();
      sprite[1].x = 125;
      sprite[1].y = 125;
      sprite[1].texture = smile; //previously loaded texture using the slick texture and texture loader
     
      //Create the amount of vertexs and sie
     final int amountOfVertex = 4;
      final int vertexSize = 2; / 2 for x and y cord only

//Create the vertex buffer
     FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertex * vertexSize);

//create and init the texture coord buffer for mapping a texture to a quad
     FloatBuffer texCoordData = BufferUtils.createFloatBuffer(amountOfVertex * vertexSize);
      texCoordData.put(new float[]{
            0.0f, 0.0f,
            1.0f, 0.0f,
            1.0f, 1.0f,
            0.0f, 1.0f});
      texCoordData.flip();
     
      //Set up the vertex buffer
     int vboVertexHandle = glGenBuffers();
      glBindTexture(GL_TEXTURE_2D, square.getTextureID());
      glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
      glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, 0);
     
//Set up the texture coord buffer
     int texCoordHandle = glGenBuffers();
      glBindTexture(GL_TEXTURE_2D, texCoordHandle);
      glBindBuffer(GL_ARRAY_BUFFER, texCoordHandle);
      glBufferData(GL_ARRAY_BUFFER, texCoordData, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, 0);

     
      //Init OpenGL      
     while(!Display.isCloseRequested())
      {
         //used for user input
        gamepad.pollInput();
         
//Clear the buffer
        glClear(GL_COLOR_BUFFER_BIT);
         
//Run though the sprite list and draw them to the screen
//Is this the part im doing right? Thanks :D
        for(int i = 0; i < 2; i++)
         {
         
//Create the verteex buffer data based on the sprite to draw
           vertexData.put(new float[]{
                  sprite[i].x, sprite[i].y,
                  sprite[i].x + sprite[i].texture.getImageWidth(), sprite[i].y,
                  sprite[i].x + sprite[i].texture.getImageWidth(), sprite[i].y + sprite[i].texture.getImageHeight(),
                  sprite[i].x, sprite[i].y + sprite[i].texture.getImageHeight()});
            vertexData.flip();
           
//bind the texture to use and handle the rest of the drawing
           glBindTexture(GL_TEXTURE_2D, sprite[i].texture.getTextureID());
            glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
            glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
            glBindBuffer(GL_ARRAY_BUFFER, 0);
           
            glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
            glVertexPointer(vertexSize, GL_FLOAT, 0, 0L);
           
            glBindBuffer(GL_ARRAY_BUFFER, texCoordHandle);
            glTexCoordPointer(vertexSize, GL_FLOAT, 0, 0L);
                     
            glEnableClientState(GL_VERTEX_ARRAY);
            glEnableClientState(GL_TEXTURE_COORD_ARRAY);
            glDrawArrays(GL_QUADS, 0, amountOfVertex);
            glDisableClientState(GL_TEXTURE_COORD_ARRAY);
            glDisableClientState(GL_VERTEX_ARRAY);
         }
         
      //Update and sync it to 60 FPS
        Display.update();
         Display.sync(60);
      }


Any code examples would be greatly appreciated!
Pages: [1]
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (126 views)
2013-05-17 21:29:12

alaslipknot (134 views)
2013-05-16 21:24:48

gouessej (164 views)
2013-05-16 00:53:38

gouessej (157 views)
2013-05-16 00:17:58

theagentd (169 views)
2013-05-15 15:01:13

theagentd (154 views)
2013-05-15 15:00:54

StreetDoggy (198 views)
2013-05-14 15:56:26

kutucuk (222 views)
2013-05-12 17:10:36

kutucuk (221 views)
2013-05-12 15:36:09

UnluckyDevil (225 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.241 seconds with 21 queries.