Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (516)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (577)
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  
  Vertex Arrays and interleaved ByteBuffers  (Read 1216 times)
0 Members and 1 Guest are viewing this topic.
Offline matheus23

JGO Kernel


Medals: 110
Projects: 3


You think about my Avatar right now!


« Posted 2012-12-16 10:59:11 »

Hello guys Smiley

I've got a problem with my current VertexArray implementation. Not like most implementations in java it uses a ByteBuffer instead of a FloatBuffer. Probably this does not work, but it should, shouldn't it?

The code for the VertexArray class is the following:
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  
package org.matheusdev.arcengine.ogl;

import java.nio.ByteBuffer;
import java.util.List;

import javax.media.opengl.GL2;

import org.matheusdev.arcengine.utils.BufferUtils;

/**
 * @author matheusdev
 *
 */

public class VertexArray {

   protected final List<ShaderAttrib> attributes;
   protected final int totalComponents;
   protected final int totalByteLength;
   protected final ByteBuffer buffer;

   /**
    * Alias for <code>VertexArray(glslShader.getAttributes(), vertices)</code>.
    */

   public VertexArray(ShaderProg glslShader, int vertices) {
      this(glslShader.getAttributes(), vertices);
   }

   public VertexArray(List<ShaderAttrib> attributes, int vertices) {
      this.attributes = attributes;

      int components = 0;
      int stride = 0;
      for (ShaderAttrib attribute : attributes) {
         components += attribute.components;
         stride += attribute.getTypeByteSize();
      }
      this.totalComponents = components;
      this.totalByteLength = stride;
      this.buffer = BufferUtils.newByteBuffer(totalByteLength * vertices);
   }

   public void begin(GL2 gl) {
      gl.glEnableClientState(GL2.GL_ARRAY_BUFFER);

      int offset = 0;
      for (ShaderAttrib attribute : attributes) {
         buffer.position(offset);

         gl.glEnableVertexAttribArray(attribute.location);
         gl.glVertexAttribPointer(
               attribute.location,
               attribute.components,
               attribute.getDataType(),
               false,
               totalByteLength,
               buffer);

         System.out.printf("glEnableVertexAttribArray(%d)%n", attribute.location);
         System.out.printf("glVertexAttribPointer(%n\tindx: %d,%n\tsize: %d,%n\ttype: %d,%n\tnormalized: false,%n\tstride: %d,%n\tbuffer: %s(offset = %d));%n",
               attribute.location, attribute.components, attribute.getDataType(), totalByteLength, buffer, offset);

         offset += attribute.byteLength;
      }
   }

   public void render(GL2 gl, int mode, int offset, int count) {
      gl.glDrawArrays(mode, offset, count);
   }

   public void end(GL2 gl) {
      for (ShaderAttrib attribute : attributes) {
         gl.glDisableVertexAttribArray(attribute.location);
      }
      gl.glDisableClientState(GL2.GL_ARRAY_BUFFER);
   }

   @Override
   public String toString() {
      StringBuffer str = new StringBuffer();
      str.append(String.format("VertexArray[components: %d, stride: %d, attributes:\n", totalComponents, totalByteLength));
      for (ShaderAttrib attribute : attributes) {
         str.append("\t").append(attribute).append("\n");
      }
      str.append("];");
      return str.toString();
   }

   public ByteBuffer getBuffer() {
      return buffer;
   }

   public int getComponentSum() {
      return totalComponents;
   }

   public int getComponentByteSum() {
      return totalByteLength;
   }

}


I've tested the
ShaderProg
class, it works perfectly with
glBegin(); / glEnd();
. The array's
.toString();
prints the following:

Quote
VertexArray[components: 2, stride: 8, attributes:
   ShaderAttrib[layout (location = 0) vec2 aVertex; components: 2; size: 1; byte-size: 8]
];

So the attributes of the shader are loaded properly. The stride is in bytes and seems to be okay (components = 2 = vec2, all floats, which means stride = 2 components * 4 bytes = 8 bytes).

The ByteBuffer is properly filled by me, with this code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
      onlyVerts = new VertexArray(Arrays.asList(
            onlyVertShader.getAttribute("aVertex")), 4);
      ByteBuffer buf0 = onlyVerts.getBuffer();
      buf0
         .putFloat(256).putFloat(0)
         .putFloat(384).putFloat(0)
         .putFloat(384).putFloat(128)
         .putFloat(256).putFloat(128)
         .flip();


It's content is then tested with this and reset:
1  
2  
3  
4  
5  
while (buf0.hasRemaining()) {
   System.out.printf("Vertex: aVertex(%G, %G)%n",
         buf0.getFloat(), buf0.getFloat());
}
buf0.position(0);


This is getting printed:
Quote
Vertex: aVertex(256.000, 0.00000)
Vertex: aVertex(384.000, 0.00000)
Vertex: aVertex(384.000, 128.000)
Vertex: aVertex(256.000, 128.000)

Finally this is getting print from the VertexArray#begin(gl) method:
Quote
glEnableVertexAttribArray(0)
glVertexAttribPointer(
   indx: 0,
   size: 2,
   type: 5126,
   normalized: false,
   stride: 8,
   buffer: java.nio.DirectByteBuffer[pos=0 lim=32 cap=32](offset = 0));

The array is rendered like this:
1  
2  
3  
4  
onlyVertShader.use(gl);
onlyVerts.begin(gl);
onlyVerts.render(gl, GL2.GL_QUADS, 0, 1);
onlyVerts.end(gl);


The vertex shader is the following:
1  
2  
3  
4  
5  
6  
7  
uniform mat4 uViewProjMatrix;

attribute vec2 aVertex;

void main(void) {
   gl_Position = uViewProjMatrix * vec4(aVertex, 0.0, 1.0);
}


The fragment shader:
1  
2  
3  
4  
5  
uniform vec3 color;

void main(void) {
   gl_FragColor = vec4(color, 1.0);
}


The uniform "vec3 color" is set to (0, 1, 1); Setting uniforms works with my ShaderProg class, it is tested with glBegin(); / glEnd();.

The problem with this is, that NOTHING is drawn... just black. Other stuff drawn in the rendering loop with non-interleaved FloatBuffers and vertex arrays (not using the VertexArray class) and shaders work, and glBegin(); / glEnd(); work too.
Just the stuff which should be drawn with the VertexArray class is not drawn. Just black, transparent, nothing.

Probably this is due to the arguments I give to
glVertexAttribPointer(...);
, but I can't find whats wrong with it Sad

I really need help! I've googled for years, as it feels now, and I have this issue longer than a week, and I just can't find the cause...

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline theagentd
« Reply #1 - Posted 2012-12-16 13:52:50 »

What is this line supposed to do? I'm pretty sure it's throwing you an error.
1  
gl.glEnableClientState(GL2.GL_ARRAY_BUFFER);


Exactly what are your arguments to gl.glVertexAttribPointer()? I don't see you passing GL_FLOAT as data type anywhere...

Myomyomyo.
Offline matheus23

JGO Kernel


Medals: 110
Projects: 3


You think about my Avatar right now!


« Reply #2 - Posted 2012-12-16 15:57:37 »

What is this line supposed to do? I'm pretty sure it's throwing you an error.
1  
gl.glEnableClientState(GL2.GL_ARRAY_BUFFER);


Exactly what are your arguments to gl.glVertexAttribPointer()? I don't see you passing GL_FLOAT as data type anywhere...

Point 1: ehmm... don't you have to enable the array buffer if you want to draw with glVertexAttribPointer? ... I'll try to remove it...

Point 2: Oh yes, I forgot to mention, this is what I posted, would have been printed for the exact arguments:
Quote
glEnableVertexAttribArray(0)
glVertexAttribPointer(
   indx: 0,
   size: 2,
   type: 5126,
   normalized: false,
   stride: 8,
   buffer: java.nio.DirectByteBuffer[pos=0 lim=32 cap=32](offset = 0));
It shows "type: 5126" and GL_FLOAT is 0x1406, which is 5126...

I should have probably pointed that out and printed it with %x...

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2012-12-16 16:16:07 »

GL_ARRAY_BUFFER is the most common VBO type. It has nothing to do with vertex arrays and is not an accepted enum for glEnableClientState(). http://www.opengl.org/sdk/docs/man2/xhtml/glEnableClientState.xml

That looks fine assuming your attribute location is actually 0.

Anyway, I don't see you actually putting your data into buffer. Isn't it just a ByteBuffer filled with 0s right now? Never mind...

Oh. glDrawArrays() take in the number of vertices you want to draw, not the number of primitives. You have 4 vertices, not one. One vertex does not make a quad so it's silently discarded.

Myomyomyo.
Offline matheus23

JGO Kernel


Medals: 110
Projects: 3


You think about my Avatar right now!


« Reply #4 - Posted 2012-12-16 16:20:23 »

Yeah... you're right about the glEnableClientState... it throws an 1280 error (INVALID_ENUM)... I've removed it, but it's still nor working Sad

This works, btw, which is not an interleaved array:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
int aVertexAttrib = normalMapShader.getAttributeLocation("aVertex");
int aTexCoordsAttrib = normalMapShader.getAttributeLocation("aTexCoords");
int aNormCoordsAttrib = normalMapShader.getAttributeLocation("aNormCoords");
gl.glEnableVertexAttribArray(aVertexAttrib);
gl.glEnableVertexAttribArray(aTexCoordsAttrib);
gl.glEnableVertexAttribArray(aNormCoordsAttrib);
gl.glVertexAttribPointer(aVertexAttrib, 2, GL2.GL_FLOAT, false, 0, vert);
gl.glVertexAttribPointer(aTexCoordsAttrib, 2, GL2.GL_FLOAT, false, 0, texCoords);
gl.glVertexAttribPointer(aNormCoordsAttrib, 2, GL2.GL_FLOAT, false, 0, normCoords);

gl.glDrawArrays(GL2.GL_QUADS, 0, 4);

gl.glDisableVertexAttribArray(aVertexAttrib);
gl.glDisableVertexAttribArray(aTexCoordsAttrib);
gl.glDisableVertexAttribArray(aNormCoordsAttrib);

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline matheus23

JGO Kernel


Medals: 110
Projects: 3


You think about my Avatar right now!


« Reply #5 - Posted 2012-12-16 16:22:45 »

Oh. glDrawArrays() take in the number of vertices you want to draw, not the number of primitives. You have 4 vertices, not one. One vertex does not make a quad so it's silently discarded.

Oh god. THANK YOU! (ninja editor ^^)

Really... I can't say how much I love you ! ... This took me weeks... a programming mistake is ALWAYS easy... it works... thank you very much Smiley

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline theagentd
« Reply #6 - Posted 2012-12-16 16:48:40 »

Been there, done that too, mate. Wink

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.

TehJavaDev (32 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (41 views)
2014-10-26 19:37:11

Luminem (22 views)
2014-10-26 10:17:50

Luminem (27 views)
2014-10-26 10:14:04

theagentd (33 views)
2014-10-25 15:46:29

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

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

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

lcass (43 views)
2014-10-15 16:18:58
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!