Max 5 API Reference
00001 // jit.gl.cube.c 00002 // sample GL group object which draws a cube. 00003 // 00004 // author: randall jones 00005 // � 2002 cycling '74 00006 00007 00008 #include "jit.common.h" 00009 #include "jit.gl.h" 00010 00011 typedef struct _jit_gl_cube 00012 { 00013 // Max object 00014 t_object ob; 00015 // 3d object extension. This is what all objects in the GL group have in common. 00016 void *ob3d; 00017 // stores one chunk of GL geometry. 00018 t_jit_glchunk *chunk; 00019 } t_jit_gl_cube; 00020 00021 void *_jit_gl_cube_class; 00022 00023 t_jit_err jit_gl_cube_init(void); 00024 t_jit_gl_cube *jit_gl_cube_new(t_symbol * dest_name); 00025 void jit_gl_cube_free(t_jit_gl_cube *x); 00026 00027 t_jit_err jit_gl_cube_draw(t_jit_gl_cube *x); 00028 t_jit_err jit_gl_cube_dest_changed(t_jit_gl_cube *x); 00029 t_jit_err build_cube_chunk(t_jit_gl_cube *x); 00030 00031 00032 // -------------------------------------------------------------------------------- 00033 00034 t_jit_err jit_gl_cube_init(void) 00035 { 00036 long ob3d_flags=0; 00037 void * ob3d; 00038 00039 _jit_gl_cube_class = jit_class_new("jit_gl_cube", (method)jit_gl_cube_new, (method)jit_gl_cube_free, 00040 sizeof(t_jit_gl_cube),A_CANT,0L); //A_CANT = untyped 00041 00042 // set up object extension for 3d object, customized with flags 00043 ob3d = jit_ob3d_setup(_jit_gl_cube_class, calcoffset(t_jit_gl_cube, ob3d), ob3d_flags); 00044 00045 // handle draw method. called in automatic mode by jit.gl.render, or otherwise through ob3d when banged. 00046 // this is A_CANT because draw setup needs to happen in the ob3d beforehand. 00047 jit_class_addmethod(_jit_gl_cube_class, (method)jit_gl_cube_draw, "ob3d_draw", A_CANT, 0L); 00048 00049 // handle dest_changed method. 00050 // this method is called by jit.render when the destination context changes: for example, 00051 // when the user moves the window from one monitor to another. Anything your object keeps 00052 // in the OpenGL machine -- textures, display lists, vertex shaders, etc. -- will need to 00053 // be rebuilt here. 00054 jit_class_addmethod(_jit_gl_cube_class, (method)jit_gl_cube_dest_changed, "dest_changed", A_CANT, 0L); 00055 00056 // must register for ob3d use. 00057 jit_class_addmethod(_jit_gl_cube_class, (method)jit_object_register, "register", A_CANT, 0L); 00058 00059 jit_class_register(_jit_gl_cube_class); 00060 00061 return JIT_ERR_NONE; 00062 } 00063 00064 00065 t_jit_gl_cube *jit_gl_cube_new(t_symbol * dest_name) 00066 { 00067 t_jit_gl_cube *x; 00068 00069 // make jit object 00070 if (x = (t_jit_gl_cube *)jit_object_alloc(_jit_gl_cube_class)) 00071 { 00072 // create and attach ob3d. 00073 jit_ob3d_new(x, dest_name); 00074 00075 // set instance variable defaults 00076 x->chunk = 0; 00077 00078 // build gl geometry chunk containing cube data. 00079 if (build_cube_chunk(x) != JIT_ERR_NONE) 00080 { 00081 error ("jit.gl.cube: couldn't make chunk!"); 00082 } 00083 } 00084 else 00085 { 00086 x = NULL; 00087 } 00088 return x; 00089 } 00090 00091 00092 void jit_gl_cube_free(t_jit_gl_cube *x) 00093 { 00094 if (x->chunk) 00095 jit_glchunk_delete(x->chunk); 00096 jit_ob3d_free(x); 00097 } 00098 00099 00100 t_jit_err jit_gl_cube_draw(t_jit_gl_cube *x) 00101 { 00102 t_jit_err result = JIT_ERR_NONE; 00103 00104 // draw our chunk of OpenGL geometry. 00105 // if the ob3d is in matrixoutput mode, the matrix output is done 00106 // in jit_ob3d_draw_chunk. otherwise OpenGL draws the chunk directly. 00107 result = jit_ob3d_draw_chunk(x->ob3d, x->chunk); 00108 00109 return result; 00110 } 00111 00112 t_jit_err jit_gl_cube_dest_changed(t_jit_gl_cube *x) 00113 { 00114 // nothing in this object to update. 00115 return JIT_ERR_NONE; 00116 } 00117 00118 00119 #pragma mark - 00120 00121 // -------------------------------------------------------------------------------- 00122 // cube geometry 00123 00124 // texture map coordinates 00125 #define X1 0.0 00126 #define X2 0.25 00127 #define X3 0.5 00128 #define X4 0.75 00129 #define X5 1.0 00130 #define Y1 0.125 00131 #define Y2 0.375 00132 #define Y3 0.625 00133 #define Y4 0.875 00134 // edge flags with padding for intermediate planes 00135 #define E0 0, 0, 0, 0, 0, 0, 0, 0 00136 #define E1 0, 0, 0, 0, 0, 0, 0, 1 00137 00138 static float cube_v[13*12*3] = 00139 { 00140 -1, 1, 1, X2, Y3, E1,//3 00141 -1, 1, -1, X2, Y4, E1,//2 00142 1, 1, -1, X3, Y4, E0,//6 00143 00144 1, 1, -1, X3, Y4, E1,//6 00145 1, 1, 1, X3, Y3, E1,//7 00146 -1, 1, 1, X2, Y3, E0,//3 00147 00148 -1, -1, -1, X1, Y2, E1,//0 00149 -1, 1, -1, X1, Y3, E1,//2 00150 -1, 1, 1, X2, Y3, E0,//3 00151 00152 -1, 1, 1, X2, Y3, E1,//3 00153 -1, -1, 1, X2, Y2, E1,//1 00154 -1, -1, -1, X1, Y2, E0,//0 00155 00156 -1, -1, 1, X2, Y2, E1,//1 00157 -1, 1, 1, X2, Y3, E1,//3 00158 1, 1, 1, X3, Y3, E0,//7 00159 00160 1, 1, 1, X3, Y3, E1,//7 00161 1, -1, 1, X3, Y2, E1,//5 00162 -1, -1, 1, X2, Y2, E0,//1 00163 00164 1, -1, 1, X3, Y2, E1,//5 00165 1, 1, 1, X3, Y3, E1,//7 00166 1, 1, -1, X4, Y3, E0,//6 00167 00168 1, 1, -1, X4, Y3, E1,//6 00169 1, -1, -1, X4, Y2, E1,//4 00170 1, -1, 1, X3, Y2, E0,//5 00171 00172 1, -1, -1, X4, Y2, E1,//4 00173 1, 1, -1, X4, Y3, E1,//6 00174 -1, 1, -1, X5, Y3, E0,//2 00175 00176 -1, 1, -1, X5, Y3, E1,//2 00177 -1, -1, -1, X5, Y2, E1,//0 00178 1, -1, -1, X4, Y2, E0,//4 00179 00180 -1, -1, -1, X2, Y1, E1,//0 00181 -1, -1, 1, X2, Y2, E1,//1 00182 1, -1, 1, X3, Y2, E0,//5 00183 00184 1, -1, 1, X3, Y2, E1,//5 00185 1, -1, -1, X3, Y1, E1,//4 00186 -1, -1, -1, X2, Y1, E0//0 00187 00188 }; 00189 00190 #undef X1 00191 #undef X2 00192 #undef X3 00193 #undef X4 00194 #undef X5 00195 #undef Y1 00196 #undef Y2 00197 #undef Y3 00198 #undef Y4 00199 #undef E0 00200 #undef E1 00201 00202 00203 // build a jit_glchunk structure containing the cube data. 00204 t_jit_err build_cube_chunk(t_jit_gl_cube *x) 00205 { 00206 float *pv; 00207 int vertices, planes; 00208 t_jit_err result = JIT_ERR_NONE; 00209 00210 // delete existing chunk if present 00211 if (x->chunk) 00212 jit_glchunk_delete(x->chunk); 00213 00214 vertices = 12*3; // 6 side * 2 triangles/side * 3 vertices/triangle 00215 planes = 13; // all of them, because edge flags are used. 00216 00217 // make a new chunk and allocate data for the number of planes, vertices, and 00218 // edge connections specified. This chunk will have 13 planes, 12*3 vertices, 00219 // and no edge connections. 00220 if (x->chunk = jit_glchunk_new(_jit_sym_gl_triangles, planes, vertices, 0)) 00221 { 00222 // renderer to ignore the color plane and vertex plane. 00223 // we are not making normals in this object. the jit.gl.render object 00224 // wil auto_generate them. 00225 x->chunk->m_flags = JIT_GL_CHUNK_IGNORE_COLORS | JIT_GL_CHUNK_IGNORE_NORMALS; 00226 00227 // get a ptr to the data in the chunk's vertex matrix. 00228 jit_object_method(x->chunk->m_vertex, gensym("getdata"), &pv); 00229 00230 // copy static vertex and texture coord data into chunk 00231 jit_copy_bytes(pv, cube_v, planes * vertices * sizeof(float)); 00232 } 00233 else 00234 result = JIT_ERR_OUT_OF_MEM; 00235 00236 00237 return result; 00238 } 00239
Copyright © 2008, Cycling '74