1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2 
3 
4 #include "TerrainBase.h"
5 #include "TerrainVertexBuffer.h"
6 
7 using namespace terrain;
8 
9 int VertexBuffer::totalBufferSize = 0;
10 
VertexBuffer()11 VertexBuffer::VertexBuffer()
12 	: data(NULL)
13 	, id(0)
14 	, size(0)
15 	, type(GL_ARRAY_BUFFER_ARB)
16 {
17 }
18 
Init(int bytesize)19 void VertexBuffer::Init(int bytesize)
20 {
21 	Free();
22 
23 	if (GLEW_ARB_vertex_buffer_object) {
24 		data = NULL;
25 		glGenBuffersARB(1, &id);
26 	} else {
27 		data = new char[bytesize];
28 	}
29 	size = bytesize;
30 	totalBufferSize+=size;
31 }
32 
~VertexBuffer()33 VertexBuffer::~VertexBuffer()
34 {
35 	Free();
36 }
37 
Free()38 void VertexBuffer::Free()
39 {
40 	if (id) {
41 		glDeleteBuffersARB(1, &id);
42 		id = 0;
43 	} else {
44 		delete [] data;
45 	}
46 	totalBufferSize-=size;
47 }
48 
LockData()49 void* VertexBuffer::LockData()
50 {
51 	if (id) {
52 		// Hurray for ATI, which has broken buffer memory mapping support :(
53 		/*glBindBufferARB(type, id);
54 		glBufferDataARB(type, size, 0, GL_STATIC_DRAW_ARB);
55 		return glMapBufferARB(type, GL_WRITE_ONLY);*/
56 		data = new char [size];
57 		return data;
58 	} else {
59 		return data;
60 	}
61 }
62 
UnlockData()63 void VertexBuffer::UnlockData()
64 {
65 	if (id) {
66 		glBindBufferARB(type, id);
67 		glBufferDataARB(type, size, data, GL_STATIC_DRAW_ARB);
68 		glBindBufferARB(type, 0);
69 		delete[] data;
70 	}
71 }
72 
Bind()73 void* VertexBuffer::Bind()
74 {
75 	if (id) {
76 		glBindBufferARB(type, id);
77 		return 0;
78 	}
79 	else return (void*)data;
80 }
81 
82 
Unbind()83 void VertexBuffer::Unbind()
84 {
85 	if (id) {
86 		glBindBufferARB(type, 0);
87 	}
88 }
89 
90 
IndexBuffer()91 IndexBuffer::IndexBuffer()
92 {
93 	type = GL_ELEMENT_ARRAY_BUFFER_ARB;
94 }
95