Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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 1470 times)
0 Members and 1 Guest are viewing this topic.
Offline madeinsoviets

Senior Newbie





« Posted 2011-01-21 15: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 16:08:30 »

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

JGO Kernel


Medals: 81
Projects: 15


★★★★★


« Reply #2 - Posted 2011-01-21 16: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 16: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: 81
Projects: 15


★★★★★


« Reply #4 - Posted 2011-01-21 16: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 16: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 17: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 19: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 13: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.

rwatson462 (37 views)
2014-12-15 09:26:44

Mr.CodeIt (31 views)
2014-12-14 19:50:38

BurntPizza (62 views)
2014-12-09 22:41:13

BurntPizza (99 views)
2014-12-08 04:46:31

JscottyBieshaar (60 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (77 views)
2014-11-29 21:32:03

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

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

toopeicgaming1999 (38 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

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
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!