[8570] | 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 | |
---|
| 26 | struct 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 | |
---|
| 34 | typedef BufferPair *pBufferPair; |
---|
| 35 | |
---|
| 36 | class 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 | |
---|
| 59 | inline void acquire( GLuint &_vbName, GLuint &_ibName ); |
---|
| 60 | |
---|
| 61 | inline void release( const GLuint &_vbName, const GLuint &_ibName ); |
---|
| 62 | protected: |
---|
| 63 | |
---|
| 64 | int capacity; |
---|
| 65 | |
---|
| 66 | int vbSize, |
---|
| 67 | ibSize; |
---|
| 68 | int lastAcquired; |
---|
| 69 | |
---|
| 70 | GLuint *freeIndexBuffers, |
---|
| 71 | *freeVertexBuffers; |
---|
| 72 | |
---|
| 73 | BufferPair *head, *tail; |
---|
| 74 | }; |
---|
| 75 | |
---|
| 76 | inline void BufferBroker::release( const GLuint &_vbName, const GLuint &_ibName ) |
---|
| 77 | { |
---|
| 78 | pBufferPair bp = head, prev = NULL; |
---|
| 79 | bool removed = false; |
---|
| 80 | |
---|
| 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 | |
---|
| 106 | inline void BufferBroker::acquire( GLuint &_vbName, GLuint &_ibName ) |
---|
| 107 | { |
---|
[8595] | 108 | |
---|
[8570] | 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 | |
---|
| 121 | typedef BufferBroker *pBufferBroker; |
---|
| 122 | |
---|
| 123 | #endif |
---|