OpenGL/GLGraphics - normals / lighting and crash report
in
Contributed Library Questions
•
2 years ago
Hi, I have created a cube, with the code below. I'm wondering why at times the cube face turns black even though I have lights pointing in all directions?
Any ideas?
Also, if I leave this applet to run for a few minutes (maybe around 5 or more), then the program crashes and I get the following error:
Plus I get a dialog window open with the errror:java(2620,0x82a800) malloc: ***mmap(size=2097152) filed (error code=12)
*** error: can't allocated region
*** set a breakpoint in malloc_error_break to debug
Invalid memory access of location 00000010 eip=8ef20fe4
sketch ... quit unexpectedly while using the libjogl.jnilib plug-in
Code:
- import processing.opengl.*;
- import codeanticode.glgraphics.*;
- import javax.media.opengl.*;
- float[][] n = { /* Normals for the 6 faces of a cube. */
- {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
- {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0} };
- int[][] faces = { /* Vertex indices for the 6 faces of a cube. */
- {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
- {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} };
- float v[][] = new float[8][3];
- float a;
- GLModel cube;
- void setup() {
- size(400,400,GLConstants.GLGRAPHICS);
- /* Setup cube vertex data. */
- v[0][0] = v[1][0] = v[2][0] = v[3][0] = -100;
- v[4][0] = v[5][0] = v[6][0] = v[7][0] = 100;
- v[0][1] = v[1][1] = v[4][1] = v[5][1] = -100;
- v[2][1] = v[3][1] = v[6][1] = v[7][1] = 100;
- v[0][2] = v[3][2] = v[4][2] = v[7][2] = 100;
- v[1][2] = v[2][2] = v[5][2] = v[6][2] = -100;
- cube = new GLModel(this, 24, QUADS, GLModel.STATIC);
- }
- void draw() {
- background(0);
- // setup the model
- cube.beginUpdateVertices();
- // normals
- ArrayList normals = new ArrayList();
- for (int i=0; i<6; i++) {
- int incr = 4 * i;
- cube.updateVertex( incr + 0, v[faces[i][0]][0], v[faces[i][0]][1], v[faces[i][0]][2]);
- cube.updateVertex( incr + 1, v[faces[i][1]][0], v[faces[i][1]][1], v[faces[i][1]][2]);
- cube.updateVertex( incr + 2, v[faces[i][2]][0], v[faces[i][2]][1], v[faces[i][2]][2]);
- cube.updateVertex( incr + 3, v[faces[i][3]][0], v[faces[i][3]][1], v[faces[i][3]][2]);
- // add normals to each face
- for (int x=0; x<4; x++) {
- normals.add(new PVector(n[i][0],n[i][1],n[i][2]));
- }
- incr ++;
- }
- cube.endUpdateVertices();
- // update normals
- cube.initNormals();
- cube.updateNormals(normals);
- // setup renderer
- GLGraphics render = (GLGraphics) g;
- GL gl = render.gl;
- render.beginGL();
- // lights
- // top / bottom
- pointLight(51, 102, 126, width/2, 0, 50);
- pointLight(51, 102, 126, width/2, height, 50);
- // left / right
- pointLight(51, 102, 126, 0, height/2, 0);
- pointLight(51, 102, 126, width, height/2, 0);
- // front
- pointLight(51, 102, 126, width/2, height/2, 200);
- // draw the second cube in the right half
- pushMatrix();
- render.translate( width/2, height/2, 0);
- gl.glRotatef(a, 1, 0, 0);
- gl.glRotatef(a*2, 0, 1, 0);
- render.model(cube);
- popMatrix();
- if (a >= 360) {
- a = 0;
- } else {
- a += 0.5;
- }
- render.endGL();
- }
1