-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyCube.h
More file actions
160 lines (127 loc) · 3.85 KB
/
MyCube.h
File metadata and controls
160 lines (127 loc) · 3.85 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#pragma once
#include <vgl.h>
#include <vec.h>
struct MyCubeVertex
{
vec4 position;
vec4 color;
vec3 normal;
};
class MyCube
{
public:
MyCube(void);
~MyCube(void);
static const int NumVertices = 36;
MyCubeVertex Vertices[NumVertices];
GLuint Init();
void SetPositionAndColorAttribute(GLuint program);
void ColorCube(vec4 * vin, vec4 * cin);
void Quad (int a, int b, int c, int d, vec4 * vin, vec4 * cin);
vec3 ComputeNormal(vec4 p0, vec4 p1, vec4 p2);
int NumCurVertices;
GLuint vao;
GLuint buffer;
bool bInitialized;
void Draw(GLuint program);
};
MyCube::MyCube(void)
{
bInitialized = false;
NumCurVertices = 0;
}
MyCube::~MyCube(void)
{
}
vec3 MyCube::ComputeNormal(vec4 p0, vec4 p1, vec4 p2)
{
vec3 a = vec3(p1.x,p1.y,p1.z) - vec3(p0.x,p0.y,p0.z) ;
vec3 b = vec3(p2.x,p2.y,p2.z) - vec3(p0.x,p0.y,p0.z) ;
vec3 n = cross(a,b); // dot(a,b);
n /= length(n);
return n;
}
void MyCube::Quad (int a, int b, int c, int d, vec4 * vin, vec4 * cin)
{
vec3 n = ComputeNormal(vin[a], vin[b], vin[c]);
Vertices[NumCurVertices].position = vin[a]; Vertices[NumCurVertices].color = cin[a];
Vertices[NumCurVertices].normal = n;
NumCurVertices++;
Vertices[NumCurVertices].position = vin[b]; Vertices[NumCurVertices].color = cin[b];
Vertices[NumCurVertices].normal = n;
NumCurVertices++;
Vertices[NumCurVertices].position = vin[c]; Vertices[NumCurVertices].color = cin[c];
Vertices[NumCurVertices].normal = n;
NumCurVertices++;
Vertices[NumCurVertices].position = vin[a]; Vertices[NumCurVertices].color = cin[a];
Vertices[NumCurVertices].normal = n;
NumCurVertices++;
Vertices[NumCurVertices].position = vin[c]; Vertices[NumCurVertices].color = cin[c];
Vertices[NumCurVertices].normal = n;
NumCurVertices++;
Vertices[NumCurVertices].position = vin[d]; Vertices[NumCurVertices].color = cin[d];
Vertices[NumCurVertices].normal = n;
NumCurVertices++;
}
void MyCube::ColorCube(vec4 * vin, vec4 * cin)
{
Quad(1,0,3,2, vin, cin);
Quad(2,3,7,6, vin, cin);
Quad(3,0,4,7, vin, cin);
Quad(6,5,1,2, vin, cin);
Quad(4,5,6,7, vin, cin);
Quad(5,4,0,1, vin, cin);
}
GLuint MyCube::Init()
{
// The Cube should be initialized only once;
if(bInitialized == true) return vao;
vec4 vertex_positions[8] ={
vec4( -0.5, -0.5, 0.5, 1.0 ),
vec4( -0.5, 0.5, 0.5, 1.0 ),
vec4( 0.5, 0.5, 0.5, 1.0 ),
vec4( 0.5, -0.5, 0.5, 1.0 ),
vec4( -0.5, -0.5, -0.5, 1.0 ),
vec4( -0.5, 0.5, -0.5, 1.0 ),
vec4( 0.5, 0.5, -0.5, 1.0 ),
vec4( 0.5, -0.5, -0.5, 1.0 ),
};
vec4 vertex_colors[8] ={
vec4( 0, 0, 0, 1),
vec4( 1, 0, 0, 1),
vec4( 1, 1, 1, 1),
vec4( 0, 1, 0, 1),
vec4( 0, 0, 1, 1),
vec4( 1, 0, 1, 1),
vec4( 1, 1, 0, 1),
vec4( 0, 1, 1, 1)
};
ColorCube(vertex_positions, vertex_colors);
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
bInitialized = true;
return vao;
}
void MyCube::SetPositionAndColorAttribute(GLuint program)
{
GLuint vPosition = glGetAttribLocation(program, "vPosition");
glEnableVertexAttribArray(vPosition);
glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, sizeof(MyCubeVertex), BUFFER_OFFSET(0));
GLuint vColor = glGetAttribLocation(program, "vColor");
glEnableVertexAttribArray(vColor);
glVertexAttribPointer(vColor, 4, GL_FLOAT, GL_FALSE, sizeof(MyCubeVertex), BUFFER_OFFSET(sizeof(vec4)));
GLuint vNormal = glGetAttribLocation(program, "vNormal");
glEnableVertexAttribArray(vNormal);
glVertexAttribPointer(vNormal, 3, GL_FLOAT, GL_FALSE, sizeof(MyCubeVertex), BUFFER_OFFSET(sizeof(vec4)+sizeof(vec4)));
}
void MyCube::Draw(GLuint program)
{
if(!bInitialized) return; // check whether it is initiazed or not.
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
SetPositionAndColorAttribute(program);
glDrawArrays(GL_TRIANGLES, 0, NumVertices);
}