Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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  
  Problems with OpenGL VBOs  (Read 1117 times)
0 Members and 1 Guest are viewing this topic.
Offline jameskmonger

Junior Newbie





« Posted 2012-11-01 17:10:24 »

Hello, I've made a basic corridor using immediate OpenGL drawing and I want to transfer it to VBOs. However, it just doesn't work. The immediate drawing works fine, yet I tried to make it using VBOs and it doesn't work. Here is what I am trying to create:

And here is the code that I used for that:
1  
2  
3  
4  
5  
6  
7  
8  
      GL11.glColor3f(1F, 0F, 0F);
      GL11.glVertex3f(0.5F, 1.5F, 1.0F);
      GL11.glColor3f(0F, 1F, 0F);
      GL11.glVertex3f(0.5F, 1.5F, -15.0F);
      GL11.glColor3f(0F, 0F, 1F);
      GL11.glVertex3f(0.5F, -1.5F, -15.0F);
      GL11.glColor3f(0.5F, 0.5F, 0.5F);
      GL11.glVertex3f(0.5F, -1.5F, 1.0F);


Yet when I try to use VBOs for the same thing, it doesn't work and I just get a black window. Here is my class to create is using VBOs.
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  
import java.nio.FloatBuffer;

import org.lwjgl.*;
import org.lwjgl.opengl.*;
import org.lwjgl.util.glu.GLU;

import static org.lwjgl.opengl.ARBBufferObject.*;
import static org.lwjgl.opengl.ARBVertexBufferObject.*;
import static org.lwjgl.opengl.GL11.*;

public class VBO {
   
   static int w = 640;
   static int h = 480;

   public static void main(String[] args) {
      try {
         initWindow();
         renderLoop();
      } catch (LWJGLException e) {
         e.printStackTrace();
      }
   }

   static void initWindow() throws LWJGLException {
      Display.setDisplayMode(new DisplayMode(w, h));
      Display.setFullscreen(false);
      Display.create();

      glViewport(0, 0, w, h);
   }

   static void renderLoop() {
      while (!Display.isCloseRequested()) {
         preRender();
         render();

         Display.update();
         Display.sync(20);
      }
      Display.destroy();
   }

   static void preRender() {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
            | GL_STENCIL_BUFFER_BIT);

      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();

      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
   }

   static void render() {
      glClearColor(1, 1, 1, 1);
      glLoadIdentity();
      glBegin(7);
      drawVertexArray();
   }

   static void drawVertexArray() {
      FloatBuffer cBuffer = BufferUtils.createFloatBuffer(12);
      cBuffer.put(1).put(0).put(0);
      cBuffer.put(0).put(1).put(0);
      cBuffer.put(0).put(0).put(1);
      cBuffer.put(0.5F).put(0.5F).put(0.5F);
      cBuffer.flip();

      FloatBuffer vBuffer = BufferUtils.createFloatBuffer(12);
      vBuffer.put(0.5F).put(1.5f).put(1.0f);
      vBuffer.put(0.5f).put(1.5f).put(-15.0f);
      vBuffer.put(0.5f).put(-1.5f).put(-15.0f);
      vBuffer.put(0.5f).put(-1.5f).put(1.0f);
      vBuffer.flip();
     
      glEnableClientState(GL_VERTEX_ARRAY);
      glEnableClientState(GL_COLOR_ARRAY);

      glColorPointer(4, /* stride */4 << 2, cBuffer);
      glVertexPointer(4, /* stride */4 << 2, vBuffer);
      glDrawArrays(GL_QUADS, 0, /* elements */4);

      glDisableClientState(GL_COLOR_ARRAY);
      glDisableClientState(GL_VERTEX_ARRAY);
   }
}
Offline StumpyStrust
« Reply #1 - Posted 2012-11-01 17:39:33 »

It looks like you are doing vertex arrays and not VBOs. The dead give away is the lack of a VBO id or any glBuffer stuffs.

Look at the LWJGL for some tutorials on using VBOs but I would get vertex arrays working first as they are almost VBOs.

Since you are not interleaving your vertex array, you should have not have a stride or offsets.

Also, you may want to do thing outside the static context.

Offline jameskmonger

Junior Newbie





« Reply #2 - Posted 2012-11-01 18:27:57 »

Thanks Tongue I was half-reading a tutorial but I was trying to do my own way at the same time Tongue I'll look up on vertex arrays some more - thanks.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline lhkbob

JGO Knight


Medals: 32



« Reply #3 - Posted 2012-11-02 01:56:53 »

Your element size and strides don't make any sense in glColorPointer and glVertexPointer.  In both cases, your element size is 3 (red/green/blue and x/y/z).  Since each consecutive attribute immediately follows the previous, your stride should be 0 (not 4 << 2, which is 16 btw).

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2012-11-02 02:11:11 »

You might want to take a look at my article on the subject:

http://www.java-gaming.org/topics/introduction-to-vertex-arrays-and-vertex-buffer-objects-opengl/24272/view.html

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
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.

Longarmx (38 views)
2014-10-17 03:59:02

Norakomi (29 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (56 views)
2014-10-14 00:39:48

TehJavaDev (55 views)
2014-10-14 00:35:47

TehJavaDev (46 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
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!