Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (525)
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   
Pages: [1]
  ignore  |  Print  
  VBOs in LWJGL  (Read 615 times)
0 Members and 1 Guest are viewing this topic.
Offline DrewLols

Senior Devvie


Medals: 3
Projects: 1


Noob going through metamorphosis...


« Posted 2014-05-26 22:20:44 »

AAAAAAH!  I spent hours looking up tutorials trying to understand vbos, ibos, and textures, and I can't even display a simple textured vbo!  I DID manage to get rendering working in immediate mode, but only because it is infinitely more simplistic.

I should probably tell you what I am trying to make.  I am making a class called TexturedMesh that will store a VBO for coordinates, another VBO for texture coordinates, and an IBO for vertex data.

Here is my class:
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  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
package render;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import core.Disposable;
import core.Renderable;
import core.Texture;


/**
 * 2-Dimensional Mesh object that can be rendered.  Uses VBOs to render data.
 * @author William Andrew Cahill
 */

public class TexturedMesh2D implements Renderable, Disposable
{
   // Buffered objects
   private Texture tex;         // Texture to use
   private int vbo;            // Verticies to draw
   private int ibo;            // Index buffer to use
   private int tvbo;            // Texture vbo to use
   
   // Vertex buffers
   private FloatBuffer vertices, texCoords;      // Vertex and Texture coordinates
   private IntBuffer indices;                  // Indices
   
   
   /**
    * Constructs the mesh
    * @param vertices List of vertices to connect to
    * @param texCoords Texture coordinates for mapping
    * @param indices Order in which to connect vertices
    * @param tex Texture to map to Mesh2D
    */

   public TexturedMesh2D(FloatBuffer vertices, FloatBuffer texCoords, IntBuffer indices, Texture tex)
   {
      create(vertices, texCoords, indices, tex);
   }
   
   
   /**
    * Constructs a TexturedMesh with the mesh simply wrapping the entire texture.
    * @param tex Texture to use
    */

   public TexturedMesh2D(Texture tex)
   {
      // Creates vertices
      FloatBuffer vertices = BufferUtils.createFloatBuffer(8).put(new float[]
      {
         0, 0,
         tex.getWidth(), 0,
         tex.getWidth(), tex.getHeight(),
         0, tex.getHeight()
      });
     
      // Creates texture coordinates
      FloatBuffer texCoords = BufferUtils.createFloatBuffer(8).put(new float[]
      {
         0, 0,
         1, 0,
         1, 1,
         0, 1
      });
     
      // Creates indices
      IntBuffer indices = BufferUtils.createIntBuffer(4).put(new int[]{0, 1, 2, 3});
     
      // Creates mesh
      create(vertices, texCoords, indices, tex);
   }
   
   
   /**
    * Creates the mesh
    */

   private void create(FloatBuffer vertices, FloatBuffer texCoords, IntBuffer indices, Texture tex)
   {
      // Checks arguments
      if(vertices == null || texCoords == null || indices == null || tex == null)
         throw new IllegalArgumentException("Arguments cannot be null");
     
      // Stores arguments
      this.vertices = vertices;
      this.texCoords = texCoords;
      this.indices = indices;
      this.tex = tex;
     
      // Creates coordinate vbo
      vbo = GL15.glGenBuffers();
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
      GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertices, GL15.GL_STATIC_DRAW);
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
     
      // Creates texture vbo
      tvbo = GL15.glGenBuffers();
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, tvbo);
      GL15.glBufferData(GL15.GL_ARRAY_BUFFER, texCoords, GL15.GL_STATIC_DRAW);
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
     
      // Creates ibo
      ibo = GL15.glGenBuffers();
      GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo);
      GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indices, GL15.GL_STATIC_DRAW);
      GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
   }
   
   
   /**
    * Sets up object pointers before using them
    */

   private void setupPointers()
   {
      // Points to vertices
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
      GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
      GL11.glVertexPointer(2, GL11.GL_FLOAT, 0, 0);
     
      // Points to texture vertices
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, tvbo);
      GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
      GL11.glVertexPointer(2, GL11.GL_FLOAT, 0, 0);
     
      // Points to indices
      GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo);
   }

   
   @Override
   public void render()
   {
      render(0, 0);
   }

   
   @Override
   public void render(float x, float y)
   {
      // Translates
      GL11.glTranslatef(x,  y,  0);
     
      // Sets up buffer pointers
      setupPointers();
     
      // Binds texture
      tex.bind();
     
      // Draws elements
      GL11.glDrawElements(GL11.GL_TRIANGLE_STRIP, indices.capacity(), GL11.GL_UNSIGNED_INT, 0);
     
      // Disables states
      GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
      GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
     
      // Translates back
      GL11.glTranslatef(-x, -y, 0);
   }
   
   
   @Override
   public boolean isDisposed()
   {
      // Later, later...
      return false;
   }

   
   @Override
   public void dispose()
   {
      // Later, later...
   }
   
   
   /**
    * Disposes of the TextureMesh2D's underlying resources
    * @param disposeChildren Flag that determines if underlying resources besides the Texture coordinates
    * should be disposed of
    */

   public void dispose(boolean disposeChildren)
   {
      // I'll get to this later...
   }
}


I had to implement a lot of code before I could even think of testing it, so I'm not surprised that nothing shows up on my screen.  I don't even know where to look in order start fixing it, though.  Help?

Edit:  When testing, I use the constructor in which only the Texture is supplied.  I made a Texture class from scratch, and there haven't been any problems with it yet.  Oh, and a shout out to theagentd for helping in this post regarding binding textures: http://www.java-gaming.org/index.php?topic=25516.0  Helped me out a lot.

Edit2:  When I call GL11.glGetError(), I get an error code of 1285 which means out of memory.  Hrm...  It happens after GL11.glDrawElements(GL11.GL_TRIANGLE_STRIP, indices.capacity(), GL11.GL_UNSIGNED_INT, 0);

Did you know that 90% of statistics are wrong?
Offline DrewLols

Senior Devvie


Medals: 3
Projects: 1


Noob going through metamorphosis...


« Reply #1 - Posted 2014-05-27 00:22:31 »

I maaaaaaay have forgotten to flip the buffers.  I shall see if I can fix this...

Edit:  Ok, so now instead of giving an error message, it just crashes.  Windows forces it to close, and I am given no indication as to why this happens.  Ugh...  Here is my updated code:
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  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
package render;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import core.Disposable;
import core.Renderable;
import core.Texture;


/**
 * 2-Dimensional Mesh object that can be rendered.  Uses VBOs to render data.
 * @author William Andrew Cahill
 */

public class TexturedMesh2D implements Renderable, Disposable
{
   // Buffered objects
   private Texture tex;         // Texture to use
   private int vbo;            // Verticies to draw
   private int ibo;            // Index buffer to use
   private int tvbo;            // Texture vbo to use
   
   // Vertex buffers
   private FloatBuffer vertices, texCoords;      // Vertex and Texture coordinates
   private IntBuffer indices;                  // Indices
   
   
   /**
    * Constructs the mesh
    * @param vertices List of vertices to connect to
    * @param texCoords Texture coordinates for mapping
    * @param indices Order in which to connect vertices
    * @param tex Texture to map to Mesh2D
    */

   public TexturedMesh2D(FloatBuffer vertices, FloatBuffer texCoords, IntBuffer indices, Texture tex)
   {
      create(vertices, texCoords, indices, tex);
   }
   
   
   /**
    * Constructs a TexturedMesh with the mesh simply wrapping the entire texture.
    * @param tex Texture to use
    */

   public TexturedMesh2D(Texture tex)
   {
      // Creates vertices
      FloatBuffer vertices = BufferUtils.createFloatBuffer(8).put(new float[]
      {
         0, 0,
         tex.getWidth(), 0,
         tex.getWidth(), tex.getHeight(),
         0, tex.getHeight()
      });
      vertices.flip();
     
      // Creates texture coordinates
      FloatBuffer texCoords = BufferUtils.createFloatBuffer(8).put(new float[]
      {
         0, 0,
         1, 0,
         1, 1,
         0, 1
      });
      texCoords.flip();
     
      // Creates indices
      IntBuffer indices = BufferUtils.createIntBuffer(4).put(new int[]{0, 1, 2, 3});
      indices.flip();
     
      // Creates mesh
      create(vertices, texCoords, indices, tex);
   }
   
   
   /**
    * Creates the mesh
    */

   private void create(FloatBuffer vertices, FloatBuffer texCoords, IntBuffer indices, Texture tex)
   {
      // Checks arguments
      if(vertices == null || texCoords == null || indices == null || tex == null)
         throw new IllegalArgumentException("Arguments cannot be null");
     
      // Stores arguments
      this.vertices = vertices;
      this.texCoords = texCoords;
      this.indices = indices;
      this.tex = tex;
     
      // Creates coordinate vbo
      vbo = GL15.glGenBuffers();
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
      GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertices, GL15.GL_STATIC_DRAW);
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
     
      // Creates texture vbo
      tvbo = GL15.glGenBuffers();
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, tvbo);
      GL15.glBufferData(GL15.GL_ARRAY_BUFFER, texCoords, GL15.GL_STATIC_DRAW);
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
      System.out.println(texCoords.capacity());
     
      // Creates ibo
      ibo = GL15.glGenBuffers();
      GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo);
      GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indices, GL15.GL_STATIC_DRAW);
      GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
   }
   
   
   /**
    * Sets up object pointers before using them
    */

   private void setupPointers()
   {
      // Points to vertices
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
      GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
      GL11.glVertexPointer(2, GL11.GL_FLOAT, 0, 0);
     
      // Points to texture vertices
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, tvbo);
      GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
      GL11.glVertexPointer(2, GL11.GL_FLOAT, 0, 0);
     
      // Points to indices
      GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, ibo);
   }

   
   @Override
   public void render()
   {
      render(0, 0);
   }

   
   @Override
   public void render(float x, float y)
   {
      // Translates
      GL11.glTranslatef(x,  y,  0);
     
      // Sets up buffer pointers
      setupPointers();
     
      // Binds texture
      tex.bind();
     
      // Draws elements
      GL11.glDrawElements(GL11.GL_TRIANGLE_STRIP, indices.capacity(), GL11.GL_UNSIGNED_INT, 0);
     
     
      // Disables states
      GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
      GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
     
      // Translates back
      GL11.glTranslatef(-x, -y, 0);
   }
   
   
   @Override
   public boolean isDisposed()
   {
      // Later, later...
      return false;
   }

   
   @Override
   public void dispose()
   {
      // Later, later...
   }
   
   
   /**
    * Disposes of the TextureMesh2D's underlying resources
    * @param disposeChildren Flag that determines if underlying resources besides the Texture coordinates
    * should be disposed of
    */

   public void dispose(boolean disposeChildren)
   {
      // I'll get to this later...
   }
}


Buffers are now flipped.

Did you know that 90% of statistics are wrong?
Offline Slyth2727
« Reply #2 - Posted 2014-05-27 00:54:22 »

Try replacing

1  
2  
3  
4  
//    (line 124-126)
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, tvbo);
      GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
      GL11.glVertexPointer(2, GL11.GL_FLOAT, 0, 0);


with

1  
2  
3  
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, tvbo);
      GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
      GL11.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0);


I could be totally wrong, I don't have this totally memorized.

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DrewLols

Senior Devvie


Medals: 3
Projects: 1


Noob going through metamorphosis...


« Reply #3 - Posted 2014-05-27 01:06:53 »

Try replacing

1  
2  
3  
4  
//    (line 124-126)
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, tvbo);
      GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
      GL11.glVertexPointer(2, GL11.GL_FLOAT, 0, 0);


with

1  
2  
3  
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, tvbo);
      GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
      GL11.glTexCoordPointer(2, GL11.GL_FLOAT, 0, 0);


I could be totally wrong, I don't have this totally memorized.

Woah!  That!  Almost worked!  It actually displayed something!  Three of the verticies of my texture, in fact!  Ah, well, now I have to figure out why THAT'S happening, but it works!  Man, this all reminds me of my assembly class, where type information does not exist, and the program can exit inexplicably due to limited runtime checks.  Can't believe I missed that.  I understand what I did wrong.  Thanks for your help!  I'll make another post once I get this class working...  Or if I fail and need help again Smiley

Did you know that 90% of statistics are wrong?
Offline DrewLols

Senior Devvie


Medals: 3
Projects: 1


Noob going through metamorphosis...


« Reply #4 - Posted 2014-05-27 01:18:17 »

I have the successfuls!  My class now appears to function without any quirky behavior.  I had to replace

GL11.glDrawElements(GL11.GL_TRIANGLE_STRIPS, indices.capacity(), GL11.GL_UNSIGNED_INT, 0);

with

GL11.glDrawElements(GL11.GL_TRIANGLE_FAN, indices.capacity(), GL11.GL_UNSIGNED_INT, 0);

to make it draw a quad, and not just a triangle.  Probably going to have to look into how those draw methods work.  Now, to dispose of the Mesh, I just need to delete all of the buffers, and destroy the underlying texture.  Then life should be good forever!

Did you know that 90% of statistics are wrong?
Offline Slyth2727
« Reply #5 - Posted 2014-05-27 20:47:23 »

I have the successfuls!  My class now appears to function without any quirky behavior.

Awesome, I'm glad to see you got it working Smiley

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
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.

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

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

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

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

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

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

Gibbo3771 (25 views)
2014-11-24 19:59:16

trollwarrior1 (38 views)
2014-11-22 12:13:56

xFryIx (77 views)
2014-11-13 12:34:49

digdugdiggy (55 views)
2014-11-12 21:11:50
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!