Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (516)
Games in Android Showcase (122)
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 Attribute Arrays aren't working  (Read 2242 times)
0 Members and 1 Guest are viewing this topic.
Offline Ecumene

Senior Duke


Medals: 10



« Posted 2014-07-24 17:43:43 »

I think I've narrowed it down to a few methods, but my model render won't work. Immediate mode works with the shader bound and everything, but somethings wrong with the model/vertex attrib code. It just gives me a blank screen. So its not the shaders, or the shader loader. Also, I doubt its the vertex. But it might be the vertex attrib wrapper.

(Model.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  
/**
 * Links the model's vertex data with the vertex attribute objects
 *
 * @param positions The vertex positions vertex attribute ID
 * @param textures The vertex texture coordinate vertex attribute ID
 * @param colors The vertex color vertex attribute ID
 * @param normals The vertex normal vertex attribute ID
 */

public void link(int positions, int textures, int colors, int normals){
   float[] vertexPos = new float[model.length * 4];
   float[] textureUV = new float[model.length * 2];
   float[] color = new float[model.length * 4];
   float[] normal = new float[model.length * 3];
   
   for(int i = 0; i < model.length; i++){
      vertexPos[4*i+0] = model[i].position.x;
      vertexPos[4*i+1] = model[i].position.y;
      vertexPos[4*i+2] = model[i].position.z;
      vertexPos[4*i+3] = model[i].position.w;
     
      textureUV[2*i+0] = model[i].textureUV.x;
      textureUV[2*i+1] = model[i].textureUV.x;
     
      color[4*i+0] = model[i].color.getRed();
      color[4*i+1] = model[i].color.getGreen();
      color[4*i+2] = model[i].color.getBlue();
      color[4*i+3] = model[i].color.getAlpha();
     
      normal[3*i+0] = model[i].normal.x;
      normal[3*i+1] = model[i].normal.y;
      normal[3*i+2] = model[i].normal.z;
   }
   
   FloatBuffer indicesBuffer = BufferUtils.createFloatBuffer(indices.length);
   indicesBuffer.put(indices);
   indicesBuffer.flip();
   
   va = GL15.glGenBuffers();
   GL30.glBindVertexArray(va);
      this.positions = new VertexAttrib(positions, 4, vertexPos);
      this.textureUVs = new VertexAttrib(textures, 2, textureUV);
      this.colors = new VertexAttrib(colors, 4, color);
      this.normals = new VertexAttrib(normals, 3, normal);
   GL30.glBindVertexArray(0);
   
   vi = GL15.glGenBuffers();
   GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vi);
   GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL15.GL_STATIC_DRAW);
   GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
/**
 * Draws the vertex attribute object
 * @param mode The mode to draw
 */

public void render(int mode){
   GL30.glBindVertexArray(va);
      positions.bind();
      textureUVs.bind();
      colors.bind();
      normals.bind();
         GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vi);
         GL11.glDrawElements(mode, indices.length, GL11.GL_UNSIGNED_BYTE, 0);
         GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
      positions.unbind();
      textureUVs.unbind();
      colors.unbind();
      normals.unbind();
   GL30.glBindVertexArray(0);
}


My render method:
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  
ambient = new Shader();
ambient.attachShaderSource(new File("./shaders/ambient.fs"), GL20.GL_FRAGMENT_SHADER);
ambient.attachShaderSource(new File("./shaders/ambient.vs"), GL20.GL_VERTEX_SHADER);

ambient.bindAttribute(0, "in_Position");
ambient.bindAttribute(1, "in_TextureUV");
ambient.bindAttribute(2, "in_Color");
ambient.bindAttribute(3, "in_Normal");
ambient.link();

model = new Model(4);
model.add(0, new Vertex(new Vector4f(0, 1, 0, 1), new Color(1, 0, 0, 1)));
model.add(1, new Vertex(new Vector4f(0, 0, 0, 1), new Color(0, 0, 1, 1)));
model.add(2, new Vertex(new Vector4f(1, 0, 0, 1), new Color(0, 1, 0, 1)));
model.add(3, new Vertex(new Vector4f(1, 1, 0, 1), new Color(1, 1, 1, 1)));
model.setDrawingOrder(0, 1, 2, 2, 3, 0);
model.link(0, 1, 2, 3);

while(!Window.isClosing()){
   GL11.glClearColor(0.5f, 0.5f, 0.5f, 1);
   GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
   
   ambient.bind();
   model.render(GL11.GL_TRIANGLES);
   Shader.unbind();
     
   Window.update();
}


Full "VertexAttrib.class": http://pastebin.com/iBy23Akb
Full "Model.class": http://pastebin.com/XFCxV9gk
Full "Vertex.class": http://pastebin.com/B0X5kryZ
Full "ambient.* ": http://pastebin.com/CbsanRWB
Full "Shader.class": http://pastebin.com/uCWpyi3Q

Screenie:


You acknowledge that this software is not designed, licensed or intended for use in the design, construction, operation or maintenance of any nuclear facility.
Offline SHC
« Reply #1 - Posted 2014-07-24 18:01:13 »

This is where you are having problem.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
GL30.glBindVertexArray(va);
      positions.bind();
      textureUVs.bind();
      colors.bind();
      normals.bind();
         GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vi);
         GL11.glDrawElements(mode, indices.length, GL11.GL_UNSIGNED_BYTE, 0);
         GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
      positions.unbind();
      textureUVs.unbind();
      colors.unbind();
      normals.unbind();
   GL30.glBindVertexArray(0);

That's not how you are supposed to bind the buffers. Do like this in the order.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
positions.bind();
glVertexAttribPointer(0, 4, GL_FLOAT, false, 0, 0);

textureUVs.bind();
glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0);

colors.bind();
glVertexAttribPointer(2, 4, GL_FLOAT, false, 0, 0);

normals.bind();
glVertexAttribPointer(3, 3, GL_FLOAT, true, 0, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vi);
glDrawElements(mode, indices.length, GL_UNSIGNED_BYTE, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

glBindBuffer(GL_ARRAY_BUFFER, 0);

The problem is you are binding your VBOs but not setting the pointers to them. Hope this helps.

Offline Ecumene

Senior Duke


Medals: 10



« Reply #2 - Posted 2014-07-24 18:05:50 »

The problem is you are binding your VBOs but not setting the pointers to them. Hope this helps.

VertexAttrib.class constructor:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
/**
 * Initializes the vertex attribute object with some data
 * to put into its buffer
 *
 * @param floats The data to add
 */

public VertexAttrib(int index, int size, boolean normalize, float ... floats) {
   data = BufferUtils.createFloatBuffer(floats.length);
   data.put(floats);
   data.flip();
   
   id = GL15.glGenBuffers();
   GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, id);
   GL15.glBufferData(GL15.GL_ARRAY_BUFFER, data, GL15.GL_STATIC_DRAW);
   GL20.glVertexAttribPointer(index, size, GL11.GL_FLOAT, normalize, 0, 0);
   
   this.index = index;
}


I'm using the pointers inside the 'vertexattrib' class. The pointer methods are being called. Thanks for the tip about normalized... normals!

You acknowledge that this software is not designed, licensed or intended for use in the design, construction, operation or maintenance of any nuclear facility.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SHC
« Reply #3 - Posted 2014-07-24 18:08:24 »

There's a problem too.

1  
2  
3  
4  
public void bind(){
   GL20.glEnableVertexAttribArray(index);
    System.out.println("enabled vaa: " + index);
}

You are just enabling the location but not making a pointer to it.

EDIT:

Found that you are setting the pointer in the constructor. Looking for other sources of the error...

Offline SHC
« Reply #4 - Posted 2014-07-24 18:13:35 »

You are creating the indices buffer as a FloatBuffer, but you are using GL_UNSIGNED_BYTE in the call to glDrawElements. Maybe changing that to GL_UNSIGNED_FLOAT should work.

Offline Ecumene

Senior Duke


Medals: 10



« Reply #5 - Posted 2014-07-24 18:20:35 »

You are creating the indices buffer as a FloatBuffer, but you are using GL_UNSIGNED_BYTE in the call to glDrawElements. Maybe changing that to GL_UNSIGNED_FLOAT should work.

There is no
GL_UNSIGNED_FLOAT
. Also, I have a working example of a VBO, and that's what it says to do :-/

You acknowledge that this software is not designed, licensed or intended for use in the design, construction, operation or maintenance of any nuclear facility.
Offline SHC
« Reply #6 - Posted 2014-07-24 18:21:46 »

Oops, my bad. I thought of saying GL_UNSIGNED_SHORT and make it a ShortBuffer.

Offline Ecumene

Senior Duke


Medals: 10



« Reply #7 - Posted 2014-07-24 18:25:30 »

Who ho! It works! Turns out, the indices were supposed to be integers. So you where right, the buffer was the wrong type. I bookmarked the page to give you another medal later. Thanks!

Screenie:

You acknowledge that this software is not designed, licensed or intended for use in the design, construction, operation or maintenance of any nuclear facility.
Offline SHC
« Reply #8 - Posted 2014-07-24 18:26:49 »

Glad that you got it working. Is that green quad your model?

Offline Ecumene

Senior Duke


Medals: 10



« Reply #9 - Posted 2014-07-24 18:28:31 »

And is that green quad your model?

Yeah, I didn't do the matrices/camera yet. So thats why it still has the default 0-1 coordinate system. Its green because of the shaders, I just have to change a name for that. Thanks!

You acknowledge that this software is not designed, licensed or intended for use in the design, construction, operation or maintenance of any nuclear facility.
Pages: [1]
  ignore  |  Print  
 
 

 

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!