Compare commits
11 Commits
b734e8270d
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 692fbf8196 | |||
| a75360a8ef | |||
| b693f6809b | |||
| 6554e0dc74 | |||
| 0f9f72af4e | |||
| 6d6bac8ca1 | |||
| 78f53fdd54 | |||
| 78ba333164 | |||
| 6e69a543f0 | |||
| 8350b85aa1 | |||
| cfc47df080 |
@@ -8,4 +8,6 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- uses: cachix/install-nix-action@v30
|
- uses: cachix/install-nix-action@v30
|
||||||
|
with:
|
||||||
|
nix_path: nixpkgs=channel:nixos-24.11
|
||||||
- run: nix-shell shell.nix --run make
|
- run: nix-shell shell.nix --run make
|
||||||
|
|||||||
@@ -7,9 +7,14 @@ public:
|
|||||||
Camera(glm::vec3 pos, float yaw, float pitch, float fov, float aspect);
|
Camera(glm::vec3 pos, float yaw, float pitch, float fov, float aspect);
|
||||||
~Camera();
|
~Camera();
|
||||||
void setPos(glm::vec3 pos);
|
void setPos(glm::vec3 pos);
|
||||||
|
void addPos(glm::vec3 pos);
|
||||||
|
void addFPos(glm::vec3 pos);
|
||||||
void setRotate(float yaw, float pitch);
|
void setRotate(float yaw, float pitch);
|
||||||
|
void addRotate(float yaw, float pitch);
|
||||||
|
glm::vec3 getPos();
|
||||||
glm::mat4 getView();
|
glm::mat4 getView();
|
||||||
glm::mat4 getProjection();
|
glm::mat4 getProjection();
|
||||||
|
float aspect;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void update();
|
void update();
|
||||||
@@ -20,6 +25,5 @@ private:
|
|||||||
float yaw;
|
float yaw;
|
||||||
float pitch;
|
float pitch;
|
||||||
float fov;
|
float fov;
|
||||||
float aspect;
|
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
31
headers/mesh.hpp
Normal file
31
headers/mesh.hpp
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#ifndef MESH_H
|
||||||
|
#define MESH_H
|
||||||
|
#include "camera.hpp"
|
||||||
|
#include "shader.hpp"
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <glm/gtc/quaternion.hpp>
|
||||||
|
|
||||||
|
class Mesh {
|
||||||
|
public:
|
||||||
|
Mesh(Shader *s, unsigned int VAO, int count);
|
||||||
|
Mesh(Shader *s, std::string path);
|
||||||
|
~Mesh();
|
||||||
|
void draw(Camera *c);
|
||||||
|
void setPosition(glm::vec3 p);
|
||||||
|
void setScale(glm::vec3 s);
|
||||||
|
void setRotation(glm::quat r);
|
||||||
|
void setOwnedBuffers(bool o);
|
||||||
|
void setOwnedShader(bool o);
|
||||||
|
Shader *getShader();
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool buffersOwned;
|
||||||
|
bool shaderOwned;
|
||||||
|
int count;
|
||||||
|
unsigned int VAO;
|
||||||
|
Shader *shader;
|
||||||
|
glm::vec3 position; /*0,0,0*/
|
||||||
|
glm::vec3 scale; /*1,1,1*/
|
||||||
|
glm::quat rotation; /*1,0,0,0*/
|
||||||
|
};
|
||||||
|
#endif
|
||||||
4
headers/obj.hpp
Normal file
4
headers/obj.hpp
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
unsigned int loadOBJ(const char *path, std::vector<float> *out_vertices);
|
||||||
@@ -11,6 +11,7 @@ public:
|
|||||||
void use();
|
void use();
|
||||||
void setInt(const std::string &name, int value);
|
void setInt(const std::string &name, int value);
|
||||||
void setFloat(const std::string &name, float value);
|
void setFloat(const std::string &name, float value);
|
||||||
|
void setVec3(const std::string &name, glm::vec3 value);
|
||||||
void setMat4(const std::string &name, glm::mat4 value);
|
void setMat4(const std::string &name, glm::mat4 value);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -75,6 +75,50 @@ float cube_vertices[] = {
|
|||||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f /**/
|
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f /**/
|
||||||
};
|
};
|
||||||
|
|
||||||
|
float uvcube[] = {
|
||||||
|
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||||
|
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||||
|
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||||
|
0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||||
|
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||||
|
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
|
||||||
|
|
||||||
|
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||||
|
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||||
|
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||||
|
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||||
|
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||||
|
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
|
||||||
|
|
||||||
|
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
|
||||||
|
-0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
|
||||||
|
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
|
||||||
|
-0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
|
||||||
|
-0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
|
||||||
|
-0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
|
||||||
|
|
||||||
|
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
|
||||||
|
0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
|
||||||
|
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
|
||||||
|
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
|
||||||
|
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
|
||||||
|
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
|
||||||
|
|
||||||
|
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
|
||||||
|
0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
|
||||||
|
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
|
||||||
|
0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
|
||||||
|
-0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
|
||||||
|
-0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
|
||||||
|
|
||||||
|
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
|
||||||
|
0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
|
||||||
|
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
|
||||||
|
0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
|
||||||
|
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
|
||||||
|
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f
|
||||||
|
};
|
||||||
|
|
||||||
const char *basicVertexShaderSource =
|
const char *basicVertexShaderSource =
|
||||||
"#version 330 core\n"
|
"#version 330 core\n"
|
||||||
"layout (location = 0) in vec3 aPos;\n"
|
"layout (location = 0) in vec3 aPos;\n"
|
||||||
|
|||||||
15
shaders/MVPTexNoRGBVert.glsl
Normal file
15
shaders/MVPTexNoRGBVert.glsl
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec3 aPos;
|
||||||
|
layout (location = 1) in vec2 aTexCoord;
|
||||||
|
|
||||||
|
out vec2 TexCoord;
|
||||||
|
|
||||||
|
uniform mat4 model;
|
||||||
|
uniform mat4 view;
|
||||||
|
uniform mat4 projection;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = projection * view * model * vec4(aPos, 1.0);
|
||||||
|
TexCoord = aTexCoord;
|
||||||
|
}
|
||||||
12
shaders/TexNoRGBFrag.glsl
Normal file
12
shaders/TexNoRGBFrag.glsl
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
in vec3 ourColor;
|
||||||
|
in vec2 TexCoord;
|
||||||
|
|
||||||
|
uniform sampler2D tex0;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
FragColor = texture(tex0, TexCoord);
|
||||||
|
}
|
||||||
50
shaders/lighting/Frag.glsl
Normal file
50
shaders/lighting/Frag.glsl
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
#version 330 core
|
||||||
|
struct Material {
|
||||||
|
vec3 ambient;
|
||||||
|
vec3 diffuse;
|
||||||
|
vec3 specular;
|
||||||
|
float shininess;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct Light {
|
||||||
|
vec3 position;
|
||||||
|
vec3 ambient;
|
||||||
|
vec3 diffuse;
|
||||||
|
vec3 specular;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
in vec3 FragPos;
|
||||||
|
in vec3 Normal;
|
||||||
|
|
||||||
|
uniform vec3 ourColor;
|
||||||
|
uniform vec3 viewPos;
|
||||||
|
uniform Material material;
|
||||||
|
uniform Light light;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
//misc
|
||||||
|
vec3 norm = normalize(Normal);
|
||||||
|
vec3 lightDir = normalize(light.position - FragPos);
|
||||||
|
|
||||||
|
// ambient
|
||||||
|
vec3 ambient = light.ambient * material.ambient;
|
||||||
|
|
||||||
|
// diffuse
|
||||||
|
float diff = max(dot(norm, lightDir), 0.0);
|
||||||
|
vec3 diffuse = light.diffuse * (diff * material.diffuse);
|
||||||
|
|
||||||
|
// specular
|
||||||
|
vec3 viewDir = normalize(viewPos - FragPos);
|
||||||
|
vec3 reflectDir = reflect(-lightDir, norm);
|
||||||
|
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
|
||||||
|
vec3 specular = light.specular * (spec * material.specular);
|
||||||
|
|
||||||
|
//out
|
||||||
|
vec3 result = ambient + diffuse + specular;
|
||||||
|
FragColor = vec4(result, 1.0);
|
||||||
|
}
|
||||||
9
shaders/lighting/LightFrag.glsl
Normal file
9
shaders/lighting/LightFrag.glsl
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
uniform vec3 ourColor;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
FragColor = vec4(ourColor, 1.0);
|
||||||
|
}
|
||||||
18
shaders/lighting/Vert.glsl
Normal file
18
shaders/lighting/Vert.glsl
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec3 aPos;
|
||||||
|
layout (location = 1) in vec3 aNormal;
|
||||||
|
|
||||||
|
out vec3 FragPos;
|
||||||
|
out vec3 Normal;
|
||||||
|
|
||||||
|
uniform mat4 model;
|
||||||
|
uniform mat4 view;
|
||||||
|
uniform mat4 projection;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
gl_Position = projection * view * model * vec4(aPos, 1.0);
|
||||||
|
FragPos = vec3(model * vec4(aPos, 1.0));
|
||||||
|
Normal = aNormal;
|
||||||
|
//Normal = mat3(transpose(inverse(model))) * aNormal;//if non unifrom scale
|
||||||
|
}
|
||||||
@@ -3,8 +3,8 @@
|
|||||||
#include <glm/ext/matrix_transform.hpp>
|
#include <glm/ext/matrix_transform.hpp>
|
||||||
|
|
||||||
Camera::Camera(glm::vec3 pos, float yaw, float pitch, float fov, float aspect)
|
Camera::Camera(glm::vec3 pos, float yaw, float pitch, float fov, float aspect)
|
||||||
: up(glm::vec3(0, 1, 0)), pos(pos), yaw(yaw), pitch(pitch), fov(fov),
|
: aspect(aspect), up(glm::vec3(0, 1, 0)), pos(pos), yaw(yaw), pitch(pitch),
|
||||||
aspect(aspect) {
|
fov(fov) {
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -16,8 +16,28 @@ void Camera::setRotate(float yaw, float pitch) {
|
|||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Camera::addRotate(float yaw, float pitch) {
|
||||||
|
this->yaw += yaw;
|
||||||
|
this->pitch += pitch;
|
||||||
|
if (this->pitch > 89.0f)
|
||||||
|
this->pitch = 89.0f;
|
||||||
|
if (this->pitch < -89.0f)
|
||||||
|
this->pitch = -89.0f;
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
void Camera::setPos(glm::vec3 pos) { this->pos = pos; }
|
void Camera::setPos(glm::vec3 pos) { this->pos = pos; }
|
||||||
|
|
||||||
|
void Camera::addPos(glm::vec3 pos) { this->pos += pos; }
|
||||||
|
|
||||||
|
void Camera::addFPos(glm::vec3 pos) {
|
||||||
|
glm::vec3 f = glm::normalize(glm::vec3(front.x, 0, front.z));
|
||||||
|
glm::vec3 r = glm::normalize(glm::cross(f, up));
|
||||||
|
this->pos += f * pos.x;
|
||||||
|
this->pos += up * pos.y;
|
||||||
|
this->pos += r * pos.z;
|
||||||
|
}
|
||||||
|
|
||||||
void Camera::update() {
|
void Camera::update() {
|
||||||
glm::vec3 direction;
|
glm::vec3 direction;
|
||||||
direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
|
direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
|
||||||
@@ -32,6 +52,8 @@ glm::mat4 Camera::getView() {
|
|||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
glm::vec3 Camera::getPos() { return pos; }
|
||||||
|
|
||||||
glm::mat4 Camera::getProjection() {
|
glm::mat4 Camera::getProjection() {
|
||||||
return glm::perspective(glm::radians(fov), aspect, 0.1f, 100.0f);
|
return glm::perspective(glm::radians(fov), aspect, 0.1f, 100.0f);
|
||||||
}
|
}
|
||||||
|
|||||||
154
src/main.cpp
154
src/main.cpp
@@ -1,27 +1,21 @@
|
|||||||
#include <glad/glad.h> /*Must be before GLFW*/
|
|
||||||
#include "camera.hpp"
|
#include "camera.hpp"
|
||||||
|
#include "glad/glad.h"
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
#include <mesh.hpp>
|
||||||
#include <glm/gtc/type_ptr.hpp>
|
|
||||||
#include <shader.hpp>
|
#include <shader.hpp>
|
||||||
#include <shapes.h>
|
|
||||||
#include <texture.hpp>
|
#include <texture.hpp>
|
||||||
|
|
||||||
#define UNUSED(x) (void)(x)
|
#define UNUSED(x) (void)(x)
|
||||||
|
|
||||||
void processInput(GLFWwindow *window);
|
glm::vec3 processInput(GLFWwindow *window);
|
||||||
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
|
||||||
|
void mouse_callback(GLFWwindow *window, double xpos, double ypos);
|
||||||
|
|
||||||
glm::vec3 cubePositions[] = {
|
float deltaTime = 0;
|
||||||
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(void) {
|
GLFWwindow *init() {
|
||||||
glfwInit();
|
glfwInit();
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
@@ -34,79 +28,133 @@ int main(void) {
|
|||||||
int is_ok = 0;
|
int is_ok = 0;
|
||||||
is_ok = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
|
is_ok = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
|
||||||
assert(is_ok && "Failed to init glad");
|
assert(is_ok && "Failed to init glad");
|
||||||
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||||
|
|
||||||
glViewport(0, 0, 800, 600);
|
glViewport(0, 0, 800, 600);
|
||||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||||
|
glfwSetCursorPosCallback(window, mouse_callback);
|
||||||
|
return window;
|
||||||
|
}
|
||||||
|
|
||||||
unsigned int VBO, VAO;
|
void showFPS(GLFWwindow *window, double delta) {
|
||||||
glGenVertexArrays(1, &VAO);
|
char title[22];
|
||||||
glBindVertexArray(VAO);
|
title[21] = '\0';
|
||||||
|
snprintf(title, 20, "OpenGl FPS: [%3.2f]", (1 / delta));
|
||||||
|
glfwSetWindowTitle(window, title);
|
||||||
|
}
|
||||||
|
|
||||||
glGenBuffers(1, &VBO);
|
int main(void) {
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
glm::vec3 lightPos = glm::vec3(1, 2.0f, 1);
|
||||||
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices), cube_vertices,
|
glm::vec3 lightColor = glm::vec3(1);
|
||||||
GL_STATIC_DRAW);
|
|
||||||
|
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)0);
|
GLFWwindow *window = init();
|
||||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)0);
|
Camera *cam = new Camera(glm::vec3(0), 0, 0, 45, 800.0 / 600);
|
||||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float),
|
glfwSetWindowUserPointer(window, cam);
|
||||||
(void *)(3 * sizeof(float)));
|
|
||||||
|
|
||||||
glEnableVertexAttribArray(0); /*pos*/
|
Shader *cubeShader = new Shader("./shaders/lighting/Vert.glsl",
|
||||||
glEnableVertexAttribArray(1); /*rgb*/
|
"./shaders/lighting/Frag.glsl");
|
||||||
glEnableVertexAttribArray(2); /*tex_cord*/
|
Shader *lightShader = new Shader("./shaders/lighting/Vert.glsl",
|
||||||
|
"./shaders/lighting/LightFrag.glsl");
|
||||||
|
|
||||||
Shader *shaderProgram =
|
Mesh *cube = new Mesh(cubeShader, "./cube.obj");
|
||||||
new Shader("./shaders/MVPTexVert.glsl", "./shaders/TexFrag.glsl");
|
Mesh *light = new Mesh(lightShader, "./cube.obj");
|
||||||
|
cube->setOwnedShader(true);
|
||||||
|
light->setOwnedShader(true);
|
||||||
|
|
||||||
Texture *texture = new Texture(GL_TEXTURE_2D, "./texture/container.jpg");
|
light->setPosition(lightPos);
|
||||||
|
light->setScale(glm::vec3(.3));
|
||||||
Camera *cam = new Camera(glm::vec3(0.0, 0.0, 5.0),-90.0f, 0.0f,45.0,800.0/600);
|
|
||||||
|
|
||||||
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
float lastFrame = 0;
|
||||||
while (!glfwWindowShouldClose(window)) {
|
while (!glfwWindowShouldClose(window)) {
|
||||||
|
float currentFrame = (float)glfwGetTime();
|
||||||
|
deltaTime = currentFrame - lastFrame;
|
||||||
|
lastFrame = currentFrame;
|
||||||
|
showFPS(window, deltaTime);
|
||||||
|
|
||||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
shaderProgram->use();
|
Shader *s = cube->getShader();
|
||||||
shaderProgram->setInt("Tex0", 0);
|
s->use();
|
||||||
shaderProgram->setMat4("view", cam->getView());
|
s->setVec3("lightColor", lightColor);
|
||||||
shaderProgram->setMat4("projection", cam->getProjection());
|
s->setVec3("ourColor", glm::vec3(0.24725, 0.1995, 0.0745));
|
||||||
texture->bind(GL_TEXTURE0);
|
s->setVec3("lightPos", lightPos);
|
||||||
|
s->setVec3("viewPos", cam->getPos());
|
||||||
|
|
||||||
glBindVertexArray(VAO);
|
s->setVec3("light.ambient", glm::vec3(0.2));
|
||||||
glEnable(GL_DEPTH_TEST);
|
s->setVec3("light.diffuse", glm::vec3(0.5));
|
||||||
|
s->setVec3("light.specular", glm::vec3(1));
|
||||||
|
s->setVec3("light.position", lightPos);
|
||||||
|
|
||||||
for (unsigned int i = 0; i < 10; i++) {
|
s->setVec3("material.ambient", glm::vec3(1.0, 0.5, 0.31));
|
||||||
glm::mat4 model = glm::mat4(1.0f);
|
s->setVec3("material.diffuse", glm::vec3(1.0, 0.5, 0.31));
|
||||||
model = glm::translate(model, cubePositions[i]);
|
s->setVec3("material.specular", glm::vec3(0.5));
|
||||||
|
s->setFloat("material.shininess", 32);
|
||||||
|
cube->draw(cam);
|
||||||
|
|
||||||
float angle = 20.0f * i;
|
s = light->getShader();
|
||||||
model =
|
s->use();
|
||||||
glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
|
s->setVec3("ourColor", lightColor);
|
||||||
model = glm::rotate(model, (float)glfwGetTime() * glm::radians(55.0f),
|
light->draw(cam);
|
||||||
glm::vec3(1.0f, 0.5f, 0.0f));
|
|
||||||
|
|
||||||
shaderProgram->setMat4("model", model);
|
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
|
||||||
}
|
|
||||||
|
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
processInput(window);
|
glm::vec3 mov = processInput(window);
|
||||||
|
mov *= deltaTime;
|
||||||
|
cam->addFPos(mov);
|
||||||
}
|
}
|
||||||
|
|
||||||
delete (shaderProgram);
|
delete cam;
|
||||||
|
delete cube;
|
||||||
|
delete light;
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void processInput(GLFWwindow *window) {
|
glm::vec3 processInput(GLFWwindow *window) {
|
||||||
|
glm::vec3 mov = glm::vec3(0);
|
||||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||||
glfwSetWindowShouldClose(window, true);
|
glfwSetWindowShouldClose(window, true);
|
||||||
|
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||||
|
mov += glm::vec3(1, 0, 0);
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||||
|
mov -= glm::vec3(1, 0, 0);
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||||
|
mov += glm::vec3(0, 0, 1);
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||||
|
mov -= glm::vec3(0, 0, 1);
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
|
||||||
|
mov += glm::vec3(0, 1, 0);
|
||||||
|
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
|
||||||
|
mov -= glm::vec3(0, 1, 0);
|
||||||
|
|
||||||
|
mov *= 5;
|
||||||
|
return mov;
|
||||||
}
|
}
|
||||||
|
|
||||||
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
|
||||||
UNUSED(window);
|
UNUSED(window);
|
||||||
glViewport(0, 0, width, height);
|
glViewport(0, 0, width, height);
|
||||||
|
Camera *cam = (Camera *)glfwGetWindowUserPointer(window);
|
||||||
|
if (cam)
|
||||||
|
cam->aspect = (float)width / height;
|
||||||
|
}
|
||||||
|
|
||||||
|
void mouse_callback(GLFWwindow *window, double xpos, double ypos) {
|
||||||
|
static float xprev = 0.0, yprev = 0.0;
|
||||||
|
|
||||||
|
float xchange = xpos - xprev;
|
||||||
|
float ychange = yprev - ypos;
|
||||||
|
xprev = xpos;
|
||||||
|
yprev = ypos;
|
||||||
|
|
||||||
|
float sensitivity = 0.1f;
|
||||||
|
xchange *= sensitivity;
|
||||||
|
ychange *= sensitivity;
|
||||||
|
|
||||||
|
Camera *cam = (Camera *)glfwGetWindowUserPointer(window);
|
||||||
|
if (cam)
|
||||||
|
cam->addRotate(xchange, ychange);
|
||||||
}
|
}
|
||||||
|
|||||||
78
src/mesh.cpp
Normal file
78
src/mesh.cpp
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
#include "camera.hpp"
|
||||||
|
#include "obj.hpp"
|
||||||
|
#include "shader.hpp"
|
||||||
|
#include <mesh.hpp>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
unsigned int meshInit(std::string path, std::vector<float> &data);
|
||||||
|
|
||||||
|
Mesh::Mesh(Shader *s, unsigned int VAO, int count)
|
||||||
|
: buffersOwned(false), shaderOwned(false), count(count), VAO(VAO),
|
||||||
|
shader(s), position(glm::vec3(0)), scale(glm::vec3(1)),
|
||||||
|
rotation(glm::quat(1.0f, 0.0f, 0.0f, 0.0f)) {}
|
||||||
|
|
||||||
|
Mesh::Mesh(Shader *s, std::string path)
|
||||||
|
: buffersOwned(true), shaderOwned(false), shader(s), position(glm::vec3(0)),
|
||||||
|
scale(glm::vec3(1)), rotation(glm::quat(1.0f, 0.0f, 0.0f, 0.0f)) {
|
||||||
|
std::vector<float> data;
|
||||||
|
VAO = meshInit(path, data);
|
||||||
|
count = data.size() / 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
Mesh::~Mesh() {
|
||||||
|
if (shaderOwned)
|
||||||
|
delete shader;
|
||||||
|
if (buffersOwned) {
|
||||||
|
glDeleteVertexArrays(1, &VAO);
|
||||||
|
// TODO vbo, ibo
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mesh::draw(Camera *c) {
|
||||||
|
glm::mat4 model = glm::mat4(1);
|
||||||
|
glBindVertexArray(VAO);
|
||||||
|
shader->use();
|
||||||
|
shader->setMat4("view", c->getView());
|
||||||
|
shader->setMat4("projection", c->getProjection());
|
||||||
|
model = glm::translate(model, position);
|
||||||
|
model = glm::scale(model, scale);
|
||||||
|
model = model * glm::mat4_cast(rotation);
|
||||||
|
shader->setMat4("model", model);
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mesh::setPosition(glm::vec3 p) { position = p; }
|
||||||
|
|
||||||
|
void Mesh::setScale(glm::vec3 s) { scale = s; }
|
||||||
|
|
||||||
|
void Mesh::setRotation(glm::quat r) { rotation = r; }
|
||||||
|
|
||||||
|
void Mesh::setOwnedBuffers(bool o) { buffersOwned = o; }
|
||||||
|
|
||||||
|
void Mesh::setOwnedShader(bool o) { shaderOwned = o; }
|
||||||
|
|
||||||
|
Shader *Mesh::getShader() { return shader; }
|
||||||
|
|
||||||
|
unsigned int meshInit(std::string path, std::vector<float> &data) {
|
||||||
|
unsigned int VBO, VAO;
|
||||||
|
glGenVertexArrays(1, &VAO);
|
||||||
|
glBindVertexArray(VAO);
|
||||||
|
|
||||||
|
glGenBuffers(1, &VBO);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
|
|
||||||
|
loadOBJ(path.c_str(), &data);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), data.data(),
|
||||||
|
GL_STATIC_DRAW);
|
||||||
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void *)0);
|
||||||
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
|
||||||
|
(void *)(3 * sizeof(float)));
|
||||||
|
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float),
|
||||||
|
(void *)(6 * sizeof(float)));
|
||||||
|
|
||||||
|
glEnableVertexAttribArray(0);
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
glEnableVertexAttribArray(2); // not in use
|
||||||
|
glBindVertexArray(0);
|
||||||
|
return VAO;
|
||||||
|
}
|
||||||
66
src/obj.cpp
Normal file
66
src/obj.cpp
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
#include "obj.hpp"
|
||||||
|
#include <cassert>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
/*this is ugly but works*/
|
||||||
|
unsigned int loadOBJ(const char *path, std::vector<float> *out_vertices) {
|
||||||
|
std::vector<glm::vec3> verts, norms;
|
||||||
|
std::vector<glm::vec2> uvs;
|
||||||
|
|
||||||
|
FILE *file = fopen(path, "r");
|
||||||
|
assert(file && "file not found");
|
||||||
|
|
||||||
|
int tmp = 0;
|
||||||
|
while (1) {
|
||||||
|
char lineHeader[128];
|
||||||
|
int res = fscanf(file, "%s", lineHeader);
|
||||||
|
if (res == EOF)
|
||||||
|
break;
|
||||||
|
if (strcmp(lineHeader, "v") == 0) {
|
||||||
|
glm::vec3 vert;
|
||||||
|
tmp = fscanf(file, "%f %f %f\n", &vert.x, &vert.y, &vert.z);
|
||||||
|
assert(tmp == 3 && "bad vert");
|
||||||
|
verts.push_back(vert);
|
||||||
|
} else if (strcmp(lineHeader, "vt") == 0) {
|
||||||
|
glm::vec2 uv;
|
||||||
|
tmp = fscanf(file, "%f %f\n", &uv.x, &uv.y);
|
||||||
|
assert(tmp == 2 && "bad uv");
|
||||||
|
uvs.push_back(uv);
|
||||||
|
} else if (strcmp(lineHeader, "vn") == 0) {
|
||||||
|
glm::vec3 norm;
|
||||||
|
tmp = fscanf(file, "%f %f %f\n", &norm.x, &norm.y, &norm.z);
|
||||||
|
assert(tmp == 3 && "bad norm");
|
||||||
|
norms.push_back(norm);
|
||||||
|
} else if (strcmp(lineHeader, "f") == 0) {
|
||||||
|
unsigned int vIndex[3], uvIndex[3], nIndex[3];
|
||||||
|
tmp = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vIndex[0],
|
||||||
|
&uvIndex[0], &nIndex[0], &vIndex[1], &uvIndex[1], &nIndex[1],
|
||||||
|
&vIndex[2], &uvIndex[2], &nIndex[2]);
|
||||||
|
assert(tmp == 9 && "bad face");
|
||||||
|
|
||||||
|
for (size_t i = 0; i < 3; ++i) {
|
||||||
|
size_t vi, ni, ui;
|
||||||
|
vi = (vIndex[i] - 1);
|
||||||
|
ni = (nIndex[i] - 1);
|
||||||
|
ui = (uvIndex[i] - 1);
|
||||||
|
assert(verts.size() > vi);
|
||||||
|
assert(norms.size() > ni);
|
||||||
|
assert(uvs.size() > ui);
|
||||||
|
auto vert = verts[vi];
|
||||||
|
auto norm = norms[ni];
|
||||||
|
auto uv = uvs[ui];
|
||||||
|
out_vertices->push_back(vert.x);
|
||||||
|
out_vertices->push_back(vert.y);
|
||||||
|
out_vertices->push_back(vert.z);
|
||||||
|
out_vertices->push_back(norm.x);
|
||||||
|
out_vertices->push_back(norm.y);
|
||||||
|
out_vertices->push_back(norm.z);
|
||||||
|
out_vertices->push_back(uv.x);
|
||||||
|
out_vertices->push_back(uv.y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return verts.size();
|
||||||
|
}
|
||||||
@@ -45,6 +45,11 @@ void Shader::setFloat(const std::string &name, float value) {
|
|||||||
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
glUniform1f(glGetUniformLocation(ID, name.c_str()), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Shader::setVec3(const std::string &name, glm::vec3 value) {
|
||||||
|
glUniform3fv(glGetUniformLocation(ID, name.c_str()), 1,
|
||||||
|
glm::value_ptr(value));
|
||||||
|
}
|
||||||
|
|
||||||
void Shader::setMat4(const std::string &name, glm::mat4 value) {
|
void Shader::setMat4(const std::string &name, glm::mat4 value) {
|
||||||
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE,
|
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE,
|
||||||
glm::value_ptr(value));
|
glm::value_ptr(value));
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ Texture::Texture(GLenum type, const char *path) : type(type) {
|
|||||||
stbi_image_free(data);
|
stbi_image_free(data);
|
||||||
};
|
};
|
||||||
|
|
||||||
Texture::~Texture() {}
|
Texture::~Texture() {glDeleteTextures(1, &ID);}
|
||||||
|
|
||||||
void Texture::bind(GLenum texture) {
|
void Texture::bind(GLenum texture) {
|
||||||
glActiveTexture(texture);
|
glActiveTexture(texture);
|
||||||
|
|||||||
Reference in New Issue
Block a user