File: TerrainVertexBuffer.cpp

package info (click to toggle)
spring 103.0%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,720 kB
  • ctags: 63,685
  • sloc: cpp: 368,283; ansic: 33,988; python: 12,417; java: 12,203; awk: 5,879; sh: 1,846; xml: 655; perl: 405; php: 211; objc: 194; makefile: 77; sed: 2
file content (94 lines) | stat: -rw-r--r-- 1,463 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */


#include "TerrainBase.h"
#include "TerrainVertexBuffer.h"

using namespace terrain;

int VertexBuffer::totalBufferSize = 0;

VertexBuffer::VertexBuffer()
	: data(NULL)
	, id(0)
	, size(0)
	, type(GL_ARRAY_BUFFER_ARB)
{
}

void VertexBuffer::Init(int bytesize)
{
	Free();

	if (GLEW_ARB_vertex_buffer_object) {
		data = NULL;
		glGenBuffersARB(1, &id);
	} else {
		data = new char[bytesize];
	}
	size = bytesize;
	totalBufferSize+=size;
}

VertexBuffer::~VertexBuffer()
{
	Free();
}

void VertexBuffer::Free()
{
	if (id) {
		glDeleteBuffersARB(1, &id);
		id = 0;
	} else {
		delete [] data;
	}
	totalBufferSize-=size;
}

void* VertexBuffer::LockData()
{
	if (id) {
		// Hurray for ATI, which has broken buffer memory mapping support :(
		/*glBindBufferARB(type, id);
		glBufferDataARB(type, size, 0, GL_STATIC_DRAW_ARB);
		return glMapBufferARB(type, GL_WRITE_ONLY);*/
		data = new char [size];
		return data;
	} else {
		return data;
	}
}

void VertexBuffer::UnlockData()
{
	if (id) {
		glBindBufferARB(type, id);
		glBufferDataARB(type, size, data, GL_STATIC_DRAW_ARB);
		glBindBufferARB(type, 0);
		delete[] data;
	}
}

void* VertexBuffer::Bind()
{
	if (id) {
		glBindBufferARB(type, id);
		return 0;
	}
	else return (void*)data;
}


void VertexBuffer::Unbind()
{
	if (id) {
		glBindBufferARB(type, 0);
	}
}


IndexBuffer::IndexBuffer()
{
	type = GL_ELEMENT_ARRAY_BUFFER_ARB;
}