Skip to content
Snippets Groups Projects
Commit c7c7f25d authored by worldline-zero's avatar worldline-zero
Browse files

add stuff

parents
Branches main
No related tags found
No related merge requests found
File added
type,big_r,small_r,distance,max_theta,rotation_multiplier,time_offset,color_r,color_g,color_b,filename
hypotrochoid,0.5,0.2,0.1,8.0,0.0,0.0,255,0,0,none
File added
File added
#ifndef IMPORT_H
#define IMPORT_H
#endif
#ifndef SHADER_H
#define SHADER_H
#include <stdio.h>
#include <stdlib.h>
#include "../lib/include/glad/gl.h"
unsigned int create_shader(const char *file, GLenum type);
unsigned int create_program(unsigned int *shaders, unsigned int n);
void print_errors(unsigned int shader, GLenum info);
#endif
#ifndef SPIRO_H
#define SPIRO_H
#include "../lib/vector_lib.h"
#include "../lib/include/glad/gl.h"
#include "../inc/shader.h"
#include <GLFW/glfw3.h>
float greatest_common_divisor(uint64_t a, uint64_t b);
float lowest_common_multiple(float a, float b);
float get_max_t(float internal_r, float external_r);
#define HYPOTROCHOID 1
#define EPITROCHOID -1
typedef struct __point {
vec2 position;
float t;
unsigned int vao;
} point;
typedef struct __graph {
float e_r;
float i_r;
float d;
float max_t;
point *points;
uint32_t n_points;
int8_t type;
float rotation_multiplier;
float angle_offset;
vec3 color;
FILE *file;
} graph;
#define SPIROGRAPH(E_R, I_R, D, T, TYPE, RM, TO, COLOR_R, COLOR_G, COLOR_B) (graph){ \
E_R, \
I_R, \
D, \
T, \
malloc(sizeof(point) * (uint64_t)degrees(T)*8), \
(uint64_t)degrees(T)*8, \
TYPE, \
RM, \
TO, \
(vec3){COLOR_R, COLOR_G, COLOR_B}, \
NULL \
}
void init_point_vertex_data(graph *g);
void calculate_points(graph *g, unsigned int ssbo_program, unsigned int points_ssbo);
void draw_spirograph_points(graph *g, unsigned int shader_program);
#endif
#ifndef WINDOW_INIT_H
#define WINDOW_INIT_H
#include "../lib/include/glad/gl.h"
#include <GLFW/glfw3.h>
GLFWwindow* init_window();
#endif
#ifndef __khrplatform_h_
#define __khrplatform_h_
/*
** Copyright (c) 2008-2018 The Khronos Group Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and/or associated documentation files (the
** "Materials"), to deal in the Materials without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Materials, and to
** permit persons to whom the Materials are furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Materials.
**
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/
/* Khronos platform-specific types and definitions.
*
* The master copy of khrplatform.h is maintained in the Khronos EGL
* Registry repository at https://github.com/KhronosGroup/EGL-Registry
* The last semantic modification to khrplatform.h was at commit ID:
* 67a3e0864c2d75ea5287b9f3d2eb74a745936692
*
* Adopters may modify this file to suit their platform. Adopters are
* encouraged to submit platform specific modifications to the Khronos
* group so that they can be included in future versions of this file.
* Please submit changes by filing pull requests or issues on
* the EGL Registry repository linked above.
*
*
* See the Implementer's Guidelines for information about where this file
* should be located on your system and for more details of its use:
* http://www.khronos.org/registry/implementers_guide.pdf
*
* This file should be included as
* #include <KHR/khrplatform.h>
* by Khronos client API header files that use its types and defines.
*
* The types in khrplatform.h should only be used to define API-specific types.
*
* Types defined in khrplatform.h:
* khronos_int8_t signed 8 bit
* khronos_uint8_t unsigned 8 bit
* khronos_int16_t signed 16 bit
* khronos_uint16_t unsigned 16 bit
* khronos_int32_t signed 32 bit
* khronos_uint32_t unsigned 32 bit
* khronos_int64_t signed 64 bit
* khronos_uint64_t unsigned 64 bit
* khronos_intptr_t signed same number of bits as a pointer
* khronos_uintptr_t unsigned same number of bits as a pointer
* khronos_ssize_t signed size
* khronos_usize_t unsigned size
* khronos_float_t signed 32 bit floating point
* khronos_time_ns_t unsigned 64 bit time in nanoseconds
* khronos_utime_nanoseconds_t unsigned time interval or absolute time in
* nanoseconds
* khronos_stime_nanoseconds_t signed time interval in nanoseconds
* khronos_boolean_enum_t enumerated boolean type. This should
* only be used as a base type when a client API's boolean type is
* an enum. Client APIs which use an integer or other type for
* booleans cannot use this as the base type for their boolean.
*
* Tokens defined in khrplatform.h:
*
* KHRONOS_FALSE, KHRONOS_TRUE Enumerated boolean false/true values.
*
* KHRONOS_SUPPORT_INT64 is 1 if 64 bit integers are supported; otherwise 0.
* KHRONOS_SUPPORT_FLOAT is 1 if floats are supported; otherwise 0.
*
* Calling convention macros defined in this file:
* KHRONOS_APICALL
* KHRONOS_APIENTRY
* KHRONOS_APIATTRIBUTES
*
* These may be used in function prototypes as:
*
* KHRONOS_APICALL void KHRONOS_APIENTRY funcname(
* int arg1,
* int arg2) KHRONOS_APIATTRIBUTES;
*/
#if defined(__SCITECH_SNAP__) && !defined(KHRONOS_STATIC)
# define KHRONOS_STATIC 1
#endif
/*-------------------------------------------------------------------------
* Definition of KHRONOS_APICALL
*-------------------------------------------------------------------------
* This precedes the return type of the function in the function prototype.
*/
#if defined(KHRONOS_STATIC)
/* If the preprocessor constant KHRONOS_STATIC is defined, make the
* header compatible with static linking. */
# define KHRONOS_APICALL
#elif defined(_WIN32)
# define KHRONOS_APICALL __declspec(dllimport)
#elif defined (__SYMBIAN32__)
# define KHRONOS_APICALL IMPORT_C
#elif defined(__ANDROID__)
# define KHRONOS_APICALL __attribute__((visibility("default")))
#else
# define KHRONOS_APICALL
#endif
/*-------------------------------------------------------------------------
* Definition of KHRONOS_APIENTRY
*-------------------------------------------------------------------------
* This follows the return type of the function and precedes the function
* name in the function prototype.
*/
#if defined(_WIN32) && !defined(_WIN32_WCE) && !defined(__SCITECH_SNAP__)
/* Win32 but not WinCE */
# define KHRONOS_APIENTRY __stdcall
#else
# define KHRONOS_APIENTRY
#endif
/*-------------------------------------------------------------------------
* Definition of KHRONOS_APIATTRIBUTES
*-------------------------------------------------------------------------
* This follows the closing parenthesis of the function prototype arguments.
*/
#if defined (__ARMCC_2__)
#define KHRONOS_APIATTRIBUTES __softfp
#else
#define KHRONOS_APIATTRIBUTES
#endif
/*-------------------------------------------------------------------------
* basic type definitions
*-----------------------------------------------------------------------*/
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__GNUC__) || defined(__SCO__) || defined(__USLC__)
/*
* Using <stdint.h>
*/
#include <stdint.h>
typedef int32_t khronos_int32_t;
typedef uint32_t khronos_uint32_t;
typedef int64_t khronos_int64_t;
typedef uint64_t khronos_uint64_t;
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
/*
* To support platform where unsigned long cannot be used interchangeably with
* inptr_t (e.g. CHERI-extended ISAs), we can use the stdint.h intptr_t.
* Ideally, we could just use (u)intptr_t everywhere, but this could result in
* ABI breakage if khronos_uintptr_t is changed from unsigned long to
* unsigned long long or similar (this results in different C++ name mangling).
* To avoid changes for existing platforms, we restrict usage of intptr_t to
* platforms where the size of a pointer is larger than the size of long.
*/
#if defined(__SIZEOF_LONG__) && defined(__SIZEOF_POINTER__)
#if __SIZEOF_POINTER__ > __SIZEOF_LONG__
#define KHRONOS_USE_INTPTR_T
#endif
#endif
#elif defined(__VMS ) || defined(__sgi)
/*
* Using <inttypes.h>
*/
#include <inttypes.h>
typedef int32_t khronos_int32_t;
typedef uint32_t khronos_uint32_t;
typedef int64_t khronos_int64_t;
typedef uint64_t khronos_uint64_t;
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
#elif defined(_WIN32) && !defined(__SCITECH_SNAP__)
/*
* Win32
*/
typedef __int32 khronos_int32_t;
typedef unsigned __int32 khronos_uint32_t;
typedef __int64 khronos_int64_t;
typedef unsigned __int64 khronos_uint64_t;
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
#elif defined(__sun__) || defined(__digital__)
/*
* Sun or Digital
*/
typedef int khronos_int32_t;
typedef unsigned int khronos_uint32_t;
#if defined(__arch64__) || defined(_LP64)
typedef long int khronos_int64_t;
typedef unsigned long int khronos_uint64_t;
#else
typedef long long int khronos_int64_t;
typedef unsigned long long int khronos_uint64_t;
#endif /* __arch64__ */
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
#elif 0
/*
* Hypothetical platform with no float or int64 support
*/
typedef int khronos_int32_t;
typedef unsigned int khronos_uint32_t;
#define KHRONOS_SUPPORT_INT64 0
#define KHRONOS_SUPPORT_FLOAT 0
#else
/*
* Generic fallback
*/
#include <stdint.h>
typedef int32_t khronos_int32_t;
typedef uint32_t khronos_uint32_t;
typedef int64_t khronos_int64_t;
typedef uint64_t khronos_uint64_t;
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
#endif
/*
* Types that are (so far) the same on all platforms
*/
typedef signed char khronos_int8_t;
typedef unsigned char khronos_uint8_t;
typedef signed short int khronos_int16_t;
typedef unsigned short int khronos_uint16_t;
/*
* Types that differ between LLP64 and LP64 architectures - in LLP64,
* pointers are 64 bits, but 'long' is still 32 bits. Win64 appears
* to be the only LLP64 architecture in current use.
*/
#ifdef KHRONOS_USE_INTPTR_T
typedef intptr_t khronos_intptr_t;
typedef uintptr_t khronos_uintptr_t;
#elif defined(_WIN64)
typedef signed long long int khronos_intptr_t;
typedef unsigned long long int khronos_uintptr_t;
#else
typedef signed long int khronos_intptr_t;
typedef unsigned long int khronos_uintptr_t;
#endif
#if defined(_WIN64)
typedef signed long long int khronos_ssize_t;
typedef unsigned long long int khronos_usize_t;
#else
typedef signed long int khronos_ssize_t;
typedef unsigned long int khronos_usize_t;
#endif
#if KHRONOS_SUPPORT_FLOAT
/*
* Float type
*/
typedef float khronos_float_t;
#endif
#if KHRONOS_SUPPORT_INT64
/* Time types
*
* These types can be used to represent a time interval in nanoseconds or
* an absolute Unadjusted System Time. Unadjusted System Time is the number
* of nanoseconds since some arbitrary system event (e.g. since the last
* time the system booted). The Unadjusted System Time is an unsigned
* 64 bit value that wraps back to 0 every 584 years. Time intervals
* may be either signed or unsigned.
*/
typedef khronos_uint64_t khronos_utime_nanoseconds_t;
typedef khronos_int64_t khronos_stime_nanoseconds_t;
#endif
/*
* Dummy value used to pad enum types to 32 bits.
*/
#ifndef KHRONOS_MAX_ENUM
#define KHRONOS_MAX_ENUM 0x7FFFFFFF
#endif
/*
* Enumerated boolean type
*
* Values other than zero should be considered to be true. Therefore
* comparisons should not be made against KHRONOS_TRUE.
*/
typedef enum {
KHRONOS_FALSE = 0,
KHRONOS_TRUE = 1,
KHRONOS_BOOLEAN_ENUM_FORCE_SIZE = KHRONOS_MAX_ENUM
} khronos_boolean_enum_t;
#endif /* __khrplatform_h_ */
This diff is collapsed.
File added
This diff is collapsed.
This diff is collapsed.
#ifndef VECTOR_LIB_H
#define VECTOR_LIB_H
#ifndef MATH_H
#define MATH_H
#define TAU 6.28318530718f
#define PI 3.14159265359f
#define PI_2 1.57079632679f
#define PI_3 1.0471975512f
#define PI_4 0.78539816339f
#define PI_6 0.52359877559f
#define PI_12 0.26179938779f
#define E 2.71828182845f
float rsqrtv(float x);
// trig functions: float x is given in radians
float sinv(float x);
float cosv(float x);
float tanv(float x);
// inverse trig functions, result in radians
// -inf < result < inf
float arctanv(float x);
// -pi/2 <= result <= pi/2
float arcsinv(float x);
// 0 <= result <= pi
float arccosv(float x);
// angle unit conversions
float radians(float angle_deg);
float degrees(float angle_rad);
#endif
#ifndef VECTOR_H
#define VECTOR_H
#include <stdint.h>
typedef union __vec2 {
struct {
float x;
float y;
};
float raw[2];
} vec2;
typedef union __vec3 {
struct {
float x;
float y;
float z;
};
struct {
uint8_t r;
uint8_t g;
uint8_t b;
};
float raw[3];
} vec3;
typedef union __vec4 {
struct {
float x;
float y;
float z;
float w;
};
struct {
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
};
uint32_t rgba;
float raw[4];
} vec4;
vec2 v2add(vec2 v1, vec2 v2);
vec3 v3add(vec3 v1, vec3 v2);
vec4 v4add(vec4 v1, vec4 v2);
vec2 v2sub(vec2 v1, vec2 v2);
vec3 v3sub(vec3 v1, vec3 v2);
vec4 v4sub(vec4 v1, vec4 v2);
vec2 v2mul(vec2 v1, vec2 v2);
vec3 v3mul(vec3 v1, vec3 v2);
vec4 v4mul(vec4 v1, vec4 v2);
vec2 v2div(vec2 v1, vec2 v2);
vec3 v3div(vec3 v1, vec3 v2);
vec4 v4div(vec4 v1, vec4 v2);
float dot(float *v1, float *v2, uint8_t comp);
vec3 cross(vec3 a, vec3 b);
vec2 v2norm(vec2 v);
vec3 v3norm(vec3 v);
vec4 v4norm(vec4 v);
vec3 v2tov3(vec2 v, float f1);
vec4 v2tov4(vec2 v, float f1, float f2);
vec2 v3tov2(vec3 v);
vec4 v3tov4(vec3 v, float f1);
vec2 v4tov2(vec4 v);
vec3 v4tov3(vec4 v);
#endif
#ifndef MATRIX_H
#define MATRIX_H
#define IDENTITY_MAT2 \
(mat2) \
{ \
1.0f, 0.0f, \
0.0f, 1.0f \
}
#define IDENTITY_MAT3 \
(mat3) \
{ \
1.0f, 0.0f, 0.0f, \
0.0f, 1.0f, 0.0f, \
0.0f, 0.0f, 1.0f \
}
#define IDENTITY_MAT4 \
(mat4) \
{ \
1.0f, 0.0f, 0.0f, 0.0f, \
0.0f, 1.0f, 0.0f, 0.0f, \
0.0f, 0.0f, 1.0f, 0.0f, \
0.0f, 0.0f, 0.0f, 1.0f \
}
typedef union __mat2 {
float raw[2][2];
struct {
float m00, m10;
float m01, m11;
};
vec2 rows[2];
} mat2;
typedef union __mat3 {
float raw[3][3];
struct {
float m00, m10, m20;
float m01, m11, m21;
float m02, m12, m22;
};
vec3 rows[3];
} mat3;
typedef union __mat4 {
float raw[4][4];
struct {
float m00, m10, m20, m30;
float m01, m11, m21, m31;
float m02, m12, m22, m32;
float m03, m13, m23, m33;
};
vec4 rows[4];
} mat4;
mat2 mmul2(mat2 m1, mat2 m2);
mat3 mmul3(mat3 m1, mat3 m2);
mat4 mmul4(mat4 m1, mat4 m2);
mat2 transpose2(mat2 m);
mat3 transpose3(mat3 m);
mat4 transpose4(mat4 m);
vec2 vmmul2(mat2 m, vec2 v);
vec3 vmmul3(mat3 m, vec3 v);
vec4 vmmul4(mat4 m, vec4 v);
#endif
#ifndef MATRIX_TRANSFORM_H
#define MATRIX_TRANSFORM_H
// transforms matrix m with a 4x4 translation matrix with position v
mat4 m4translatev2(mat4 m, vec2 v);
mat4 m4translatev3(mat4 m, vec3 v);
mat4 m4translatev4(mat4 m, vec4 v);
// transforms matrix m with a 4x4 rotation matrix created from an angle a in radians around an axis s (unit vector)
mat4 m4rotate(mat4 m, float a, vec3 s);
// transforms a matrix m with a 4x4 scale matrix created from a set of scalars s
mat4 m4scale(mat4 m, vec3 s);
#endif
#endif
main 0 → 100755
File added
makefile 0 → 100644
CC = gcc
CFLAGS = -Wall -Wno-missing-braces -Wimplicit-fallthrough -g
SRC = ./src/* ./lib/src/gl.c ./lib/libvectors.a
OBJNAME = main
LDFLAGS = -lGL -ldl -lglfw
build:
$(CC) $(LDFLAGS) $(INCFLAGS) $(CFLAGS) $(SRC) -o $(OBJNAME)
#version 430
in vec2 pos;
out vec4 color;
uniform float time;
void main() {
color = vec4(sin(time), cos(time), 1.0f, 1.0f);
}
#version 430
struct point {
vec2 position;
float t;
uint pad;
};
layout (local_size_x = 360, local_size_y = 1, local_size_z = 1) in;
layout (std430, binding = 2) volatile buffer points_data {
point graph_points[];
};
uniform float time;
uniform float big_r;
uniform float small_r;
uniform float d;
uniform int type;
uniform float rotation_multiplier;
uniform float angle_offset;
void main() {
float angle_to_rotate = time * rotation_multiplier;
mat2 opt_rotation;
opt_rotation[0] = vec2(cos(angle_to_rotate), sin(angle_to_rotate));
opt_rotation[1] = vec2(-sin(angle_to_rotate), cos(angle_to_rotate));
float t = graph_points[gl_GlobalInvocationID.x].t + angle_offset;
vec2 result;
result.x = (big_r - (type*small_r))*cos(t) + type*d*cos(((big_r - (type*small_r))/small_r) * t);
result.y = (big_r - (type*small_r))*sin(t) - d*sin(((big_r - (type*small_r))/small_r) * t);
graph_points[gl_GlobalInvocationID.x].position = opt_rotation * result;
}
#version 430
uniform vec2 position;
out vec2 pos;
void main() {
pos = position;
gl_Position = vec4(pos, 0.0f, 1.0f);
}
#include "../inc/window_init.h"
#include "../inc/shader.h"
#include "../lib/vector_lib.h"
#include "../inc/spiro.h"
#include <unistd.h>
int main(int argc, char **argv) {
graph g = SPIROGRAPH(0.5f, 0.2f, 0.1f, 16*TAU, HYPOTROCHOID, 0.0f, 0.0f, 255, 255, 255);
GLFWwindow *window = init_window();
init_point_vertex_data(&g);
unsigned int main_shaders[2] = {
create_shader("shaders/vertex.glsl", GL_VERTEX_SHADER),
create_shader("shaders/fragment.glsl", GL_FRAGMENT_SHADER)
};
unsigned int main_shader_program = create_program(main_shaders, 2);
FILE *fp;
if (argc == 2) {
fp = fopen(argv[1], "r");
}
unsigned int ssbo_shader = create_shader("shaders/spiro.glsl", GL_COMPUTE_SHADER);
unsigned int ssbo_program = create_program(&ssbo_shader, 1);
unsigned int points_ssbo;
glGenBuffers(1, &points_ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, points_ssbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(point) * g.n_points, g.points, GL_DYNAMIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, points_ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
while (!glfwWindowShouldClose(window)) {
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
calculate_points(&g, ssbo_program, points_ssbo);
draw_spirograph_points(&g, main_shader_program);
if (argc >= 2) {
char file_data[3];
if (feof(fp) != 0) {
fseek(fp, 0, SEEK_SET);
}
fread(file_data, sizeof(char), 3, fp);
g.e_r = sinv((float)file_data[0]) * 0.5f;
g.i_r = cosv((float)file_data[1]) * 0.5f;
g.d = sinv((float)file_data[2]) * cosv((float)file_data[2]) * 0.5f;
} else {
g.e_r = sinv(glfwGetTime()*0.5f) * 0.2f;
g.i_r = cosv(glfwGetTime()*0.5f) * 0.5f;
g.d = sinv(glfwGetTime()*0.5f) * -cosv(glfwGetTime()*0.5f) * 0.2f;
//g.angle_offset = (float)-glfwGetTime();
//printf("%f\n", (float)glfwGetTime());
g.type = 2.0f * sinv(glfwGetTime()*2.71828f) - 1.0f;
}
glfwSwapBuffers(window);
glfwPollEvents();
}
if (argc >= 2) fclose(fp);
glDeleteBuffers(1, &points_ssbo);
free(g.points);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment