aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjvech <jmvalenciae@unal.edu.co>2022-08-25 21:39:11 -0500
committerjvech <jmvalenciae@unal.edu.co>2022-08-25 21:39:11 -0500
commit3b24a477cf566296aa758058aaca96cf0c117141 (patch)
treef89b1e37d47edaff6b33968d26620e51923a8722
parent3bad7358dc55a969aec23bb5a5932c072d4beedd (diff)
feat: linear library implemented
-rw-r--r--Makefile9
-rw-r--r--TODO15
-rw-r--r--shaders/fragment.vsh7
-rw-r--r--shaders/vertex.vsh8
-rw-r--r--src/linear.c243
-rw-r--r--src/linear.h38
-rw-r--r--src/main.c74
7 files changed, 356 insertions, 38 deletions
diff --git a/Makefile b/Makefile
index 5a591cc..56f81cc 100644
--- a/Makefile
+++ b/Makefile
@@ -1,12 +1,12 @@
CC := cc
CFLAGS := -Wall -Wall -pedantic -std=c11
-DLIBS := $(shell pkg-config --libs glfw3 opengl glew)
+DLIBS := -lm $(shell pkg-config --libs glfw3 opengl glew)
OBJDIR = objs
SRCDIR = src
-OBJS = $(addprefix objs/,main.o shader.o)
+OBJS = $(addprefix objs/,main.o shader.o linear.o)
BIN = mverse
-all: build
+all: build run
$(OBJS): | $(OBJDIR)
@@ -19,5 +19,8 @@ $(OBJDIR)/%.o: $(SRCDIR)/%.c
build: $(OBJS)
${CC} $^ -o ${BIN} ${DLIBS}
+run:
+ ./${BIN}
+
clean:
@rm $(OBJS) -v
diff --git a/TODO b/TODO
index e9929ad..d28e068 100644
--- a/TODO
+++ b/TODO
@@ -1,4 +1,19 @@
+General
+- Add textures
+ ? Try to implement it without external libraries
+ - Make a PNG image reader
+
+- Add 3D objects
+ - define a format for 3D objects
+ - Render a set of vertices loaded from a file
+
Learning
* Make a Hello Window
* Make a Hello Triangle
* Make a Shader Loader Library
+
+Linear
+- Implement the Inverse function of mat4
+- Implement the determinant function of mat4
+
+Shader
diff --git a/shaders/fragment.vsh b/shaders/fragment.vsh
index fcb48a4..38bc00d 100644
--- a/shaders/fragment.vsh
+++ b/shaders/fragment.vsh
@@ -1,8 +1,9 @@
-#version 330 core
-in vec4 vertexColor;
+# version 330 core
+
+in vec3 vertexColor;
out vec4 FragColor;
void main()
{
- FragColor = vertexColor;
+ FragColor = vec4(vertexColor, 1.0);
}
diff --git a/shaders/vertex.vsh b/shaders/vertex.vsh
index d195db3..220041d 100644
--- a/shaders/vertex.vsh
+++ b/shaders/vertex.vsh
@@ -1,11 +1,13 @@
# version 330 core
layout (location = 0) in vec3 aPos;
+layout (location = 1) in vec3 aColor;
+uniform mat4 trans;
-out vec4 vertexColor;
+out vec3 vertexColor;
void main()
{
- gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0f);
- vertexColor = vec4(0.2, 0.7, 0.2, 0.8);
+ gl_Position = trans * vec4(aPos, 1.0f);
+ vertexColor = aColor;
}
diff --git a/src/linear.c b/src/linear.c
index e69de29..06c2e33 100644
--- a/src/linear.c
+++ b/src/linear.c
@@ -0,0 +1,243 @@
+#include "linear.h"
+#include <math.h>
+
+mat4
+linearLookAt(vec3 position, vec3 target, vec3 world_up)
+{
+ mat4 out = linearMat4Identity();
+ mat4 translate;
+ vec3 cam_dir, cam_right, cam_up;
+ /* position - target */
+ cam_dir = linearVec3Add(position, linearVec3ScalarMulp(target, -1.0));
+ cam_dir = linearVec3Normalize(cam_dir);
+
+ cam_right = linearVec3CrossProduct(world_up, cam_dir);
+ cam_right = linearVec3Normalize(cam_right);
+
+ cam_up = linearVec3CrossProduct(cam_dir, cam_right);
+
+ int i;
+ for (i = 0; i < 3; i++) {
+ out.matrix[0][i] = cam_right.vector[i];
+ out.matrix[1][i] = cam_up.vector[i];
+ out.matrix[2][i] = cam_dir.vector[i];
+ }
+ translate = linearTranslate(-position.vector[0],
+ -position.vector[1],
+ -position.vector[2]);
+ return linearMat4Mul(out, translate);
+}
+
+mat4
+linearPerspective(float FoV, float ratio, float near, float far)
+{
+ mat4 out = linearMat4Fill(0.0);
+ float FoV_radians = FoV * M_PI / 180;
+ float width = near * tanf(FoV_radians) * ratio;
+ float height = near * tanf(FoV_radians);
+
+ out.matrix[0][0] = near / width;
+ out.matrix[1][1] = near / height;
+ out.matrix[2][2] = -(far + near) / (far - near);
+ out.matrix[2][3] = -2 * far * near / (far - near);
+ out.matrix[3][2] = -1;
+ return out;
+}
+
+mat4
+linearTranslate(float T_x, float T_y, float T_z)
+{
+ mat4 out = linearMat4Identity();
+ out.matrix[0][3] = T_x;
+ out.matrix[1][3] = T_y;
+ out.matrix[2][3] = T_z;
+ return out;
+}
+
+mat4
+linearTranslatev(vec3 T)
+{
+ return linearTranslate(T.vector[0], T.vector[1], T.vector[2]);
+}
+
+mat4
+linearScale(float S_x, float S_y, float S_z)
+{
+ mat4 out = linearMat4Identity();
+ out.matrix[0][0] = S_x;
+ out.matrix[1][1] = S_y;
+ out.matrix[2][2] = S_z;
+ return out;
+}
+
+mat4
+linearScalev(vec3 S)
+{
+ return linearScale(S.vector[0], S.vector[1], S.vector[2]);
+}
+
+mat4
+linearRotate(float degree, vec3 R_xyz)
+{
+ mat4 out = linearMat4Identity();
+ vec3 R_xyz_normalized = linearVec3Normalize(R_xyz);
+ float radians = degree * M_PI/180.0;
+ float Rx = R_xyz_normalized.vector[0];
+ float Ry = R_xyz_normalized.vector[1];
+ float Rz = R_xyz_normalized.vector[2];
+ float rcos = cosf(radians);
+ float rsin = sinf(radians);
+
+ out.matrix[0][0] = rcos + pow(Rx, 2) * ( 1 - rcos);
+ out.matrix[0][1] = Rx * Ry * (1 - rcos) - Rz * rsin;
+ out.matrix[0][2] = Rx * Rz * (1 - rcos) + Ry * rsin;
+
+ out.matrix[1][0] = Rx * Ry * (1 - rcos) + Rz * rsin;
+ out.matrix[1][1] = rcos + pow(Ry, 2) * ( 1 - rcos);
+ out.matrix[1][2] = Ry * Rz * (1 - rcos) - Rx * rsin;
+
+ out.matrix[2][0] = Rz * Rx * (1 - rcos) - Ry * rsin;
+ out.matrix[2][1] = Ry * Rz * (1 - rcos) + Rx * rsin;
+ out.matrix[2][2] = rcos + pow(Rz, 2) * ( 1 - rcos);
+ return out;
+}
+
+mat4
+linearMat4Fill(float value)
+{
+ int i, j;
+ mat4 out;
+ for (i = 0; i < 4; i++) {
+ for (j = 0; j < 4; j++) {
+ out.matrix[i][j] = value;
+ }
+ }
+ return out;
+}
+
+mat4
+linearMat4Identity()
+{
+ int i, j;
+ mat4 out;
+ for (i = 0; i < 4; i++) {
+ for (j = 0; j < 4; j++) {
+ if (i == j) out.matrix[i][j] = 1.0;
+ else out.matrix[i][j] = 0.0;
+ }
+ }
+ return out;
+}
+
+mat4
+linearMat4Transpose(mat4 x)
+{
+ mat4 out;
+ int i, j;
+ for (i = 0; i < 4; i++) {
+ for (j = 0; j < 4; j++) {
+ out.matrix[i][j] = x.matrix[j][i];
+ }
+ }
+ return out;
+}
+
+mat4
+linearMat4Mul(mat4 x1, mat4 x2)
+{
+ mat4 out;
+ int i, j, k;
+ for (i = 0; i < 4; i++) {
+ for (j = 0; j < 4; j++) {
+ out.matrix[i][j] = 0.0;
+ for (k = 0; k < 4; k++)
+ out.matrix[i][j] += x1.matrix[i][k] * x2.matrix[k][j];
+ }
+ }
+ return out;
+}
+
+mat4
+linearMat4Add(mat4 x1, mat4 x2)
+{
+ int i, j;
+ mat4 out;
+ for (i = 0; i < 4; i++) {
+ for (j = 0; j < 4; j++) {
+ out.matrix[i][j] = x1.matrix[i][j] + x2.matrix[i][j];
+ }
+ }
+ return out;
+}
+
+float
+linearMat4Det(mat4 x)
+{
+ return 0.0;
+}
+
+vec3
+linearVec3ScalarMulp(vec3 x, float scalar)
+{
+ vec3 out;
+ int i;
+ for (i = 0; i < 3; i++) {
+ out.vector[i] = scalar * x.vector[i];
+ }
+ return out;
+}
+
+vec3
+linearVec3(float x, float y, float z)
+{
+ vec3 out;
+ out.vector[0] = x;
+ out.vector[1] = y;
+ out.vector[2] = z;
+ return out;
+}
+
+vec3
+linearVec3Add(vec3 x, vec3 y)
+{
+ vec3 out;
+ int i;
+ for (i = 0; i < 3; i++) {
+ out.vector[i] = x.vector[i] + y.vector[i];
+ }
+ return out;
+}
+
+vec3
+linearVec3Normalize(vec3 x)
+{
+ vec3 out;
+ float norm = sqrtf(linearVec3DotProduct(x, x));
+ int i;
+ if (norm == 0) return x;
+ for (i = 0; i < 3; i++) {
+ out.vector[i] = x.vector[i] / norm;
+ }
+ return out;
+}
+
+vec3
+linearVec3CrossProduct(vec3 x, vec3 y)
+{
+ vec3 out;
+ out.vector[0] = x.vector[1] * y.vector[2] - x.vector[2] * y.vector[1];
+ out.vector[1] = - x.vector[0] * y.vector[2] + x.vector[2] * y.vector[0];
+ out.vector[2] = x.vector[0] * y.vector[1] - x.vector[1] * y.vector[0];
+ return out;
+}
+
+float
+linearVec3DotProduct(vec3 x, vec3 y)
+{
+ float out = 0;
+ int i;
+ for (i = 0; i < 3; i++) {
+ out += x.vector[i] * y.vector[i];
+ }
+ return out;
+}
diff --git a/src/linear.h b/src/linear.h
index e69de29..86b93d7 100644
--- a/src/linear.h
+++ b/src/linear.h
@@ -0,0 +1,38 @@
+#ifndef __LINEAR__
+#define __LINEAR__
+
+#ifndef M_PI
+#define M_PI (3.14159265358979323846)
+#endif
+
+typedef struct {
+ float matrix[4][4];
+} mat4;
+
+typedef struct {
+ float vector[3];
+} vec3;
+
+mat4 linearTranslatev(vec3 translate_vector); //TODO
+mat4 linearScalev(vec3 scale_vector); //TODO
+mat4 linearTranslate(float translate_x, float translate_y, float translate_z);
+mat4 linearScale(float scale_x, float scale_y, float scale_z);
+mat4 linearRotate(float degree, vec3 rotation_axis);
+mat4 linearPerspective(float FoV, float ratio, float near, float far);
+mat4 linearLookAt(vec3 position, vec3 target, vec3 up);
+
+mat4 linearMat4Fill(float value);
+mat4 linearMat4Identity();
+mat4 linearMat4Mul(mat4 x1, mat4 x2);
+mat4 linearMat4Transpose(mat4 x);
+mat4 linearMat4Inv(mat4 x); //TODO
+mat4 linearMat4Add(mat4 x1, mat4 x2); //TODO
+float linearMat4Det(mat4 x); //TODO
+
+vec3 linearVec3(float x, float y, float z);
+vec3 linearVec3Normalize(vec3 vector);
+vec3 linearVec3Add(vec3 vector1, vec3 vector2);
+vec3 linearVec3ScalarMulp(vec3 vector, float scalar);
+vec3 linearVec3CrossProduct(vec3 vector1, vec3 vector2);
+float linearVec3DotProduct(vec3 vector1, vec3 vector2);
+#endif
diff --git a/src/main.c b/src/main.c
index af2c65a..27181ef 100644
--- a/src/main.c
+++ b/src/main.c
@@ -2,25 +2,32 @@
#include <stdlib.h>
#include <errno.h>
#include <string.h>
+#include <signal.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
-#include "shader.h"
#include "main.h"
+#include "linear.h"
+#include "shader.h"
float vertices[] = {
- -0.5f, -0.5f, 0.0f,
- 0.5f, -0.5f, 0.0f,
- 0.0f, 0.5f, 0.0f
+ -0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f,
+ 0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f,
+ -0.5f, 0.5f, 0.0f, 1.0, 1.0f, 0.0f,
+ 0.5f, 0.5f, 0.0f, 1.0, 1.0f, 0.0f,
+};
+
+unsigned int indices[] = {
+ 0, 1, 2,
+ 2, 3, 1
};
-//static void sysError(const char *msg);
static void userError(const char *msg, const char *detail);
static void glfw_size_callback(GLFWwindow *window, int width, int height);
-static void processInput(GLFWwindow *window, struct Background *c);
+static void processInput(GLFWwindow *window);
-void
+void
userError(const char *msg, const char *detail)
{
fprintf(stderr, "%s: %s\n", msg, detail);
@@ -33,19 +40,19 @@ glfw_size_callback(GLFWwindow *window, int width, int height)
glViewport(0, 0, width, height);
}
-static void
-processInput(GLFWwindow *window, struct Background *c)
+void
+processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS
- || glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) {
+ || glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
glfwSetWindowShouldClose(window, 1);
- } else if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS
- && glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS
- && c->R >= 0.0) {
- c->R -= 0.1;
- } else if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS && c->R <= 1.0) {
- c->R += 0.1;
- }
+
+ if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS)
+ glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
+ if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS)
+ glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+ if (glfwGetKey(window, GLFW_KEY_3) == GLFW_PRESS)
+ glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
}
int main()
@@ -74,42 +81,51 @@ int main()
userError("glewInit() failed", (const char *)glewGetErrorString(glewErrno));
}
- struct Background colors = {.R = 0.0, .G = 0.0, .B = 0.0, .A = 0.0};
-
- unsigned int VBO, VAO;
+ unsigned int VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
- glBindVertexArray(VAO);
-
glGenBuffers(1, &VBO);
+ glGenBuffers(1, &EBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
+
+ glBindVertexArray(VAO);
+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)0);
glEnableVertexAttribArray(0);
+ glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)(3 * sizeof(float)));
+ glEnableVertexAttribArray(1);
+
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
unsigned int shaderProgram = shaderCreateProgram("shaders/vertex.vsh",
"shaders/fragment.vsh");
+ unsigned int transformLoc;
+ mat4 transform;
+
while (!glfwWindowShouldClose(window)) {
- processInput(window, &colors);
+ processInput(window);
- glClearColor(colors.R, colors.G, colors.B, colors.A);
+ glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
- glDrawArrays(GL_TRIANGLES, 0, 3);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
+
+ transformLoc = glGetUniformLocation(shaderProgram, "trans");
+ transform = linearRotate(45.0, linearVec3(0, 0, 1));
+ glUniformMatrix4fv(transformLoc, 1, GL_FALSE, transform.matrix[0]);
+ glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);
glfwPollEvents();
}
- int nattributes;
- glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &nattributes);
- printf("%d\n", nattributes);
glfwTerminate();
return 0;
}
Feel free to download, copy and edit any repo