188 lines
6.4 KiB
C++
188 lines
6.4 KiB
C++
#include"misc/headers.hpp"
|
|
#include "shaderReader.hpp"
|
|
#define STB_IMAGE_IMPLEMENTATION
|
|
#include <stb/stb_image.h>
|
|
#include <glm/glm.hpp>
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
#include <iostream>
|
|
|
|
#define w 1280
|
|
#define h 720
|
|
|
|
void input(GLFWwindow *window);
|
|
float vertices[] = {
|
|
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f,0.0f,-1.0f,
|
|
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,0.0f,-1.0f,
|
|
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f,0.0f,-1.0f,
|
|
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f,0.0f,-1.0f,
|
|
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,0.0f,-1.0f,
|
|
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f,0.0f,-1.0f,
|
|
|
|
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f,0.0f,1.0f,
|
|
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,0.0f,1.0f,
|
|
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f,0.0f,1.0f,
|
|
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f,0.0f,1.0f,
|
|
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,0.0f,1.0f,
|
|
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f,0.0f,1.0f,
|
|
|
|
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f, -1.0f,0.0f,0.0f,
|
|
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, -1.0f,0.0f,0.0f,
|
|
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, -1.0f,0.0f,0.0f,
|
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|
|
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,1.0f,0.0f
|
|
};
|
|
|
|
glm::vec3 cubePositions[] = {
|
|
glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(2.0f, 5.0f, -15.0f),
|
|
glm::vec3(-1.5f, -2.2f, -2.5f), glm::vec3(-3.8f, -2.0f, -12.3f),
|
|
glm::vec3(2.4f, -0.4f, -3.5f), glm::vec3(-1.7f, 3.0f, -7.5f),
|
|
glm::vec3(1.3f, -2.0f, -2.5f), glm::vec3(1.5f, 2.0f, -2.5f),
|
|
glm::vec3(1.5f, 0.2f, -1.5f), glm::vec3(-1.3f, 1.0f, -1.5f)};
|
|
|
|
int main() {
|
|
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_X11);
|
|
glfwInit();
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
GLFWwindow *window = glfwCreateWindow(w, h, "title", nullptr, nullptr);
|
|
glfwMakeContextCurrent(window);
|
|
gladLoadGL();
|
|
|
|
|
|
Shader s("Shaders/OG_V.glsl", "Shaders/OG_F.glsl");
|
|
s.use();
|
|
|
|
|
|
uint32_t VAO, VBO, EBO, lightVAO;
|
|
|
|
glGenVertexArrays(1, &VAO);
|
|
glGenBuffers(1, &VBO);
|
|
glBindVertexArray(VAO);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
|
|
glEnableVertexAttribArray(0);
|
|
|
|
glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE, 8 * sizeof(float),(void*)(3 * sizeof(float)));
|
|
glEnableVertexAttribArray(1);
|
|
|
|
glVertexAttribPointer(2,3,GL_FLOAT,GL_FALSE, 8 * sizeof(float), (void*)(5 * sizeof(float)));
|
|
glEnableVertexAttribArray(2);
|
|
|
|
glGenVertexArrays(1, &lightVAO);
|
|
glBindVertexArray(lightVAO);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
|
|
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE, 8 * sizeof(float), (void*)0);
|
|
glEnableVertexAttribArray(0);
|
|
|
|
uint32_t tex1;
|
|
glGenTextures(1,&tex1);
|
|
glBindTexture(GL_TEXTURE_2D,tex1);
|
|
|
|
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, nrChannels;
|
|
stbi_set_flip_vertically_on_load(true);
|
|
unsigned char* data = stbi_load("test.png", &width,&height,&nrChannels,0);
|
|
if (data)
|
|
{
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
|
glGenerateMipmap(GL_TEXTURE_2D);
|
|
} else {
|
|
std::cout << "failed to load test.png" << std::endl;
|
|
}
|
|
|
|
stbi_image_free(data);
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
s.use();
|
|
s.setInt("texture0", 0);
|
|
|
|
while (!glfwWindowShouldClose(window)) {
|
|
glfwPollEvents();
|
|
input(window);
|
|
|
|
glClearColor(0, 0, 0, 1);
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
glBindTexture(GL_TEXTURE0, tex1);
|
|
|
|
s.use();
|
|
glm::mat4 view = glm::mat4(1.0f);
|
|
glm::mat4 projection = glm::mat4(1.0f);
|
|
view = glm::translate(view,glm::vec3(0,0,-3));
|
|
projection = glm::perspective(glm::radians(45.0f),(float)w/(float)h,0.1f,100.0f);
|
|
s.setMat4("view", view);
|
|
s.setMat4("projection", projection);
|
|
|
|
s.setVec3("lightPos", glm::vec3(cos(glfwGetTime()),0,sin(glfwGetTime())));
|
|
s.setVec3("lightColor", glm::vec3(1,1,1));
|
|
s.setVec3("objectColor",glm::vec3(0.2,0.2,0.2));
|
|
s.setVec3("viewPos",glm::vec3(0,0,-3));
|
|
|
|
|
|
glBindVertexArray(VAO);
|
|
for (uint16_t i = 0; i < 20; i++)
|
|
{
|
|
glm::mat4 model = glm::mat4(1.0f);
|
|
model = glm::translate(model, cubePositions[i]);
|
|
float angle = 20.0f * i;
|
|
model = glm::rotate(model,glm::radians(angle),glm::vec3(1,0.3,0.5));
|
|
s.setMat4("model",model);
|
|
glDrawArrays(GL_TRIANGLES,0,36);
|
|
}
|
|
|
|
s.use();
|
|
|
|
glBindVertexArray(lightVAO);
|
|
glm::mat4 model2 = glm::mat4(1.0f);
|
|
model2 = glm::translate(model2, glm::vec3(cos(glfwGetTime()), 0, sin(glfwGetTime())));
|
|
s.setMat4("model",model2);
|
|
glDrawArrays(GL_TRIANGLES,0,36);
|
|
|
|
glfwSwapBuffers(window);
|
|
}
|
|
glfwDestroyWindow(window);
|
|
glfwTerminate();
|
|
return 0;
|
|
}
|
|
|
|
void input(GLFWwindow *window) {
|
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
|
glfwSetWindowShouldClose(window, true);
|
|
}
|
|
}
|