Jump to content
  • Advertisement

Recommended Posts

I'm currently working on a level editor, which will support editing meshes. I've been trying to duplicate a VAO and its VBOs, so I can then modify it, however, I'm stuck trying to duplicate the VAO's index buffer. I can copy the other VBOs just fine.

The first VBO is the first attribute (and the index buffer is created before any buffers for the attributes are, and so that is why I'm subtracting one from the first VBO).

            int vao = Loader.getInstance().createVAO();
            int firstVBO = 0;
            int[] vbos = new int[meshComponent.model.getMesh().getDimensions()];
    
            int count = meshComponent.model.getMesh().getVertexCount();
            {
                GL15.glBindBuffer(GL31.GL_COPY_READ_BUFFER, meshComponent.model.getMesh().getFirstVBO() - 1);
                int size = GL15.glGetBufferParameteri(GL31.GL_COPY_READ_BUFFER, GL15.GL_BUFFER_SIZE);
    
                int vbo = Loader.getInstance().createEmptyIndexBuffer(size / 4);
                GL15.glBindBuffer(GL31.GL_COPY_WRITE_BUFFER, vbo);
    
                GL31.glCopyBufferSubData(GL31.GL_COPY_READ_BUFFER, GL31.GL_COPY_WRITE_BUFFER, 0, 0, size);
            }
    
            for (int i = 0; i < vbos.length; i++) {
                int mul = 3; // the dimensions of the VBO
                if (i == 1)
                    mul = 2; // texture coordinates
    
                int vbo = Loader.getInstance().createEmptyVBO(meshComponent.model.getMesh().getVertexCount() * mul);
                if (i == 0)
                    firstVBO = vbo;
    
                GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
                GL20.glVertexAttribPointer(i, mul, GL11.GL_FLOAT, false, mul * 4, 0);
                GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
    
                GL15.glBindBuffer(GL31.GL_COPY_READ_BUFFER, meshComponent.model.getMesh().getFirstVBO() + i);
                GL15.glBindBuffer(GL31.GL_COPY_WRITE_BUFFER, vbo);
    
                GL31.glCopyBufferSubData(GL31.GL_COPY_READ_BUFFER, GL31.GL_COPY_WRITE_BUFFER, 0, 0, meshComponent.model.getMesh().getVertexCount() * mul);
            }
            GL15.glBindBuffer(GL31.GL_COPY_READ_BUFFER, 0);
            GL15.glBindBuffer(GL31.GL_COPY_WRITE_BUFFER, 0);
            Loader.getInstance().unbindVAO();

When I try to clone a cube, only the top and bottom faces are rendered (even with backface culling disabled). Here is a picture of the bug. If I divide size by 4 (as 4 bytes is an int) in the glCopyBufferSubData call, then only 3 triangles are copied (one is missing on the top face as well as all of the triangles on the side - picture here). Furthermore, using glGetBufferSubData on the index buffer (of both the copied and original VAOs) fails silently, however, it works on the attributes.

Thank you in advance for any help.

Share this post


Link to post
Share on other sites
Advertisement

The VAO vertex and index buffers id are independent of the VAO id. Granted I did not spend much time reading the code. Also pardon me for sounding harsh as there maybe a language misunderstanding also. So from the top ( and maybe this will help to better understand what you are trying to acheive .

1) What exactly do you mean by duplicating a VAO?
-VAO cannot be duplicated, each VAO needs its separate id.

2)What does cloning the cube mean?
If you mean being able to draw the same cube multiple times, then there is no need to create more that one instance of the cube and draw the same cube multiple times with different transform.

Share this post


Link to post
Share on other sites

I've got it working. It turns out the vertices, texture coordinates and normals array was being truncated (the arrays were much longer than I expected).

Just for some clarification, I wanted to copy the VBOs/data from an existing VAO into a new one, so then I could modify that VAO and its VBOs, without affecting the other models in the scene. For example, I could "clone" a cube's VAO, then skew and stretch that to make the handle for a staircase, and use cubes for steps. If they had the same VAO, then the steps, of course, will also be skewed and stretched.

I'm mainly going to be using this system to prototype/blockmesh in the editor, then export to 3ds max to UV map and clean up, then import back into the engine as an OBJ, and that way I won't have to worry about scales.

Share this post


Link to post
Share on other sites
3 hours ago, willepj said:

I've got it working.

That's great. I find I always learn a lot more in the process of working through problems like this, even if the things I learned didn't directly help me in solving my problem.

Also I noticed that your UI looks a lot like Qt. What windowing library do you use?

Share this post


Link to post
Share on other sites
8 minutes ago, fleabay said:

That's great. I find I always learn a lot more in the process of working through problems like this, even if the things I learned didn't directly help me in solving my problem.

Also I noticed that your UI looks a lot like Qt. What windowing library do you use?

It's written in Java, and so it uses JFrames and JPanels. The look and feel is Substance's Graphite skin. I made a few changes to the theme, which can be found here. Unfortunately, it's a "hard" fork and I can't remember what I changed.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!