1#define _USE_MATH_DEFINES
18const std::vector<vec4>
cube = {{0, 0, 0, 1}, {0, 0, 50, 1}, {0, 50, 50, 1}, {0, 50, 0, 1}, {50, 0, 0, 1}, {50, 0, 50, 1}, {50, 50, 50, 1}, {50, 50, 0, 1}};
19const std::vector<Vertex>
square = {
Vertex(
vec3(0, 0, 0),
vec3(0, 0, 1)),
Vertex(
vec3(5, 0, 0),
vec3(0, 0, 1)),
Vertex(
vec3(5, 5, 0),
vec3(0, 0, 1)),
Vertex(
vec3(0, 5, 0),
vec3(0, 0, 1))};
22const std::vector<uint32_t>
cube_indices = {0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7};
23const std::vector<uint32_t>
cube_indices_triangles = {0, 1, 2, 2, 3, 0, 4, 5, 6, 6, 7, 4};
35static void error_callback(
int error,
const char *description) {
36 fprintf(stderr,
"GLFW Error: %s\n", description);
39static bool captured =
false;
43void key_callback(GLFWwindow *window,
int key,
int scancode,
int action,
int mods) {
51 glfwSetWindowShouldClose(window,
true);
54 glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
58 glfwGetCursorPos(window, &mx, &my);
59 glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
78 if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
81 if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
82 cam1.
eye += (left * cameraSpeed);
84 if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
87 if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
88 cam1.
eye -= (left * cameraSpeed);
90 if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) {
93 if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) {
98 auto light_speed = 0.3f;
100 if (glfwGetKey(window, GLFW_KEY_KP_8) == GLFW_PRESS) {
104 if (glfwGetKey(window, GLFW_KEY_KP_2) == GLFW_PRESS) {
108 if (glfwGetKey(window, GLFW_KEY_KP_4) == GLFW_PRESS) {
112 if (glfwGetKey(window, GLFW_KEY_KP_6) == GLFW_PRESS) {
116 if (glfwGetKey(window, GLFW_KEY_KP_ADD) == GLFW_PRESS) {
120 if (glfwGetKey(window, GLFW_KEY_KP_SUBTRACT) == GLFW_PRESS) {
132 if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
135 if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
138 if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
141 if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
144 if (glfwGetKey(window, GLFW_KEY_KP_0) == GLFW_PRESS) {
153 if (glfwGetKey(window, GLFW_KEY_KP_1) == GLFW_PRESS) {
159static void cursor_position_callback(GLFWwindow *window,
double x,
double y) {
162 float xoffset = mx - x;
163 float yoffset = my - y;
177static void framebuffer_size_callback(GLFWwindow *window,
int width,
int height) {
188 GLcall(glActiveTexture(GL_TEXTURE0));
190 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
191 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
193 GLcall(glBindTexture(GL_TEXTURE_2D, 0));
197 if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
198 auto eng = (
engine *)glfwGetWindowUserPointer(window);
199 glfwGetCursorPos(window, &mx, &my);
200 const size_t index = (eng->fboCPU->y_size - my) * eng->fboCPU->x_size + mx;
201 color col = eng->fboCPU->colorlayer[index];
202 printf(
"clicked color: (%d,%d,%d)\n", col.r(), col.g(), col.b());
208 auto eng = (
engine *)glfwGetWindowUserPointer(window);
215 for (
unsigned int i = 0; i < size; i++) {
218 return center / size;
221int main(
int argc,
char **argv) {
223 auto threads = std::thread::hardware_concurrency();
229 glfwSetErrorCallback(error_callback);
231 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
232 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
233 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
234 window = glfwCreateWindow(640, 480,
"main", NULL, NULL);
241 glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
242 glfwSetCursorPosCallback(window, cursor_position_callback);
246 glfwMakeContextCurrent(window);
248 GLenum err = glewInit();
249 if (GLEW_OK != err) {
250 fprintf(stderr,
"GLEW Error: %s\n", glewGetErrorString(err));
259 auto path = searchRes();
286 for (
auto &mesh : city->meshes) {
287 if (mesh->name.find(
"lightCube") != std::string::npos) {
289 for (
auto &vertex : mesh->m_vertices) {
290 auto debugTranslate = trans::translate(-center);
291 vertex.position = debugTranslate * vertex.position;
301 auto lastframe = std::chrono::high_resolution_clock::now();
302 std::string fpsString =
"FPS: ";
303 const auto place = fpsString.find_first_of(
" ") + 1;
305 while (!glfwWindowShouldClose(window)) {
306 deltatime = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - lastframe).count();
307 lastframe = std::chrono::high_resolution_clock::now();
317 fpsString.replace(fpsString.begin() + place, fpsString.end(), std::to_string(1e6 /
deltatime));
318 glfwSetWindowTitle(window, fpsString.c_str());
334 for (
auto &mesh : city->meshes) {
336 if (mesh->doLightCalculations && mesh->draw) {
357 glfwSwapBuffers(window);
360 glfwDestroyWindow(window);
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset)
const std::vector< vec4 > axes_endpoints
void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
const std::vector< Vertex > square
int main(int argc, char **argv)
void mouse_button_callback(GLFWwindow *window, int button, int action, int mods)
const std::vector< vec4 > cube
void processHoldEvent(GLFWwindow *window)
vec3 getCenterOfMass(const Vertex *vertices, const unsigned int size)
const std::vector< uint32_t > square_indices
const std::vector< uint32_t > cube_indices
const std::vector< uint32_t > cube_indices_triangles
camera class implementing simple camera movement
void DelPitch(float angle_in_deg)
void newDelYaw(float angle_in_deg)
node * loadModel_obj(std::string const &path, const std::string &name, bool flipUV=false)
loads model with given path and register it with given name.
class to store information about material read from .mtl file along with obj
class that holds color with 8 bits per channel with 4 channels rgba.
backend for all rendering purposes
void makeRequiredTriangles(const std::vector< Vertex > &vertices, const std::vector< uint32_t > &indices, const mat4f &modelmat)
fill triangles array by doing required clipping and culling
void clear()
clear all drawing surface
Mesh * currentMesh
which material to use for further render calls
bool cullBackface
switch for backface culling
void draw()
send framebuffer from cpu to gpu as texture to be able to render using opengl
std::vector< pointLight > pointLights
float ambientLightIntensity
lights.
float nearPlane
nearplane of camera for clipping and culling
Framebuffer implementation for cpu.
anything that can be transformed( has modelmatrix )
static float dot(const vec3_T< float > &a, const vec3_T< float > &b)
static vec3_T< float > cross(const vec3_T< float > &a, const vec3_T< float > &b)
vec3_T< float > normalize()