Max 5 API Reference
00001 /* 00002 * Copyright 2001-2005 - Cycling '74 00003 * Randall Jones - rej@2uptech.com 00004 * 00005 * OpenGL Chunk structure used to store one gl-command's-worth of data, 00006 * in a format which can be passed easily to glDrawRangeElements 00007 */ 00008 00009 /****************************************************************************/ 00010 00011 #include "jit.gl.h" 00012 #include "jit.gl.chunk.h" 00013 #include "jit.gl.ob3d.h" 00014 #include "jit.gl.ob3d.internal.h" 00015 00016 /****************************************************************************/ 00017 00018 00019 /** 00020 * Allocates and initializes a t_jit_glchunk struct. 00021 * 00022 * @ingroup ob3dmod 00023 * 00024 * @param prim drawing primitive name 00025 * @param planes number of planes to allocate in vertex matrix 00026 * @param vertices number of vertices to allocate in vertex matrix 00027 * @param indices number of indices to allocate in index matrix, if used 00028 * 00029 * @return t_jit_glchunk pointer 00030 * 00031 */ 00032 t_jit_glchunk * jit_glchunk_new(t_symbol * prim, int planes, int vertices, int indices) 00033 { 00034 t_jit_matrix_info new_info; 00035 int OK = true; 00036 t_jit_glchunk * x = (t_jit_glchunk *)jit_getbytes(sizeof(t_jit_glchunk)); 00037 if (x) 00038 { 00039 x->m_flags = 0; 00040 x->m_vertex = 0; 00041 x->m_index = 0; 00042 x->m_vertex_name = _jit_sym_nothing; 00043 x->m_index_name = _jit_sym_nothing; 00044 x->prim = prim; 00045 x->next_chunk = NULL; 00046 00047 jit_matrix_info_default(&new_info); 00048 new_info.type = _jit_sym_float32; 00049 new_info.dimcount = 1; 00050 new_info.planecount = planes; 00051 new_info.dim[0] = vertices; 00052 if (!(x->m_vertex = jit_object_new(_jit_sym_jit_matrix, &new_info))) OK = false; 00053 00054 if (indices) 00055 { 00056 jit_matrix_info_default(&new_info); 00057 new_info.type = _jit_sym_long; 00058 new_info.dimcount = 1; 00059 new_info.planecount = 1; 00060 new_info.dim[0] = indices; 00061 if (!(x->m_index = jit_object_new(_jit_sym_jit_matrix, &new_info))) OK = false; 00062 } 00063 00064 if (!OK) 00065 { 00066 jit_glchunk_delete(x); 00067 x = 0; 00068 } 00069 } 00070 return x; 00071 } 00072 00073 /** 00074 * Allocates and initializes a t_jit_glchunk struct with 2D grid matrix. 00075 * 00076 * @ingroup ob3dmod 00077 * 00078 * @param prim drawing primitive name 00079 * @param planes number of planes to allocate in vertex matrix 00080 * @param width width of vertex matrix to allocate 00081 * @param height height of vertex matrix to allocate 00082 * 00083 * @return t_jit_glchunk pointer 00084 * 00085 */ 00086 t_jit_glchunk * jit_glchunk_grid_new(t_symbol * prim, int planes, int width, int height) 00087 { 00088 t_jit_matrix_info new_info; 00089 int OK = true; 00090 t_jit_glchunk * x = (t_jit_glchunk *)jit_getbytes(sizeof(t_jit_glchunk)); 00091 if (x) 00092 { 00093 x->m_flags = 0; 00094 x->m_vertex = 0; 00095 x->m_index = 0; 00096 x->m_vertex_name = _jit_sym_nothing; 00097 x->m_index_name = _jit_sym_nothing; 00098 x->prim = prim; 00099 x->next_chunk = NULL; 00100 00101 jit_matrix_info_default(&new_info); 00102 new_info.type = _jit_sym_float32; 00103 new_info.dimcount = 2; 00104 new_info.planecount = planes; 00105 new_info.dim[0] = width; 00106 new_info.dim[1] = height; 00107 if (!(x->m_vertex = jit_object_new(_jit_sym_jit_matrix, &new_info))) OK = false; 00108 00109 if (!OK) 00110 { 00111 jit_glchunk_delete(x); 00112 x = 0; 00113 } 00114 } 00115 return x; 00116 } 00117 00118 /** 00119 * Disposes t_jit_glchunk struct. 00120 * 00121 * @ingroup ob3dmod 00122 * 00123 * @param x t_jit_glchunk pointer 00124 * 00125 */ 00126 void jit_glchunk_delete(t_jit_glchunk * x) 00127 { 00128 if (x) 00129 { 00130 if (x->m_vertex) jit_object_free(x->m_vertex); 00131 if (x->m_index) jit_object_free(x->m_index); 00132 if (x->next_chunk) 00133 { 00134 jit_glchunk_delete((t_jit_glchunk *)x->next_chunk); 00135 x->next_chunk = 0; 00136 } 00137 jit_freebytes(x, sizeof(t_jit_glchunk)); 00138 } 00139 } 00140 00141 /** 00142 * Allocates t_jit_glchunk struct, and copies from t_jit_gl_struct provided. 00143 * 00144 * @ingroup ob3dmod 00145 * 00146 * @param new pointer to new t_jit_glchunk pointer 00147 * @param orig priginal t_jit_glchunk pointer 00148 * 00149 * @return t_jit_err error code 00150 */ 00151 t_jit_err jit_glchunk_copy(t_jit_glchunk ** new, t_jit_glchunk * orig) 00152 { 00153 t_jit_matrix_info orig_minfo; 00154 t_jit_glchunk * x = (t_jit_glchunk *)jit_getbytes(sizeof(t_jit_glchunk)); 00155 t_jit_err err = JIT_ERR_NONE; 00156 t_jit_glchunk * next_new = NULL; 00157 00158 if (x) 00159 { 00160 // copy flags 00161 x->m_flags = orig->m_flags; 00162 x->prim = orig->prim; 00163 x->m_vertex = NULL; 00164 x->m_index = NULL; 00165 x->m_vertex_name = _jit_sym_nothing; 00166 x->m_index_name = _jit_sym_nothing; 00167 x->next_chunk = NULL; 00168 00169 // copy vertex matrix 00170 if (orig->m_vertex) 00171 { 00172 jit_object_method(orig->m_vertex, _jit_sym_getinfo, &orig_minfo); 00173 00174 if (!(x->m_vertex = jit_object_new(_jit_sym_jit_matrix, &orig_minfo))) 00175 { 00176 err = JIT_ERR_OUT_OF_MEM; 00177 goto bail; 00178 } 00179 jit_object_method(x->m_vertex, _jit_sym_frommatrix, orig->m_vertex, NULL); 00180 } 00181 00182 // copy connection indices matrix 00183 if (orig->m_index) 00184 { 00185 jit_object_method(orig->m_index, _jit_sym_getinfo, &orig_minfo); 00186 00187 if (!(x->m_index = jit_object_new(_jit_sym_jit_matrix, &orig_minfo))) 00188 { 00189 err = JIT_ERR_OUT_OF_MEM; 00190 goto bail; 00191 } 00192 jit_object_method(x->m_index, _jit_sym_frommatrix, orig->m_index, NULL); 00193 } 00194 00195 // recurse 00196 if (orig->next_chunk) 00197 { 00198 if (err == JIT_ERR_NONE) 00199 { 00200 if (JIT_ERR_NONE == jit_glchunk_copy(&next_new, orig->next_chunk)); 00201 x->next_chunk = next_new; 00202 } 00203 } 00204 } 00205 00206 bail: 00207 if (err != JIT_ERR_NONE) 00208 { 00209 jit_glchunk_delete(x); 00210 x = 0; 00211 } 00212 00213 *new = x; 00214 return err; 00215 } 00216 00217 /****************************************************************************/
Copyright © 2008, Cycling '74