Results 1 to 1 of 1
Thread: Opengl problem.
- 09-18-2012, 11:43 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 61
- Rep Power
- 0
Opengl problem.
When I try to create icosphere my app crash.Here is the code:
Java Code:package com.droidnova.android.games.vortex; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.nio.ShortBuffer; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.opengl.GLSurfaceView; public class VortexRenderer implements GLSurfaceView.Renderer { private static final String LOG_TAG = VortexRenderer.class.getSimpleName(); // a raw buffer to hold indices allowing a reuse of points. private ShortBuffer _indexBuffer; // a raw buffer to hold the vertices private FloatBuffer _vertexBuffer; // a raw buffer to hold the colors private FloatBuffer _colorBuffer; private int _nrOfVertices = 0; private float _xAngle; private float _yAngle; public void onSurfaceChanged(GL10 gl, int w, int h) { gl.glViewport(0, 0, w, h); } public void setXAngle(float angle) { _xAngle = angle; } public float getXAngle() { return _xAngle; } public void setYAngle(float angle) { _yAngle = angle; } public float getYAngle() { return _yAngle; } private float _red = 0.9f; private float _green = 0.2f; private float _blue = 0.2f; private float _angle; private short[] _indicesArray = {0, 1, 2}; public void setAngle(float angle) { _angle = angle; } public void onSurfaceCreated(GL10 gl, EGLConfig config) { // preparation // enable the differentiation of which side may be visible gl.glEnable(GL10.GL_CULL_FACE); // which is the front? the one which is drawn counter clockwise gl.glFrontFace(GL10.GL_CCW); // which one should NOT be drawn gl.glCullFace(GL10.GL_BACK); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glEnableClientState(GL10.GL_COLOR_ARRAY); initTriangle(); } private void initTriangle() { float[] coords = { -1.476907f,-1.825372f,0.371984f, -0.753300f,-1.272592f,0.897709f, -1.753295f,-1.272592f,1.222633f, -2.371334f,-1.272588f,0.371984f, -1.753295f,-1.272592f,-0.478665f, -0.753300f,-1.272592f,-0.153741f, -1.200519f,-0.378152f,1.222633f, -2.200515f,-0.378152f,0.897709f, -2.200515f,-0.378152f,-0.153741f, -1.200519f,-0.378152f,-0.478665f, -0.582481f,-0.378156f,0.371984f, -1.476907f,0.174628f,0.371984f, -1.051585f,-1.676026f,0.680995f, -1.214038f,-1.351110f,1.180996f, -1.639363f,-1.676026f,0.871979f, -1.051585f,-1.676026f,0.062973f, -0.626259f,-1.351108f,0.371984f, -2.165097f,-1.351108f,0.871981f, -2.002637f,-1.676024f,0.371984f, -2.165097f,-1.351108f,-0.128013f, -1.639363f,-1.676026f,-0.128011f, -1.214038f,-1.351110f,-0.437028f, -0.525849f,-0.825372f,0.062971f, -0.525849f,-0.825372f,0.680997f, -0.889122f,-0.825372f,1.181001f, -1.476907f,-0.825372f,1.371984f, -2.064693f,-0.825372f,1.181001f, -2.427965f,-0.825372f,0.680997f, -2.427965f,-0.825372f,0.062971f, -2.064693f,-0.825372f,-0.437033f, -1.476907f,-0.825372f,-0.628016f, -0.889122f,-0.825372f,-0.437033f, -0.788718f,-0.299636f,0.871981f, -1.739776f,-0.299634f,1.180996f, -2.327555f,-0.299636f,0.371984f, -1.739776f,-0.299634f,-0.437028f, -0.788718f,-0.299636f,-0.128013f, -0.951177f,0.025280f,0.371984f, -1.314452f,0.025282f,0.871979f, -1.902230f,0.025282f,0.680995f, -1.902230f,0.025282f,0.062973f, -1.314452f,0.025282f,-0.128011f }; _nrOfVertices = coords.length; float[] colors = { 1f, 0f, 0f, 1f, // point 0 red 0f, 1f, 0f, 1f, // point 1 green 0f, 0f, 1f, 1f, // point 2 blue 1f, 1f, 1f, 1f, // point 3 white }; short[] indices = new short[] { 0, 1, 3, // rwg 0, 2, 1, // rbg 0, 3, 2, // rbw 1, 2, 3, // bwg }; // float has 4 bytes, coordinate * 4 bytes ByteBuffer vbb = ByteBuffer.allocateDirect(coords.length * 4); vbb.order(ByteOrder.nativeOrder()); _vertexBuffer = vbb.asFloatBuffer(); // short has 2 bytes, indices * 2 bytes ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2); ibb.order(ByteOrder.nativeOrder()); _indexBuffer = ibb.asShortBuffer(); // float has 4 bytes, colors (RGBA) * 4 bytes ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length * 4); cbb.order(ByteOrder.nativeOrder()); _colorBuffer = cbb.asFloatBuffer(); _vertexBuffer.put(coords); _indexBuffer.put(indices); _colorBuffer.put(colors); _vertexBuffer.position(0); _indexBuffer.position(0); _colorBuffer.position(0); } public void onDrawFrame(GL10 gl) { // define the color we want to be displayed as the "clipping wall" gl.glClearColor(0f, 0f, 0f, 1.0f); // reset the matrix - good to fix the rotation to a static angle gl.glLoadIdentity(); // clear the color buffer to show the ClearColor we called above... gl.glClear(GL10.GL_COLOR_BUFFER_BIT); // set rotation gl.glRotatef(_xAngle, 1f, 0f, 0f); gl.glRotatef(_yAngle, 0f, 1f, 0f); //gl.glColor4f(0.5f, 0f, 0f, 0.5f); gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer); gl.glColorPointer(4, GL10.GL_FLOAT, 0, _colorBuffer); gl.glDrawElements(GL10.GL_TRIANGLES, _nrOfVertices, GL10.GL_UNSIGNED_SHORT, _indexBuffer); } public void setColor(float r, float g, float b) { _red = r; _green = g; _blue = b; } }
Similar Threads
-
SkyBox in OpenGL
By adam21. in forum Java GamingReplies: 1Last Post: 11-22-2011, 09:22 PM -
Got problem with Java OpenGL
By Igbear in forum Advanced JavaReplies: 1Last Post: 05-07-2011, 07:51 PM -
SWT OpenGL snippet: draw a square
By Java Tip in forum SWTReplies: 0Last Post: 06-28-2008, 09:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks