Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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 MapBuffer problem  (Read 2301 times)
0 Members and 1 Guest are viewing this topic.
Offline Irbis

Senior Newbie





« Posted 2011-05-02 12:38:27 »

Hi! I use JOGL 1.1.1.a. I can draw my cube using vbo but when I want to animate the cube I have a problem with change vertices position.

Init VBO
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
gl.glGenBuffers(bufferSize, IntBuffer.wrap(buffer));

verticesID = buffer[0];
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, verticesID);
gl.glBufferData(GL.GL_ARRAY_BUFFER, BufferUtil.SIZEOF_FLOAT * vertices.capacity(), vertices, GL.GL_DYNAMIC_DRAW);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);

normalsID = buffer[1];
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, normalsID);
gl.glBufferData(GL.GL_ARRAY_BUFFER, BufferUtil.SIZEOF_FLOAT * normals.capacity(), normals, GL.GL_DYNAMIC_DRAW);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);

gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL.GL_NORMAL_ARRAY);


Display and Update Vertices
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  
gl.glPushMatrix();

gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT, material.getAmbient(), 0);
gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_DIFFUSE, material.getDiffuse(), 0);
gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_SPECULAR, material.getSpecular(), 0);
gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_EMISSION, material.getEmission(), 0);
gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_SHININESS, material.getShininess(), 0);

gl.glBindBuffer(GL.GL_ARRAY_BUFFER, verticesID);
gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);

gl.glBindBuffer(GL.GL_ARRAY_BUFFER, normalsID);
gl.glNormalPointer(GL.GL_FLOAT, 0, 0);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);

gl.glDrawArrays(GL.GL_QUADS, 0, 24);

gl.glBindBuffer(GL.GL_ARRAY_BUFFER, verticesID);

byteBuffer = gl.glMapBuffer(GL.GL_ARRAY_BUFFER, GL.GL_READ_WRITE);
if(bufferRW != null)
{
     srcVertices = byteBuffer.asFloatBuffer();
     
     vertices.rewind();
           
     for(int i = 0; i < 24; i++)
     {
          vX = srcVertices.get();
          vY = srcVertices.get();
          vZ = srcVertices.get();

          vertices.put(vX);
          vertices.put(vY + 0.2f);
          vertices.put(vZ);
     }
}
gl.glUnmapBuffer(GL.GL_ARRAY_BUFFER);
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
gl.glPopMatrix();


The cube should move up but nothing happens. What is wrong with my code ?
Offline lhkbob

JGO Knight


Medals: 32



« Reply #1 - Posted 2011-05-02 13:45:16 »

You are writing to the vertices buffer but srcVertices and bytBuffer are the two mapped buffers.  You need to write to either byteBuffer or srcVertices to make the graphics card see the data.  In your for loop, just do:
1  
2  
3  
4  
5  
6  
7  
vX = vertices.get();
vY= vertices.get();
vZ = vertices.get();

srcVertices.put(vX);
srcVertices.put(vY + .2f);
srcVertices.put(vZ);

Offline Irbis

Senior Newbie





« Reply #2 - Posted 2011-05-02 14:22:00 »

I changed my code but still nothing happens.
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-05-02 15:45:35 »

Can you post all of your code, so I can copy it and try giving it a run, this is something where it's easier for me to experiment with directly than brainstorm.

Offline Irbis

Senior Newbie





« Reply #4 - Posted 2011-05-02 16:56:19 »

Netbeans 6.7.1 + JOGL 1.1.1.a (plugin). Complete Netbeans project: http://www.speedyshare.com/files/28257488/SimpleGLCanvas.rar
Offline lhkbob

JGO Knight


Medals: 32



« Reply #5 - Posted 2011-05-02 20:15:44 »

I should have seen this sooner, but the problem is that you want to read and write from srcVertices only.  In your method, you read from srcVertices correctly but didn't pass the modified values back to the mapped buffer. Since I just swapped your code around a bit, you would keep reading un-updated values from vertices and storing them into the mapped buffer correctly.

Here is a code block that works:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
srcVertices = byteBuffer.asFloatBuffer(); 
srcVertices.rewind();                    
                                         
for(int i = 0; i < 24; i++)              
{                                                                            
    vX = srcVertices.get(i * 3);          
    vY = srcVertices.get(i * 3 + 1);      
    vZ = srcVertices.get(i * 3 + 2);      
                                         
    srcVertices.put(i * 3, vX);          
    srcVertices.put(i * 3 + 1, vY + 0.5f);
    srcVertices.put(i * 3 + 2, vZ);                                        
}                                        


This should get mapped buffers working.  I do want to say that this isn't the best way to animate a cube.  If you're just experimenting with mapped buffers, that's fine, but you should know that there are transform matrices that you can modify to achieve the same translation effects without needing to map memory from the graphics card into a buffer.

Offline Irbis

Senior Newbie





« Reply #6 - Posted 2011-05-03 11:24:30 »

Thank you. I examine how VBO works. It is good thing when you have many objects, for example in some physics simulations. However a transfer of data between a gpu memory and client memory might be a bottleneck. I want to use JCUDA to interact with JOGL. Then VBO cooperate with JCUDA and a data transfer between the client and server is needless. What do you mean by transform matrices ? Could you show some example? JOGL 1.1.1.a supports opengl 3.0. How big are difference between using VBO in JOGL 2 and JOGL 1.1.1.a ?
Offline lhkbob

JGO Knight


Medals: 32



« Reply #7 - Posted 2011-05-03 14:55:08 »

Using VBOs is not really different from JOGL2 and JOGL1.1.1a.  They both wrap OpenGL, the main difference is that JOGL2 is the latest version and is still being maintained and updated so I would recommend switching to it.  It shouldn't be difficult to change over, in most cases you'll switch GL to GL2 and it's as simple as that.

As far as transforms go, every vertex that is rendered is multiplied with a 4x4 transform matrix. This lets the vertex be rotated and translated.  You control these with commands such glTranslate, glRotate, glMultMatrix, and glLoadMatrix.  If you haven't learned about these concepts in OpenGL yet, I would recommend reading some OpenGL tutorials (even if they're in C/C++ since the concepts are the same) before worrying about JCUDA.

Offline Irbis

Senior Newbie





« Reply #8 - Posted 2011-05-03 22:07:30 »

I know these concepts (that are fundamentals of OpenGL), but I wrong understood you. My English isn't perfect. I assumed that using glTranslate to animate is not a good idea. Of course when I only want to animate the cube and nothing else happens I can use glTranslate instead of glMapBuffer to achieve the same effect:

1  
2  
3  
4  
5  
gl.glPushMatrix();
gl.glTranslatef(0.0f, y, 0.0f);
gl.glDrawArrays(GL.GL_QUADS, 0, 24);
gl.glPopMatrix();
y += 0.5f;


In more complex situation, for example in same physics simulation where many object interact with each other using glTranslate to animate objects seems to be unsuitable. I'm going to work in JOGL 2, because I want to learn opengl 3.3 (and subsequent) features. I know a few very good opengl tutorials but first I need to make good JOGL2 framework in Netbeans. Which JOGL2 tutorial can you recommend me ?
Offline lhkbob

JGO Knight


Medals: 32



« Reply #9 - Posted 2011-05-04 15:01:26 »

The transform matrices are a great way to animate complete objects, like the position of a player in the world.  In physics, if it's a rigid body simulation, you only need to use the transform matrices.  When you do cloth simulation, then you'll want to use mapped buffers to animate the actual geometry.

I didn't use a tutorial for JOGL2 ever, I just looked at the Javadocs and found it pretty straightforward to change from JOGL1 to JOGL2. But here is the wiki for their mainsite, it will probably be helpful enough: http://jogamp.org/wiki/index.php/Main_Page

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.

Mr.CodeIt (24 views)
2014-12-23 03:34:11

rwatson462 (55 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (91 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (83 views)
2014-12-05 12:39:02

SHC (92 views)
2014-12-03 16:27:13

CopyableCougar4 (101 views)
2014-11-29 21:32:03

toopeicgaming1999 (160 views)
2014-11-26 15:22:04

toopeicgaming1999 (164 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

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
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!