0

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
thejava
  • 108
  • 2
  • 10
  • That part that crashes is probably `l.toArray()` inside your for loop, as `start` and `end` is `null` inside your array, or? – vallentin Dec 30 '13 at 08:52
  • No l.toArray() returns an array of size 3 with a format equivalent to `new float[] { x, y, z }` to represent a 3D location (the equivalent of a 3D vector). – thejava Dec 30 '13 at 17:27
  • I'll include the location class. – thejava Dec 30 '13 at 17:28
  • Well, yes I could guess that, but two of the `Location` objects are null. – vallentin Dec 30 '13 at 19:37
  • No, I have more code that went into the `Box` class. I just left it out. Some of this code initialized the two `Location` objects and set the values of `height`, `width`, and `depth`. – thejava Dec 30 '13 at 21:56
  • Should I use a VAO instead? What's the difference? – thejava Dec 30 '13 at 23:16

1 Answers1

0

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).

Ryxuma
  • 672
  • 5
  • 18
  • Could you give an example? This doesn't answer why it crashes. – thejava Dec 30 '13 at 22:59
  • @thejava We need a stacktrace to do something, we can't figure it out very well with the information "it crashes" – Ryxuma Dec 31 '13 at 00:19
  • I added the stacktrace. – thejava Dec 31 '13 at 04:26
  • @thejava That's not a stacktrace. That means that you've messed up somewhere with lwjgl, maybe something to do with natives? :L – Ryxuma Dec 31 '13 at 22:03
  • Do you mean I messed with the source code of lwjgl? I didn't. I've gotten this message before, but it went away when I fixed my problem. I'm pretty sure I'm just having a problem with what I'm doing with opengl. Could you maybe give an example of how I would implement what I'm trying to do? I think it's a concept I've messed up on or something. – thejava Dec 31 '13 at 22:29
  • @thejava no i mean that you've tried to do something that just messes up lwjgl, I honestly don't know what :/ – Ryxuma Dec 31 '13 at 23:39