From 756490a02d84288ee635a8aa33811e043ca10b44 Mon Sep 17 00:00:00 2001 From: jvech Date: Sat, 10 Jun 2023 21:13:02 -0500 Subject: add: style code improvments done --- Makefile | 4 +-- TODO | 8 ++--- src/linear.h | 2 +- src/main.c | 102 +++++++++++++++++++++++++++++++++++------------------------ src/obj.c | 10 ++++-- 5 files changed, 75 insertions(+), 51 deletions(-) diff --git a/Makefile b/Makefile index eec9109..4e2382d 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CC := cc +CC := clang CFLAGS := -Wall -pedantic -pedantic-errors -std=c99 DLIBS := -lm $(shell pkg-config --libs glfw3 opengl glew) INCLUDE := $(addprefix -I,./include) @@ -25,7 +25,7 @@ build: $(OBJS) ${CC} $^ -o ${BIN} ${DLIBS} run: - ./${BIN} + ./${BIN} models/cessna.obj install: build install -Dm 644 ${VERTEX} -t ${SHADERS_DIR} diff --git a/TODO b/TODO index d4097ca..7308b12 100644 --- a/TODO +++ b/TODO @@ -1,15 +1,15 @@ Graphics -- Load obj files +* Load obj files * Vertex coordinates, normals and textures * read faces with more than 4 vertices - - Read object materials + * Read object materials - Render a set of vertices loaded from a file * Render vertex coordinates - - Render Normal vectors + * Render Normal vectors - Render vertex Textures Linear - Implement the Inverse function of mat4 - Implement the determinant function of mat4 -- Implement Orhographic view transformation +* Implement Orhographic view transformation diff --git a/src/linear.h b/src/linear.h index deec495..3733c70 100644 --- a/src/linear.h +++ b/src/linear.h @@ -47,7 +47,7 @@ Mat4 linearMat4Mul(Mat4 x1, Mat4 x2); Mat4 linearMat4Muln(int n, ...); Mat4 linearMat4Transpose(Mat4 x); Mat4 linearMat4Inv(Mat4 x); //TODO -Mat4 linearMat4Add(Mat4 x1, Mat4 x2); //TODO +Mat4 linearMat4Add(Mat4 x1, Mat4 x2); float linearMat4Det(Mat4 x); //TODO Vec3 linearVec3(float x, float y, float z); diff --git a/src/main.c b/src/main.c index f58fa28..d0ce19b 100644 --- a/src/main.c +++ b/src/main.c @@ -42,6 +42,9 @@ struct Camera { Vec3 up; }; +static void loadCLI(int argc, char *argv[], char **vertexPath, char **fragmentPath); +static void initOpengl(void); +static void initGlfw(void); 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); @@ -55,6 +58,53 @@ static void usage(int status); static float cameraSpeed = 2.0; +void +loadCLI(int argc, char *argv[], char **vertexPath, char **fragmentPath) +{ + int opt; + while ((opt = getopt(argc, argv, "hv:f:")) != -1) { + switch (opt) { + case 'h': + usage(0); + break; + case 'v': + *vertexPath = optarg; + break; + case 'f': + *fragmentPath = optarg; + break; + default: + usage(2); + } + } + + if (optind >= argc) userError("cli Error", "expected argument after options\n"); + else if (!vertexPath) userError("environment Error", "MVERSE_VERTEX not defined"); + else if (!fragmentPath) userError("environment Error", "MVERSE_FRAGMENT not defined"); + +} + +void +initGlfw(void) +{ + const char *glfwErrno; + if (!glfwInit()) { + glfwGetError(&glfwErrno); + glfwTerminate(); + userError("glfwInit() Error", glfwErrno); + } +} + +void +initOpengl(void) +{ + GLubyte glewErrno = glewInit(); + if (glewErrno != GLEW_OK) { + glfwTerminate(); + userError("initGlfw() Error", (const char *)glewGetErrorString(glewErrno)); + } +} + void userError(const char *msg, const char *detail) { @@ -271,48 +321,20 @@ int main(int argc, char *argv[]) { Obj obj; GLFWwindow *window; - const char *glfwErrno; - - int opt; char *vertexFile, *fragmentFile; + unsigned int shader; + vertexFile = getenv("MVERSE_VERTEX"); fragmentFile = getenv("MVERSE_FRAGMENT"); - while ((opt = getopt(argc, argv, "hv:f:")) != -1) { - switch (opt) { - case 'h': - usage(0); - break; - case 'v': - vertexFile = optarg; - break; - case 'f': - fragmentFile = optarg; - break; - default: - usage(2); - } - } - - if (optind >= argc) userError("cli Error", "expected argument after options\n"); - else if (!vertexFile) userError("environment Error", "MVERSE_VERTEX not defined"); - else if (!fragmentFile) userError("environment Error", "MVERSE_FRAGMENT not defined"); - + loadCLI(argc, argv, &vertexFile, &fragmentFile); argv += optind; argc -= optind; obj = objCreate(argv[0]); - if (obj.mesh == NULL) { - glfwTerminate(); - userError("objCreateMesh Error", "NULL mesh returned"); - } - - if (!glfwInit()) { - glfwGetError(&glfwErrno); - glfwTerminate(); - userError("glfwInit() Error", glfwErrno); - } + // glfw Init + initGlfw(); window = glfwCreateWindow(640, 480, "Mverse", NULL, NULL); if (!window) { @@ -320,18 +342,14 @@ int main(int argc, char *argv[]) userError("glfwCreateWindow() Error", "Can't create window"); } + // Window Setup glfwSetFramebufferSizeCallback(window, glfw_size_callback); - glfwMakeContextCurrent(window); - glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); - GLubyte glewErrno = glewInit(); - if (glewErrno != GLEW_OK) { - glfwTerminate(); - userError("glewInit Error", (const char *)glewGetErrorString(glewErrno)); - } - - unsigned int shader = shaderCreateProgram(vertexFile, fragmentFile); + glfwMakeContextCurrent(window); + initOpengl(); + shader = shaderCreateProgram(vertexFile, fragmentFile); objSetUp(obj); diff --git a/src/obj.c b/src/obj.c index d832604..ea69f18 100644 --- a/src/obj.c +++ b/src/obj.c @@ -77,7 +77,7 @@ objCreate(const char *filename) // On error return NULL if (fi == NULL || mesh == NULL) { - fprintf(stderr, "objCreateMesh() Error: %s\n", strerror(errno)); + perror("objCreateMesh() Error"); fclose(fi); exit(1); } @@ -133,7 +133,13 @@ objCreate(const char *filename) } void -readF(char *line, Mesh *mesh, int meshIndex, struct Setv3 *v, struct Setv2 *vt, struct Setv3 *vn) +readF(char *line, + Mesh *mesh, + int meshIndex, + struct Setv3 *v, + struct Setv2 *vt, + struct Setv3 *vn + ) { Vertex vertexBuffer; struct Seti *f; -- cgit v1.2.3-70-g09d2