after lot of googling, stumped 1 issue in rendering vbo cube in lwjgl. when enabled normals, jvm crash. has way set normals in plane.java. still learning vbos, can't seem figure how fix this. take , let me know possibly went wrong?
cube.java:
package net.contour.src.voxel; import org.lwjgl.opengl.gl11; public class cube { public float x; public float y; public float z; float size = 1.0f; private plane top; private plane bottom; private plane left; private plane right; private plane front; private plane back; public cube(float x, float y, float z) { this.x = x; this.y = y; this.z = z; this.top = new plane(new float[]{ size / 2, size / 2, size / 2, -size / 2, size / 2, size / 2, -size / 2, size / 2, -size / 2, size / 2, size / 2, -size / 2 }, new float[]{ 0.0f, 1.0f, 0.0f }); this.bottom = new plane(new float[]{ size / 2, -size / 2, size / 2, -size / 2, -size / 2, size / 2, -size / 2, -size / 2, -size / 2, size / 2, -size / 2, -size / 2 }, new float[]{ 0.0f, -1.0f, 0.0f }); this.front = new plane(new float[]{ size / 2, size / 2, -size / 2, -size / 2, size / 2, -size / 2, -size / 2, -size / 2, -size / 2, size / 2, -size / 2, -size / 2 }, new float[]{ 0.0f, 0.0f, 1.0f }); this.back = new plane(new float[]{ size / 2, size / 2, size / 2, -size / 2, size / 2, size / 2, -size / 2, -size / 2, size / 2, size / 2, -size / 2, size / 2 }, new float[]{ 0.0f, 0.0f, -1.0f }); this.left = new plane(new float[]{ -size / 2, size / 2, size / 2, -size / 2, size / 2, -size / 2, -size / 2, -size / 2, -size / 2, -size / 2, -size / 2, size / 2 }, new float[]{ -1.0f, 0.0f, 0.0f }); this.right = new plane(new float[]{ size / 2, size / 2, -size / 2, size / 2, size / 2, size / 2, size / 2, -size / 2, size / 2, size / 2, -size / 2, -size / 2 }, new float[]{ 1.0f, 0.0f, 0.0f }); } public void render() { gl11.glpushmatrix(); gl11.gltranslatef(x, y, z); top.render(); bottom.render(); front.render(); back.render(); left.render(); right.render(); gl11.glpopmatrix(); } public void destroy() { top.destroy(); bottom.destroy(); front.destroy(); back.destroy(); left.destroy(); right.destroy(); } }
plane.java:
package net.contour.src.voxel; import static org.lwjgl.opengl.gl11.gl_color_array; import static org.lwjgl.opengl.gl11.gl_float; import static org.lwjgl.opengl.gl11.gl_normal_array; import static org.lwjgl.opengl.gl11.gl_quads; import static org.lwjgl.opengl.gl11.gl_vertex_array; import static org.lwjgl.opengl.gl11.glcolorpointer; import static org.lwjgl.opengl.gl11.gldisableclientstate; import static org.lwjgl.opengl.gl11.gldrawarrays; import static org.lwjgl.opengl.gl11.glenableclientstate; import static org.lwjgl.opengl.gl11.glnormalpointer; import static org.lwjgl.opengl.gl11.glvertexpointer; import static org.lwjgl.opengl.gl15.gl_array_buffer; import static org.lwjgl.opengl.gl15.gl_static_draw; import static org.lwjgl.opengl.gl15.glbindbuffer; import static org.lwjgl.opengl.gl15.glbufferdata; import static org.lwjgl.opengl.gl15.gldeletebuffers; import static org.lwjgl.opengl.gl15.glgenbuffers; import java.nio.floatbuffer; import org.lwjgl.bufferutils; public class plane { float size = 0.5f; private final int amountofvertices = 4; private final int vertexsize = 3; private final int colorsize = 3; private final int normalsize = 3; private floatbuffer vertexdata; private floatbuffer colordata; private floatbuffer normaldata; private int vbovertexhandle; private int vbocolorhandle; private int vbonormalhandle; public plane(float[] v, float[] n) { vertexdata = bufferutils.createfloatbuffer(amountofvertices * vertexsize); vertexdata.put(new float[] { v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9], v[10], v[11] }); vertexdata.flip(); colordata = bufferutils.createfloatbuffer(amountofvertices * colorsize); colordata.put(new float[] { 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0 }); colordata.flip(); normaldata = bufferutils.createfloatbuffer(normalsize); normaldata.put(new float[] { n[0], n[1], n[2] }); normaldata.flip(); vbovertexhandle = glgenbuffers(); glbindbuffer(gl_array_buffer, vbovertexhandle); glbufferdata(gl_array_buffer, vertexdata, gl_static_draw); glbindbuffer(gl_array_buffer, 0); vbocolorhandle = glgenbuffers(); glbindbuffer(gl_array_buffer, vbocolorhandle); glbufferdata(gl_array_buffer, colordata, gl_static_draw); glbindbuffer(gl_array_buffer, 0); vbonormalhandle = glgenbuffers(); glbindbuffer(gl_array_buffer, vbonormalhandle); glbufferdata(gl_array_buffer, normaldata, gl_static_draw); glbindbuffer(gl_array_buffer, 0); } public void render() { glbindbuffer(gl_array_buffer, vbovertexhandle); glvertexpointer(vertexsize, gl_float, 0, 0l); glbindbuffer(gl_array_buffer, vbocolorhandle); glcolorpointer(colorsize, gl_float, 0, 0l); glbindbuffer(gl_array_buffer, vbonormalhandle); glnormalpointer(normalsize, gl_float, 0l); glenableclientstate(gl_vertex_array); glenableclientstate(gl_color_array); glenableclientstate(gl_normal_array); gldrawarrays(gl_quads, 0, amountofvertices); gldisableclientstate(gl_normal_array); gldisableclientstate(gl_color_array); gldisableclientstate(gl_vertex_array); } public void destroy() { gldeletebuffers(vbovertexhandle); gldeletebuffers(vbocolorhandle); } }
let me know guys think. i've tested code on 3 separate computers, every 1 of them had same result.
i believe need have normal each vertex. (after commenting above, dug through similar code had laying around verify this). supplying single normal coordinate, , code expecting 4 (one each vertex).
Comments
Post a Comment