forked from flyerchris/cghw2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
322 lines (262 loc) · 10.9 KB
/
main.cpp
File metadata and controls
322 lines (262 loc) · 10.9 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <fstream>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <vector>
#include <tiny_obj_loader.h>
struct object_struct {
unsigned int program;
unsigned int vao;
unsigned int vbo[4];
unsigned int texture;
glm::mat4 model;
object_struct() : model(glm::mat4(1.0f)) { }
};
std::vector<object_struct> objects; // vertex array object,vertex buffer object and texture(color) for objs
unsigned int program, program2;
std::vector<int> indicesCount; // number of indice of objs
static void error_callback(int error, const char *description) {
fputs(description, stderr);
}
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
static unsigned int setup_shader(const char *vertex_shader, const char *fragment_shader) {
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, (const GLchar **) &vertex_shader, nullptr);
glCompileShader(vs);
int status, maxLength;
char *infoLog = nullptr;
glGetShaderiv(vs, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE) {
glGetShaderiv(vs, GL_INFO_LOG_LENGTH, &maxLength);
/* The maxLength includes the NULL character */
infoLog = new char[maxLength];
glGetShaderInfoLog(vs, maxLength, &maxLength, infoLog);
fprintf(stderr, "Vertex Shader Error: %s\n", infoLog);
/* Handle the error in an appropriate way such as displaying a message or writing to a log file. */
/* In this simple program, we'll just leave */
delete[] infoLog;
return 0;
}
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, (const GLchar **) &fragment_shader, nullptr);
glCompileShader(fs);
glGetShaderiv(fs, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE) {
glGetShaderiv(fs, GL_INFO_LOG_LENGTH, &maxLength);
/* The maxLength includes the NULL character */
infoLog = new char[maxLength];
glGetShaderInfoLog(fs, maxLength, &maxLength, infoLog);
fprintf(stderr, "Fragment Shader Error: %s\n", infoLog);
/* Handle the error in an appropriate way such as displaying a message or writing to a log file. */
/* In this simple program, we'll just leave */
delete[] infoLog;
return 0;
}
unsigned int program = glCreateProgram();
// Attach our shaders to our program
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (status == GL_FALSE) {
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);
/* The maxLength includes the NULL character */
infoLog = new char[maxLength];
glGetProgramInfoLog(program, maxLength, NULL, infoLog);
glGetProgramInfoLog(program, maxLength, &maxLength, infoLog);
fprintf(stderr, "Link Error: %s\n", infoLog);
/* Handle the error in an appropriate way such as displaying a message or writing to a log file. */
/* In this simple program, we'll just leave */
delete[] infoLog;
return 0;
}
return program;
}
static std::string readfile(const char *filename) {
std::ifstream ifs(filename);
if (!ifs)
exit(EXIT_FAILURE);
return std::string((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()));
}
// mini bmp loader written by HSU YOU-LUN
static unsigned char *load_bmp(const char *bmp, unsigned int *width, unsigned int *height, unsigned short int *bits) {
unsigned char *result = nullptr;
FILE *fp = fopen(bmp, "rb");
if (!fp)
return nullptr;
char type[2];
unsigned int size, offset;
// check for magic signature
fread(type, sizeof(type), 1, fp);
if (type[0] == 0x42 || type[1] == 0x4d) {
fread(&size, sizeof(size), 1, fp);
// ignore 2 two-byte reversed fields
fseek(fp, 4, SEEK_CUR);
fread(&offset, sizeof(offset), 1, fp);
// ignore size of bmpinfoheader field
fseek(fp, 4, SEEK_CUR);
fread(width, sizeof(*width), 1, fp);
fread(height, sizeof(*height), 1, fp);
// ignore planes field
fseek(fp, 2, SEEK_CUR);
fread(bits, sizeof(*bits), 1, fp);
unsigned char *pos = result = new unsigned char[size - offset];
fseek(fp, offset, SEEK_SET);
while (size - ftell(fp) > 0)
pos += fread(pos, 1, size - ftell(fp), fp);
}
fclose(fp);
return result;
}
static int add_obj(unsigned int program, const char *filename, const char *texbmp) {
object_struct new_node;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string err = tinyobj::LoadObj(shapes, materials, filename);
if (!err.empty() || shapes.size() == 0) {
std::cerr << err << std::endl;
exit(1);
}
glGenVertexArrays(1, &new_node.vao);
glGenBuffers(4, new_node.vbo);
glGenTextures(1, &new_node.texture);
glBindVertexArray(new_node.vao);
// Upload postion array
glBindBuffer(GL_ARRAY_BUFFER, new_node.vbo[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * shapes[0].mesh.positions.size(),
shapes[0].mesh.positions.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
if (shapes[0].mesh.texcoords.size() > 0) {
// Upload texCoord array
glBindBuffer(GL_ARRAY_BUFFER, new_node.vbo[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * shapes[0].mesh.texcoords.size(),
shapes[0].mesh.texcoords.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
glBindTexture(GL_TEXTURE_2D, new_node.texture);
unsigned int width, height;
unsigned short int bits;
unsigned char *bgr = load_bmp(texbmp, &width, &height, &bits);
GLenum format = (bits == 24 ? GL_BGR : GL_BGRA);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, format, GL_UNSIGNED_BYTE, bgr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glGenerateMipmap(GL_TEXTURE_2D);
delete[] bgr;
}
if (shapes[0].mesh.normals.size() > 0) {
// Upload normal array
glBindBuffer(GL_ARRAY_BUFFER, new_node.vbo[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * shapes[0].mesh.normals.size(),
shapes[0].mesh.normals.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
}
// Setup index buffer for glDrawElements
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, new_node.vbo[3]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * shapes[0].mesh.indices.size(),
shapes[0].mesh.indices.data(), GL_STATIC_DRAW);
indicesCount.push_back(shapes[0].mesh.indices.size());
glBindVertexArray(0);
new_node.program = program;
objects.push_back(new_node);
return objects.size() - 1;
}
static void releaseObjects() {
for (int i = 0; i < objects.size(); i++) {
glDeleteVertexArrays(1, &objects[i].vao);
glDeleteTextures(1, &objects[i].texture);
glDeleteBuffers(4, objects[i].vbo);
glDeleteProgram(objects[i].program);
}
}
static void setUniformMat4(unsigned int program, const std::string &name, const glm::mat4 &mat) {
// This line can be ignore. But, if you have multiple shader program
// You must check if currect binding is the one you want
glUseProgram(program);
GLint loc = glGetUniformLocation(program, name.c_str());
if (loc == -1) return;
// mat4 of glm is column major, same as opengl
// we don't need to transpose it. so..GL_FALSE
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(mat));
}
static void render() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (int i = 0; i < objects.size(); i++) {
glUseProgram(objects[i].program);
glBindVertexArray(objects[i].vao);
glBindTexture(GL_TEXTURE_2D, objects[i].texture);
//you should send some data to shader here
glDrawElements(GL_TRIANGLES, indicesCount[i], GL_UNSIGNED_INT, nullptr);
}
glBindVertexArray(0);
}
int main(int argc, char *argv[]) {
GLFWwindow *window;
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
// OpenGL 3.3, Mac OS X is reported to have some problem. However I don't have Mac to test
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// For Mac OS X
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(800, 600, "Simple Example", NULL, NULL);
if (!window) {
glfwTerminate();
return EXIT_FAILURE;
}
glfwMakeContextCurrent(window);
// This line MUST put below glfwMakeContextCurrent
glewExperimental = GL_TRUE;
glewInit();
// Enable vsync
glfwSwapInterval(1);
// Setup input callback
glfwSetKeyCallback(window, key_callback);
// load shader program
program = setup_shader(readfile("shader/vs.txt").c_str(), readfile("shader/fs.txt").c_str());
program2 = setup_shader(readfile("shader/vs.txt").c_str(), readfile("shader/fs.txt").c_str());
int sun = add_obj(program, "render/sun.obj", "render/sun.bmp");
int earth = add_obj(program, "render/earth.obj", "render/earth.bmp");
glEnable(GL_DEPTH_TEST);
glCullFace(GL_BACK);
// Enable blend mode for billboard
//glEnable(GL_BLEND);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
setUniformMat4(program, "vp", glm::perspective(glm::radians(45.0f), 640.0f / 480, 1.0f, 100.f) *
glm::lookAt(glm::vec3(20.0f), glm::vec3(), glm::vec3(0, 1, 0)) * glm::mat4(1.0f));
setUniformMat4(program2, "vp", glm::mat4(1.0));
glm::mat4 tl = glm::translate(glm::mat4(), glm::vec3(15.0f, 0.0f, 0.0));
glm::mat4 rot;
glm::mat4 rev;
float last, start;
last = start = glfwGetTime();
int fps = 0;
objects[sun].model = glm::scale(glm::mat4(1.0f), glm::vec3(0.85f));
while (!glfwWindowShouldClose(window)) {//program will keep draw here until you close the window
float delta = glfwGetTime() - start;
render();
glfwSwapBuffers(window);
glfwPollEvents();
fps++;
if (glfwGetTime() - last > 1.0) {
std::cout << (double) fps / (glfwGetTime() - last) << std::endl;
fps = 0;
last = glfwGetTime();
}
}
releaseObjects();
glfwDestroyWindow(window);
glfwTerminate();
return EXIT_SUCCESS;
}