Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  [Solved] VBO issue - having trouble with rendering  (Read 2764 times)
0 Members and 1 Guest are viewing this topic.
Offline Suds

Senior Newbie




Lead Developer, DefeatThePurpose Entertainment


« Posted 2011-10-13 07:35:22 »

EDIT: I forgot that this site isn't LWJGL exclusive; I'm using LWJGL.   Cheesy

Hey, I'm experimenting with setting up a VBO to render my (cube based) world. This code should draw a cube (at least that's the intention), it generates no errors:

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  
public class Renderfield3D implements IDrawable{
    private int _verticesID = 0;
    private int _indicesID = 0;
    private int _coloursID = 0;
   
    @Override
    public void load() {
        float[] vertices = new float[]
        {  // X     Y     Z
           0.0f, 0.0f, 0.0f, // 0 LBF
           1.0f, 0.0f, 0.0f, // 1 RBF
           0.0f, 0.0f, 1.0f, // 2 LBB
           1.0f, 0.0f, 1.0f, // 3 RBB
           
            0.0f, 1.0f, 0.0f, // 4 LTF
           1.0f, 1.0f, 0.0f, // 5 RTF
           0.0f, 1.0f, 1.0f, // 6 LTB
           1.0f, 1.0f, 1.0f  // 7 RTB  
       };
       
        int[] indices = new int[]
        {
            4, 5, 1, 0, // FRONT
           5, 7, 3, 1, // RIGHT
           7, 6, 2, 3, // BACK
           6, 4, 0, 2, // LEFT
           0, 1, 3, 2, // BOTTOM
           6, 7, 5, 4 // TOP
       };
       
        float[] colours = new float[]
        {
            1.0f, 1.0f, 1.0f, 1.0f
        };
       
        _verticesID = VBOHandler.createVBO();
        _indicesID  = VBOHandler.createVBO();
        _coloursID  = VBOHandler.createVBO();
       
        FloatBuffer bufferedColours = BufferUtils.createFloatBuffer(colours.length);
                    bufferedColours.put(colours);
       
        FloatBuffer bufferedvertices = BufferUtils.createFloatBuffer(vertices.length);
                    bufferedvertices.put(vertices);
                   
        IntBuffer   bufferedindices  = BufferUtils.createIntBuffer(indices.length);
                    bufferedindices.put(indices);
                   
        VBOHandler.bufferData(_verticesID, bufferedvertices);
        VBOHandler.bufferElementData(_indicesID, bufferedindices);
    }

    @Override
    public void draw() {
      GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
      ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, _verticesID);
      GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0);
     
      GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
      ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, _coloursID);
      GL11.glColorPointer(4, GL11.GL_FLOAT, 0, 0);

      ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, _indicesID);
      GL12.glDrawRangeElements(GL11.GL_QUADS, 0, 24, 24,
                        GL11.GL_UNSIGNED_INT, 0);
    }
   
}

public class VBOHandler {
    public static IntBuffer createVBOs(int amount)
    {
        if(GLContext.getCapabilities().GL_ARB_vertex_buffer_object)
        {
            IntBuffer buffer = BufferUtils.createIntBuffer(amount);
            ARBVertexBufferObject.glGenBuffersARB(buffer);
            return buffer;
        }
       
        return null;
    }
   
    public static int createVBO()
    {
        return createVBOs(1).get(0);
    }
   
    public static void bufferData(int id, FloatBuffer buffer)
    {
        if(GLContext.getCapabilities().GL_ARB_vertex_buffer_object)
        {
            ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, id);
            ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, buffer,
                    ARBVertexBufferObject.GL_STATIC_DRAW_ARB);
        }
    }
   
    public static void bufferElementData(int id, IntBuffer buffer)
    {
        if(GLContext.getCapabilities().GL_ARB_vertex_buffer_object)
        {
           
            ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, id);
            ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, buffer,
                    ARBVertexBufferObject.GL_STATIC_DRAW_ARB);
        }        
    }
}


I've been over the code on "Using Vertex Buffer Objects" at the LWJGL site a fair few times now. The trouble I am having, is that my game does not draw a cube at all. There are two classes in that paste - the VBOHandler class that I wrote is at the bottom. To rule out camera issues, I set up a camera that points at a reference grid around the origin too. This is a screenshot of what my game sees


The reference grid is made with just plain vertices - not buffered, not using an index buffer, nothing. Just vertices.

Can anyone see what is wrong with my code there?

Offline theagentd
« Reply #1 - Posted 2011-10-13 09:19:06 »

You forgot to flip your buffers. >_>
EDIT: If you're making a DK2 clone, let me tell you I peed on that streetlight first! xD

Myomyomyo.
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2011-10-13 09:40:25 »

@theagentd
Damn too many people forgetting to flip buffers these days! Flipping buffers is a pain in the arse!!

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

Senior Newbie




Lead Developer, DefeatThePurpose Entertainment


« Reply #3 - Posted 2011-10-13 09:41:49 »

You forgot to flip your buffers. >_>
EDIT: If you're making a DK2 clone, let me tell you I peed on that streetlight first! xD

I am indeed making a DK2 clone. :-) But only to build the engine I need for a different, unique game. How did yours go / is going?

Thanks for the response. I took a look at the LWJGL VBO page again and did a ctrl+F for "flip" -- no mention. I then went to the FloatBuffer documentation and worked out what flip() does before employing some "random engineering" with buffered*.flip() calls and yielded nothing.

Can you show me where in my code a buffer flip *should* occur?

Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2011-10-13 09:52:29 »

Before line 49 when you give VBOHandler the data, you should flip your buffers by calling .flip() on all 3 of them.

Offline Suds

Senior Newbie




Lead Developer, DefeatThePurpose Entertainment


« Reply #5 - Posted 2011-10-13 10:03:37 »

Perfect, thanks! It didn't work at first, but then I also noticed that I never actually buffered my colour data. So I threw in "VBOHandler.bufferData(_coloursID, bufferedColours);" as well as your solution and it worked.

Time to hunt around for a "Thanks" or "+rep" button on the forum, and go update all my other posts :-)

Offline theagentd
« Reply #6 - Posted 2011-10-13 13:46:28 »

@theagentd
Damn too many people forgetting to flip buffers these days! Flipping buffers is a pain in the arse!!
Haha, time for a sticky in the OpenGL sub forum? xD I've forgot to flip my buffers so many times since I started with LWJGL that that's the first thing I check if something isn't working when buffers are involved.
I am indeed making a DK2 clone. :-) But only to build the engine I need for a different, unique game. How did yours go / is going?
I haven't gotten very far yet so I won't be posting any screenshots. Hell, I'm not even sure I will ever manage to finish it. I only have terrain rendering, fog of war and exploration (things don't update until your units see them) done at the moment. I'm currently working on the unit system, player system and rooms, all at the same time, while trying to keep all the other stuff I'll have to implement in mind. Loading maps/units from files, multiplayer, semi-static objects, decorations, AI... Everything has to fit together in the end, so because I'm trying to take everything into consideration it's going a little bit slow at the moment. I'm having a big CPU performance issue with my fog of war too, which will require some optimizations later.
Without fog of war I get around 200 FPS with 800 units and a 256x256 map. As the the game is extremely CPU-limited now (I will implement multithreading later) and I only have very simple graphics, I can enable as much anti-aliasing as I want without affecting the FPS at all, and I'm on a laptop.

Myomyomyo.
Offline ra4king

JGO Kernel


Medals: 347
Projects: 3
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2011-10-13 21:28:41 »

Time to hunt around for a "Thanks" or "+rep" button on the forum, and go update all my other posts :-)
See that nifty "Appreciate" link next to the "Quote" button? Use it for anyone who helps you Cheesy

Offline Suds

Senior Newbie




Lead Developer, DefeatThePurpose Entertainment


« Reply #8 - Posted 2011-10-13 23:26:23 »



Nope...

Without fog of war I get around 200 FPS with 800 units and a 256x256 map. As the the game is extremely CPU-limited now (I will implement multithreading later) and I only have very simple graphics, I can enable as much anti-aliasing as I want without affecting the FPS at all, and I'm on a laptop.

Wow. That's not bad. I was getting ~80fps running my clone with 25 units on a 128sq map with XNA. Mine has a map generator, renderer, and a LOT of AI. There's a video (a few months old now) that reflects my progress at the "Game" link in my sig. It doesn't use buffers either, so the first thing I'm doing with my port is making it use VBOs with a chunk system.

Offline theagentd
« Reply #9 - Posted 2011-10-14 02:08:08 »

Fog of war doesn't have to be rendered every frame, s remaking it every 4th to 10th should be enough with some clever interpolation between the last one map and the current one. The problem is that just rendering the full fog of war (which can cost up to 20ms when the units are in the thousands) gives me so much micro stuttering that it doesn't really change anything. I'll have to split up the fog of war rendering between multiple frames (at the cost of SLI/Crossfire support xD), but I'm not gonna dive into this now. It works, and I have more important things to implement. My point is that I can make fog of war rendering basically free.

Your video looks really nice! Keep it up!

Concerning the appreciate link, were you logged in when taking that screenshot?

Myomyomyo.
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.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (44 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (32 views)
2014-09-21 01:30:30

moogie (40 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!