Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain/src/lib/graphics/importer/terrain/buffer_broker.h @ 8641

Last change on this file since 8641 was 8641, checked in by ponder, 18 years ago

The level four pages are now merged into large buffers. Its not finished, though.

File size: 3.0 KB
Line 
1/*
2        orxonox - the future of 3D-vertical-scrollers
3 
4        Copyright (C) 2006 orx
5 
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, or (at your option)
9        any later version.
10 
11        ### File Specific:
12        main programmer: Marco Biasini
13
14 */
15
16#ifndef _BUFFERBROKER_H
17#define _BUFFERBROKER_H
18
19#include "glincl.h"
20#include "debug.h"
21
22/**
23 * @brief       Manages the streaming of the vertex buffer objects for the terrain page.
24 */
25
26struct BufferPair {
27        BufferPair( int _i, int _v ) { indexName = _i; vertexName = _v; next = NULL; }
28        BufferPair() { indexName = vertexName = 0; next = NULL; }
29        GLuint                  indexName;     
30        GLuint                  vertexName;
31        BufferPair              *next;
32};
33
34typedef BufferPair *pBufferPair;
35
36class BufferBroker {
37        public:
38               
39                BufferBroker( int _num, int _vbSize, int _ibSize )
40                {
41                        capacity = _num;
42                        freeIndexBuffers = new GLuint[capacity];
43                        freeVertexBuffers =     new GLuint[capacity];
44                        lastAcquired = 0;
45                        vbSize = _vbSize; ibSize = _ibSize;
46                        glGenBuffersARB( capacity, freeIndexBuffers );
47                        glGenBuffersARB( capacity, freeVertexBuffers );
48                        for ( int i = 0; i < capacity; ++i ) {
49                                glBindBufferARB( GL_ARRAY_BUFFER_ARB, freeVertexBuffers[i] );
50                                glBufferDataARB( GL_ARRAY_BUFFER_ARB, vbSize, 
51                                        NULL, GL_DYNAMIC_DRAW_ARB );
52                                glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, freeIndexBuffers[i] );
53                                glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, ibSize, 
54                                        NULL, GL_DYNAMIC_DRAW_ARB );           
55                        }
56                        head = tail = NULL;
57                }
58                inline int getIBSize() { return ibSize; }
59                inline int getVBSize() { return vbSize; }               
60                inline void acquire( GLuint &_vbName, GLuint &_ibName );
61               
62                inline void release( const GLuint &_vbName, const GLuint &_ibName );
63        protected:
64
65                int                                             capacity;
66               
67                int                                             vbSize,
68                                                                ibSize;
69                int                                             lastAcquired;
70               
71                GLuint                                  *freeIndexBuffers,
72                                                                *freeVertexBuffers;                                             
73                                                               
74                BufferPair                              *head, *tail;
75};
76
77inline void BufferBroker::release( const GLuint &_vbName, const GLuint &_ibName )
78{
79        pBufferPair bp = head, prev = NULL;
80        bool removed = false;
81        while ( bp ) {
82
83                if ( bp->indexName == _ibName && bp->vertexName == _vbName ) {
84                        if ( prev ) {
85                                prev->next = bp->next;                                 
86                        }
87                        else {
88                                head = bp->next;
89                        }
90                        if ( bp->next == NULL )
91                                tail = prev;
92                        int index = capacity-lastAcquired;
93                        lastAcquired--;
94                        freeIndexBuffers[index] = bp->indexName;
95                        freeVertexBuffers[index] = bp->vertexName;
96                        delete bp;
97                        removed = true;
98                        break; 
99                }
100                prev = bp;
101                bp = bp->next;
102        }
103        assert( removed );
104}
105
106inline void BufferBroker::acquire( GLuint &_vbName, GLuint &_ibName )
107{
108        assert( capacity > lastAcquired );
109        int index = capacity-lastAcquired-1;
110        _vbName = freeVertexBuffers[index]; _ibName = freeIndexBuffers[index];
111        lastAcquired++;
112        if ( tail ) {
113                tail->next = new BufferPair( _ibName, _vbName );
114                tail = tail->next;
115        }
116        else {
117                tail = head = new BufferPair( _ibName, _vbName );       
118        }
119}
120
121typedef BufferBroker *pBufferBroker;
122
123#endif
Note: See TracBrowser for help on using the repository browser.