Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  vbo MapBuffer problem  (Read 1082 times)
0 Members and 1 Guest are viewing this topic.
Offline Irbis

JGO n00b
*

Posts: 16



« on: 2011-05-02 08: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 Neuromancer
****

Posts: 1174
Medals: 35



« Reply #1 on: 2011-05-02 09: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

JGO n00b
*

Posts: 16



« Reply #2 on: 2011-05-02 10:22:00 »

I changed my code but still nothing happens.
Games published by our own members! Go get 'em!
Offline lhkbob

JGO Neuromancer
****

Posts: 1174
Medals: 35



« Reply #3 on: 2011-05-02 11: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

JGO n00b
*

Posts: 16



« Reply #4 on: 2011-05-02 12: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 Neuromancer
****

Posts: 1174
Medals: 35



« Reply #5 on: 2011-05-02 16: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

JGO n00b
*

Posts: 16



« Reply #6 on: 2011-05-03 07: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 Neuromancer
****

Posts: 1174
Medals: 35



« Reply #7 on: 2011-05-03 10: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

JGO n00b
*

Posts: 16



« Reply #8 on: 2011-05-03 18: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 Neuromancer
****

Posts: 1174
Medals: 35



« Reply #9 on: 2011-05-04 11: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]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.126 seconds with 21 queries.