Compare commits
4 Commits
c639afe08b
...
b734e8270d
| Author | SHA1 | Date | |
|---|---|---|---|
| b734e8270d | |||
| 452e9bdc92 | |||
| 16d9959ace | |||
| e0a695c7b1 |
11
.gitea/workflows/nix-check.yaml
Normal file
11
.gitea/workflows/nix-check.yaml
Normal file
@ -0,0 +1,11 @@
|
||||
name: Verify build
|
||||
run-name: ${{ gitea.actor }} is building
|
||||
on: [push]
|
||||
|
||||
jobs:
|
||||
verify_build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: cachix/install-nix-action@v30
|
||||
- run: nix-shell shell.nix --run make
|
||||
25
headers/camera.hpp
Normal file
25
headers/camera.hpp
Normal file
@ -0,0 +1,25 @@
|
||||
#ifndef CAMERA_H
|
||||
#define CAMERA_H
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class Camera {
|
||||
public:
|
||||
Camera(glm::vec3 pos, float yaw, float pitch, float fov, float aspect);
|
||||
~Camera();
|
||||
void setPos(glm::vec3 pos);
|
||||
void setRotate(float yaw, float pitch);
|
||||
glm::mat4 getView();
|
||||
glm::mat4 getProjection();
|
||||
|
||||
private:
|
||||
void update();
|
||||
glm::mat4 projection;
|
||||
glm::vec3 up;
|
||||
glm::vec3 front;
|
||||
glm::vec3 pos;
|
||||
float yaw;
|
||||
float pitch;
|
||||
float fov;
|
||||
float aspect;
|
||||
};
|
||||
#endif
|
||||
15
headers/texture.hpp
Normal file
15
headers/texture.hpp
Normal file
@ -0,0 +1,15 @@
|
||||
#ifndef TEXTURE_H
|
||||
#define TEXTURE_H
|
||||
#include <glad/glad.h>
|
||||
|
||||
class Texture {
|
||||
public:
|
||||
Texture(GLenum type, const char *path);
|
||||
~Texture();
|
||||
void bind(GLenum texture);
|
||||
|
||||
private:
|
||||
unsigned int ID;
|
||||
GLenum type;
|
||||
};
|
||||
#endif
|
||||
37
src/camera.cpp
Normal file
37
src/camera.cpp
Normal file
@ -0,0 +1,37 @@
|
||||
#include <camera.hpp>
|
||||
#include <glm/ext/matrix_clip_space.hpp>
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
|
||||
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) {
|
||||
update();
|
||||
}
|
||||
|
||||
Camera::~Camera() {}
|
||||
|
||||
void Camera::setRotate(float yaw, float pitch) {
|
||||
this->yaw = yaw;
|
||||
this->pitch = pitch;
|
||||
update();
|
||||
}
|
||||
|
||||
void Camera::setPos(glm::vec3 pos) { this->pos = pos; }
|
||||
|
||||
void Camera::update() {
|
||||
glm::vec3 direction;
|
||||
direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
|
||||
direction.y = sin(glm::radians(pitch));
|
||||
direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
|
||||
front = glm::normalize(direction);
|
||||
}
|
||||
|
||||
glm::mat4 Camera::getView() {
|
||||
glm::mat4 view = glm::mat4(1.0f);
|
||||
view = glm::lookAt(pos, pos + front, up);
|
||||
return view;
|
||||
}
|
||||
|
||||
glm::mat4 Camera::getProjection() {
|
||||
return glm::perspective(glm::radians(fov), aspect, 0.1f, 100.0f);
|
||||
}
|
||||
39
src/main.cpp
39
src/main.cpp
@ -1,4 +1,5 @@
|
||||
#include <glad/glad.h> /*Must be before GLFW*/
|
||||
#include "camera.hpp"
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <cassert>
|
||||
#include <glm/glm.hpp>
|
||||
@ -6,7 +7,7 @@
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <shader.hpp>
|
||||
#include <shapes.h>
|
||||
#include <stb_image.h>
|
||||
#include <texture.hpp>
|
||||
|
||||
#define UNUSED(x) (void)(x)
|
||||
|
||||
@ -55,48 +56,24 @@ int main(void) {
|
||||
glEnableVertexAttribArray(1); /*rgb*/
|
||||
glEnableVertexAttribArray(2); /*tex_cord*/
|
||||
|
||||
Shader *shaderProgram;
|
||||
unsigned int texture;
|
||||
shaderProgram =
|
||||
Shader *shaderProgram =
|
||||
new Shader("./shaders/MVPTexVert.glsl", "./shaders/TexFrag.glsl");
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
|
||||
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_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
||||
GL_LINEAR_MIPMAP_LINEAR);
|
||||
Texture *texture = new Texture(GL_TEXTURE_2D, "./texture/container.jpg");
|
||||
|
||||
int width, height, nrChannels;
|
||||
unsigned char *data;
|
||||
stbi_set_flip_vertically_on_load(true);
|
||||
|
||||
data = stbi_load("./texture/container.jpg", &width, &height, &nrChannels, 0);
|
||||
assert(data && "Failed to load image");
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
|
||||
GL_UNSIGNED_BYTE, data);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
stbi_image_free(data);
|
||||
Camera *cam = new Camera(glm::vec3(0.0, 0.0, 5.0),-90.0f, 0.0f,45.0,800.0/600);
|
||||
|
||||
while (!glfwWindowShouldClose(window)) {
|
||||
glm::mat4 view = glm::mat4(1.0f); /*move camera*/
|
||||
glm::mat4 projection = glm::perspective(
|
||||
glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f); /*camera settings*/
|
||||
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
|
||||
|
||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
shaderProgram->use();
|
||||
shaderProgram->setInt("Tex0", 0);
|
||||
shaderProgram->setMat4("view", view);
|
||||
shaderProgram->setMat4("projection", projection);
|
||||
shaderProgram->setMat4("view", cam->getView());
|
||||
shaderProgram->setMat4("projection", cam->getProjection());
|
||||
texture->bind(GL_TEXTURE0);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glBindVertexArray(VAO);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
|
||||
33
src/texture.cpp
Normal file
33
src/texture.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include <cassert>
|
||||
#include <stb_image.h>
|
||||
#include <texture.hpp>
|
||||
|
||||
Texture::Texture(GLenum type, const char *path) : type(type) {
|
||||
glGenTextures(1, &ID);
|
||||
glBindTexture(type, ID);
|
||||
|
||||
glTexParameteri(type, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(type, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
|
||||
int width, height, nrChannels;
|
||||
unsigned char *data;
|
||||
stbi_set_flip_vertically_on_load(true);
|
||||
|
||||
data = stbi_load(path, &width, &height, &nrChannels, 0);
|
||||
assert(data && "Failed to load image");
|
||||
|
||||
glTexImage2D(type, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE,
|
||||
data);
|
||||
glGenerateMipmap(type);
|
||||
|
||||
stbi_image_free(data);
|
||||
};
|
||||
|
||||
Texture::~Texture() {}
|
||||
|
||||
void Texture::bind(GLenum texture) {
|
||||
glActiveTexture(texture);
|
||||
glBindTexture(type, ID);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user