LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: oggs91 on December 16, 2011, 19:55:23

Title: VAO and VBO
Post by: oggs91 on December 16, 2011, 19:55:23
i ran into some problems with my Model (or better Mesh) class
i got this working under OpenTK in C# but want to port it to java

is there a difference in lwjgl if i use GL20.glBindBuffer() or ARBVertexBufferObject.glBindBufferARB() ??
OpenTK has just one namespace for those methods

The applications shows the window with correct clear color but crashes if i stop it. -> access violation
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00000000083bcc5b, pid=1760, tid=7680
#
# JRE version: 7.0-b147
# Java VM: Java HotSpot(TM) 64-Bit Server VM (21.0-b17 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [ig4icd64.dll+0x19cc5b]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Users\Dominik\workspace\OpenGL1\hs_err_pid1760.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.sun.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

if i comment out both calls of glVertexAttribPointer the application doesn't crash the jvm at exit anymore, but still doesn't draw anything, but this might be a problem of my Camera class

Not working java code:

package Graphix;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;

public class Model
{
private int vaoHandle;

public Model()
{
int positionVboHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, positionVboHandle);
FloatBuffer positionBuffer = ByteBuffer
.allocateDirect(positionVboData.length * 4)
.order(ByteOrder.nativeOrder()).asFloatBuffer()
.put(positionVboData);
positionBuffer.flip();
glBufferData(GL_ARRAY_BUFFER, positionBuffer, GL_STATIC_DRAW);

assert positionVboHandle == 0;

int normalVboHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, normalVboHandle);
FloatBuffer normalBuffer = ByteBuffer
.allocateDirect(positionVboData.length * 4)
.order(ByteOrder.nativeOrder()).asFloatBuffer()
.put(positionVboData);
normalBuffer.flip();
glBufferData(GL_ARRAY_BUFFER, normalBuffer, GL_STATIC_DRAW);

assert normalVboHandle == 0;

int eboHandle = glGenBuffers();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboHandle);
IntBuffer ib = ByteBuffer.allocateDirect(indicesVboData.length * 4)
.order(ByteOrder.nativeOrder()).asIntBuffer()
.put(indicesVboData);
ib.flip();
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ib, GL_STATIC_DRAW);

assert eboHandle == 0;

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

vaoHandle = glGenVertexArrays();
glBindVertexArray(vaoHandle);

assert vaoHandle == 0;

// Vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, positionVboHandle);
glVertexAttribPointer(0, 3, GL_FLOAT, true, Float.SIZE / 8 * 3, 0); // Cause
// Access
// Violation

// Normals
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, normalVboHandle);
glVertexAttribPointer(1, 3, GL_FLOAT, true, Float.SIZE / 8 * 3, 0); // Cause
// Access
// Violation

// Indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboHandle);

glBindVertexArray(0);
}

public void Draw()
{
glBindVertexArray(vaoHandle);
glDrawElements(GL_TRIANGLES, indicesVboData.length, GL_INT, 0);
}

private float[] positionVboData = new float[]{-1.0f, -1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f,
-1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f};

private int[] indicesVboData = new int[]{
// front face
0, 1, 2, 2, 3, 0,
// top face
3, 2, 6, 6, 7, 3,
// back face
7, 6, 5, 5, 4, 7,
// left face
4, 0, 3, 3, 7, 4,
// bottom face
0, 1, 5, 5, 4, 0,
// right face
1, 5, 6, 6, 2, 1,};
}



Working C# Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using System.Diagnostics;

namespace OpenTkTest2
{
    public class Model
    {
        public Matrix4 transformation=new Matrix4(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1);

        public int positionVboHandle,
            normalVboHandle,
            eboHandle,
            vaoHandle;

        public Model()
        {
            CreateVBOs();

            GL.GenVertexArrays(1, out vaoHandle);
            GL.BindVertexArray(vaoHandle);

            //Vertices
            GL.EnableVertexAttribArray(0);
            GL.BindBuffer(BufferTarget.ArrayBuffer, positionVboHandle);
            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, true, Vector3.SizeInBytes, 0);

            //Normals
            GL.EnableVertexAttribArray(1);
            GL.BindBuffer(BufferTarget.ArrayBuffer, normalVboHandle);
            GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, true, Vector3.SizeInBytes, 0);

            //Indices
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, eboHandle);

            GL.BindVertexArray(0);
        }

        Vector3[] positionVboData = new Vector3[]{
            new Vector3(-1.0f, -1.0f,  1.0f),
            new Vector3( 1.0f, -1.0f,  1.0f),
            new Vector3( 1.0f,  1.0f,  1.0f),
            new Vector3(-1.0f,  1.0f,  1.0f),
            new Vector3(-1.0f, -1.0f, -1.0f),
            new Vector3( 1.0f, -1.0f, -1.0f),
            new Vector3( 1.0f,  1.0f, -1.0f),
            new Vector3(-1.0f,  1.0f, -1.0f) };

        int[] indicesVboData = new int[]{
             // front face
                0, 1, 2, 2, 3, 0,
                // top face
                3, 2, 6, 6, 7, 3,
                // back face
                7, 6, 5, 5, 4, 7,
                // left face
                4, 0, 3, 3, 7, 4,
                // bottom face
                0, 1, 5, 5, 4, 0,
                // right face
                1, 5, 6, 6, 2, 1, };

        void CreateVBOs()
        {
            GL.GenBuffers(1, out positionVboHandle);
            GL.BindBuffer(BufferTarget.ArrayBuffer, positionVboHandle);
            GL.BufferData<Vector3>(BufferTarget.ArrayBuffer,
                new IntPtr(positionVboData.Length * Vector3.SizeInBytes),
                positionVboData, BufferUsageHint.StaticDraw);

            GL.GenBuffers(1, out normalVboHandle);
            GL.BindBuffer(BufferTarget.ArrayBuffer, normalVboHandle);
            GL.BufferData<Vector3>(BufferTarget.ArrayBuffer,
                new IntPtr(positionVboData.Length * Vector3.SizeInBytes),
                positionVboData, BufferUsageHint.StaticDraw);

            GL.GenBuffers(1, out eboHandle);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, eboHandle);
            GL.BufferData(BufferTarget.ElementArrayBuffer,
                new IntPtr(sizeof(uint) * indicesVboData.Length),
                indicesVboData, BufferUsageHint.StaticDraw);

            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
        }

        public Matrix4 Transformation()
        {
            return transformation;
        }

        public void Translate(Matrix4 trans)
        {
            transformation *= trans;
        }

        public void Rotate(Matrix4 rot)
        {
 
        }

        public void Draw()
        {
            GL.BindVertexArray(vaoHandle);
            GL.DrawElements(BeginMode.Triangles, indicesVboData.Length,
                    DrawElementsType.UnsignedInt, IntPtr.Zero);
        }
    }
}

Title: Re: VAO and VBO
Post by: broumbroum on December 20, 2011, 05:05:17
the first buffer (position) should not be == 0, so your assertion is wrong.
Title: Re: VAO and VBO
Post by: oggs91 on December 21, 2011, 20:49:52
Quote from: broumbroum on December 20, 2011, 05:05:17
the first buffer (position) should not be == 0, so your assertion is wrong.

i've deactivated assertations, ignore it, the three calls of glGenBuffer() return 1,2,3
Title: Re: VAO and VBO
Post by: broumbroum on December 23, 2011, 04:09:17
I don't understand these "normals", not sure what is your intent : FloatBuffer normalBuffer = ByteBuffer
.allocateDirect(positionVboData.length * 4)
.order(ByteOrder.nativeOrder()).asFloatBuffer()
.put(positionVboData);

Also, Buffers might not rewind() before returning from the put(), both as target and as argument.
You could also investigate on how the V.B.O_ARB extension behaves against standard VBO handlers. http://www.songho.ca/opengl/gl_vbo.html

:)