Blender  V2.59
GPU_buffers.h
Go to the documentation of this file.
00001 /*
00002  * $Id: GPU_buffers.h 38866 2011-07-31 02:24:06Z nicholasbishop $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version. The Blender
00010  * Foundation also sells licenses for use in proprietary software under
00011  * the Blender License.  See http://www.blender.org/BL/ for information
00012  * about this.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software Foundation,
00021  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00022  *
00023  * The Original Code is Copyright (C) 2005 Blender Foundation.
00024  * All rights reserved.
00025  *
00026  * The Original Code is: all of this file.
00027  *
00028  * Contributor(s): Brecht Van Lommel.
00029  *
00030  * ***** END GPL LICENSE BLOCK *****
00031  */
00032 
00037 #ifndef __GPU_BUFFERS_H__
00038 #define __GPU_BUFFERS_H__
00039 
00040 #ifdef _DEBUG
00041 /*#define DEBUG_VBO(X) printf(X)*/
00042 #define DEBUG_VBO(X)
00043 #else
00044 #define DEBUG_VBO(X)
00045 #endif
00046 
00047 struct DerivedMesh;
00048 struct DMGridData;
00049 struct GHash;
00050 struct DMGridData;
00051 struct GPUVertPointLink;
00052 
00053 typedef struct GPUBuffer {
00054         int size;       /* in bytes */
00055         void *pointer;  /* used with vertex arrays */
00056         unsigned int id;        /* used with vertex buffer objects */
00057 } GPUBuffer;
00058 
00059 typedef struct GPUBufferMaterial {
00060         /* range of points used for this material */
00061         int start;
00062         int totpoint;
00063 
00064         /* original material index */
00065         short mat_nr;
00066 } GPUBufferMaterial;
00067 
00068 /* meshes are split up by material since changing materials requires
00069    GL state changes that can't occur in the middle of drawing an
00070    array.
00071 
00072    some simplifying assumptions are made:
00073    * all quads are treated as two triangles.
00074    * no vertex sharing is used; each triangle gets its own copy of the
00075      vertices it uses (this makes it easy to deal with a vertex used
00076      by faces with different properties, such as smooth/solid shading,
00077      different MCols, etc.)
00078 
00079    to avoid confusion between the original MVert vertices and the
00080    arrays of OpenGL vertices, the latter are referred to here and in
00081    the source as `points'. similarly, the OpenGL triangles generated
00082    for MFaces are referred to as triangles rather than faces.
00083  */
00084 typedef struct GPUDrawObject {
00085         GPUBuffer *points;
00086         GPUBuffer *normals;
00087         GPUBuffer *uv;
00088         GPUBuffer *colors;
00089         GPUBuffer *edges;
00090         GPUBuffer *uvedges;
00091 
00092         /* for each triangle, the original MFace index */
00093         int *triangle_to_mface;
00094 
00095         /* for each original vertex, the list of related points */
00096         struct GPUVertPointLink *vert_points;
00097         /* storage for the vert_points lists */
00098         struct GPUVertPointLink *vert_points_mem;
00099         int vert_points_usage;
00100         
00101         int colType;
00102 
00103         GPUBufferMaterial *materials;
00104         int totmaterial;
00105         
00106         int tot_triangle_point;
00107         int tot_loose_point;
00108         
00109         /* caches of the original DerivedMesh values */
00110         int totvert;
00111         int totedge;
00112 
00113         /* if there was a failure allocating some buffer, use old
00114            rendering code */
00115         int legacy;
00116 } GPUDrawObject;
00117 
00118 /* used for GLSL materials */
00119 typedef struct GPUAttrib {
00120         int index;
00121         int size;
00122         int type;
00123 } GPUAttrib;
00124 
00125 void GPU_global_buffer_pool_free(void);
00126 
00127 GPUBuffer *GPU_buffer_alloc(int size);
00128 void GPU_buffer_free(GPUBuffer *buffer);
00129 
00130 GPUDrawObject *GPU_drawobject_new( struct DerivedMesh *dm );
00131 void GPU_drawobject_free( struct DerivedMesh *dm );
00132 
00133 /* called before drawing */
00134 void GPU_vertex_setup( struct DerivedMesh *dm );
00135 void GPU_normal_setup( struct DerivedMesh *dm );
00136 void GPU_uv_setup( struct DerivedMesh *dm );
00137 void GPU_color_setup( struct DerivedMesh *dm );
00138 void GPU_edge_setup( struct DerivedMesh *dm );  /* does not mix with other data */
00139 void GPU_uvedge_setup( struct DerivedMesh *dm );
00140 void GPU_interleaved_setup( GPUBuffer *buffer, int data[] );
00141 int GPU_attrib_element_size( GPUAttrib data[], int numdata );
00142 void GPU_interleaved_attrib_setup( GPUBuffer *buffer, GPUAttrib data[], int numdata );
00143 
00144 /* can't lock more than one buffer at once */
00145 void *GPU_buffer_lock( GPUBuffer *buffer );     
00146 void *GPU_buffer_lock_stream( GPUBuffer *buffer );
00147 void GPU_buffer_unlock( GPUBuffer *buffer );
00148 
00149 /* upload three unsigned chars, representing RGB colors, for each vertex. Resets dm->drawObject->colType to -1 */
00150 void GPU_color3_upload( struct DerivedMesh *dm, unsigned char *data );
00151 /* upload four unsigned chars, representing RGBA colors, for each vertex. Resets dm->drawObject->colType to -1 */
00152 void GPU_color4_upload( struct DerivedMesh *dm, unsigned char *data );
00153 /* switch color rendering on=1/off=0 */
00154 void GPU_color_switch( int mode );
00155 
00156 /* used for drawing edges */
00157 void GPU_buffer_draw_elements( GPUBuffer *elements, unsigned int mode, int start, int count );
00158 
00159 /* called after drawing */
00160 void GPU_buffer_unbind(void);
00161 
00162 /* used to check whether to use the old (without buffers) code */
00163 int GPU_buffer_legacy( struct DerivedMesh *dm );
00164 
00165 /* Buffers for non-DerivedMesh drawing */
00166 void *GPU_build_mesh_buffers(struct GHash *map, struct MVert *mvert,
00167                         struct MFace *mface, int *face_indices,
00168                         int totface, int *vert_indices, int uniq_verts,
00169                         int totvert);
00170 void GPU_update_mesh_buffers(void *buffers, struct MVert *mvert,
00171                         int *vert_indices, int totvert);
00172 void *GPU_build_grid_buffers(struct DMGridData **grids,
00173         int *grid_indices, int totgrid, int gridsize);
00174 void GPU_update_grid_buffers(void *buffers_v, struct DMGridData **grids,
00175         int *grid_indices, int totgrid, int gridsize, int smooth);
00176 void GPU_draw_buffers(void *buffers);
00177 void GPU_free_buffers(void *buffers);
00178 
00179 #endif