-
Notifications
You must be signed in to change notification settings - Fork 0
/
coin.cpp
196 lines (158 loc) · 4.74 KB
/
coin.cpp
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "headers/coin.h"
Coin::Coin(int _x, int _y)
{
mX = _x;
mY = _y;
GLfloat vertices[] =
{
0.5f, 0.5f,0.0f, //TOP RIGHT
0.5f, -0.5f, 0.0f, // BOTTOM RIGHT
-0.5f, -0.5f,0.0f, // BOTTOM LEFT
-0.5f, 0.5f, 0.0f //TOP LEFT
};
GLuint indices[] =
{
0,1,3,
1,2,3
};
// Shaders
GLchar* vertexShaderSource =
"#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"uniform mat4 projectionMat;\n"
"uniform mat4 modelMat;\n"
"uniform mat4 viewMat;\n"
"void main()\n"
"{\n"
"gl_Position = projectionMat * viewMat* modelMat * vec4(position, 1.0);\n"
"}\0";
GLchar* fragmentShaderSource = "#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
"color = vec4(0.8f, 0.9f, 0.2f, 1.0f);\n"
"}\n\0";
mVertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(mVertexShader , 1, &vertexShaderSource, NULL);
glCompileShader(mVertexShader);
CheckShader(mVertexShader);
mFragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(mFragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(mFragmentShader);
CheckShader(mFragmentShader);
mShaderProgram = glCreateProgram();
glAttachShader(mShaderProgram, mVertexShader );
glAttachShader(mShaderProgram, mFragmentShader);
glLinkProgram(mShaderProgram);
CheckProgramShader(mShaderProgram);
glGenVertexArrays(1, &mVAO);
glGenBuffers(1, &mVBO);
glGenBuffers(1, &mEBO);
glBindVertexArray(mVAO);
// 0. Copy our vertices array in a buffer for OpenGL to use
glBindBuffer(GL_ARRAY_BUFFER, mVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mEBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices,
GL_STATIC_DRAW);
// 1. Then set the vertex attributes pointers
glBindBuffer(GL_ARRAY_BUFFER, 0);
//4. Unbind the VAO
glBindVertexArray(0);
}
void Coin::Update(double _deltaTime)
{
if(!mDetectable)
{
mShowTimer += _deltaTime;
//std::cout << "Doing stuff" << std::endl;
if(mShowTimer >= 3)
{
Show();
mShowTimer = 0;
}
}
}
void Coin::Show()
{
srand(time(NULL));
mX = rand() % 20 - 10;
mY = rand() % 12 - 6;
mDetectable = true;
}
void Coin::Hide()
{
mDetectable = false;
}
void Coin::CheckShader(GLuint _shader)
{
GLint success;
GLchar infoLog[512];
glGetShaderiv(_shader, GL_COMPILE_STATUS, &success);
if(!success)
{
glGetShaderInfoLog(_shader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::LOLZ::COMPILATION_FAILED\n" << infoLog << std::endl;
}
}
int Coin::GetXPos()
{
return mX;
}
int Coin::GetYPos()
{
return mY;
}
void Coin::SetPosition(int _x, int _y)
{
mX = _x;
mY = _y;
}
void Coin::CheckProgramShader(GLuint _progShader)
{
GLint success;
GLchar infoLog[512];
glGetProgramiv(_progShader, GL_LINK_STATUS, &success);
if(!success)
{
glGetProgramInfoLog(_progShader, 512, NULL, infoLog);
}
}
bool Coin::IsDetectable()
{
return mDetectable;
}
void Coin::Render()
{
glUseProgram(mShaderProgram);
glm::mat4x4 projectionMat = glm::perspective(45.0f,800.0f/600.0f,0.1f,100.0f);
GLuint projectionMatLoc = glGetUniformLocation(mShaderProgram, "projectionMat");
glUniformMatrix4fv(projectionMatLoc, 1, GL_FALSE,
glm::value_ptr(projectionMat));
GLuint modelMatLoc = glGetUniformLocation(mShaderProgram, "modelMat");
glm::mat4x4 modelMat = glm::mat4x4();
modelMat = glm::translate(modelMat,glm::vec3(mX,mY,0));
modelMat = glm::scale(modelMat,glm::vec3(0.9f,0.9f,0));
glUniformMatrix4fv(modelMatLoc , 1, GL_FALSE, glm::value_ptr(modelMat));
GLuint viewMatLoc = glGetUniformLocation(mShaderProgram, "viewMat");
glm::mat4x4 viewMat = glm::mat4x4();
viewMat = glm::lookAt(glm::vec3(0,0,15),glm::vec3(),glm::vec3(0.0, 1.0f, 0.0f));
glUniformMatrix4fv(viewMatLoc, 1, GL_FALSE, glm::value_ptr(viewMat));
/* glm::mat4x4 trans;
trans = glm::translate(trans,glm::vec3(0,0,5));
glUniformMatrix4fv(transformLoc, 1, GL_FALSE,
glm::value_ptr(glm::ortho(0.0f, 800.0f, 0.0f, 600.0f, 0.1f, 100.0f)));*/
//glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr());
glBindVertexArray(mVAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mEBO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
Coin::~Coin()
{
//dtor
glDeleteShader(mVertexShader);
glDeleteShader(mFragmentShader);
}