move line art to line_art namespace
This commit is contained in:
parent
e6c927bb39
commit
04c265e253
3
Makefile
3
Makefile
@ -32,7 +32,8 @@ OBJS = \
|
||||
src/minecraft.o \
|
||||
src/hud.o \
|
||||
src/lighting.o \
|
||||
src/collision_scene.o
|
||||
src/collision_scene.o \
|
||||
src/line_art.o
|
||||
|
||||
all: test.so
|
||||
|
||||
|
||||
18
include/line_art.h
Normal file
18
include/line_art.h
Normal file
@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
namespace line_art {
|
||||
void load();
|
||||
|
||||
void load_ray_vertex_buffer(XMVECTOR const & a, XMVECTOR const & b);
|
||||
|
||||
XMMATRIX view();
|
||||
XMMATRIX projection();
|
||||
|
||||
void set_transform(XMMATRIX const & transform);
|
||||
void draw_line(XMMATRIX const & transform, XMVECTOR const & a, XMVECTOR const & b);
|
||||
void draw_sphere(XMMATRIX const & transform, XMVECTOR const & center, float radius);
|
||||
void draw_capsule(XMMATRIX const & transform, XMVECTOR a, XMVECTOR b, float radius);
|
||||
void draw_cube(XMMATRIX const & transform, XMVECTOR const & position);
|
||||
void scene_start(XMMATRIX const & transform);
|
||||
void set_color(float r, float g, float b);
|
||||
}
|
||||
@ -4,184 +4,12 @@
|
||||
|
||||
#include "collision.h"
|
||||
#include "collision_scene.h"
|
||||
#include "line_art.h"
|
||||
|
||||
namespace collision_scene {
|
||||
|
||||
struct location {
|
||||
struct {
|
||||
unsigned int position;
|
||||
} attrib;
|
||||
struct {
|
||||
unsigned int transform;
|
||||
unsigned int use_grid_transform;
|
||||
unsigned int base_color;
|
||||
} uniform;
|
||||
};
|
||||
|
||||
static unsigned int program;
|
||||
static location location;
|
||||
|
||||
static unsigned int vertex_array_object;
|
||||
static unsigned int per_vertex_buffer;
|
||||
|
||||
static unsigned int index_buffer;
|
||||
|
||||
static XMVECTOR point_position[4];
|
||||
|
||||
static unsigned int ray_vertex_buffer;
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wnarrowing"
|
||||
const _Float16 vertex_data[] = {
|
||||
// 0: lines
|
||||
-1.0, 0.0, 1.0, 0.0, // horizontal, left to right
|
||||
0.0, 1.0, 0.0, -1.0, // vertical, top to bottom
|
||||
|
||||
// 4: "cube"
|
||||
-1.0, 1.0, // tl
|
||||
1.0, 1.0, // tr
|
||||
1.0, -1.0, // br
|
||||
-1.0, -1.0, // bl
|
||||
|
||||
// 8: "circle"
|
||||
0.000000, -1.000000,
|
||||
-0.195090, -0.980785,
|
||||
-0.382683, -0.923880,
|
||||
-0.555570, -0.831470,
|
||||
-0.707107, -0.707107,
|
||||
-0.831470, -0.555570,
|
||||
-0.923880, -0.382683,
|
||||
-0.980785, -0.195090,
|
||||
-1.000000, 0.000000,
|
||||
-0.980785, 0.195090,
|
||||
-0.923880, 0.382683,
|
||||
-0.831470, 0.555570,
|
||||
-0.707107, 0.707107,
|
||||
-0.555570, 0.831470,
|
||||
-0.382683, 0.923880,
|
||||
-0.195090, 0.980785,
|
||||
0.000000, 1.000000,
|
||||
0.195090, 0.980785,
|
||||
0.382683, 0.923880,
|
||||
0.555570, 0.831470,
|
||||
0.707107, 0.707107,
|
||||
0.831470, 0.555570,
|
||||
0.923880, 0.382683,
|
||||
0.980785, 0.195090,
|
||||
1.000000, 0.000000,
|
||||
0.980785, -0.195090,
|
||||
0.923880, -0.382683,
|
||||
0.831470, -0.555570,
|
||||
0.707107, -0.707107,
|
||||
0.555570, -0.831470,
|
||||
0.382683, -0.923880,
|
||||
0.195090, -0.980785,
|
||||
};
|
||||
#pragma GCC diagnostic pop
|
||||
const int vertex_data_size = (sizeof (vertex_data));
|
||||
|
||||
const int per_vertex_size = (sizeof (_Float16)) * 2;
|
||||
|
||||
const unsigned short index_data[] = {
|
||||
// "cube" (line strip)
|
||||
0, 1, 2, 3, 0,
|
||||
|
||||
// "circle" (line strip)
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
||||
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
|
||||
0,
|
||||
};
|
||||
const int index_data_size = (sizeof (index_data));
|
||||
|
||||
void load_program()
|
||||
{
|
||||
program = compile_from_files("shader/collision_scene.vert",
|
||||
nullptr,
|
||||
"shader/collision_scene.frag");
|
||||
|
||||
location.attrib.position = glGetAttribLocation(program, "Position");
|
||||
|
||||
location.uniform.transform = glGetUniformLocation(program, "Transform");
|
||||
location.uniform.use_grid_transform = glGetUniformLocation(program, "UseGridTransform");
|
||||
location.uniform.base_color = glGetUniformLocation(program, "BaseColor");
|
||||
}
|
||||
|
||||
void load_vertex_attributes()
|
||||
{
|
||||
glGenVertexArrays(1, &vertex_array_object);
|
||||
glBindVertexArray(vertex_array_object);
|
||||
|
||||
glVertexBindingDivisor(0, 0);
|
||||
|
||||
glEnableVertexAttribArray(location.attrib.position);
|
||||
glVertexAttribFormat(location.attrib.position, 2, GL_HALF_FLOAT, GL_FALSE, 0);
|
||||
glVertexAttribBinding(location.attrib.position, 0);
|
||||
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void load_per_vertex_buffer()
|
||||
{
|
||||
glGenBuffers(1, &per_vertex_buffer);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, per_vertex_buffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertex_data_size, vertex_data, GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
void load_index_buffer()
|
||||
{
|
||||
glGenBuffers(1, &index_buffer);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_data_size, index_data, GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
void load()
|
||||
{
|
||||
load_program();
|
||||
load_vertex_attributes();
|
||||
|
||||
load_per_vertex_buffer();
|
||||
|
||||
load_index_buffer();
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
point_position[i] = XMVectorSet(-1, -1, 0, 1);
|
||||
|
||||
// ray buffer
|
||||
glGenBuffers(1, &ray_vertex_buffer);
|
||||
}
|
||||
|
||||
void load_ray_vertex_buffer(XMVECTOR const & a, XMVECTOR const & b)
|
||||
{
|
||||
_Float16 data[] = {
|
||||
(_Float16)XMVectorGetX(a), (_Float16)XMVectorGetY(a),
|
||||
(_Float16)XMVectorGetX(b), (_Float16)XMVectorGetY(b),
|
||||
};
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, ray_vertex_buffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, 2 * 4, data, GL_DYNAMIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
static inline XMMATRIX view()
|
||||
{
|
||||
XMVECTOR eye = XMVectorSet(0, 0, 1, 0);
|
||||
XMVECTOR at = XMVectorSet(0, 0, 0, 0);
|
||||
XMVECTOR up = XMVectorSet(0, 1, 0, 0);
|
||||
return XMMatrixLookAtRH(eye, at, up);
|
||||
}
|
||||
|
||||
static inline XMMATRIX projection()
|
||||
{
|
||||
return XMMatrixOrthographicRH(10, 10, 0, 10);
|
||||
}
|
||||
|
||||
const float point_radius = 0.05f;
|
||||
|
||||
void update(int up, int down, int left, int right,
|
||||
@ -210,61 +38,6 @@ namespace collision_scene {
|
||||
point_position[i] = XMVector3Transform(point_position[i], XMMatrixTranslation(strafe[i], forward[i], 0));
|
||||
}
|
||||
|
||||
static inline void set_transform(XMMATRIX const & transform)
|
||||
{
|
||||
XMFLOAT4X4 float_transform;
|
||||
XMStoreFloat4x4(&float_transform, transform);
|
||||
glUniformMatrix4fv(location.uniform.transform, 1, false, (float *)&float_transform);
|
||||
}
|
||||
|
||||
const int circle_base_vertex = 8;
|
||||
const int circle_base_index = 5 * (sizeof (unsigned short));
|
||||
|
||||
void draw_line(XMMATRIX const & transform, XMVECTOR const & a, XMVECTOR const & b)
|
||||
{
|
||||
load_ray_vertex_buffer(a, b);
|
||||
set_transform(transform);
|
||||
glBindVertexBuffer(0, ray_vertex_buffer, 0, per_vertex_size);
|
||||
glDrawArrays(GL_LINES, 0, 4);
|
||||
}
|
||||
|
||||
void draw_sphere(XMMATRIX const & transform, XMVECTOR const & center, float radius)
|
||||
{
|
||||
XMMATRIX sphere_transform
|
||||
= XMMatrixScaling(radius, radius, radius)
|
||||
* XMMatrixTranslationFromVector(center)
|
||||
* transform;
|
||||
set_transform(sphere_transform);
|
||||
glBindVertexBuffer(0, per_vertex_buffer, 0, per_vertex_size);
|
||||
glDrawElementsBaseVertex(GL_LINE_STRIP, 33, GL_UNSIGNED_SHORT, (void*)(circle_base_index), circle_base_vertex);
|
||||
}
|
||||
|
||||
void draw_capsule(XMMATRIX const & transform, XMVECTOR a, XMVECTOR b, float radius)
|
||||
{
|
||||
draw_sphere(transform, a, radius);
|
||||
draw_sphere(transform, b, radius);
|
||||
|
||||
XMVECTOR abn = XMVector3Normalize(a - b);
|
||||
XMVECTOR p = XMVectorSet(0, 0, 1, 0);
|
||||
XMVECTOR pxabn = XMVector3Cross(abn, p);
|
||||
|
||||
draw_line(transform, a + pxabn * radius, b + pxabn * radius);
|
||||
draw_line(transform, a - pxabn * radius, b - pxabn * radius);
|
||||
}
|
||||
|
||||
void draw_cube(XMMATRIX const & transform, XMVECTOR const & position)
|
||||
{
|
||||
float cube_half = 0.5;
|
||||
XMMATRIX cube_transform
|
||||
= XMMatrixScaling(cube_half, cube_half, cube_half)
|
||||
* XMMatrixTranslationFromVector(position)
|
||||
* transform;
|
||||
set_transform(cube_transform);
|
||||
const int cube_base_vertex = 4;
|
||||
const int cube_base_index = 0;
|
||||
glDrawElementsBaseVertex(GL_LINE_STRIP, 5, GL_UNSIGNED_SHORT, (void*)(cube_base_index), cube_base_vertex);
|
||||
}
|
||||
|
||||
static const XMVECTOR cubes[] = {
|
||||
XMVectorSet(1, 1, 0, 1),
|
||||
XMVectorSet(-1, 1, 0, 1),
|
||||
@ -274,6 +47,12 @@ namespace collision_scene {
|
||||
};
|
||||
static const int cubes_length = (sizeof (cubes)) / (sizeof (cubes[0]));
|
||||
|
||||
void load()
|
||||
{
|
||||
for (int i = 0; i < 4; i++)
|
||||
point_position[i] = XMVectorSet(-1, -1, 0, 1);
|
||||
}
|
||||
|
||||
void check_collisions(collision::Sphere const & sphere, XMVECTOR const & direction,
|
||||
collision::state & state)
|
||||
{
|
||||
@ -301,26 +80,9 @@ namespace collision_scene {
|
||||
|
||||
void draw()
|
||||
{
|
||||
glUseProgram(program);
|
||||
XMMATRIX transform = line_art::view() * line_art::projection();
|
||||
|
||||
glBlendFunc(GL_ONE, GL_ZERO);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDepthFunc(GL_ALWAYS);
|
||||
|
||||
glBindVertexArray(vertex_array_object);
|
||||
glBindVertexBuffer(0, per_vertex_buffer, 0, per_vertex_size);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
|
||||
|
||||
XMMATRIX transform = view() * projection();
|
||||
|
||||
// grid
|
||||
glLineWidth(1.0f);
|
||||
set_transform(transform);
|
||||
glUniform3f(location.uniform.base_color, 0, 1, 0);
|
||||
glUniform1i(location.uniform.use_grid_transform, 1);
|
||||
glDrawArraysInstanced(GL_LINES, 0, 4, 7);
|
||||
|
||||
glUniform1i(location.uniform.use_grid_transform, 0);
|
||||
line_art::scene_start(transform);
|
||||
|
||||
collision::Sphere sphere(point_position[0], 0.48);
|
||||
XMVECTOR direction = point_position[1] - point_position[0];
|
||||
@ -332,19 +94,18 @@ namespace collision_scene {
|
||||
glLineWidth(3.0f);
|
||||
for (int i = 0; i < cubes_length; i++) {
|
||||
XMVECTOR center = cubes[i];
|
||||
draw_cube(transform, center);
|
||||
line_art::draw_cube(transform, center);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// collision response
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
glUniform3f(location.uniform.base_color, 0, 0.0, 1.0);
|
||||
draw_sphere(transform, sphere.center, sphere.radius);
|
||||
glUniform3f(location.uniform.base_color, 0, 0.5, 1.0);
|
||||
draw_sphere(transform, sphere.center + direction, point_radius);
|
||||
draw_line(transform, sphere.center, sphere.center + direction);
|
||||
line_art::set_color(0, 0.0, 1.0);
|
||||
line_art::draw_sphere(transform, sphere.center, sphere.radius);
|
||||
line_art::set_color(0, 0.5, 1.0);
|
||||
line_art::draw_sphere(transform, sphere.center + direction, point_radius);
|
||||
line_art::draw_line(transform, sphere.center, sphere.center + direction);
|
||||
|
||||
int intersections = 0;
|
||||
while (intersections < 10) {
|
||||
@ -360,13 +121,13 @@ namespace collision_scene {
|
||||
state.intersection_position,
|
||||
intersection_normal);
|
||||
|
||||
glUniform3f(location.uniform.base_color, 1.0, 0.5, 1.0);
|
||||
draw_line(transform, state.intersection_position, state.intersection_position + new_direction);
|
||||
line_art::set_color(1.0, 0.5, 1.0);
|
||||
line_art::draw_line(transform, state.intersection_position, state.intersection_position + new_direction);
|
||||
|
||||
glUniform3f(location.uniform.base_color, 1.0, 0.5, 0.0);
|
||||
draw_sphere(transform, state.intersection_position, sphere.radius);
|
||||
glUniform3f(location.uniform.base_color, 1.0, 0.0, 0.0);
|
||||
draw_sphere(transform, state.intersection_point, point_radius);
|
||||
line_art::set_color(1.0, 0.5, 0.0);
|
||||
line_art::draw_sphere(transform, state.intersection_position, sphere.radius);
|
||||
line_art::set_color(1.0, 0.0, 0.0);
|
||||
line_art::draw_sphere(transform, state.intersection_point, point_radius);
|
||||
|
||||
// collide again
|
||||
sphere.center = state.intersection_position;
|
||||
@ -378,62 +139,62 @@ namespace collision_scene {
|
||||
if (intersections == 10) {
|
||||
//direction == XMVectorZero();
|
||||
} else {
|
||||
glUniform3f(location.uniform.base_color, 1.0, 1.0, 0.0);
|
||||
draw_sphere(transform, sphere.center + direction, sphere.radius);
|
||||
line_art::set_color(1.0, 1.0, 0.0);
|
||||
line_art::draw_sphere(transform, sphere.center + direction, sphere.radius);
|
||||
}
|
||||
|
||||
/*
|
||||
XMVECTOR pa = XMVectorSelect(sphere_aabb.min, sphere_aabb.max, g_XMSelect1000);
|
||||
XMVECTOR pb = XMVectorSelect(sphere_aabb.min, sphere_aabb.max, g_XMSelect0101);
|
||||
draw_line(transform, sphere_aabb.min, pa);
|
||||
draw_line(transform, pa, sphere_aabb.max);
|
||||
draw_line(transform, sphere_aabb.max, pb);
|
||||
draw_line(transform, pb, sphere_aabb.min);
|
||||
line_art::draw_line(transform, sphere_aabb.min, pa);
|
||||
line_art::draw_line(transform, pa, sphere_aabb.max);
|
||||
line_art::draw_line(transform, sphere_aabb.max, pb);
|
||||
line_art::draw_line(transform, pb, sphere_aabb.min);
|
||||
*/
|
||||
//sphere_aabb.max,
|
||||
|
||||
/*
|
||||
// segments
|
||||
glUniform3f(location.uniform.base_color, 0, 0.5, 1.0);
|
||||
draw_line(transform, point_position[0], point_position[1]);
|
||||
glUniform3f(location.uniform.base_color, 0, 1.0, 0.5);
|
||||
draw_line(transform, point_position[2], point_position[3]);
|
||||
line_art::set_color(0, 0.5, 1.0);
|
||||
line_art::draw_line(transform, point_position[0], point_position[1]);
|
||||
line_art::set_color(0, 1.0, 0.5);
|
||||
line_art::draw_line(transform, point_position[2], point_position[3]);
|
||||
|
||||
// points
|
||||
glUniform3f(location.uniform.base_color, 0, 0.5, 1.0);
|
||||
draw_sphere(transform, point_position[0], point_radius);
|
||||
draw_sphere(transform, point_position[1], point_radius);
|
||||
line_art::set_color(0, 0.5, 1.0);
|
||||
line_art::draw_sphere(transform, point_position[0], point_radius);
|
||||
line_art::draw_sphere(transform, point_position[1], point_radius);
|
||||
|
||||
glUniform3f(location.uniform.base_color, 0, 1.0, 0.5);
|
||||
draw_sphere(transform, point_position[2], point_radius);
|
||||
draw_sphere(transform, point_position[3], point_radius);
|
||||
line_art::set_color(0, 1.0, 0.5);
|
||||
line_art::draw_sphere(transform, point_position[2], point_radius);
|
||||
line_art::draw_sphere(transform, point_position[3], point_radius);
|
||||
|
||||
float t1, t2;
|
||||
XMVECTOR c1, c2;
|
||||
collision::closest_point_segment_segment(point_position[0], point_position[1],
|
||||
point_position[2], point_position[3],
|
||||
t1, t2, c1, c2);
|
||||
glUniform3f(location.uniform.base_color, 0.0, 0.5, 1.0);
|
||||
draw_sphere(transform, c1, point_radius);
|
||||
glUniform3f(location.uniform.base_color, 0.0, 1.0, 0.5);
|
||||
draw_sphere(transform, c2, point_radius);
|
||||
line_art::set_color(0.0, 0.5, 1.0);
|
||||
line_art::draw_sphere(transform, c1, point_radius);
|
||||
line_art::set_color(0.0, 1.0, 0.5);
|
||||
line_art::draw_sphere(transform, c2, point_radius);
|
||||
|
||||
collision::Capsule capsule1(point_position[0], point_position[1], 0.5);
|
||||
collision::Capsule capsule2(point_position[2], point_position[3], 0.0);
|
||||
glUniform3f(location.uniform.base_color, 0, 0.5, 1.0);
|
||||
draw_capsule(transform, capsule1.a, capsule1.b, capsule1.radius);
|
||||
glUniform3f(location.uniform.base_color, 0, 1.0, 0.5);
|
||||
//draw_capsule(transform, capsule2.a, capsule2.b, capsule2.radius);
|
||||
line_art::set_color(0, 0.5, 1.0);
|
||||
line_art::draw_capsule(transform, capsule1.a, capsule1.b, capsule1.radius);
|
||||
line_art::set_color(0, 1.0, 0.5);
|
||||
//line_art::draw_capsule(transform, capsule2.a, capsule2.b, capsule2.radius);
|
||||
|
||||
XMVECTOR c1_point;
|
||||
XMVECTOR c2_point;
|
||||
bool collided = collision::intersect_capsule_capsule(capsule1, capsule2, c1_point, c2_point);
|
||||
if (collided) {
|
||||
glUniform3f(location.uniform.base_color, 1.0, 0.0, 0.0);
|
||||
draw_sphere(transform, c1_point, point_radius);
|
||||
line_art::set_color(1.0, 0.0, 0.0);
|
||||
line_art::draw_sphere(transform, c1_point, point_radius);
|
||||
|
||||
glUniform3f(location.uniform.base_color, 1.0, 0.5, 0.0);
|
||||
draw_sphere(transform, c2_point, point_radius);
|
||||
line_art::set_color(1.0, 0.5, 0.0);
|
||||
line_art::draw_sphere(transform, c2_point, point_radius);
|
||||
}
|
||||
*/
|
||||
|
||||
@ -446,7 +207,7 @@ namespace collision_scene {
|
||||
* XMMatrixTranslationFromVector(cube_position)
|
||||
* transform;
|
||||
set_transform(cube_transform);
|
||||
glUniform3f(location.uniform.base_color, 0, 0.5, 1.0);
|
||||
line_art::set_color(0, 0.5, 1.0);
|
||||
glUniform1i(location.uniform.use_grid_transform, 0);
|
||||
const int cube_base_vertex = 4;
|
||||
const int cube_base_index = 0;
|
||||
@ -454,7 +215,7 @@ namespace collision_scene {
|
||||
|
||||
// circle
|
||||
|
||||
glUniform3f(location.uniform.base_color, 1.0, 0.5, 0.0);
|
||||
line_art::set_color(1.0, 0.5, 0.0);
|
||||
draw_sphere(transform, point_position, point_radius);
|
||||
draw_sphere(transform, point_1_position, point_radius);
|
||||
|
||||
@ -464,15 +225,15 @@ namespace collision_scene {
|
||||
float t;
|
||||
bool intersection = intersect_ray_aabb(point_position, direction, cube_aabb, t, intersection_point);
|
||||
if (intersection && t > 0.0f) {
|
||||
glUniform3f(location.uniform.base_color, 0.9, 0.0, 0.0);
|
||||
line_art::set_color(0.9, 0.0, 0.0);
|
||||
draw_line(transform, point_position, intersection_point);
|
||||
draw_sphere(transform, intersection_point, point_radius);
|
||||
} else {
|
||||
glUniform3f(location.uniform.base_color, 0.5, 0.5, 0.5);
|
||||
line_art::set_color(0.5, 0.5, 0.5);
|
||||
draw_line(transform, point_position, point_position + direction * 20.0f);
|
||||
}
|
||||
|
||||
glUniform3f(location.uniform.base_color, 0.5, 0.0, 0.5);
|
||||
line_art::set_color(0.5, 0.0, 0.5);
|
||||
XMVECTOR ca = XMVectorSet(1, 2.5, 0, 0);
|
||||
XMVECTOR cb = XMVectorSet(-1, 2, 0, 0);
|
||||
draw_capsule(transform, ca, cb, 0.5);
|
||||
|
||||
256
src/line_art.cpp
Normal file
256
src/line_art.cpp
Normal file
@ -0,0 +1,256 @@
|
||||
#include "glad/gl.h"
|
||||
#include "directxmath/directxmath.h"
|
||||
#include "opengl.h"
|
||||
#include "line_art.h"
|
||||
|
||||
namespace line_art {
|
||||
|
||||
const float vertex_data[] = {
|
||||
// 0: lines
|
||||
-1.0, 0.0, 1.0, 0.0, // horizontal, left to right
|
||||
0.0, 1.0, 0.0, -1.0, // vertical, top to bottom
|
||||
|
||||
// 4: "cube"
|
||||
-1.0, 1.0, // tl
|
||||
1.0, 1.0, // tr
|
||||
1.0, -1.0, // br
|
||||
-1.0, -1.0, // bl
|
||||
|
||||
// 8: "circle"
|
||||
0.000000, -1.000000,
|
||||
-0.195090, -0.980785,
|
||||
-0.382683, -0.923880,
|
||||
-0.555570, -0.831470,
|
||||
-0.707107, -0.707107,
|
||||
-0.831470, -0.555570,
|
||||
-0.923880, -0.382683,
|
||||
-0.980785, -0.195090,
|
||||
-1.000000, 0.000000,
|
||||
-0.980785, 0.195090,
|
||||
-0.923880, 0.382683,
|
||||
-0.831470, 0.555570,
|
||||
-0.707107, 0.707107,
|
||||
-0.555570, 0.831470,
|
||||
-0.382683, 0.923880,
|
||||
-0.195090, 0.980785,
|
||||
0.000000, 1.000000,
|
||||
0.195090, 0.980785,
|
||||
0.382683, 0.923880,
|
||||
0.555570, 0.831470,
|
||||
0.707107, 0.707107,
|
||||
0.831470, 0.555570,
|
||||
0.923880, 0.382683,
|
||||
0.980785, 0.195090,
|
||||
1.000000, 0.000000,
|
||||
0.980785, -0.195090,
|
||||
0.923880, -0.382683,
|
||||
0.831470, -0.555570,
|
||||
0.707107, -0.707107,
|
||||
0.555570, -0.831470,
|
||||
0.382683, -0.923880,
|
||||
0.195090, -0.980785,
|
||||
};
|
||||
const int vertex_data_size = (sizeof (vertex_data));
|
||||
|
||||
const int per_vertex_size = (sizeof (vertex_data[0])) * 2;
|
||||
|
||||
const unsigned short index_data[] = {
|
||||
// "cube" (line strip)
|
||||
0, 1, 2, 3, 0,
|
||||
|
||||
// "circle" (line strip)
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
||||
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
|
||||
0,
|
||||
};
|
||||
const int index_data_size = (sizeof (index_data));
|
||||
|
||||
struct location {
|
||||
struct {
|
||||
unsigned int position;
|
||||
} attrib;
|
||||
struct {
|
||||
unsigned int transform;
|
||||
unsigned int use_grid_transform;
|
||||
unsigned int base_color;
|
||||
} uniform;
|
||||
};
|
||||
|
||||
static unsigned int program;
|
||||
static location location;
|
||||
|
||||
static unsigned int vertex_array_object;
|
||||
static unsigned int per_vertex_buffer;
|
||||
|
||||
static unsigned int index_buffer;
|
||||
|
||||
static unsigned int ray_vertex_buffer;
|
||||
|
||||
static void load_program()
|
||||
{
|
||||
program = compile_from_files("shader/line_art.vert",
|
||||
nullptr,
|
||||
"shader/line_art.frag");
|
||||
|
||||
location.attrib.position = glGetAttribLocation(program, "Position");
|
||||
|
||||
location.uniform.transform = glGetUniformLocation(program, "Transform");
|
||||
location.uniform.use_grid_transform = glGetUniformLocation(program, "UseGridTransform");
|
||||
location.uniform.base_color = glGetUniformLocation(program, "BaseColor");
|
||||
}
|
||||
|
||||
static void load_vertex_attributes()
|
||||
{
|
||||
glGenVertexArrays(1, &vertex_array_object);
|
||||
glBindVertexArray(vertex_array_object);
|
||||
|
||||
glVertexBindingDivisor(0, 0);
|
||||
|
||||
glEnableVertexAttribArray(location.attrib.position);
|
||||
glVertexAttribFormat(location.attrib.position, 2, GL_FLOAT, GL_FALSE, 0);
|
||||
glVertexAttribBinding(location.attrib.position, 0);
|
||||
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
static void load_per_vertex_buffer()
|
||||
{
|
||||
glGenBuffers(1, &per_vertex_buffer);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, per_vertex_buffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertex_data_size, vertex_data, GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
static void load_index_buffer()
|
||||
{
|
||||
glGenBuffers(1, &index_buffer);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_data_size, index_data, GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
void load()
|
||||
{
|
||||
load_program();
|
||||
load_vertex_attributes();
|
||||
|
||||
load_per_vertex_buffer();
|
||||
|
||||
load_index_buffer();
|
||||
|
||||
// ray buffer
|
||||
glGenBuffers(1, &ray_vertex_buffer);
|
||||
}
|
||||
|
||||
void load_ray_vertex_buffer(XMVECTOR const & a, XMVECTOR const & b)
|
||||
{
|
||||
float data[] = {
|
||||
XMVectorGetX(a), XMVectorGetY(a),
|
||||
XMVectorGetX(b), XMVectorGetY(b),
|
||||
};
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, ray_vertex_buffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, (sizeof (data)), data, GL_DYNAMIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
XMMATRIX view()
|
||||
{
|
||||
XMVECTOR eye = XMVectorSet(0, 0, 1, 0);
|
||||
XMVECTOR at = XMVectorSet(0, 0, 0, 0);
|
||||
XMVECTOR up = XMVectorSet(0, 1, 0, 0);
|
||||
return XMMatrixLookAtRH(eye, at, up);
|
||||
}
|
||||
|
||||
XMMATRIX projection()
|
||||
{
|
||||
return XMMatrixOrthographicRH(10, 10, 0, 10);
|
||||
}
|
||||
|
||||
void set_transform(XMMATRIX const & transform)
|
||||
{
|
||||
XMFLOAT4X4 float_transform;
|
||||
XMStoreFloat4x4(&float_transform, transform);
|
||||
glUniformMatrix4fv(location.uniform.transform, 1, false, (float *)&float_transform);
|
||||
}
|
||||
|
||||
const int circle_base_vertex = 8;
|
||||
const int circle_base_index = 5 * (sizeof (unsigned short));
|
||||
|
||||
void draw_line(XMMATRIX const & transform, XMVECTOR const & a, XMVECTOR const & b)
|
||||
{
|
||||
load_ray_vertex_buffer(a, b);
|
||||
set_transform(transform);
|
||||
glBindVertexBuffer(0, ray_vertex_buffer, 0, per_vertex_size);
|
||||
glDrawArrays(GL_LINES, 0, 4);
|
||||
}
|
||||
|
||||
void draw_sphere(XMMATRIX const & transform, XMVECTOR const & center, float radius)
|
||||
{
|
||||
XMMATRIX sphere_transform
|
||||
= XMMatrixScaling(radius, radius, radius)
|
||||
* XMMatrixTranslationFromVector(center)
|
||||
* transform;
|
||||
set_transform(sphere_transform);
|
||||
glBindVertexBuffer(0, per_vertex_buffer, 0, per_vertex_size);
|
||||
glDrawElementsBaseVertex(GL_LINE_STRIP, 33, GL_UNSIGNED_SHORT, (void*)(circle_base_index), circle_base_vertex);
|
||||
}
|
||||
|
||||
void draw_capsule(XMMATRIX const & transform, XMVECTOR a, XMVECTOR b, float radius)
|
||||
{
|
||||
draw_sphere(transform, a, radius);
|
||||
draw_sphere(transform, b, radius);
|
||||
|
||||
XMVECTOR abn = XMVector3Normalize(a - b);
|
||||
XMVECTOR p = XMVectorSet(0, 0, 1, 0);
|
||||
XMVECTOR pxabn = XMVector3Cross(abn, p);
|
||||
|
||||
draw_line(transform, a + pxabn * radius, b + pxabn * radius);
|
||||
draw_line(transform, a - pxabn * radius, b - pxabn * radius);
|
||||
}
|
||||
|
||||
void draw_cube(XMMATRIX const & transform, XMVECTOR const & position)
|
||||
{
|
||||
float cube_half = 0.5;
|
||||
XMMATRIX cube_transform
|
||||
= XMMatrixScaling(cube_half, cube_half, cube_half)
|
||||
* XMMatrixTranslationFromVector(position)
|
||||
* transform;
|
||||
set_transform(cube_transform);
|
||||
const int cube_base_vertex = 4;
|
||||
const int cube_base_index = 0;
|
||||
glDrawElementsBaseVertex(GL_LINE_STRIP, 5, GL_UNSIGNED_SHORT, (void*)(cube_base_index), cube_base_vertex);
|
||||
}
|
||||
|
||||
void scene_start(XMMATRIX const & transform)
|
||||
{
|
||||
glUseProgram(program);
|
||||
|
||||
glBlendFunc(GL_ONE, GL_ZERO);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glDepthFunc(GL_ALWAYS);
|
||||
|
||||
glBindVertexArray(vertex_array_object);
|
||||
glBindVertexBuffer(0, per_vertex_buffer, 0, per_vertex_size);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
|
||||
|
||||
// grid
|
||||
glLineWidth(1.0f);
|
||||
set_transform(transform);
|
||||
glUniform3f(location.uniform.base_color, 0, 1, 0);
|
||||
glUniform1i(location.uniform.use_grid_transform, 1);
|
||||
glDrawArraysInstanced(GL_LINES, 0, 4, 7);
|
||||
|
||||
glUniform1i(location.uniform.use_grid_transform, 0);
|
||||
}
|
||||
|
||||
void set_color(float r, float g, float b)
|
||||
{
|
||||
glUniform3f(location.uniform.base_color, r, g, b);
|
||||
}
|
||||
}
|
||||
44
src/test.cpp
44
src/test.cpp
@ -17,6 +17,7 @@
|
||||
#include "minecraft.h"
|
||||
#include "hud.h"
|
||||
#include "lighting.h"
|
||||
#include "line_art.h"
|
||||
#include "collision_scene.h"
|
||||
#include "collision.h"
|
||||
|
||||
@ -282,9 +283,10 @@ void load(const char * source_path)
|
||||
non_block::load_vertex_attributes();
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// collision_scene
|
||||
// debug scenes
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
line_art::load();
|
||||
collision_scene::load();
|
||||
}
|
||||
|
||||
@ -535,31 +537,31 @@ void update_mouse(int x, int y)
|
||||
|
||||
void draw()
|
||||
{
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
glClearDepth(-1.0f);
|
||||
if (false) {
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
glClearDepth(-1.0f);
|
||||
|
||||
// possibly re-initialize geometry buffer if window width/height changes
|
||||
init_geometry_buffer(geometry_buffer_pnc, geometry_buffer_pnc_types);
|
||||
// possibly re-initialize geometry buffer if window width/height changes
|
||||
init_geometry_buffer(geometry_buffer_pnc, geometry_buffer_pnc_types);
|
||||
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, geometry_buffer_pnc.framebuffer);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, geometry_buffer_pnc.framebuffer);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
minecraft::draw();
|
||||
//draw_line();
|
||||
non_block::draw();
|
||||
minecraft::draw();
|
||||
//draw_line();
|
||||
non_block::draw();
|
||||
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
lighting::draw();
|
||||
//draw_quad();
|
||||
hud::draw();
|
||||
|
||||
/*
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
collision_scene::draw();
|
||||
*/
|
||||
lighting::draw();
|
||||
//draw_quad();
|
||||
hud::draw();
|
||||
} else {
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
collision_scene::draw();
|
||||
}
|
||||
|
||||
last_frame_time = current_time;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user