Guest User

Untitled

a guest
Oct 16th, 2023
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. #include "glm/ext/matrix_clip_space.hpp"
  2. #include "glm/ext/matrix_float4x4.hpp"
  3. #include "glm/ext/matrix_transform.hpp"
  4. #define STB_IMAGE_IMPLEMENTATION
  5.  
  6. #include "stb_image.h"
  7. #include "include/glad/glad.h"
  8. #include "canned_glfw_window.hpp"
  9. #include "glm/glm.hpp"
  10. #include "glm/gtc/matrix_transform.hpp"
  11. #include "glm/gtc/type_ptr.hpp"
  12. #include <GLFW/glfw3.h>
  13. #include <algorithm>
  14. #include <iostream>
  15. #include <memory>
  16. #include <vector>
  17.  
  18. void log_callback(GLenum source, GLenum type, GLuint id, GLenum severity,
  19. GLsizei length, const GLchar *msg, const void *userparam) {
  20. std::cout << std::string_view(msg) << std::endl;
  21. }
  22.  
  23.  
  24. int main() {
  25. GLFWWindow main_window(800, 600, "This might be a pretty solid screensaver", NULL, NULL, 3, 3, GLFW_OPENGL_CORE_PROFILE);
  26. glEnable(GL_DEBUG_OUTPUT);
  27. glEnable(GL_DEPTH_TEST);
  28. glDebugMessageCallback(log_callback, nullptr);
  29. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  30.  
  31. float vertices[] = {
  32. -0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
  33. 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
  34. 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
  35. 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
  36. -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
  37. -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
  38.  
  39. -0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
  40. 0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
  41. 0.5f, 0.5f, 0.5f, 0.0f, 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, 0.0f,
  71. -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
  72. -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f
  73. };
  74.  
  75. std::vector<glm::vec3> positions = {
  76. glm::vec3( 0.0f, 0.0f, 0.0f),
  77. glm::vec3( 2.0f, 5.0f, -15.0f),
  78. glm::vec3(-1.5f, -2.2f, -2.5f),
  79. glm::vec3(-3.8f, -2.0f, -12.3f),
  80. glm::vec3( 2.4f, -0.4f, -3.5f),
  81. glm::vec3(-1.7f, 3.0f, -7.5f),
  82. glm::vec3( 1.3f, -2.0f, -2.5f),
  83. glm::vec3( 1.5f, 2.0f, -2.5f),
  84. glm::vec3( 1.5f, 0.2f, -1.5f),
  85. glm::vec3(-1.3f, 1.0f, -1.5f)
  86. };
  87.  
  88. std::unique_ptr<GLShader> vertex_shader(new GLShader("./obamium_shaders/vertex_shader_with_perspective.glsl", GL_VERTEX_SHADER));
  89. std::unique_ptr<GLShader> fragment_shader(new GLShader("./obamium_shaders/fragment_shader_with_perspective.glsl", GL_FRAGMENT_SHADER));
  90. std::cout << "vertex_shader: " << vertex_shader->get_compile_status() << std::endl;
  91. std::cout << "fragment_shader: " << fragment_shader->get_compile_status() << std::endl;
  92.  
  93. GLShaderProgram shader_program({&vertex_shader, &fragment_shader});
  94.  
  95. VAObject vao(1);
  96. vao.bind_vao();
  97.  
  98. BufferObject vertex_buffer(1, GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  99.  
  100. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), 0);
  101. glEnableVertexAttribArray(0);
  102.  
  103. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), reinterpret_cast<void*>(3 * sizeof(float)));
  104. glEnableVertexAttribArray(1);
  105.  
  106. shader_program.use_shader_program();
  107.  
  108. while (!main_window.should_close()) {
  109. main_window.set_color(0.0f, 0.0f, 0.0f, 0.0f);
  110. main_window.clear_buffer(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  111.  
  112. glm::mat4 view = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -3.0f));
  113. glm::mat4 projection = glm::perspective(glm::radians(45.0f), 800.0f/600.0f, 0.1f, 100.0f);
  114.  
  115. shader_program.set_uniform_matrix<4>("view", 1, GL_FALSE, glm::value_ptr(view));
  116. shader_program.set_uniform_matrix<4>("projection", 1, GL_FALSE, glm::value_ptr(projection));
  117.  
  118. vao.bind_vao();
  119. for (glm::vec3 position : positions) {
  120. glm::mat4 model = glm::translate(glm::mat4(1.0f), position);
  121. model = glm::rotate(glm::mat4(1.0f), static_cast<float>(glfwGetTime()) * glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 1.0f));
  122.  
  123. shader_program.set_uniform_matrix<4>("model", 1, GL_FALSE, glm::value_ptr(model));
  124.  
  125. glDrawArrays(GL_TRIANGLES, 0, (sizeof(vertices) / sizeof(float)) / 6);
  126. }
  127.  
  128. main_window.main_loop_setup();
  129. }
  130.  
  131. return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment