Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (546)
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  
  Interleaved VBO problems.  (Read 628 times)
0 Members and 1 Guest are viewing this topic.
Offline trollwarrior1
« Posted 2014-01-02 15:07:52 »

So I got this code. I get black screen as my output. When I render with immediate rendering mode, works nice and all that. With this code, I get black screen.

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  
         int size = assets.size();
         int vertices = 4;
         int vertexSize = 2;
         int textureSize = 2;
         int colorSize = 4;

         int overallSize = size * (vertices * (vertexSize + textureSize + colorSize));

         FloatBuffer buffer = BufferUtils.createFloatBuffer(overallSize);

         for (Asset asset : assets) {

            // class Asset contains: float x, y, width, height, px, py, rot; Sprite sprite; Color color;
           
            Rect rect = new Rect(asset.x, asset.y, asset.width, asset.height); // contains 4 vertices, 8 points. x0, y0, x1, y1, x2, y2, x3, y3
           rect.rotate(asset.rot, new vec2(asset.px, asset.py));

            Sprite sprite = asset.sprite; // contains: int width, height; SpriteSheet sheet;
           
            //SpriteSheet is basically a class which contains texture handle.
           
            float x0 = (1f / sprite.sheet.width) * sprite.width * sprite.x; // Texture position x0
           float y0 = (1f / sprite.sheet.height) * sprite.height * sprite.y; // Texture position y0

            float x1 = (1f / sprite.sheet.width) * sprite.width * (sprite.x + 1); // Texture position x1
           float y1 = (1f / sprite.sheet.height) * sprite.height * (sprite.y + 1); // Texture position y1

            Color color = asset.color; // contains r,g,b,a floats.
           
           
           
            buffer.put(rect.x0).put(rect.y0);
            buffer.put(x0).put(y0);
            buffer.put(color.r).put(color.g).put(color.b).put(color.a);
           
            buffer.put(rect.x1).put(rect.y1);
            buffer.put(x1).put(y0);
            buffer.put(color.r).put(color.g).put(color.b).put(color.a);
           
            buffer.put(rect.x2).put(rect.y2);
            buffer.put(x1).put(y1);
            buffer.put(color.r).put(color.g).put(color.b).put(color.a);
           
            buffer.put(rect.x3).put(rect.y3);
            buffer.put(x0).put(y1);
            buffer.put(color.r).put(color.g).put(color.b).put(color.a);
           
         }

         for (Asset asset : assets) {
           
         }

         for (Asset asset : assets) {
           
         }

         SpriteSheet.sheet.bind(); // Bind the texture.
       
         glBindBuffer(target, handle);
         glBufferData(target, buffer, GL_STATIC_DRAW);

         glEnableClientState(GL_VERTEX_ARRAY);
         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
         glEnableClientState(GL_COLOR_ARRAY);

//         x1, y1, s1, t1, r1, g1, b1, a1, x2, y2,
        int stride = 8 * 4;
         glVertexPointer(vertexSize, GL_FLOAT, stride, 0L);
         glTexCoordPointer(textureSize, GL_FLOAT, stride, 8L);
         glColorPointer(colorSize, GL_FLOAT, stride, 16L);
         
         glDrawArrays(GL_QUADS, 0, size * vertices);

         glDisableClientState(GL_VERTEX_ARRAY);
         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
         glDisableClientState(GL_COLOR_ARRAY);


I will post a fix if I find it before someone helps me.
Offline SHC
« Reply #1 - Posted 2014-01-02 15:24:23 »

Rewind the buffer before sending to OpenGL.

1  
buffer.rewind();

Offline trollwarrior1
« Reply #2 - Posted 2014-01-02 15:27:52 »

Rewind the buffer before sending to OpenGL.

1  
buffer.rewind();


Thanks, forgot that part again.. However, it didn't solve the problem. Cry
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #3 - Posted 2014-01-02 15:44:32 »

Does
assets.size()
return the amount of assets? Shouldn't you call
assets.length
instead? I can't see an issue otherwise.

Why are all OpenGL tutorials written in Brainf**k?
Offline trollwarrior1
« Reply #4 - Posted 2014-01-02 15:49:50 »

Does
assets.size()
return the amount of assets? Shouldn't you call
assets.length
instead? I can't see an issue otherwise.

It is a List. It is not an array.
Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #5 - Posted 2014-01-02 15:57:26 »

Oops that was stupid of me to say. I forgot that lists return the length from size().
I can't see a problem with your code; I'm pretty sure that it should work. Are you sure that your vertices/texture coordinates are between the ranges -1 and 1?

Why are all OpenGL tutorials written in Brainf**k?
Offline trollwarrior1
« Reply #6 - Posted 2014-01-02 16:00:06 »

Oops that was stupid of me to say. I forgot that lists return the length from size().
I can't see a problem with your code; I'm pretty sure that it should work. Are you sure that your vertices/texture coordinates are between the ranges -1 and 1?

Well with this rendering code, everything works fine.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
         Rect rect = new Rect(x, y, width, height);
         rect.rotate(rotation, new vec2(px, py));

         glColor4f(color.r, color.g, color.b, color.a);

         float x0 = (1f / sprite.sheet.width) * sprite.width * sprite.x;
         float y0 = (1f / sprite.sheet.height) * sprite.height * sprite.y;

         float x1 = (1f / sprite.sheet.width) * sprite.width * (sprite.x + 1);
         float y1 = (1f / sprite.sheet.height) * sprite.height * (sprite.y + 1);

         glTexCoord2f(x0, y0);
         glVertex2f(rect.x0 + xo, rect.y0 + yo);

         glTexCoord2f(x1, y0);
         glVertex2f(rect.x1 + xo, rect.y1 + yo);

         glTexCoord2f(x1, y1);
         glVertex2f(rect.x2 + xo, rect.y2 + yo);

         glTexCoord2f(x0, y1);
         glVertex2f(rect.x3 + xo, rect.y3 + yo);


if you're saying code looks fine, I will just have to look for some stupid little error Tongue
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #7 - Posted 2014-01-02 16:35:28 »

Vertex coordinates don't have to be constrained to -1 - 1. They can be as large as you like.

Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #8 - Posted 2014-01-02 16:42:47 »

Vertex coordinates don't have to be constrained to -1 - 1. They can be as large as you like.

Yes, but NDCs range from -1 to 1. I assumed the OP wasn't using glOrtho or managing his own matrices, but if he was and he was ranging his co-ordinates between -1 and 1, maybe that's his issue.

Why are all OpenGL tutorials written in Brainf**k?
Offline trollwarrior1
« Reply #9 - Posted 2014-01-02 21:25:09 »

I can create single VBOs just fine. The problem comes when I try to make an interleaved VBO. Just doesn't work Cry
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Screem
« Reply #10 - Posted 2014-01-03 00:11:20 »

I only know some basics about OpenGL, but

1  
2  
3  
4  
glVertexPointer(vertexSize, GL_FLOAT, stride, 0L);
glTexCoordPointer(textureSize, GL_FLOAT, stride, 8L); // 8L should be a pointer to the first vertex in the array
// so it might have to be (first tex coord location * bytes in a float) = (3 * 4) = 12 instead.
glColorPointer(colorSize, GL_FLOAT, stride, 16L); // Same here, should be (5 * 4) = 20 instead


Source: http://www.opengl.org/sdk/docs/man2/xhtml/glVertexPointer.xml

Hope this helps. Cheesy

Offline Mike

JGO Ninja


Medals: 71
Projects: 1
Exp: 6 years


Java guru wanabee


« Reply #11 - Posted 2014-01-03 00:14:33 »

Best. Post. Ever.

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

Mike

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline SHC
« Reply #12 - Posted 2014-01-03 03:37:03 »

The stride is the length of the tuple in bytes. So since your tuple is

1  
.... b, a, {x, y, s, t, r, g, b, a}, x, y ....

The number of elements in each tuple is 8 and 4 is the size of a float in bytes. So
stride
will be 8*4 which is 32. You got it right.

Now let's see about the
offset
, it's the location of first vertex in the array. So for your tuple,

1  
2  
3  
4  
5  
6  
7  
8  
9  
// The first ones in your tuple is the vertex info
SO OFFSET IS 0

// Next comes the texture coordinates which start after the two vertex coordinates
SO OFFSET IS 3*4 WHICH IS 12

// Next there are colour coordinates which start after two vertex coordinates
// and two texture coordinates.
SO OFFSET IS 5*4 WHICH IS 20

So you change the following code

1  
2  
3  
glVertexPointer(vertexSize, GL_FLOAT, stride, 0L);
glTexCoordPointer(textureSize, GL_FLOAT, stride, 8L);
glColorPointer(colorSize, GL_FLOAT, stride, 16L);

To the following code.

1  
2  
3  
glVertexPointer(vertexSize, GL_FLOAT, stride, 0L);
glTexCoordPointer(textureSize, GL_FLOAT, stride, 12L);
glColorPointer(colorSize, GL_FLOAT, stride, 20L);

Hope this solves your problem.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #13 - Posted 2014-01-03 06:25:19 »


Thanks. I really ought to add the shader / vertex-attributes way, one day. Pointing

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #14 - Posted 2014-01-03 06:32:35 »

So you change the following code

1  
2  
3  
glVertexPointer(vertexSize, GL_FLOAT, stride, 0L);
glTexCoordPointer(textureSize, GL_FLOAT, stride, 8L);
glColorPointer(colorSize, GL_FLOAT, stride, 16L);

To the following code.

1  
2  
3  
glVertexPointer(vertexSize, GL_FLOAT, stride, 0L);
glTexCoordPointer(textureSize, GL_FLOAT, stride, 12L);
glColorPointer(colorSize, GL_FLOAT, stride, 20L);

Hope this solves your problem.
trollwarrior1s offsets and strides are correct. It's indeed 0,8,16 with a stride of 32. Pointing

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.

atombrot (22 views)
2014-08-19 09:29:53

Tekkerue (22 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (57 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (29 views)
2014-08-08 02:01:56

Norakomi (36 views)
2014-08-06 19:49:38

BurntPizza (66 views)
2014-08-03 02:57:17
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!