2 This file is part of the graphics core library.
4 Copyright (c) 2004, 2005 John Tsiombikas <nuclear@siggraph.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /* fundamendal data structures for 3D graphics (inline definitions and templates)
23 * Author: John Tsiombikas 2004
25 * Mihalis Georgoulopoulos 2004
26 * John Tsiombikas 2005
33 #include "3dengfx/3denginefx_types.hpp"
35 SysCaps get_system_capabilities();
36 #endif // USING_3DENGFX
40 template <class DataType>
41 GeometryArray<DataType>::GeometryArray(bool dynamic) {
44 buffer_object = INVALID_VBO;
50 template <class DataType>
51 GeometryArray<DataType>::GeometryArray(const DataType *data, unsigned long count, bool dynamic) {
54 buffer_object = INVALID_VBO;
57 set_data(data, count);
60 template <class DataType>
61 GeometryArray<DataType>::GeometryArray(const GeometryArray<DataType> &ga) {
65 buffer_object = INVALID_VBO;
67 set_data(ga.data, ga.count);
70 template <class DataType>
71 GeometryArray<DataType>::~GeometryArray() {
72 if(data) delete [] data;
74 if(buffer_object != INVALID_VBO) {
75 glext::glDeleteBuffers(1, &buffer_object);
77 #endif // USING_3DENGFX
80 template <class DataType>
81 GeometryArray<DataType> &GeometryArray<DataType>::operator =(const GeometryArray<DataType> &ga) {
83 if(data) delete [] data;
85 set_data(ga.data, ga.count);
90 template <class DataType>
91 void GeometryArray<DataType>::sync_buffer_object() {
95 if(buffer_object == INVALID_VBO) {
96 glext::glGenBuffers(1, &buffer_object);
97 glext::glBindBuffer(GL_ARRAY_BUFFER_ARB, buffer_object);
98 glext::glBufferData(GL_ARRAY_BUFFER_ARB, count * sizeof(DataType), data, GL_STATIC_DRAW_ARB);
99 glext::glBindBuffer(GL_ARRAY_BUFFER_ARB, 0);
102 while(glGetError() != GL_NO_ERROR);
103 glext::glBindBuffer(GL_ARRAY_BUFFER_ARB, buffer_object);
105 glext::glBufferData(GL_ARRAY_BUFFER_ARB, count * sizeof(DataType), data, GL_STATIC_DRAW_ARB);
106 glext::glBindBuffer(GL_ARRAY_BUFFER_ARB, 0);
108 #endif // USING_3DENGFX
113 template <class DataType>
114 inline void GeometryArray<DataType>::set_data(const DataType *data, unsigned long count) {
116 if(!this->data || count != this->count) {
118 delete [] this->data;
120 this->data = new DataType[count];
123 memcpy(this->data, data, count * sizeof(DataType));
128 if(buffer_object != INVALID_VBO && count != this->count) {
129 glext::glDeleteBuffers(1, &buffer_object);
131 sync_buffer_object();
134 #endif // USING_3DENGFX
137 template <class DataType>
138 inline const DataType *GeometryArray<DataType>::get_data() const {
142 template <class DataType>
143 inline DataType *GeometryArray<DataType>::get_mod_data() {
148 template <class DataType>
149 inline unsigned long GeometryArray<DataType>::get_count() const {
153 template <class DataType>
154 void GeometryArray<DataType>::set_dynamic(bool enable) {
156 SysCaps sys_caps = get_system_capabilities();
159 if(!dynamic && !sys_caps.vertex_buffers) {
164 #endif // USING_3DENGFX
167 template <class DataType>
168 inline bool GeometryArray<DataType>::get_dynamic() const {
172 template <class DataType>
173 inline unsigned int GeometryArray<DataType>::get_buffer_object() const {
174 if(!dynamic && !vbo_in_sync) {
175 const_cast<GeometryArray<DataType>*>(this)->sync_buffer_object();
178 return buffer_object;
181 // inline functions of <index> specialization of GeometryArray
183 inline const Index *GeometryArray<Index>::get_data() const {
187 inline Index *GeometryArray<Index>::get_mod_data() {
192 inline unsigned long GeometryArray<Index>::get_count() const {
196 inline void GeometryArray<Index>::set_dynamic(bool enable) {
198 SysCaps sys_caps = get_system_capabilities();
201 if(!dynamic && !sys_caps.vertex_buffers) {
206 #endif // USING_3DENGFX
209 inline bool GeometryArray<Index>::get_dynamic() const {
213 inline unsigned int GeometryArray<Index>::get_buffer_object() const {
214 if(!dynamic && !vbo_in_sync) {
215 const_cast<GeometryArray<Index>*>(this)->sync_buffer_object();
218 return buffer_object;
222 ///////// Triangle Mesh Implementation (inline functions) //////////
223 inline const VertexArray *TriMesh::get_vertex_array() const {
227 inline VertexArray *TriMesh::get_mod_vertex_array() {
228 vertex_stats_valid = false;
230 index_graph_valid = false;
231 triangle_normals_valid = triangle_normals_normalized = false;
235 inline const TriangleArray *TriMesh::get_triangle_array() const {
239 inline TriangleArray *TriMesh::get_mod_triangle_array() {
240 indices_valid = false;
242 index_graph_valid = false;
243 triangle_normals_valid = triangle_normals_normalized = false;