Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
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  
  vbo - glDrawElements problem  (Read 3313 times)
0 Members and 1 Guest are viewing this topic.
Offline Irbis

Senior Newbie





« Posted 2011-08-12 12:57:10 »

I want to draw something using vbo and indices but my code doesn't work - I get a black screen. What's wrong ?

Creating vbo's:
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  
  vboVertices = new float[]{3.0f, 3.0f, 3.0f, 1.0f, 
                            -3.0f, 3.0f, 3.0f, 1.0f,
                            -3.0f, -3.0f, 3.0f, 1.0f,
                            3.0f, -3.0f, 3.0f, 1.0f,
                            -3.0f, 3.0f, -3.0f, 1.0f,
                            3.0f, 3.0f, -3.0f, 1.0f,
                            3.0f, -3.0f, -3.0f, 1.0f,
                            -3.0f, -3.0f, -3.0f, 1.0f};
       
  vboIndices = new byte[]{0,1, 0,3, 0,5,
                          1,4, 1,2, 2,7,
                          2,3, 3,6, 4,7,
                          4,5, 5,6, 6,7};

  int buffer[] = new int[2];
  gl.glGenBuffers(2, IntBuffer.wrap(buffer));
  vbo[0] = buffer[0];
  vbo[1] = buffer[1];

  gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vbo[0]);
  gl.glBufferData(GL3.GL_ARRAY_BUFFER, vboVertices.length * Sizeof.FLOAT, FloatBuffer.wrap(vboVertices), GL3.GL_DYNAMIC_DRAW);
  gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, 0);
       
  gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vbo[1]);
  gl.glBufferData(GL3.GL_ELEMENT_ARRAY_BUFFER, vboIndices.length * Sizeof.BYTE, ByteBuffer.wrap(vboIndices), GL3.GL_DYNAMIC_DRAW);
  gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, 0);


Rendering

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
gl.glUseProgram(shaderProgramID);
projectionMatrixUniform = gl.glGetUniformLocation(shaderProgramID, "pMatrix");
gl.glUniformMatrix4fv(projectionMatrixUniform, 1, false, projectionMatrix, 0);
modelViewMatrixUniform = gl.glGetUniformLocation(shaderProgramID, "mMatrix");
gl.glUniformMatrix4fv(modelViewMatrixUniform, 1, false, modelViewMatrix, 0);
colorUniform = gl.glGetUniformLocation(shaderProgramID, "color");
gl.glUniform4f(colorUniform, 1.0f, 1.0f, 1.0f, 1.0f);

gl.glEnableVertexAttribArray(0);
gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vbo[0]);
gl.glVertexAttribPointer(0, 4, GL3.GL_FLOAT, false, 0, 0);
gl.glBindBuffer(GL3.GL_ELEMENT_ARRAY_BUFFER, vbo[1]);
gl.glDrawElements(GL3.GL_LINES, 12, GL3.GL_BYTE, 0);
gl.glDisableVertexAttribArray(0);
gl.glUseProgram(0);


Offline theagentd
« Reply #1 - Posted 2011-08-12 13:46:43 »

Are you sure that the location of whatever attribute you are trying to set with
1  
gl.glVertexAttribPointer(0, 4, GL3.GL_FLOAT, false, 0, 0);
is 0? What does your shader look like?

Myomyomyo.
Offline Irbis

Senior Newbie





« Reply #2 - Posted 2011-08-12 14:25:22 »

Vertex shader:

1  
2  
3  
4  
5  
6  
7  
8  
9  
private static String vertexShader =
    "#version 330" + "\n" +
    "layout (location = 0) in vec4 position;" + "\n" +
    "uniform mat4 pMatrix;" + "\n" +
    "uniform mat4 mMatrix;" + "\n" +
    "void main()" + "\n" +
    "{" + "\n" +
    "gl_Position = pMatrix * mMatrix * position;" + "\n" +
    "}";


Fragment shader:

1  
2  
3  
4  
5  
6  
7  
8  
private static String fragmentShader =
    "#version 330" + "\n" +
    "out vec4 outputColor;" + "\n" +
    "uniform vec4 color;" + "\n" +  
    "void main()" + "\n" +
    "{" + "\n" +
    "outputColor = color;" + "\n" +
    "}";      
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 2011-08-12 16:43:22 »

Try using a DebugGL to see if you've made an OpenGL error, which prevent anything from rendering.

Offline Irbis

Senior Newbie





« Reply #4 - Posted 2011-08-12 18:29:26 »

Exception in thread "AWT-EventQueue-0-AWTAnimator-1" javax.media.opengl.GLException: javax.media.opengl.GLException: Thread[AWT-EventQueue-0,6,main] glGetError() returned the following error codes after a call to glBufferData(<int> 0x8893, <long> 24, <java.nio.Buffer> java.nio.HeapByteBuffer[pos=0 lim=24 cap=24], <int> 0x88E8): GL_INVALID_OPERATION ( 1282 0x502),
...

Caused by: javax.media.opengl.GLException: Thread[AWT-EventQueue-0,6,main] glGetError() returned the following error codes after a call to glBufferData(<int> 0x8893, <long> 24, <java.nio.Buffer> java.nio.HeapByteBuffer[pos=0 lim=24 cap=24], <int> 0x88E8): GL_INVALID_OPERATION ( 1282 0x502)

...

refer to:
1  
gl.glBufferData(GL3.GL_ELEMENT_ARRAY_BUFFER, vboFrameIndices.length * Sizeof.BYTE, ByteBuffer.wrap(vboFrameIndices), GL3.GL_DYNAMIC_DRAW);




Exception in thread "AWT-EventQueue-0" javax.media.opengl.GLException: Thread[AWT-EventQueue-0,6,main] glGetError() returned the following error codes after a call to glDrawElements(<int> 0x1, <int> 0xC, <int> 0x1400, <long> 0): GL_INVALID_ENUM ( 1280 0x500)
...

refer to:
1  
gl.glDrawElements(GL3.GL_LINES, 12, GL3.GL_BYTE, 0);

Offline lhkbob

JGO Knight


Medals: 32



« Reply #5 - Posted 2011-08-12 19:12:16 »

Regarding the 1st exception, you don't have an element array vbo bound when you call glBufferData.  In your code, change your call  GL_ARRAY_BUFFER to GL_ELEMENT_ARRAY_BUFFER in your calls to glBindBuffer() before and after the call glBufferData(GL_ELEMENT_ARRAY_BUFFER, ...).

Regarding the 2nd exception, the docs say that the INVALID_ENUM is generated when the mode argument (1st) is not an accepted value.  However, GL_LINES is valid.  I did notice that GL_BYTE is not an accepted type value, you can only use UNSIGNED_x types so you'd need to switch it to GL_UNSIGNED_BYTE.  I don't know why their documentation doesn't say that an unsupported type will cause an error, but it's likely the cause.

You don't need to worry about changing the byte values since BYTE and UNSIGNED_BYTE have the same bit patterns when they are from 0 to 127 (if you need values higher than that but still fit into 0-255, just cast the higher values to byte and it will work out for you).

Offline theagentd
« Reply #6 - Posted 2011-08-12 23:07:29 »

GL_BYTE being unsupported is completely logical. You can't have negative indices, can you?  Wink Good job spotting it though!

Myomyomyo.
Offline lhkbob

JGO Knight


Medals: 32



« Reply #7 - Posted 2011-08-12 23:27:16 »

GL_BYTE being unsupported is completely logical. You can't have negative indices, can you?  Wink Good job spotting it though!

I think its logical from a theoretical point of view, the docs just don't say an error is raised if you pass it in, which is what I thought was strange (expected it to be supported but not make sense, or have a documented error).

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 (52 views)
2014-10-17 03:59:02

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

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

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

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

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

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

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

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

BurntPizza (86 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!