Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (527)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (594)
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 09: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]
 

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

The first screenshot will be displayed as a thumbnail.

PocketCrafter7 (14 views)
2014-11-28 16:25:35

PocketCrafter7 (9 views)
2014-11-28 16:25:09

PocketCrafter7 (10 views)
2014-11-28 16:24:29

toopeicgaming1999 (76 views)
2014-11-26 15:22:04

toopeicgaming1999 (67 views)
2014-11-26 15:20:36

toopeicgaming1999 (16 views)
2014-11-26 15:20:08

SHC (30 views)
2014-11-25 12:00:59

SHC (28 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (28 views)
2014-11-24 19:59:16
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!