-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGLprimitives.cpp
More file actions
144 lines (133 loc) · 4.62 KB
/
GLprimitives.cpp
File metadata and controls
144 lines (133 loc) · 4.62 KB
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
// Copyright © 2008-2012 Pioneer Developers. See AUTHORS.txt for details
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
#include "GLprimitives.h"
// GLew
#include "glew.h"
#include "utils.h"
// Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
////////////////////////////////////////////////////////////////
// CGLquad related data and definitions
namespace QuadData {
static const GLfloat s_vertex_buffer_data[18] = {
// triangle 1
-1.0f,-1.0f, 0.0f, // v1
1.0f, 1.0f, 0.0f, // v3
-1.0f, 1.0f, 0.0f, // v2
// triangle 2
1.0f, 1.0f, 0.0f, // v1
-1.0f,-1.0f, 0.0f, // v3
1.0f,-1.0f, 0.0f // v2
};
static const GLfloat s_normal_buffer_data[18] = {
// triangle 1
0.0f, 0.0f, 1.0f, // v1
0.0f, 0.0f, 1.0f, // v3
0.0f, 0.0f, 1.0f, // v2
// triangle 2
0.0f, 0.0f, 1.0f, // v1
0.0f, 0.0f, 1.0f, // v3
0.0f, 0.0f, 1.0f // v2
};
static const GLfloat s_uv_buffer_data[12] = {
// triangle 1
0.0f, 0.0f, // v1
1.0f, 1.0f, // v3
0.0f, 1.0f, // v2
// triangle 2
1.0f, 1.0f, // v1
0.0f, 0.0f, // v3
1.0f, 0.0f // v2
};
}; // namespace QuadData
CGLquad::CGLquad(const bool bNormals_, const bool bUVs_)
: mVBO(sizeof(QuadData::s_vertex_buffer_data)/sizeof(QuadData::s_vertex_buffer_data[0]),
&QuadData::s_vertex_buffer_data[0],
bNormals_ ? &QuadData::s_normal_buffer_data[0] : nullptr, // normals are optional
bUVs_ ? &QuadData::s_uv_buffer_data[0] : nullptr) // UVcoords are optional
{
}
CGLquad::~CGLquad()
{
}
void CGLquad::Render() const
{
ScopedBindRelease<CGLvbo> vbo(mVBO);
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 2*3); // From index 0 to 2*3 -> 2 triangles
checkGLError();
}
////////////////////////////////////////////////////////////////
// CGLcube related data and definitions
namespace CubeData {
static GLfloat s_vertex_buffer_data[6][12] = {
{ 0.58f, 0.58f, 0.58f, -0.58f, 0.58f, 0.58f, -0.58f, -0.58f, 0.58f, 0.58f, -0.58f, 0.58f },
{ 0.58f, -0.58f, 0.58f, -0.58f, -0.58f, 0.58f, -0.58f, -0.58f, -0.58f, 0.58f, -0.58f, -0.58f },
{ 0.58f, 0.58f, 0.58f, 0.58f, -0.58f, 0.58f, 0.58f, -0.58f, -0.58f, 0.58f, 0.58f, -0.58f },
{ -0.58f, 0.58f, 0.58f, 0.58f, 0.58f, 0.58f, 0.58f, 0.58f, -0.58f, -0.58f, 0.58f, -0.58f },
{ -0.58f, -0.58f, 0.58f, -0.58f, 0.58f, 0.58f, -0.58f, 0.58f, -0.58f, -0.58f, -0.58f, -0.58f },
{ 0.58f, -0.58f, -0.58f, -0.58f, -0.58f, -0.58f, -0.58f, 0.58f, -0.58f, 0.58f, 0.58f, -0.58f }
};
static const GLfloat s_normal_buffer_data[6][12] = {
{ 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f},
{ 0.0f,-1.0f, 0.0f, 0.0f,-1.0f, 0.0f, 0.0f,-1.0f, 0.0f, 0.0f,-1.0f, 0.0f},
{ 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
{ 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f},
{-1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f},
{ 0.0f, 0.0f,-1.0f, 0.0f, 0.0f,-1.0f, 0.0f, 0.0f,-1.0f, 0.0f, 0.0f,-1.0f}
};
static GLfloat s_uv_buffer_data[8] = {
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f
};
}; // namespace CubeData
#ifdef _DEBUG
CGLcube::CGLcube(const bool bNormals_, const bool bUVs_, const GLuint textures[scNumFaces], const glm::vec4 colours[scNumFaces], const GLuint colourUniform)
#else
CGLcube::CGLcube(const bool bNormals_, const bool bUVs_, const GLuint textures[scNumFaces])
#endif
{
for(uint32_t i=0; i<scNumFaces; i++) {
assert(GL_TRUE==glIsTexture(textures[i]));
mFaceTextures[i] = textures[i];
}
#ifdef _DEBUG
for(uint32_t i=0; i<scNumFaces; i++) {
mColours[i] = colours[i];
}
mColourUniform = colourUniform;
#endif
for(uint32_t i=0; i<scNumFaces; i++) {
mpVBO[i] = new CGLvbo( (sizeof(CubeData::s_vertex_buffer_data)/sizeof(CubeData::s_vertex_buffer_data[i])),
&CubeData::s_vertex_buffer_data[i],
bNormals_ ? &CubeData::s_normal_buffer_data[0] : nullptr,
bUVs_ ? &CubeData::s_uv_buffer_data[0] : nullptr);
}
}
CGLcube::~CGLcube()
{
for(uint32_t i=0; i<scNumFaces; i++)
{
delete mpVBO[i];
mpVBO[i] = nullptr;
}
}
void CGLcube::Render() const
{
if(nullptr==mpVBO[0])
return;
// render each of the faces
for (int i=0; i<scNumFaces; i++) {
ScopedBindRelease<CGLvbo> vbo(*mpVBO[i]);
#ifdef _DEBUG
glUniform4fv(mColourUniform, 1, &mColours[i][0]);
#endif
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mFaceTextures[i]);
glDrawArrays(GL_QUADS, 0, 4);
}
checkGLError();
}