/
main.cpp
395 lines (338 loc) · 13 KB
/
main.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include <GL/glew.h> //must be before glfw, because most header files we need are in glew
#include <GLFW/glfw3.h>
#include <iostream>
#include <cstdlib>
#include <string>
#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"
#define GLM_FORCE_RADIANS
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, program1, program2, program3;
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); //compile vertex shader
int status, maxLength;
char *infoLog=nullptr;
glGetShaderiv(vs, GL_COMPILE_STATUS, &status); //get compile status
if(status==GL_FALSE) //if compile error
{
glGetShaderiv(vs, GL_INFO_LOG_LENGTH, &maxLength); //get error message length
/* The maxLength includes the NULL character */
infoLog = new char[maxLength];
glGetShaderInfoLog(vs, maxLength, &maxLength, infoLog); //get error message
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;
}
// for fragment shader --> same as vertex shader
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);
//glActiveTexture(GL_TEXTURE0); //Activate texture unit before binding texture, used when having multiple texture
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(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++) {
//VAO VBO are binded in add_Object function
glUseProgram(objects[i].program);
glBindVertexArray(objects[i].vao);
glBindTexture(GL_TEXTURE_2D, objects[i].texture);
//you should send some data to shader here
GLint modelLoc = glGetUniformLocation(objects[i].program, "model");
glm::mat4 mPosition;
switch (i) {
case 0: //top left
mPosition = glm::translate(mPosition, glm::vec3(0, 10, 10));
mPosition = glm::rotate(mPosition, (float)glfwGetTime()*1.0f, glm::vec3(0.0f, 1.0f, 0.0f));
break;
case 1: //top right
mPosition = glm::translate(mPosition, glm::vec3(0, 10, -10));
mPosition = glm::rotate(mPosition, (float)glfwGetTime()*1.0f, glm::vec3(0.0f, 1.0f, 0.0f));
break;
case 2: //bottom left
mPosition = glm::translate(mPosition, glm::vec3(0,-10, 10));
mPosition = glm::rotate(mPosition, (float)glfwGetTime()*1.0f, glm::vec3(0.0f, 1.0f, 0.0f));
break;
case 3://bottom right
mPosition = glm::translate(mPosition, glm::vec3(0, -10, -10));
mPosition = glm::rotate(mPosition, (float)glfwGetTime()*1.0f, glm::vec3(0.0f, 1.0f, 0.0f));
break;
}
//mPosition = glm::translate(mPosition, modelPositions[i]);
/*
else { //for sun
mPosition = glm::rotate(mPosition, (float)glfwGetTime()*1.0f, glm::vec3(0.0f, 1.0f, 0.0f));
}*/
//mPosition = glm::rotate(mPosition, (float)glfwGetTime()*1.0f, glm::vec3(0.0f, 1.0f, 0.0f));
//mPosition = glm::scale(glm::mat4(1.0f), glm::vec3(1.5f));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(mPosition));
//std::cout << i<< std::endl;
glDrawElements(GL_TRIANGLES, indicesCount[i], GL_UNSIGNED_INT, nullptr);
}
glBindVertexArray(0);
}
static void set_vp(unsigned int prog) {
setUniformMat4(prog, "vp", glm::perspective(glm::radians(45.0f), 640.0f / 480, 1.0f, 100.f) *
glm::lookAt(glm::vec3(40.0f), glm::vec3(), glm::vec3(0, 1, 0)) * glm::mat4(1.0f));
}
static void set_viewPosition(unsigned int prog) {
glUseProgram(prog);
GLint loc = glGetUniformLocation(prog, "viewPos");
glUniform3f(loc, 40.0f, 0, 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); //set hint to glfwCreateWindow, (target, hintValue)
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
//hint--> window not resizable, explicit use core-profile, opengl version 3.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); //set current window as main window to focus
// This line MUST put below glfwMakeContextCurrent
glewExperimental = GL_TRUE; //tell glew to use more modern technique for managing OpenGL functionality
glewInit();
// Enable vsync
glfwSwapInterval(1);
glProvokingVertex(GL_FIRST_VERTEX_CONVENTION);
// Setup input callback
glfwSetKeyCallback(window, key_callback); //set key event handler
// load shader program
program = setup_shader(readfile("flat_vs.txt").c_str(), readfile("flat_fs.txt").c_str());
program1 = setup_shader(readfile("gourand_vs.txt").c_str(), readfile("gourand_fs.txt").c_str());
program2 = setup_shader(readfile("phong_vs.txt").c_str(), readfile("phong_fs.txt").c_str());
program3 = setup_shader(readfile("blin_vs.txt").c_str(), readfile("blin_fs.txt").c_str());
int sun = add_obj(program, "sun.obj","sun.bmp");
int sun1 = add_obj(program1, "sun.obj", "sun.bmp");
int sun2 = add_obj(program2, "sun.obj", "sun.bmp");
int sun3 = add_obj(program3, "sun.obj", "sun.bmp");
glEnable(GL_DEPTH_TEST);
// prevent faces rendering to the front while they're behind other faces.
glCullFace(GL_BACK);
// discard back-facing trangle
// Enable blend mode for billboard
//glEnable(GL_BLEND);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//-----------------------------------------------------------setting view point----------------------------------//
set_vp(program);
set_vp(program1);
set_vp(program2);
set_vp(program3);
//-----------------------------------------------------------end of setting--------------------------------------//
//------------------------------------------------------setting viewing position---------------------------------//
set_viewPosition(program);
set_viewPosition(program1);
set_viewPosition(program2);
set_viewPosition(program3);
//------------------------------------------------------end of setting-------------------------------------------//
float last, start;
last = start = glfwGetTime();
int fps=0;
while (!glfwWindowShouldClose(window))
{//program will keep draw here until you close the window
float delta = glfwGetTime() - start;
render();
glfwSwapBuffers(window); //swap the color buffer and show it as output to the screen.
glfwPollEvents(); //check if there is any event being triggered
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;
}