-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
150 lines (113 loc) · 4.16 KB
/
main.cc
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
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <stb_image.h>
#include <iostream>
#include <cmath>
#include <filesystem>
#include <program.hh>
#include <camera.hh>
#include <model.hh>
using namespace glm;
void renderQuad();
uint load_texture(const char *path)
{
uint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
// Texture parameter -> Bilinear and repeat
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
int width, height, nbChannels;
unsigned char *data = stbi_load(path, &width, &height, &nbChannels, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
return tex;
}
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
void framebuffer_size_callback(GLFWwindow *window, int width, int height)
{
glViewport(0, 0, width, height);
}
GLFWwindow *init_window(uint width, uint height)
{
GLFWwindow *window = glfwCreateWindow(width, height, "ZIZI", NULL, NULL);
if (!window)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
exit(-1);
}
return window;
}
int main(int argc, char *argv[])
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
uint width = 1920;
uint height = 1080;
auto window = init_window(width, height);
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD\n";
exit(-1);
}
// Tell OpenGL window size
glViewport(0, 0, width, height);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// Enable z-buffer computation
glEnable(GL_DEPTH_TEST);
// Init program and shaders
auto shaders = program();
shaders.add_shader("vertex.glsl", GL_VERTEX_SHADER);
shaders.add_shader("fragment.glsl", GL_FRAGMENT_SHADER);
shaders.link();
// Set shader
shaders.use();
shaders.addUniformVec3(vec3(0, 1.0, -1), "lightPos");
shaders.addUniformVec3(vec3(1.0, 1.0, 1.0), "lightIntensity");
auto teapot = Model("textures/wall/teapot.obj");
// Set camera
Camera cam = Camera(vec3(0, 0, -2.0), vec3(0, 0, 0), vec3(0, 1, 0));
mat4 view = cam.look_at();
shaders.addUniformVec3(cam.get_position(), "viewPos");
shaders.addUniformMat4(view, "view");
// Transform
mat4 projection = mat4(1.0);
projection = perspective(radians(60.0f), (float)width / (float)height, 0.1f, 100.0f);
shaders.addUniformMat4(projection, "projection");
mat4 model = mat4(1.0);
model = translate(model, vec3(0,-1,1.5));
model = rotate(model, glm::radians(-45.0f), glm::normalize(glm::vec3(0.0, 1.0, 0.0)));
// model = rotate(model, glm::radians((float)glfwGetTime() * -10.0f), glm::normalize(glm::vec3(1.0, 0.0, 1.0))); // rotate the quad to show normal mapping from multiple directions
// model = rotate(model, glm::radians(-90.0f), glm::normalize(glm::vec3(0.0, 0.0, 1.0)));
shaders.addUniformMat4(model, "model");
// Render loop
while(!glfwWindowShouldClose(window))
{
// Input
processInput(window);
// Render
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shaders.use();
model = rotate(model, glm::radians((float)glfwGetTime() / (float)glfwGetTime() * 0.5f), glm::normalize(glm::vec3(0.0, 1.0, 0.0))); // rotate the quad to show normal mapping from multiple directions
shaders.addUniformMat4(model, "model");
// Activate shader
teapot.draw(shaders);
// Check and call events
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}