Compare commits
2 commits
f1cfe964a8
...
85f87152ad
Author | SHA1 | Date | |
---|---|---|---|
|
85f87152ad | ||
|
eefee6537a |
12 changed files with 416 additions and 99 deletions
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
[submodule "src/vendor/glm"]
|
||||||
|
path = src/vendor/glm
|
||||||
|
url = https://github.com/g-truc/glm
|
|
@ -9,6 +9,9 @@ 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,
|
||||||
|
|
14
shaders/fragment.glsl
Normal file
14
shaders/fragment.glsl
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
#version 330 core
|
||||||
|
|
||||||
|
|
||||||
|
in vec2 TexCoord;
|
||||||
|
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
uniform sampler2D tex;
|
||||||
|
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
FragColor = texture(tex, TexCoord);
|
||||||
|
}
|
18
shaders/vertex.glsl
Normal file
18
shaders/vertex.glsl
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
#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;
|
||||||
|
}
|
78
src/camera.cpp
Normal file
78
src/camera.cpp
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
#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);
|
||||||
|
}
|
33
src/camera.hpp
Normal file
33
src/camera.hpp
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
#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);
|
||||||
|
};
|
||||||
|
|
31
src/common.cpp
Normal file
31
src/common.cpp
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
#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;
|
||||||
|
}
|
8
src/common.hpp
Normal file
8
src/common.hpp
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
typedef int Errno;
|
||||||
|
|
||||||
|
|
||||||
|
Errno read_entire_file(FILE *f, char **out_data);
|
217
src/main.cpp
217
src/main.cpp
|
@ -1,72 +1,25 @@
|
||||||
#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"
|
||||||
|
|
||||||
unsigned int create_shader(unsigned int type, const char **source)
|
#include "vendor/glm/glm/vec3.hpp"
|
||||||
{
|
#include "vendor/glm/glm/vec4.hpp"
|
||||||
unsigned int shader = glCreateShader(type);
|
#include "vendor/glm/glm/trigonometric.hpp"
|
||||||
if (!shader) {
|
#include "vendor/glm/glm/geometric.hpp"
|
||||||
printf("err: create_shader: glCreateShader returned 0\n");
|
#include "vendor/glm/glm/mat4x4.hpp"
|
||||||
return 0;
|
#include "vendor/glm/glm/gtc/type_ptr.hpp"
|
||||||
}
|
|
||||||
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;
|
std::function<void(int width, int height)> framebuffer_size_callback = nullptr;
|
||||||
}
|
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()
|
||||||
{
|
{
|
||||||
|
@ -91,42 +44,66 @@ int main()
|
||||||
gladLoadGL();
|
gladLoadGL();
|
||||||
glfwSwapInterval(1);
|
glfwSwapInterval(1);
|
||||||
|
|
||||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
glfwSetFramebufferSizeCallback(window, [](GLFWwindow *_, int width, int height) {
|
||||||
|
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[] = {
|
||||||
// position // color // texture coords
|
-0.5f, -0.5f, -0.5f, 0.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, 0.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, 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, 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
|
||||||
};
|
};
|
||||||
|
|
||||||
static const char *vertex_shader_source = ""
|
unsigned int program = create_program("shaders/vertex.glsl", "shaders/fragment.glsl");
|
||||||
"#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);
|
||||||
|
@ -143,26 +120,68 @@ 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);
|
||||||
|
|
||||||
// 2D position
|
// position
|
||||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (const void*)0);
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (const void*)0);
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
|
|
||||||
// RGB color
|
// texture coords
|
||||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (const void*)(2 * sizeof(float)));
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (const void*)(3 * sizeof(float)));
|
||||||
glEnableVertexAttribArray(1);
|
glEnableVertexAttribArray(1);
|
||||||
|
|
||||||
// texture coords
|
glm::mat4 projection = glm::mat4(1.0f);
|
||||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (const void*)(5 * sizeof(float)));
|
Camera camera;
|
||||||
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);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_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();
|
||||||
|
|
105
src/shader.cpp
Normal file
105
src/shader.cpp
Normal file
|
@ -0,0 +1,105 @@
|
||||||
|
#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;
|
||||||
|
}
|
4
src/shader.hpp
Normal file
4
src/shader.hpp
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
|
||||||
|
unsigned int create_program(const char *vertex_shader_path, const char *fragment_shader_path);
|
1
src/vendor/glm
vendored
Submodule
1
src/vendor/glm
vendored
Submodule
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit 5c46b9c07008ae65cb81ab79cd677ecc1934b903
|
Loading…
Reference in a new issue