move from float3 to float2, and general cleanup

This commit is contained in:
2025-06-05 00:13:05 +02:00
parent a897cc7b67
commit 5ae974d84f
2 changed files with 84 additions and 13 deletions

View File

@@ -1,16 +1,14 @@
// Copyright (c) 2025 Quinn
// Licensed under the MIT Licence. See LICENSE for details
#define GLAD_GL_IMPLEMENTATION
#include "window.h"
#include <glad/gl.h>
#include <stdio.h>
#include <string.h>
#include "../error.h"
#include "../util/vec/float3.h"
// include before GLFW
#define GLAD_GL_IMPLEMENTATION
#include <glad/gl.h>
#include "../util/vec/float2.h"
// include system libraries
#include <GLFW/glfw3.h>
@@ -96,10 +94,15 @@ void window_loop(void) {
uint32_t pipe = init_pipe();
float3 vert[] = {
{-0.8F, -0.8F, +0.0F},
{+0.8F, -0.8F, +0.0F},
{+0.0F, +0.8F, +0.0F},
unsigned vertc = 6;
float2 vert[] = {
{-1, -1 },
{1, -1 },
{1, -0.9F},
{-1, -0.9F},
{1, -0.9F},
{-1, -1 },
};
uint32_t vbo; // vertex buffer object
@@ -110,10 +113,12 @@ void window_loop(void) {
GLuint vao; // vertex array object
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), NULL);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float2), NULL);
glBindVertexArray(0);
glDisableVertexAttribArray(0);
while (!glfwWindowShouldClose(win)) {
glfwWaitEvents(); // wait till an update has been given
@@ -129,8 +134,7 @@ void window_loop(void) {
glUseProgram(pipe);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDrawArrays(GL_TRIANGLES, 0, vertc);
glfwSwapBuffers(win);
}

67
src/util/vec/float2.h Normal file
View File

@@ -0,0 +1,67 @@
#pragma once
#include <math.h>
#include "../atrb.h"
// stores a 3D point using a floating-point number
struct float2 {
float x;
float y;
};
typedef struct float2 float2;
// adds one float2 to another
atrb_const static inline float2 float2_add(float2 v1, float2 v2) {
return (float2){v1.x + v2.x, v1.y + v2.y};
}
// subtracts one float2 from another
atrb_const static inline float2 float2_sub(float2 v1, float2 v2) {
return (float2){v1.x - v2.x, v1.y - v2.y};
}
// multiplies one float2 by another
atrb_const static inline float2 float2_mul(float2 v1, float2 v2) {
return (float2){v1.x * v2.x, v1.y * v2.y};
}
// divides one float2 by another
atrb_const static inline float2 float2_div(float2 v1, float2 v2) {
return (float2){v1.x / v2.x, v1.y / v2.y};
}
// preforms a scalar multiplication upon the float2 (multiplies the float2 by some value)
atrb_const static inline float2 float2_mul_s(float2 v, float n) {
return (float2){v.x * n, v.y * n};
}
// preforms a scalar division upon the float2 (divides the float2 by some value)
atrb_const static inline float2 float2_div_s(float2 v, float n) {
return (float2){v.x / n, v.y / n};
}
atrb_const static inline float2 float2_neg(float2 v) {
return (float2){-v.x, -v.y };
}
// gets the squared magnitude/length of float2
atrb_const static inline float float2_mag2(float2 v) {
return (v.x * v.x) + (v.y * v.y);
}
// gets the length of float2 (length)
atrb_const static inline float float2_mag(float2 v) {
return sqrtf(float2_mag2(v));
}
// normalizes the float2
atrb_const static inline float2 float2_norm(float2 v) {
float s = 1.0F / float2_mag(v); // get the scaling factor
return float2_mul_s(v, s); // scale the vector by the scaling factor (slightly more efficient than dividing)
}
// gets the dot product of two float2s
atrb_const static inline float float2_dot(float2 v1, float2 v2) {
return v1.x * v2.x + v1.y * v2.y;
}