Texturing 3D cubes with LWJGL using VBOs

481 views Asked by At

I am having a big problem with OpenGL. I've tried to get a simple box to render with textures. However, even this code, which is supposed to just draw the box crashes. How do I add textures to a 3D box using VBOs and how do I get this code not to crash?

class Box {
Location start, end;

......  More Code Here .....

public Location[] getVertices() {
    return new Location[] {
        start,                                   new Location(start).add(width, 0, 0),
        new Location(start).add(0, 0, depth),    new Location(start).add(width, 0, depth),
        end,                                     new Location(end).subtract(width, 0, 0),
        new Location(end).subtract(0, 0, depth), new Location(end).subtract(width, 0, depth)
    };
}

public void draw() {
    Location[] vertices = getVertices();
    FloatBuffer verts = BufferUtils.createFloatBuffer(vertices.length * 3);
    for(Location l : vertices) {
        verts.put(l.toArray());
    }

    int vertHandle = glGenBuffers();

    glBindBuffer(GL_ARRAY_BUFFER, vertHandle);
    glBufferData(GL_ARRAY_BUFFER, verts, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
    glDrawArrays(GL_QUADS, 0, 4);
    glDisableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}
}

EDIT: Here's the location class.

public class Location {

public float x, y, z;

//.......... Code here.......

public Location(Location l) {
    this.x = l.x;
    this.y = l.y;
    this.z = l.z;
}

public Location(float x, float y, float z) {
    this.x = x;
    this.y = y;
    this.z = z;
}

//... Code here.......

// Sets
public Location add(Location l) {
    this.x += l.x;
    this.y += l.y;
    this.z += l.z;
    return this;
}

public Location add(float x, float y, float z) {
    this.x += x;
    this.y += y;
    this.z += z;
    return this;
}

// Sets
public Location subtract(Location l) {
    this.x -= l.x;
    this.y -= l.y;
    this.z -= l.z;
    return this;
}

public Location subtract(float x, float y, float z) {
    this.x -= x;
    this.y -= y;
    this.z -= z;
    return this;
}

//..Code Here.....
public float[] toArray() {
    return new float[] {
        x, y, z
    };
}

//... Code Here....
}

EDIT: Here's my initGL() method:

void initGL() {
    // Initialize OpenGL
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0, 0, Display.getWidth(), Display.getHeight());

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // Enable Depth Testing
    glEnable(GL_DEPTH_TEST);

    // Enable client states
    //glEnableClientState(GL_VERTEX_ARRAY); Do I need this???
    //glEnableClientState(GL_COLOR_ARRAY);

    // Are these right for drawing textures?
    glEnable(GL_TEXTURE_2D);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    // enable alpha blending
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

}

And here's my renderGL() method:

public void renderGL() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer

    for(Box b : boxes) {
        b.draw();
    }

    //System.out.println(player.midpoint());
    Display.update();

    // Camera stuff. This has been working and so I don't think it's causing an issue
    glLoadIdentity();
    player.lookThrough();

}

EDIT: This is all I'm getting myself for a stacktrace:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000013b5cda0, pid=5844, tid=5824
#
# JRE version: 7.0_11-b21
# Java VM: Java HotSpot(TM) 64-Bit Server VM (23.6-b04 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [ig75icd64.dll+0x7cda0]  RegisterProcTableCallback+0x74cd0
#
# 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\Christian\Documents\JM3\BasicGame\hs_err_pid5844.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.
#
Java Result: 1
1

There are 1 answers

6
OrangeMan On

You need to bind a texture before rendering the cube. Also as well as your verts float buffer you'll need one to store the texture co-ordinates (0 to 1). For the texture co-ords do pretty much excatly what you do with the vertexes but replace glVertexAttribPointer with glTexCoordAttribPointer (just wherever you have vertex replace with texCoord pretty much).