Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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  
  Out of memory while drawing VBOs  (Read 1338 times)
0 Members and 1 Guest are viewing this topic.
Offline madeinsoviets

Senior Newbie





« Posted 2011-01-21 16:56:18 »

Guys, i really stuck with this..(( Want to draw two simple triangles as a quad through VBO, but can see only triangle rendered through immediate mode((

App 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  
package vbo;

import java.nio.FloatBuffer;
import java.nio.IntBuffer;

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

import lwjgl.VBOHelper;
import app.IKeyListener;
import app.LWJGLApp;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;

public class VBOApp extends LWJGLApp implements IKeyListener {

   boolean end = false;
   
   VBOApp(String title, int width, int height) {
      super(title, width, height);
   }

   /**
    * @param args
    */

   public static void main(String[] args) {
      VBOApp app = new VBOApp("VBO example", 800, 600);
      app.start();
   }

   int vertBuf = 0;
   int colBuf = 0;
   int indBuf = 0;  

   @Override
   public boolean onInit() {
      addKeyListener(this);
     
      if(!VBOHelper.checkHardwareVBOSupport()){
         return false;
      }
     
      // Buffers
     FloatBuffer vBuf = BufferUtils.createFloatBuffer(12);
      FloatBuffer cBuf = BufferUtils.createFloatBuffer(16);
      IntBuffer iBuf = BufferUtils.createIntBuffer(6);
       
      float color[] = {1, 0, 1, 0};
     
      // First
     float first[] = {-1, -1, 0};
      vBuf.put(first);      
      cBuf.put(color);
           
      // Second
     float second[] = {-1, 1, 0};
      vBuf.put(second);            
      cBuf.put(color);
     
      // Third
     float third[] = {1, 1, 0};
      vBuf.put(third);            
      cBuf.put(color);
     
      // Fourth
     float fourth[] = {1, -1, 0};
      vBuf.put(fourth);      
      cBuf.put(color);
     
      // Indices
     int indices[] = {0, 1, 2, 0, 2, 3};
      iBuf.put(indices);
     
      // Creating buffers and filling data
     vertBuf = VBOHelper.createVBOID();
      VBOHelper.bufferData(vertBuf, vBuf, GL_STATIC_DRAW);
     
      colBuf = VBOHelper.createVBOID();
      VBOHelper.bufferData(colBuf, cBuf, GL_STATIC_DRAW);
     
      indBuf = VBOHelper.createVBOID();
      VBOHelper.bufferElementData(indBuf, iBuf, GL_STATIC_DRAW);
     
      return true;
   }
   
   @Override
   public boolean prepareFrame(long msecElapsed) {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
     
      glTranslatef(0, 0, -6.0f);

      glBegin(GL_TRIANGLES);                  // Drawing Using Triangles
     glVertex3f( 0.0f, 1.0f, 0.0f);            // Top
     glVertex3f(-1.0f,-1.0f, 0.0f);            // Bottom Left
     glVertex3f( 1.0f,-1.0f, 0.0f);            // Bottom Right
     glEnd();
     
      VBOHelper.bindVBO(vertBuf, indBuf, colBuf);
      VBOHelper.drawTriangles(6);
     
      return !end;
   }

   @Override
   public void onDestroy() {
      // Destroying buffers
     VBOHelper.destroyBuffer(vertBuf);
      VBOHelper.destroyBuffer(colBuf);
      VBOHelper.destroyBuffer(indBuf);
   }

   @Override
   public void keyboardEvent(int key, boolean pressed) {
      if(key == Keyboard.KEY_ESCAPE){
         end = true;
      }      
   }

}


VBO helper 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  
package lwjgl;

import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import org.lwjgl.opengl.GLContext;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;

import utils.Log;

public class VBOHelper {
   
   /**
    * Checking CAPs
    */

   public static boolean checkHardwareVBOSupport(){
      if(!GLContext.getCapabilities().GL_ARB_vertex_buffer_object){
         Log.hardwareNotSupport("GL_ARB_vertex_buffer_object");
      }
      return GLContext.getCapabilities().GL_ARB_vertex_buffer_object;
   }

   /**
    * Creating, filling, destroying
    */

   // Creating id for our VBO
  public static int createVBOID() {
      return glGenBuffers();
   }

   // Float data for our VBO (vertex, color, normal)(default usage = GL_STATIC_DRAW)
  public static void bufferData(int id, FloatBuffer data, int usage) {      
      glBindBuffer(GL_ARRAY_BUFFER, id);
      glBufferData(GL_ARRAY_BUFFER, data, usage);
   }

   // Int data for our VBO (indices)(default usage = GL_STATIC_DRAW)
  public static void bufferElementData(int id, IntBuffer data, int usage) {
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
      glBufferData(GL_ELEMENT_ARRAY_BUFFER, data, usage);
   }
   
   public static void destroyBuffer(int id){
      glDeleteBuffers(id);
   }
   
   /**
    * Binding, drawing
    */

   // Preparing OpenGL for drawing and binding buffers
  public static void bindVBO(int vertexBufferID, int indexBufferID, int colourBufferID) {
      glEnableClientState(GL_VERTEX_ARRAY);
      glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
      glVertexPointer(3, GL_FLOAT, 0, 0);
             
      glEnableClientState(GL_COLOR_ARRAY);
      glBindBuffer(GL_ARRAY_BUFFER, colourBufferID);
      glColorPointer(4, GL_FLOAT, 0, 0);
       
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
   }
   
   
   public static void drawTriangles(int indexBufferSize){
      glDrawElements(GL_TRIANGLES, indexBufferSize, GL_UNSIGNED_INT, 0);
   }  
}
Offline madeinsoviets

Senior Newbie





« Reply #1 - Posted 2011-01-21 17:08:30 »

Maybe someone can just add some working code snippet?
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 74
Projects: 15


★★★★★


« Reply #2 - Posted 2011-01-21 17:13:59 »

Snippet 1

Snippet 2

Snippet 3
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline madeinsoviets

Senior Newbie





« Reply #3 - Posted 2011-01-21 17:39:54 »

First one is a tutorial from oficial site - but i'm pretty sure it it outdated - second and third (same) are outdated too. VBOs now is in core i think...
P.S. i just cannot get into version system - what means org.lwjgl.opengl.GL15? why it does'n include for example org.lwjgl.opengl.GL11? Every time i found any tutorial it looks like OpenGL was rewrited complitedly..;((
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 74
Projects: 15


★★★★★


« Reply #4 - Posted 2011-01-21 17:45:10 »

yes VBO's are now part of OpenGL 3.0 "org.lwjgl.opengl.GL30"; just replace the parts of the extension on those snippets with GL30.*



if you don't like the static class names before every opengl call then just use java's static import class and it will look like normal c opengl.

e.g.
1  
import static org.lwjgl.opengl.GL11.*;

and then you can use all the calls from that class without using the "GL11." bit.
Offline madeinsoviets

Senior Newbie





« Reply #5 - Posted 2011-01-21 17:53:38 »

thanks, i'l try..
And by the way - why they always use methods like ARBVertexBufferObject.glGenBuffersARB(buffer); instead of methods used in most of tutorials/literature like glGenBuffers()
Offline madeinsoviets

Senior Newbie





« Reply #6 - Posted 2011-01-21 18:09:31 »

Rewrited VBOHelper as in tutorial - still do not work...........((
http://www.lwjgl.org/wiki/index.php?title=Using_Vertex_Buffer_Objects_(VBO)
Offline madeinsoviets

Senior Newbie





« Reply #7 - Posted 2011-01-21 20:08:22 »

Problem solved.. But i don't know how - i did it pretty the same way...Wink
Offline madeinsoviets

Senior Newbie





« Reply #8 - Posted 2011-01-24 14:48:42 »

Forget to rewind buffers before sending them to OpenGL..
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.

Riven (10 views)
2014-07-29 18:09:19

Riven (8 views)
2014-07-29 18:08:52

Dwinin (8 views)
2014-07-29 10:59:34

E.R. Fleming (25 views)
2014-07-29 03:07:13

E.R. Fleming (10 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!