-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.cpp
147 lines (119 loc) · 4.56 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
#include "../common/util.h"
#include "../common/shader.h"
#include "../common/camera.h"
#include "material.h"
#include "mesh.h"
#include "skybox.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#define NUM_ASTEROIDS 2000
#define SEED 1993
const char* VERTEX_SRC = "#version 330 core\n"
"layout(location=0) in vec3 position;" // Vertex position (x, y, z)
"layout(location=1) in vec2 texcoord;" // Texture coordinate (u, v)
"layout(location=2) in mat4 model_inst;" // Model matrix for this instance
"uniform mat4 view;"
"uniform mat4 projection;"
"out vec2 fTexcoord;" // Pass to fragment shader
"void main()"
"{"
" fTexcoord = texcoord;" // Pass texcoord to fragment shader
" gl_Position = projection * view * model_inst * vec4(position, 1.0);" // Place vertex at (x, y, z, 1) and then transform it according to the projection, view and model matrices
"}";
const char* FRAGMENT_SRC = "#version 330 core\n"
"in vec2 fTexcoord;" // From the vertex shader
"uniform sampler2D diffuse;" // The texture
"out vec4 outputColor;" // The color of the resulting fragment
"void main()"
"{"
" outputColor = texture(diffuse, fTexcoord);"
"}";
int main(void)
{
GLFWwindow* window;
window = init("Instancing", 640, 480);
if(!window)
{
return -1;
}
glEnable(GL_DEPTH_TEST);
// Hide the cursor (escape will exit the application)
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// Set the camera
Camera camera(CAMERA_PERSPECTIVE, 45.0f, 0.1f, 1000.0f, 640.0f, 480.0f);
setCamera(&camera);
// Load the material
Material mat;
if(!mat.load(VERTEX_SRC, FRAGMENT_SRC))
{
std::cerr << "Could not load shaders" << std::endl;
return -1;
}
mat.use();
int w, h;
GLuint texture = loadImage("asteroid.png", &w, &h, 0, false);
if(!texture)
{
std::cerr << "Could not load texture" << std::endl;
return -1;
}
mat.setDiffuseTexture(texture);
// Load the mesh
Mesh mesh;
if(!mesh.load("asteroid.obj"))
{
std::cerr << "Could not load mesh" << std::endl;
return -1;
}
// Set random positions for the asteroids
std::vector<glm::mat4> models;
models.resize(NUM_ASTEROIDS);
srand(SEED);
for(int i = 0; i < NUM_ASTEROIDS; ++i)
{
glm::mat4 model;
// Translate
model = glm::translate(model, glm::vec3(rand() % 100 - 50.0f, rand() % 100 - 50.0f, rand() % 100));
// Scale
float scale = (rand() % 200) / 100.0f + 0.1f;
model = glm::scale(model, glm::vec3(scale, scale, scale));
// Rotate
model = glm::rotate(model, glm::radians((float)(rand() % 100)), glm::vec3(1.0f, 1.0f, 1.0f));
models[i] = model;
}
mesh.setInstances(NUM_ASTEROIDS, models);
// Load a skybox
GLuint cubemap = loadCubeMap("cm_xp.png", "cm_xn.png", "cm_yp.png", "cm_yn.png", "cm_zp.png", "cm_zn.png");
if(!cubemap)
{
std::cerr << "Could not load cubemap" << std::endl;
return -1;
}
Skybox skybox(cubemap);
// Set the clear color to a light grey
glClearColor(0.75f, 0.75f, 0.75f, 1.0f);
while(!glfwWindowShouldClose(window))
{
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
break;
}
updateCamera(640, 480, window);
// Clear (note the addition of GL_DEPTH_BUFFER_BIT)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Upload the MVP matrices
mat.bind();
mat.setUniform("view", camera.getView());
mat.setUniform("projection", camera.getProjection());
mesh.render();
skybox.render(camera.getView(), camera.getProjection());
// Swap buffers to show current image on screen (for more information google 'backbuffer')
glfwSwapBuffers(window);
glfwPollEvents();
}
// Clean up
glDeleteTextures(1, &texture);
glfwTerminate();
return 0;
}