lighting and imgui
This commit is contained in:
199
main.cpp
199
main.cpp
@@ -1,40 +1,49 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include <shader.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <imgui.h>
|
||||
#include <imgui_impl_glfw.h>
|
||||
#include <imgui_impl_opengl3.h>
|
||||
|
||||
#include <camera.h>
|
||||
#include <cubeFactory.h>
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
||||
void processInput(GLFWwindow* window);
|
||||
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
|
||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
|
||||
|
||||
int SCR_WIDTH = 800;
|
||||
int SCR_HEIGHT = 800;
|
||||
|
||||
float triangleVertices[] = {
|
||||
-0.5f,-0.5f,0.0f, 1.0f,0.0f,0.0f,
|
||||
0.5f,-0.5f,0.0f, 0.0f,1.0f,0.0f,
|
||||
0.0f, 0.5f,0.0f, 0.0f,0.0f,1.0f
|
||||
};
|
||||
Camera camera(glm::vec3(0.0f, 0.0f, 20.0f));
|
||||
float lastX = SCR_WIDTH / 2.0f;
|
||||
float lastY = SCR_HEIGHT / 2.0f;
|
||||
bool firstMouse = true;
|
||||
|
||||
float squareVertices[] = {
|
||||
-0.8f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||
-0.8f, 0.8f, 0.0f, 1.0f, 0.0f, 0.0f,
|
||||
-1.0f, 0.8f, 0.0f, 0.0f, 1.0f, 0.0f,
|
||||
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f
|
||||
};
|
||||
float deltaTime = 0.0f;
|
||||
float lastFrame = 0.0f;
|
||||
float elapsedTime = 0.0f;
|
||||
|
||||
unsigned int indices[] = {
|
||||
0,1,3,
|
||||
1,2,3
|
||||
};
|
||||
//glm::vec3 lightPos(1.2f, 1.0f, 2.0f);
|
||||
|
||||
int main() {
|
||||
int main()
|
||||
{
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGui::CreateContext();
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
|
||||
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
|
||||
|
||||
std::cout << "Starting GLFW context, OpenGL 3.3" << std::endl;
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
@@ -46,106 +55,128 @@ int main() {
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
|
||||
glfwMakeContextCurrent(window);
|
||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
glfwSetScrollCallback(window, scroll_callback);
|
||||
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
std::cout << "Failed to initialize GLAD" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
Shader ourShader("vertex.vert", "fragment.frag");
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);
|
||||
std::cout << "Loaded GLFW context, OpenGL 3.3" << std::endl;
|
||||
|
||||
unsigned int VAO_1, VAO_2, VBO_1, VBO_2, EBO;
|
||||
glGenVertexArrays(1, &VAO_1);
|
||||
glGenVertexArrays(1, &VAO_2);
|
||||
glGenBuffers(1, &VBO_1);
|
||||
glGenBuffers(1, &VBO_2);
|
||||
glGenBuffers(1, &EBO);
|
||||
CubeFactory cubeFactory;
|
||||
|
||||
glBindVertexArray(VAO_1);
|
||||
float offset = 2.0f;
|
||||
int xAmount = 10;
|
||||
int yAmount = 10;
|
||||
int zAmount = 10;
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO_1);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(triangleVertices), triangleVertices, GL_STATIC_DRAW);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
for (int x = 0; x < xAmount; x++)
|
||||
{
|
||||
for (int y = 0; y < yAmount; y++)
|
||||
{
|
||||
for (int z = 0; z < zAmount; z++)
|
||||
{
|
||||
glm::vec3 position = glm::vec3( (x * offset) - (xAmount * offset / 2.0f),
|
||||
(y * offset) - (yAmount * offset / 2.0f),
|
||||
(z * offset) - (zAmount * offset / 2.0f));
|
||||
glm::vec3 scale = glm::vec3(1.0f, 1.0f, 1.0f);
|
||||
glm::vec3 rotation = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
glm::vec3 color = glm::vec3(x/10.0f, y/10.0f, z/10.0f);
|
||||
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
cubeFactory.addCube(position, scale, rotation, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
glBindVertexArray(VAO_2);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO_2);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(squareVertices), squareVertices, GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
||||
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
//std::cout << "Amount of cubes: " << cubeFactory.getCubes().size() << std::endl;
|
||||
|
||||
float r = 0.0f, g = 0.0f, b = 0.0f;
|
||||
float r = 0.05f, g = 0.05f, b = 0.05f;
|
||||
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
//timing
|
||||
float currentFrame = glfwGetTime();
|
||||
deltaTime = currentFrame - lastFrame;
|
||||
lastFrame = currentFrame;
|
||||
elapsedTime += deltaTime;
|
||||
|
||||
//input
|
||||
processInput(window);
|
||||
|
||||
//rendering
|
||||
if (r < 1) {
|
||||
r = r + 0.01f;
|
||||
}
|
||||
else if (g < 1) {
|
||||
g = g + 0.01f;
|
||||
}
|
||||
else if (b < 1) {
|
||||
b = b + 0.01f;
|
||||
}
|
||||
else {
|
||||
r = 0;
|
||||
g = 0;
|
||||
b = 0;
|
||||
}
|
||||
|
||||
glClearColor(r, g, b, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
//transforms
|
||||
glm::mat4 defaultTransform = glm::mat4(1.0f);
|
||||
glm::mat4 rotatedTransform = glm::rotate(defaultTransform, (float)glfwGetTime(), glm::vec3(0.0f, 0.0f, 1.0f));
|
||||
// view/projection transformations
|
||||
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
||||
glm::mat4 view = camera.GetViewMatrix();
|
||||
|
||||
//draw
|
||||
ourShader.use();
|
||||
unsigned int transformLoc = glGetUniformLocation(ourShader.ID, "transform");
|
||||
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(rotatedTransform));
|
||||
glBindVertexArray(VAO_1);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
glm::vec3 lightPos = glm::vec3(100.2f, 100.0f, 200.0f);
|
||||
|
||||
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(defaultTransform));
|
||||
glBindVertexArray(VAO_2);
|
||||
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||
cubeFactory.render(view, projection, lightPos, deltaTime);
|
||||
|
||||
//check and call
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
//cubeFactory.clear();
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
void processInput(GLFWwindow* window) {
|
||||
void processInput(GLFWwindow* window)
|
||||
{
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
}
|
||||
}
|
||||
|
||||
const float cameraSpeed = 5.0f * deltaTime;
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||
camera.ProcessKeyboard(FORWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||
camera.ProcessKeyboard(BACKWARD, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||
camera.ProcessKeyboard(LEFT, deltaTime);
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||
camera.ProcessKeyboard(RIGHT, deltaTime);
|
||||
}
|
||||
|
||||
void mouse_callback(GLFWwindow* window, double xposIn, double yposIn)
|
||||
{
|
||||
//std::cout << "Mouse callback" << std::endl;
|
||||
float xpos = static_cast<float>(xposIn);
|
||||
float ypos = static_cast<float>(yposIn);
|
||||
|
||||
if (firstMouse)
|
||||
{
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
firstMouse = false;
|
||||
}
|
||||
|
||||
float xoffset = xpos - lastX;
|
||||
float yoffset = lastY - ypos;
|
||||
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
|
||||
camera.ProcessMouseMovement(xoffset, yoffset);
|
||||
}
|
||||
|
||||
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
|
||||
{
|
||||
camera.ProcessMouseScroll(static_cast<float>(yoffset));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user