threesixty3d
YaBB Newbies
Offline
Posts: 22
Italy
Re: Preloading (sort of)
Reply #3 - Apr 8th , 2009, 11:58pm
Code: import processing.opengl.*; import javax.media.opengl.*; import javax.media.opengl.glu.*; import com.sun.opengl.util.*; import java.util.Vector; import java.nio.*; float roty = 0.0; // GL stuff GLU glu = new GLU(); PGraphicsOpenGL pgl; GL gl; String opengl_version_string = ""; float opengl_version = 0.0; boolean GL_ARB_vertex_buffer_object_available = false; // viewer, initializer ms_viewer viewer; ms_initializer initializer; //////////////////////////////////////////////////////////////// // initializer class ms_initializer extends Thread { ms_viewer viewer; int i = 0; // constructor ms_initializer( ms_viewer _viewer ) { viewer = _viewer; } // methods void start() { super.start(); } void run() { viewer.load(); } void quit() { } } //////////////////////////////////////////////////////////////// // app constructor void setup() { size( 640, 480, OPENGL ); // enable 2x (default) anti-aliasing if supported hint( ENABLE_OPENGL_2X_SMOOTH ); // draws all geometry with smooth (anti-aliased) edges smooth(); // viewer viewer = new ms_viewer( this ); // black background background( 000000 ); // load with initializer initializer = new ms_initializer( viewer ); // UNCOMMENT NEXT LINE // initializer.start(); } //////////////////////////////////////////////////////////////// // app update void draw() { if ( !viewer.loaded ) { // load without initializer // COMMENT NEXT LINE viewer.load(); // preload animation // ... } else { // ready to go initializer.quit(); viewer.render(); } } //////////////////////////////////////////////////////////////// // app mouse dragged event void mouseDragged() { roty += mouseX - pmouseX; } //////////////////////////////////////////////////////////////// // app destructor void stop() { viewer.destructor(); } //////////////////////////////////////////////////////////////// // viewer class ms_viewer { PApplet parent; boolean loaded = false; Vector triangles = new Vector(); Vector positions = new Vector(); Vector normals = new Vector(); int[] elements_vbo = new int[1]; int[] positions_vbo = new int[1]; int[] normals_vbo = new int[1]; // constructor ms_viewer( PApplet _parent ) { this.parent = _parent; } // destructor void destructor() { if ( GL_ARB_vertex_buffer_object_available ) { gl.glDeleteBuffers( 1, positions_vbo, 0 ); gl.glDeleteBuffers( 1, normals_vbo, 0 ); gl.glDeleteBuffers( 1, elements_vbo, 0 ); } } // loader void load() { // opengl - begin pgl = (PGraphicsOpenGL) g; // g may change gl = pgl.beginGL(); // always use the GL object returned by pgl // setup vbo GL_ARB_vertex_buffer_object_available = gl.isExtensionAvailable( "GL_ARB_vertex_buffer_object" ); if ( GL_ARB_vertex_buffer_object_available ) { positions.addElement( new ms_vector3( -1.0f, -1.0f, 1.0f ) ); positions.addElement( new ms_vector3( 1.0f, -1.0f, 1.0f ) ); positions.addElement( new ms_vector3( 1.0f, 1.0f, 1.0f ) ); positions.addElement( new ms_vector3( -1.0f, 1.0f, 1.0f ) ); positions.addElement( new ms_vector3( -1.0f, -1.0f, -1.0f ) ); positions.addElement( new ms_vector3( 1.0f, -1.0f, -1.0f ) ); positions.addElement( new ms_vector3( 1.0f, 1.0f, -1.0f ) ); positions.addElement( new ms_vector3( -1.0f, 1.0f, -1.0f ) ); normals.addElement( new ms_vector3( 0.0, 1.0, 0.0 ) ); normals.addElement( new ms_vector3( 0.0, 1.0, 0.0 ) ); normals.addElement( new ms_vector3( 0.0, 1.0, 0.0 ) ); normals.addElement( new ms_vector3( 0.0, 1.0, 0.0 ) ); normals.addElement( new ms_vector3( 0.0, 1.0, 0.0 ) ); normals.addElement( new ms_vector3( 0.0, 1.0, 0.0 ) ); normals.addElement( new ms_vector3( 0.0, 1.0, 0.0 ) ); normals.addElement( new ms_vector3( 0.0, 1.0, 0.0 ) ); triangles.addElement( new ms_triangle( 0, 5, 1 ) ); triangles.addElement( new ms_triangle( 0, 4, 5 ) ); triangles.addElement( new ms_triangle( 1, 6, 2 ) ); triangles.addElement( new ms_triangle( 1, 5, 6 ) ); triangles.addElement( new ms_triangle( 2, 6, 7 ) ); triangles.addElement( new ms_triangle( 2, 7, 3 ) ); triangles.addElement( new ms_triangle( 0, 3, 7 ) ); triangles.addElement( new ms_triangle( 0, 7, 4 ) ); triangles.addElement( new ms_triangle( 0, 1, 2 ) ); triangles.addElement( new ms_triangle( 0, 2, 3 ) ); triangles.addElement( new ms_triangle( 4, 7, 6 ) ); triangles.addElement( new ms_triangle( 4, 6, 5 ) ); gl.glGenBuffers( 1, positions_vbo, 0 ); gl.glBindBuffer( GL.GL_ARRAY_BUFFER, positions_vbo[0] ); gl.glBufferData( GL.GL_ARRAY_BUFFER, positions.size() * 3 * BufferUtil.SIZEOF_FLOAT, ms_vector3s_to_float_buffer( positions ), GL.GL_STATIC_DRAW ); gl.glGenBuffers( 1, normals_vbo, 0 ); gl.glBindBuffer( GL.GL_ARRAY_BUFFER, normals_vbo[0] ); gl.glBufferData( GL.GL_ARRAY_BUFFER, normals.size() * 3 * BufferUtil.SIZEOF_FLOAT, ms_vector3s_to_float_buffer( normals ), GL.GL_STATIC_DRAW ); gl.glGenBuffers( 1, elements_vbo, 0 ); gl.glBindBuffer( GL.GL_ELEMENT_ARRAY_BUFFER, elements_vbo[0] ); gl.glBufferData( GL.GL_ELEMENT_ARRAY_BUFFER, triangles.size() * 3 * BufferUtil.SIZEOF_INT, ms_triangles_to_int_buffer( triangles ), GL.GL_STATIC_DRAW ); } // opengl - end pgl.endGL(); loaded = true; }