File: Geometry.h

package info (click to toggle)
jazz2-native 3.5.0-1
  • links: PTS, VCS
  • area: contrib
  • in suites:
  • size: 16,836 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (149 lines) | stat: -rw-r--r-- 5,281 bytes parent folder | download | duplicates (2)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#pragma once

#include "GL/GLBufferObject.h"
#include "RenderBuffersManager.h"

#include <memory>

namespace nCine
{
	/// Contains geometric data for a drawable node
	class Geometry
	{
		friend class RenderCommand;

	public:
		/// Default constructor
		Geometry();
		~Geometry();

		Geometry(const Geometry&) = delete;
		Geometry& operator=(const Geometry&) = delete;

		/// Returns the primitive type (`GL_TRIANGLES`, `GL_TRIANGLE_STRIP`, ...)
		inline GLenum GetPrimitiveType() const {
			return primitiveType_;
		}
		/// Returns the index of the first vertex to draw
		inline GLint GetFirstVertex() const {
			return firstVertex_;
		}
		/// Returns the number of vertices
		inline GLsizei GetVertexCount() const {
			return numVertices_;
		}
		/// Returns the number of float elements that composes the vertex format
		inline std::uint32_t GetElementsPerVertex() const {
			return numElementsPerVertex_;
		}

		/// Sets all three drawing parameters
		void SetDrawParameters(GLenum primitiveType, GLint firstVertex, GLsizei numVertices);
		/// Sets the primitive type (`GL_TRIANGLES`, `GL_TRIANGLE_STRIP`, ...)
		inline void SetPrimitiveType(GLenum primitiveType) {
			primitiveType_ = primitiveType;
		}
		/// Sets the index number of the first vertex to draw
		inline void SetFirstVertex(GLint firstVertex) {
			firstVertex_ = firstVertex;
		}
		/// Sets the number of vertices
		inline void SetVertexCount(GLsizei numVertices) {
			numVertices_ = numVertices;
		}
		/// Sets the number of float elements that composes the vertex format
		inline void SetElementsPerVertex(std::uint32_t numElements) {
			numElementsPerVertex_ = numElements;
		}
		/// Creates a custom VBO that is unique to this `Geometry` object
		void CreateCustomVbo(std::uint32_t numFloats, GLenum usage);
		/// Retrieves a pointer that can be used to write vertex data from a custom VBO owned by this object
		/*! This overloaded version allows a custom alignment specification */
		GLfloat* AcquireVertexPointer(std::uint32_t numFloats, std::uint32_t numFloatsAlignment);
		/// Retrieves a pointer that can be used to write vertex data from a custom VBO owned by this object
		inline GLfloat* AcquireVertexPointer(std::uint32_t numFloats) {
			return AcquireVertexPointer(numFloats, 1);
		}
		/// Retrieves a pointer that can be used to write vertex data from a VBO owned by the buffers manager
		GLfloat* AcquireVertexPointer();
		/// Releases the pointer used to write vertex data
		void ReleaseVertexPointer();

		/// Returns a pointer into host memory containing vertex data to be copied into a VBO
		inline const float* GetHostVertexPointer() const {
			return hostVertexPointer_;
		}
		/// Sets a pointer into host memory containing vertex data to be copied into a VBO
		void SetHostVertexPointer(const float* vertexPointer);

		/// Shares the VBO of another `Geometry` object
		void ShareVbo(const Geometry* geometry);

		/// Returns the number of indices used to render the geometry
		inline std::uint32_t GetIndexCount() const {
			return numIndices_;
		}
		/// Sets the index number of the first index to draw
		inline void SetFirstIndex(GLushort firstIndex) {
			firstIndex_ = firstIndex;
		}
		/// Sets the number of indices used to render the geometry
		inline void SetIndexCount(std::uint32_t numIndices) {
			numIndices_ = numIndices;
		}
		/// Creates a custom IBO that is unique to this `Geometry` object
		void CreateCustomIbo(std::uint32_t numIndices, GLenum usage);
		/// Retrieves a pointer that can be used to write index data from a custom IBO owned by this object
		GLushort* AcquireIndexPointer(std::uint32_t numIndices);
		/// Retrieves a pointer that can be used to write index data from a IBO owned by the buffers manager
		GLushort* AcquireIndexPointer();
		/// Releases the pointer used to write index data
		void ReleaseIndexPointer();

		/// Returns a pointer into host memory containing index data to be copied into a IBO
		inline const GLushort* GetHostIndexPointer() const {
			return hostIndexPointer_;
		}
		/// Sets a pointer into host memory containing index data to be copied into a IBO
		void SetHostIndexPointer(const GLushort* indexPointer);

		/// Shares the IBO of another `Geometry` object
		void ShareIbo(const Geometry* geometry);

	private:
		GLenum primitiveType_;
		GLint firstVertex_;
		GLsizei numVertices_;
		std::uint32_t numElementsPerVertex_;
		GLushort firstIndex_;
		std::uint32_t numIndices_;
		const float* hostVertexPointer_;
		const GLushort* hostIndexPointer_;

		std::unique_ptr<GLBufferObject> vbo_;
		GLenum vboUsageFlags_;
		RenderBuffersManager::Parameters vboParams_;
		const RenderBuffersManager::Parameters* sharedVboParams_;

		std::unique_ptr<GLBufferObject> ibo_;
		GLenum iboUsageFlags_;
		RenderBuffersManager::Parameters iboParams_;
		const RenderBuffersManager::Parameters* sharedIboParams_;

		bool hasDirtyVertices_;
		bool hasDirtyIndices_;

		void Bind();
		void Draw(GLsizei numInstances);
		void CommitVertices();
		void CommitIndices();

		inline const RenderBuffersManager::Parameters& GetVboParams() const {
			return sharedVboParams_ ? *sharedVboParams_ : vboParams_;
		}
		inline const RenderBuffersManager::Parameters& GetIboParams() const {
			return sharedIboParams_ ? *sharedIboParams_ : iboParams_;
		}
	};

}