Compare commits

..

No commits in common. "85f87152ad920edc71550b39d06398a52bf6c65a" and "f1cfe964a865974cc77e54b777ab462b63a489d8" have entirely different histories.

12 changed files with 99 additions and 416 deletions

3
.gitmodules vendored
View file

@ -1,3 +0,0 @@
[submodule "src/vendor/glm"]
path = src/vendor/glm
url = https://github.com/g-truc/glm

View file

@ -9,9 +9,6 @@ incdir = include_directories(['src', 'src/vendor'])
executable( executable(
'game', 'game',
'./src/common.cpp',
'./src/shader.cpp',
'./src/camera.cpp',
'./src/main.cpp', './src/main.cpp',
'./src/vendor/glad/glad.c', './src/vendor/glad/glad.c',
include_directories : incdir, include_directories : incdir,

View file

@ -1,14 +0,0 @@
#version 330 core
in vec2 TexCoord;
out vec4 FragColor;
uniform sampler2D tex;
void main()
{
FragColor = texture(tex, TexCoord);
}

View file

@ -1,18 +0,0 @@
#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;
}

View file

@ -1,78 +0,0 @@
#include "camera.hpp"
#include "vendor/glm/glm/mat4x4.hpp"
#include "vendor/glm/glm/geometric.hpp"
#include "vendor/glm/glm/gtc/type_ptr.hpp"
void Camera::process_mouse(double x, double y)
{
bool dirty = last_mouse_x != x || last_mouse_y != y;
if (!mouse_moved_once) {
mouse_moved_once = true;
last_mouse_x = x;
last_mouse_y = y;
dirty = true;
}
if (!dirty) {
return;
}
double x_offset = (x - last_mouse_x) * turn_speed;
double y_offset = (last_mouse_y - y) * turn_speed;
yaw += x_offset;
pitch += y_offset;
if (pitch > 89.0f)
pitch = 89.0f;
if (pitch < -89.0f)
pitch = -89.0f;
double pitch_factor = cos(glm::radians(pitch));
front = (glm::vec3){
cos(glm::radians(yaw)) * pitch_factor,
sin(glm::radians(pitch)),
sin(glm::radians(yaw)) * pitch_factor
};
last_mouse_x = x;
last_mouse_y = y;
view = compute_view();
}
void Camera::movement(float delta_time, CameraMovement movement)
{
const float factor = movement_speed * delta_time;
switch (movement) {
case MovementUp:
position += factor * up;
break;
case MovementDown:
position += factor * down;
break;
case MovementLeft:
position -= factor * glm::normalize(glm::cross(front, up));
break;
case MovementRight:
position += factor * glm::normalize(glm::cross(front, up));
break;
case MovementBack:
position -= factor * front;
break;
case MovementForward:
position += factor * front;
break;
}
view = compute_view();
}
const glm::mat4 Camera::compute_view(void)
{
return glm::lookAt(position, position + front, up);
}

View file

@ -1,33 +0,0 @@
#pragma once
#include "vendor/glm/glm/mat4x4.hpp"
#include "vendor/glm/glm/vec3.hpp"
struct Camera {
enum CameraMovement {
MovementUp,
MovementDown,
MovementLeft,
MovementRight,
MovementBack,
MovementForward,
};
double yaw = -90.0f;
double pitch = 0.0f;
double movement_speed = 2.0f;
double turn_speed = 0.1f;
double last_mouse_x, last_mouse_y = 0.0f;
bool mouse_moved_once = false;
glm::vec3 position { 0.0f, 0.0f, 3.0f };
glm::vec3 front { 0.0f, 0.0f, -1.0f };
glm::vec3 up { 0.0f, 1.0f, 0.0f };
glm::vec3 down = { 0.0f, -1.0f, 0.0f };
glm::mat4 view { 1.0f };
void process_mouse(double x, double y);
void movement(float delta_time, CameraMovement movement);
const glm::mat4 compute_view(void);
};

View file

@ -1,31 +0,0 @@
#include "common.hpp"
#include <cstdlib>
#include <cstdio>
#include <cerrno>
Errno read_entire_file(FILE *f, char **out_data)
{
long saved = ftell(f);
if (saved < 0) return errno;
if (fseek(f, 0, SEEK_END) < 0) return errno;
long size = ftell(f);
if (size < 0) return errno;
if (fseek(f, saved, SEEK_SET) < 0) return errno;
char *data = (char*)malloc(size + 1);
if (!data) {
return errno;
}
fread(data, 1, size, f);
if (ferror(f)) {
free(data);
return errno;
}
data[size] = 0;
*out_data = data;
return 0;
}

View file

@ -1,8 +0,0 @@
#pragma once
#include <cstdio>
typedef int Errno;
Errno read_entire_file(FILE *f, char **out_data);

View file

@ -1,25 +1,72 @@
#include <cstdio> #include <cstdio>
#include <iostream> #include <iostream>
#include <functional>
#include "camera.hpp"
#include "shader.hpp"
#include "vendor/glad/glad.h" #include "vendor/glad/glad.h"
#include "vendor/glm/glm/ext/matrix_transform.hpp"
#define GLFW_INCLUDE_NONE #define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#define QOI_IMPLEMENTATION #define QOI_IMPLEMENTATION
#include "vendor/qoi.h" #include "vendor/qoi.h"
#include "vendor/glm/glm/vec3.hpp" unsigned int create_shader(unsigned int type, const char **source)
#include "vendor/glm/glm/vec4.hpp" {
#include "vendor/glm/glm/trigonometric.hpp" unsigned int shader = glCreateShader(type);
#include "vendor/glm/glm/geometric.hpp" if (!shader) {
#include "vendor/glm/glm/mat4x4.hpp" printf("err: create_shader: glCreateShader returned 0\n");
#include "vendor/glm/glm/gtc/type_ptr.hpp" return 0;
}
glShaderSource(shader, 1, source, NULL);
glCompileShader(shader);
int success;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
char log[512];
glGetShaderInfoLog(shader, 512, NULL, log);
printf("err: create_shader: compilation failed, log: %s\n", log);
return 0;
}
std::function<void(int width, int height)> framebuffer_size_callback = nullptr; return shader;
std::function<void(double x, double y)> cursor_position_callback = nullptr; }
unsigned int create_program(const char *vertex_shader_source, const char *fragment_shader_source)
{
unsigned int program = glCreateProgram();
if (!program) {
printf("err: create_program: glCreateProgram returned 0\n");
return 0;
}
unsigned int vertex_shader = create_shader(GL_VERTEX_SHADER, &vertex_shader_source);
if (!vertex_shader) {
printf("err: create_program: create_shader failed returned 0 (vertex_shader)\n");
return 0;
}
unsigned int fragment_shader = create_shader(GL_FRAGMENT_SHADER, &fragment_shader_source);
if (!fragment_shader) {
printf("err: create_program: create_shader failed returned 0 (fragment_shader)\n");
return 0;
}
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
int success;
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (!success) {
char log[512];
glGetProgramInfoLog(program, 512, NULL, log);
printf("err: create_program: link failed, log: %s\n", log);
return 0;
}
return program;
}
void framebuffer_size_callback(GLFWwindow *window, int width, int height)
{
glViewport(0, 0, width, height);
}
int main() int main()
{ {
@ -44,66 +91,42 @@ int main()
gladLoadGL(); gladLoadGL();
glfwSwapInterval(1); glfwSwapInterval(1);
glfwSetFramebufferSizeCallback(window, [](GLFWwindow *_, int width, int height) { glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
if (framebuffer_size_callback) {
framebuffer_size_callback(width, height);
}
});
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPosCallback(window, [](GLFWwindow *_, double x, double y) {
if (cursor_position_callback) {
cursor_position_callback(x, y);
}
});
glEnable(GL_DEPTH_TEST);
float verts[] = { float verts[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, // position // color // texture coords
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.5f, 1.0f, 0.0f, 0.0f, 0.5f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
}; };
unsigned int program = create_program("shaders/vertex.glsl", "shaders/fragment.glsl"); static const char *vertex_shader_source = ""
"#version 330 core\n"
"layout (location = 0) in vec2 aPos;\n"
"layout (location = 1) in vec3 aColor;\n"
"layout (location = 2) in vec2 aTexCoord;\n"
"out vec3 fragColor;\n"
"out vec2 TexCoord;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);\n"
" fragColor = aColor;\n"
" TexCoord = aTexCoord;\n"
"}\0";
static const char *fragment_shader_source = ""
"#version 330 core\n"
"in vec3 fragColor;\n"
"in vec2 TexCoord;\n"
"out vec4 FragColor;\n"
"uniform sampler2D tex;\n"
"void main()\n"
"{\n"
" FragColor = texture(tex, TexCoord);\n"
"}\0";
unsigned int program = create_program(vertex_shader_source, fragment_shader_source);
unsigned int texture; unsigned int texture;
glGenTextures(1, &texture); glGenTextures(1, &texture);
@ -120,68 +143,26 @@ int main()
glBindBuffer(GL_ARRAY_BUFFER, buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), &verts, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(verts), &verts, GL_STATIC_DRAW);
// position // 2D position
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (const void*)0); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (const void*)0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
// texture coords // RGB color
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (const void*)(3 * sizeof(float))); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (const void*)(2 * sizeof(float)));
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
glm::mat4 projection = glm::mat4(1.0f); // texture coords
Camera camera; glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (const void*)(5 * sizeof(float)));
glEnableVertexAttribArray(2);
framebuffer_size_callback = [&projection, program](int width, int height) {
projection = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 100.0f);
glUniformMatrix4fv(glGetUniformLocation(program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glViewport(0, 0, width, height);
};
cursor_position_callback = [&camera](double x, double y) {
camera.process_mouse(x, y);
};
float delta_time = 0.0f;
float last_frame_time = 0.0f;
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
float current_frame_time = glfwGetTime();
delta_time = current_frame_time - last_frame_time;
last_frame_time = current_frame_time;
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
camera.movement(delta_time, Camera::MovementForward);
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
camera.movement(delta_time, Camera::MovementBack);
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
camera.movement(delta_time, Camera::MovementLeft);
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
camera.movement(delta_time, Camera::MovementRight);
}
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) {
camera.movement(delta_time, Camera::MovementUp);
}
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) {
camera.movement(delta_time, Camera::MovementDown);
}
glClearColor(0.5, 0.5, 0.5, 1.0); glClearColor(0.5, 0.5, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program); glUseProgram(program);
glUniformMatrix4fv(glGetUniformLocation(program, "view"), 1, GL_FALSE, glm::value_ptr(camera.view));
glBindTexture(GL_TEXTURE_2D, texture); glBindTexture(GL_TEXTURE_2D, texture);
glBindVertexArray(vertex_array); glBindVertexArray(vertex_array);
glDrawArrays(GL_TRIANGLES, 0, 3);
for (int i = 0; i < 12; i++) {
glm::mat4 model = glm::translate(glm::mat4(1.0), glm::vec3(2 * i, 0.0f, 0.0f));
glUniformMatrix4fv(glGetUniformLocation(program, "model"), 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 36);
}
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();

View file

@ -1,105 +0,0 @@
#include "shader.hpp"
#include "common.hpp"
#include "vendor/glad/glad.h"
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <cstdlib>
unsigned int create_shader(unsigned int type, const char **source)
{
unsigned int shader = glCreateShader(type);
if (!shader) {
printf("err: create_shader: glCreateShader returned 0\n");
return 0;
}
glShaderSource(shader, 1, source, NULL);
glCompileShader(shader);
int success;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
char log[512];
glGetShaderInfoLog(shader, 512, NULL, log);
printf("err: create_shader: compilation failed, log: %s\n", log);
return 0;
}
return shader;
}
unsigned int create_program_source(const char *vertex_shader_source, const char *fragment_shader_source)
{
unsigned int program = glCreateProgram();
if (!program) {
printf("err: create_program: glCreateProgram returned 0\n");
return 0;
}
unsigned int vertex_shader = create_shader(GL_VERTEX_SHADER, &vertex_shader_source);
if (!vertex_shader) {
printf("err: create_program: create_shader failed returned 0 (vertex_shader)\n");
return 0;
}
unsigned int fragment_shader = create_shader(GL_FRAGMENT_SHADER, &fragment_shader_source);
if (!fragment_shader) {
printf("err: create_program: create_shader failed returned 0 (fragment_shader)\n");
return 0;
}
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
int success;
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (!success) {
char log[512];
glGetProgramInfoLog(program, 512, NULL, log);
printf("err: create_program: link failed, log: %s\n", log);
return 0;
}
return program;
}
unsigned int create_program(const char *vertex_shader_path, const char *fragment_shader_path)
{
FILE *vertex_shader_file, *fragment_shader_file = NULL;
char *vertex_shader_source = NULL;
char *fragment_shader_source = NULL;
Errno err = 0;
unsigned int ret = 0;
vertex_shader_file = fopen(vertex_shader_path, "r");
if (!vertex_shader_file) {
fprintf(stderr, "create_program: failed to open vertex shader file (%s): %s\n", vertex_shader_path, strerror(errno));
goto done;
}
err = read_entire_file(vertex_shader_file, &vertex_shader_source);
if (err) {
fprintf(stderr, "create_program: failed to read vertex shader file (%s): %s\n", vertex_shader_path, strerror(err));
goto done;
}
fragment_shader_file = fopen(fragment_shader_path, "r");
if (!fragment_shader_file) {
fprintf(stderr, "create_program: failed to open fragment shader file (%s): %s\n", fragment_shader_path, strerror(errno));
goto done;
}
err = read_entire_file(fragment_shader_file, &fragment_shader_source);
if (err) {
fprintf(stderr, "create_program: failed to read fragment shader file (%s): %s\n", fragment_shader_path, strerror(err));
goto done;
}
ret = create_program_source(vertex_shader_source, fragment_shader_source);
done:
if (vertex_shader_file) fclose(vertex_shader_file);
if (fragment_shader_file) fclose(fragment_shader_file);
if (vertex_shader_source) free(vertex_shader_source);
if (fragment_shader_source) free(fragment_shader_source);
return ret;
}

View file

@ -1,4 +0,0 @@
#pragma once
unsigned int create_program(const char *vertex_shader_path, const char *fragment_shader_path);

1
src/vendor/glm vendored

@ -1 +0,0 @@
Subproject commit 5c46b9c07008ae65cb81ab79cd677ecc1934b903