Changes to genesis

root
root
3 months ago
Updated readme.scroll
readme.scroll
Changed around line 1
- viewSourceButton
- https://github.com/twny/genesis
+ viewSourceButton https://github.com/twny/genesis
root
root
3 months ago
Updated readme.scroll
readme.scroll
Changed around line 1
- viewSourceButton https://github.com/twny/genesis
+ viewSourceButton
+ https://github.com/twny/genesis
root
root
3 months ago
Updated readme.scroll
readme.scroll
Changed around line 1
- viewSourceBaseUrl https://github.com/twny/genesis
- viewSourceButton
+ viewSourceButton https://github.com/twny/genesis
root
root
3 months ago
Updated readme.scroll
readme.scroll
Changed around line 1
+ viewSourceBaseUrl https://github.com/twny/genesis
+ viewSourceButton
root
root
3 months ago
Updated readme.scroll
readme.scroll
Changed around line 10: screenshot.png
+
+ center
+
- Go from OpenGL to Vulcan.
- Support other platforms besides MacOS.
root
root
3 months ago
Updated readme.scroll
readme.scroll
Changed around line 10: screenshot.png
-
- Go from OpenGL to Vulcan.
- Support other platforms besides MacOS.
root
root
3 months ago
Updated readme.scroll
readme.scroll
Changed around line 11: screenshot.png
- Currently MacOS only.
-
- Go from OpenGL to Vulcan.
+ - Go from OpenGL to Vulcan.
+ - Support other platforms besides MacOS.
root
root
3 months ago
Updated readme.scroll
readme.scroll
Changed around line 8: center
-
root
root
3 months ago
Updated readme.scroll
readme.scroll
Changed around line 6: theme dark
-
root
root
3 months ago
Updated readme.scroll
readme.scroll
Changed around line 3: permalink index.html
+ center
+ A game engine in c.
- A game engine in c.
-
root
root
3 months ago
Updated readme.scroll
readme.scroll
Changed around line 6: theme dark
+ width 400px
root
root
3 months ago
Updated readme.scroll
readme.scroll
Changed around line 1
+ permalink index.html
+
Breck Yunits
Breck Yunits
3 months ago
readme.scroll
Changed around line 3: theme dark
+ screenshot.png
+
screenshot.png
Breck Yunits
Breck Yunits
3 months ago
readme.scroll
Changed around line 1
+ buildHtml
+ theme dark
+
Breck Yunits
Breck Yunits
3 months ago
README.md
Changed around line 0
- # Gensis
readme.scroll
Changed around line 1
+ # Genesis
+
+ A game engine in c.
+
+ Built in Public on Twitter.
+ https://x.com/tawnniee/status/1840521375616016662
+
+ Currently MacOS only.
+
+ # Roadmap
+ Go from OpenGL to Vulcan.
Breck Yunits
Breck Yunits
3 months ago
Makefile
Changed around line 17: mac_deps:
- mac_deps:
- @echo "Installing dependencies for macos environment"
- brew install glew glfw cglm
-
twny
twny
3 months ago
Update Makefile. gitignore main
Angel Leon
Angel Leon
3 months ago
Refactor input handling: Implement structured key mappings for dot and camera movement - Introduced `DotKeyMapping` and `CameraKeyMapping` structs to map keys to their respective actions. - Added `handleDotMovement` and `handleCameraMovement` functions to streamline input processing. - Replaced repetitive `if` statements in `processInput` with loop-based handlers for cleaner code. - Implemented camera movement action functions (`moveCameraForward`, `moveCameraBackward`, `moveCameraLeft`, `moveCameraRight`) for modular control. - Enhanced code maintainability and scalability, simplifying future additions of new input controls. - Improved readability by organizing input logic into clearly defined structures and functions.
main.c
Changed around line 41: vec3 cameraUp = {0.0f, 1.0f, 0.0f};
+ // Key mapping for dot movement
+ typedef struct {
+ int key;
+ float *axis;
+ float direction;
+ } DotKeyMapping;
+
+ // Key to function map for camera movement
+ typedef struct {
+ int key;
+ void (*action)(float cameraSpeed);
+ } CameraKeyMapping;
+
+ // Camera movement action functions
+ void moveCameraForward(float cameraSpeed);
+ void moveCameraBackward(float cameraSpeed);
+ void moveCameraLeft(float cameraSpeed);
+ void moveCameraRight(float cameraSpeed);
+
+ void handleDotMovement(GLFWwindow *window, float moveSpeed, int *movement) {
+ //Easier to add other mappings
+ DotKeyMapping dotMappings[] = {
+ {GLFW_KEY_W, &dotZ, -1.0f},
+ {GLFW_KEY_S, &dotZ, 1.0f},
+ {GLFW_KEY_A, &dotX, -1.0f},
+ {GLFW_KEY_D, &dotX, 1.0f},
+ {GLFW_KEY_Q, &dotY, 1.0f},
+ {GLFW_KEY_E, &dotY, -1.0f},
+ };
+
+ const int numDotMappings = sizeof(dotMappings) / sizeof(dotMappings[0]);
+
+ for (int i = 0; i < numDotMappings; ++i) {
+ if (glfwGetKey(window, dotMappings[i].key) == GLFW_PRESS) {
+ *(dotMappings[i].axis) += dotMappings[i].direction * moveSpeed;
+ *movement = 1;
+ }
+ }
+ }
+
+ void handleCameraMovement(GLFWwindow *window, float cameraSpeed) {
+ CameraKeyMapping cameraMappings[] = {
+ {GLFW_KEY_UP, moveCameraForward},
+ {GLFW_KEY_DOWN, moveCameraBackward},
+ {GLFW_KEY_LEFT, moveCameraLeft},
+ {GLFW_KEY_RIGHT, moveCameraRight},
+ };
+
+ const int numCameraMappings = sizeof(cameraMappings) / sizeof(cameraMappings[0]);
+
+ for (int i = 0; i < numCameraMappings; ++i) {
+ if (glfwGetKey(window, cameraMappings[i].key) == GLFW_PRESS) {
+ cameraMappings[i].action(cameraSpeed);
+ }
+ }
+ }
+
+ // Camera movement action implementations
+ void moveCameraForward(float cameraSpeed) {
+ cameraPos[0] += cameraFront[0] * cameraSpeed;
+ cameraPos[1] += cameraFront[1] * cameraSpeed;
+ cameraPos[2] += cameraFront[2] * cameraSpeed;
+ }
+
+ void moveCameraBackward(float cameraSpeed) {
+ cameraPos[0] -= cameraFront[0] * cameraSpeed;
+ cameraPos[1] -= cameraFront[1] * cameraSpeed;
+ cameraPos[2] -= cameraFront[2] * cameraSpeed;
+ }
+
+ void moveCameraLeft(float cameraSpeed) {
+ vec3 right;
+ glm_cross(cameraFront, cameraUp, right);
+ glm_normalize(right);
+ cameraPos[0] -= right[0] * cameraSpeed;
+ cameraPos[1] -= right[1] * cameraSpeed;
+ cameraPos[2] -= right[2] * cameraSpeed;
+ }
+
+ void moveCameraRight(float cameraSpeed) {
+ vec3 right;
+ glm_cross(cameraFront, cameraUp, right);
+ glm_normalize(right);
+ cameraPos[0] += right[0] * cameraSpeed;
+ cameraPos[1] += right[1] * cameraSpeed;
+ cameraPos[2] += right[2] * cameraSpeed;
+ }
+
Changed around line 141: void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
+ const float cameraSpeed = 0.1f;
- // Dot movement
- if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
- dotZ -= moveSpeed;
- movement = 1;
- }
- if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
- dotZ += moveSpeed;
- movement = 1;
- }
- if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
- dotX -= moveSpeed;
- movement = 1;
- }
- if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
- dotX += moveSpeed;
- movement = 1;
- }
- if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) {
- dotY += moveSpeed;
- movement = 1;
- }
- if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) {
- dotY -= moveSpeed;
- movement = 1;
- }
+ handleDotMovement(window, moveSpeed, &movement);
+
- const float cameraSpeed = 0.1f;
- if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
- cameraPos[0] += cameraFront[0] * cameraSpeed;
- cameraPos[1] += cameraFront[1] * cameraSpeed;
- cameraPos[2] += cameraFront[2] * cameraSpeed;
- }
- if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
- cameraPos[0] -= cameraFront[0] * cameraSpeed;
- cameraPos[1] -= cameraFront[1] * cameraSpeed;
- cameraPos[2] -= cameraFront[2] * cameraSpeed;
- }
- if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
- vec3 right;
- glm_cross(cameraFront, cameraUp, right);
- glm_normalize(right);
- cameraPos[0] -= right[0] * cameraSpeed;
- cameraPos[1] -= right[1] * cameraSpeed;
- cameraPos[2] -= right[2] * cameraSpeed;
- }
- if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
- vec3 right;
- glm_cross(cameraFront, cameraUp, right);
- glm_normalize(right);
- cameraPos[0] += right[0] * cameraSpeed;
- cameraPos[1] += right[1] * cameraSpeed;
- cameraPos[2] += right[2] * cameraSpeed;
- }
+ handleCameraMovement(window, cameraSpeed);
Angel Leon
Angel Leon
3 months ago
make mac_deps new Makefile target to install dependencies needed to build on macos, assumes user has brew
Makefile
Changed around line 13: all: main
+ mac_deps:
+ @echo "Installing dependencies for macos environment"
+ brew install glew glfw cglm
+
Angel Leon
Angel Leon
3 months ago
might as well get started with c17, it's 2024
Makefile
Changed around line 1
- CFLAGS = -Wall -std=c11
+ CFLAGS = -Wall -std=c17
CoderJustin8925
CoderJustin8925
3 months ago
Update main.c i'm an awesome programmer, and that's a fact!!
main.c
Changed around line 1
- // main.c
+ // main.c i=i+1 C baby C!!!
Angel Leon
Angel Leon
3 months ago
delete binary from repo, it's sketchy (security wise)
main
twny
twny
3 months ago
genesis
main
main.c
Changed around line 160: int main() {
- GLFWwindow* window = glfwCreateWindow(800, 600, "Simple 3D Engine", NULL, NULL);
+ GLFWwindow* window = glfwCreateWindow(800, 600, "Genesis", NULL, NULL);
twny
twny
3 months ago
Merge pull request #8 from gubatron/refactor/input-handling-structured-key-mappings Refactor input handling: structured key mappings for dot andd camera movement
twny
twny
3 months ago
Merge pull request #7 from gubatron/mac-deps make mac_deps
twny
twny
3 months ago
Merge pull request #6 from gubatron/might-as-well-have-c17-its-2024 might as well get started with c17, it's 2024
twny
twny
3 months ago
Merge pull request #1 from CoderJustin8925/patch-1 Update main.c pullllll
twny
twny
3 months ago
Merge pull request #5 from gubatron/no-binaries-in-repo-its-sketchy No binaries in repo its sketchy
Angel Leon
Angel Leon
3 months ago
Refactor input handling: Implement structured key mappings for dot and camera movement - Introduced `DotKeyMapping` and `CameraKeyMapping` structs to map keys to their respective actions. - Added `handleDotMovement` and `handleCameraMovement` functions to streamline input processing. - Replaced repetitive `if` statements in `processInput` with loop-based handlers for cleaner code. - Implemented camera movement action functions (`moveCameraForward`, `moveCameraBackward`, `moveCameraLeft`, `moveCameraRight`) for modular control. - Enhanced code maintainability and scalability, simplifying future additions of new input controls. - Improved readability by organizing input logic into clearly defined structures and functions.
main.c
Changed around line 41: vec3 cameraUp = {0.0f, 1.0f, 0.0f};
+ // Key mapping for dot movement
+ typedef struct {
+ int key;
+ float *axis;
+ float direction;
+ } DotKeyMapping;
+
+ // Key to function map for camera movement
+ typedef struct {
+ int key;
+ void (*action)(float cameraSpeed);
+ } CameraKeyMapping;
+
+ // Camera movement action functions
+ void moveCameraForward(float cameraSpeed);
+ void moveCameraBackward(float cameraSpeed);
+ void moveCameraLeft(float cameraSpeed);
+ void moveCameraRight(float cameraSpeed);
+
+ void handleDotMovement(GLFWwindow *window, float moveSpeed, int *movement) {
+ //Easier to add other mappings
+ DotKeyMapping dotMappings[] = {
+ {GLFW_KEY_W, &dotZ, -1.0f},
+ {GLFW_KEY_S, &dotZ, 1.0f},
+ {GLFW_KEY_A, &dotX, -1.0f},
+ {GLFW_KEY_D, &dotX, 1.0f},
+ {GLFW_KEY_Q, &dotY, 1.0f},
+ {GLFW_KEY_E, &dotY, -1.0f},
+ };
+
+ const int numDotMappings = sizeof(dotMappings) / sizeof(dotMappings[0]);
+
+ for (int i = 0; i < numDotMappings; ++i) {
+ if (glfwGetKey(window, dotMappings[i].key) == GLFW_PRESS) {
+ *(dotMappings[i].axis) += dotMappings[i].direction * moveSpeed;
+ *movement = 1;
+ }
+ }
+ }
+
+ void handleCameraMovement(GLFWwindow *window, float cameraSpeed) {
+ CameraKeyMapping cameraMappings[] = {
+ {GLFW_KEY_UP, moveCameraForward},
+ {GLFW_KEY_DOWN, moveCameraBackward},
+ {GLFW_KEY_LEFT, moveCameraLeft},
+ {GLFW_KEY_RIGHT, moveCameraRight},
+ };
+
+ const int numCameraMappings = sizeof(cameraMappings) / sizeof(cameraMappings[0]);
+
+ for (int i = 0; i < numCameraMappings; ++i) {
+ if (glfwGetKey(window, cameraMappings[i].key) == GLFW_PRESS) {
+ cameraMappings[i].action(cameraSpeed);
+ }
+ }
+ }
+
+ // Camera movement action implementations
+ void moveCameraForward(float cameraSpeed) {
+ cameraPos[0] += cameraFront[0] * cameraSpeed;
+ cameraPos[1] += cameraFront[1] * cameraSpeed;
+ cameraPos[2] += cameraFront[2] * cameraSpeed;
+ }
+
+ void moveCameraBackward(float cameraSpeed) {
+ cameraPos[0] -= cameraFront[0] * cameraSpeed;
+ cameraPos[1] -= cameraFront[1] * cameraSpeed;
+ cameraPos[2] -= cameraFront[2] * cameraSpeed;
+ }
+
+ void moveCameraLeft(float cameraSpeed) {
+ vec3 right;
+ glm_cross(cameraFront, cameraUp, right);
+ glm_normalize(right);
+ cameraPos[0] -= right[0] * cameraSpeed;
+ cameraPos[1] -= right[1] * cameraSpeed;
+ cameraPos[2] -= right[2] * cameraSpeed;
+ }
+
+ void moveCameraRight(float cameraSpeed) {
+ vec3 right;
+ glm_cross(cameraFront, cameraUp, right);
+ glm_normalize(right);
+ cameraPos[0] += right[0] * cameraSpeed;
+ cameraPos[1] += right[1] * cameraSpeed;
+ cameraPos[2] += right[2] * cameraSpeed;
+ }
+
Changed around line 141: void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
+ const float cameraSpeed = 0.1f;
- // Dot movement
- if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
- dotZ -= moveSpeed;
- movement = 1;
- }
- if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
- dotZ += moveSpeed;
- movement = 1;
- }
- if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
- dotX -= moveSpeed;
- movement = 1;
- }
- if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
- dotX += moveSpeed;
- movement = 1;
- }
- if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) {
- dotY += moveSpeed;
- movement = 1;
- }
- if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) {
- dotY -= moveSpeed;
- movement = 1;
- }
+ handleDotMovement(window, moveSpeed, &movement);
+
- const float cameraSpeed = 0.1f;
- if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
- cameraPos[0] += cameraFront[0] * cameraSpeed;
- cameraPos[1] += cameraFront[1] * cameraSpeed;
- cameraPos[2] += cameraFront[2] * cameraSpeed;
- }
- if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
- cameraPos[0] -= cameraFront[0] * cameraSpeed;
- cameraPos[1] -= cameraFront[1] * cameraSpeed;
- cameraPos[2] -= cameraFront[2] * cameraSpeed;
- }
- if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
- vec3 right;
- glm_cross(cameraFront, cameraUp, right);
- glm_normalize(right);
- cameraPos[0] -= right[0] * cameraSpeed;
- cameraPos[1] -= right[1] * cameraSpeed;
- cameraPos[2] -= right[2] * cameraSpeed;
- }
- if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
- vec3 right;
- glm_cross(cameraFront, cameraUp, right);
- glm_normalize(right);
- cameraPos[0] += right[0] * cameraSpeed;
- cameraPos[1] += right[1] * cameraSpeed;
- cameraPos[2] += right[2] * cameraSpeed;
- }
+ handleCameraMovement(window, cameraSpeed);
Angel Leon
Angel Leon
3 months ago
make mac_deps new Makefile target to install dependencies needed to build on macos, assumes user has brew
Makefile
Changed around line 13: all: main
+ mac_deps:
+ @echo "Installing dependencies for macos environment"
+ brew install glew glfw cglm
+
Angel Leon
Angel Leon
3 months ago
might as well get started with c17, it's 2024
Makefile
Changed around line 1
- CFLAGS = -Wall -std=c11
+ CFLAGS = -Wall -std=c17
Angel Leon
Angel Leon
3 months ago
add .gitignore
.gitignore
Changed around line 1
+ main
Angel Leon
Angel Leon
3 months ago
delete binary from repo, it's sketchy (security wise)
main
Tai Groot
Tai Groot
3 months ago
Merge branch 'main' into patch-1
Tai Groot
Tai Groot
3 months ago
ignore main
.gitignore
Changed around line 1
+ main
twny
twny
3 months ago
genesis
main
main.c
Changed around line 160: int main() {
- GLFWwindow* window = glfwCreateWindow(800, 600, "Simple 3D Engine", NULL, NULL);
+ GLFWwindow* window = glfwCreateWindow(800, 600, "Genesis", NULL, NULL);
Tai Groot
Tai Groot
3 months ago
Update Makefile
Makefile
Changed around line 2: CC = gcc
- INCLUDE = -I/opt/homebrew/include
+ INCLUDE = /opt/homebrew/include
- LIBPATH = -L/opt/homebrew/lib
+ LIBPATH = /opt/homebrew/lib
- $(CC) $(CFLAGS) main.c -o main $(INCLUDE) $(LIBPATH) $(LIBS)
+ $(CC) $(CFLAGS) main.c -o main -I$(INCLUDE) -L$(LIBPATH) $(LIBS)
CoderJustin8925
CoderJustin8925
3 months ago
Update main.c i'm an awesome programmer, and that's a fact!!
main.c
Changed around line 1
- // main.c
+ // main.c i=i+1 C baby C!!!
twny
twny
3 months ago
init
README.md
Changed around line 1
+ # Gensis
twny
twny
3 months ago
init
Makefile
Changed around line 1
+ CC = gcc
+ CFLAGS = -Wall -std=c11
+
+ # Include paths
+ INCLUDE = -I/opt/homebrew/include
+
+ # Library paths and libraries
+ LIBPATH = -L/opt/homebrew/lib
+ LIBS = -lglfw -lGLEW -lcglm -framework OpenGL -framework Cocoa -framework IOKit
+
+ all: main
+
+ main: main.c
+ $(CC) $(CFLAGS) main.c -o main $(INCLUDE) $(LIBPATH) $(LIBS)
+
+ clean:
+ rm -f main
main.c
Changed around line 1
+ // main.c
+
+ #include
+ #include
+ #include
+
+ #include
+ #include
+ #include
+
+ // Vertex shader source code
+ const char* vertexShaderSource = "#version 120\n"
+ "attribute vec3 position;\n"
+ "attribute vec3 color;\n"
+ "varying vec3 fragColor;\n"
+ "uniform mat4 projection;\n"
+ "uniform mat4 view;\n"
+ "uniform mat4 model;\n"
+ "void main() {\n"
+ " gl_Position = projection * view * model * vec4(position, 1.0);\n"
+ " fragColor = color;\n"
+ "}\0";
+
+ // Fragment shader source code
+ const char* fragmentShaderSource = "#version 120\n"
+ "varying vec3 fragColor;\n"
+ "void main() {\n"
+ " gl_FragColor = vec4(fragColor, 1.0);\n"
+ "}\n\0";
+
+ // Dot position
+ float dotX = 0.0f;
+ float dotY = 0.0f;
+ float dotZ = 0.0f;
+
+ // Camera variables
+ vec3 cameraPos = {0.0f, 5.0f, 10.0f};
+ vec3 cameraFront = {0.0f, -0.5f, -1.0f};
+ vec3 cameraUp = {0.0f, 1.0f, 0.0f};
+
+ // Projection matrix
+ mat4 projection;
+
+ // Callback for window resizing
+ void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
+ // Adjust the viewport
+ glViewport(0, 0, width, height);
+
+ // Recalculate the projection matrix
+ float ratio = (float)width / (float)height;
+ glm_perspective(glm_rad(45.0f), ratio, 0.1f, 100.0f, projection);
+ }
+
+ void processInput(GLFWwindow *window) {
+ const float moveSpeed = 0.1f;
+ int movement = 0;
+
+ // Dot movement
+ if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
+ dotZ -= moveSpeed;
+ movement = 1;
+ }
+ if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
+ dotZ += moveSpeed;
+ movement = 1;
+ }
+ if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
+ dotX -= moveSpeed;
+ movement = 1;
+ }
+ if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
+ dotX += moveSpeed;
+ movement = 1;
+ }
+ if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) {
+ dotY += moveSpeed;
+ movement = 1;
+ }
+ if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) {
+ dotY -= moveSpeed;
+ movement = 1;
+ }
+
+ // Print coordinates when they change
+ if (movement) {
+ printf("Dot Position: X=%.2f, Y=%.2f, Z=%.2f\n", dotX, dotY, dotZ);
+ }
+
+ // Camera movement
+ const float cameraSpeed = 0.1f;
+ if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
+ cameraPos[0] += cameraFront[0] * cameraSpeed;
+ cameraPos[1] += cameraFront[1] * cameraSpeed;
+ cameraPos[2] += cameraFront[2] * cameraSpeed;
+ }
+ if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
+ cameraPos[0] -= cameraFront[0] * cameraSpeed;
+ cameraPos[1] -= cameraFront[1] * cameraSpeed;
+ cameraPos[2] -= cameraFront[2] * cameraSpeed;
+ }
+ if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
+ vec3 right;
+ glm_cross(cameraFront, cameraUp, right);
+ glm_normalize(right);
+ cameraPos[0] -= right[0] * cameraSpeed;
+ cameraPos[1] -= right[1] * cameraSpeed;
+ cameraPos[2] -= right[2] * cameraSpeed;
+ }
+ if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
+ vec3 right;
+ glm_cross(cameraFront, cameraUp, right);
+ glm_normalize(right);
+ cameraPos[0] += right[0] * cameraSpeed;
+ cameraPos[1] += right[1] * cameraSpeed;
+ cameraPos[2] += right[2] * cameraSpeed;
+ }
+ }
+
+ void generateGrid(float size, int divisions, GLfloat** vertices, int* vertexCount) {
+ int lineCount = (divisions + 1) * 4;
+ *vertexCount = lineCount * 3 * 2; // 3 components per vertex, 2 vertices per line
+ *vertices = malloc((*vertexCount) * sizeof(GLfloat));
+
+ float halfSize = size / 2.0f;
+ float divisionSize = size / divisions;
+ int index = 0;
+
+ for (int i = 0; i <= divisions; ++i) {
+ float coord = -halfSize + i * divisionSize;
+
+ // Lines parallel to X-axis (along Z)
+ (*vertices)[index++] = -halfSize; // x1
+ (*vertices)[index++] = 0.0f; // y1
+ (*vertices)[index++] = coord; // z1
+
+ (*vertices)[index++] = halfSize; // x2
+ (*vertices)[index++] = 0.0f; // y2
+ (*vertices)[index++] = coord; // z2
+
+ // Lines parallel to Z-axis (along X)
+ (*vertices)[index++] = coord; // x1
+ (*vertices)[index++] = 0.0f; // y1
+ (*vertices)[index++] = -halfSize; // z1
+
+ (*vertices)[index++] = coord; // x2
+ (*vertices)[index++] = 0.0f; // y2
+ (*vertices)[index++] = halfSize; // z2
+ }
+ }
+
+ int main() {
+ // Initialize GLFW
+ if (!glfwInit()) {
+ fprintf(stderr, "Error initializing GLFW\n");
+ return -1;
+ }
+
+ // Set OpenGL version (legacy for simplicity)
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
+
+ // Create a windowed mode window and its OpenGL context
+ GLFWwindow* window = glfwCreateWindow(800, 600, "Simple 3D Engine", NULL, NULL);
+ if (!window) {
+ fprintf(stderr, "Error creating window\n");
+ glfwTerminate();
+ return -1;
+ }
+
+ // Make the window's context current
+ glfwMakeContextCurrent(window);
+
+ // Set the framebuffer size callback
+ glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
+
+ // Initialize GLEW (needs to be done after making context current)
+ glewExperimental = GL_TRUE;
+ GLenum err = glewInit();
+ if (GLEW_OK != err) {
+ fprintf(stderr, "Error initializing GLEW: %s\n", glewGetErrorString(err));
+ return -1;
+ }
+
+ // Compile shaders
+ GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
+ glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
+ glCompileShader(vertexShader);
+
+ // Check vertex shader
+ GLint success;
+ GLchar infoLog[512];
+ glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
+ if (!success) {
+ glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
+ fprintf(stderr, "Vertex Shader Compilation Failed. InfoLog:\n%s\n", infoLog);
+ }
+
+ GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
+ glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
+ glCompileShader(fragmentShader);
+
+ // Check fragment shader
+ glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
+ if (!success) {
+ glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
+ fprintf(stderr, "Fragment Shader Compilation Failed. InfoLog:\n%s\n", infoLog);
+ }
+
+ // Link shaders to create a shader program
+ GLuint shaderProgram = glCreateProgram();
+ glAttachShader(shaderProgram, vertexShader);
+ glAttachShader(shaderProgram, fragmentShader);
+ glBindAttribLocation(shaderProgram, 0, "position");
+ glBindAttribLocation(shaderProgram, 1, "color");
+ glLinkProgram(shaderProgram);
+
+ // Check shader program
+ glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
+ if (!success) {
+ glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
+ fprintf(stderr, "Shader Program Linking Failed. InfoLog:\n%s\n", infoLog);
+ }
+
+ glDeleteShader(vertexShader);
+ glDeleteShader(fragmentShader);
+
+ // Get uniform locations
+ GLint projLoc = glGetUniformLocation(shaderProgram, "projection");
+ GLint viewLoc = glGetUniformLocation(shaderProgram, "view");
+ GLint modelLoc = glGetUniformLocation(shaderProgram, "model");
+
+ // Define the dot (as a point)
+ GLfloat dotVertices[] = {
+ // Positions // Colors
+ 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f // Red color
+ };
+
+ // Generate VAO and VBO for the dot
+ GLuint dotVAO, dotVBO;
+ glGenVertexArrays(1, &dotVAO);
+ glGenBuffers(1, &dotVBO);
+
+ // Bind VAO for the dot
+ glBindVertexArray(dotVAO);
+
+ // Bind VBO and upload data
+ glBindBuffer(GL_ARRAY_BUFFER, dotVBO);
+ glBufferData(GL_ARRAY_BUFFER, sizeof(dotVertices), dotVertices, GL_STATIC_DRAW);
+
+ // Position attribute
+ GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
+ glEnableVertexAttribArray(posAttrib);
+ glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
+
+ // Color attribute
+ GLint colAttrib = glGetAttribLocation(shaderProgram, "color");
+ glEnableVertexAttribArray(colAttrib);
+ glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
+
+ // Unbind VAO
+ glBindVertexArray(0);
+
+ // Define axes vertices
+ GLfloat axesVertices[] = {
+ // Positions // Colors
+ // X-axis (Red)
+ -10.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
+ 10.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
+
+ // Y-axis (Green)
+ 0.0f, -10.0f, 0.0f, 0.0f, 1.0f, 0.0f,
+ 0.0f, 10.0f, 0.0f, 0.0f, 1.0f, 0.0f,
+
+ // Z-axis (Blue)
+ 0.0f, 0.0f, -10.0f, 0.0f, 0.0f, 1.0f,
+ 0.0f, 0.0f, 10.0f, 0.0f, 0.0f, 1.0f,
+ };
+
+ // Generate VAO and VBO for axes
+ GLuint axesVAO, axesVBO;
+ glGenVertexArrays(1, &axesVAO);
+ glGenBuffers(1, &axesVBO);
+
+ glBindVertexArray(axesVAO);
+
+ glBindBuffer(GL_ARRAY_BUFFER, axesVBO);
+ glBufferData(GL_ARRAY_BUFFER, sizeof(axesVertices), axesVertices, GL_STATIC_DRAW);
+
+ // Position attribute
+ glEnableVertexAttribArray(posAttrib);
+ glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
+
+ // Color attribute
+ glEnableVertexAttribArray(colAttrib);
+ glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
+
+ glBindVertexArray(0);
+
+ // Generate grid vertices
+ GLfloat* gridVertices;
+ int gridVertexCount;
+ generateGrid(20.0f, 20, &gridVertices, &gridVertexCount);
+
+ // Set grid color (light grey)
+ int gridVertexCountColors = gridVertexCount / 3 * 3;
+ GLfloat* gridColors = malloc(gridVertexCountColors * sizeof(GLfloat));
+ for (int i = 0; i < gridVertexCount / 3; ++i) {
+ gridColors[i * 3 + 0] = 0.7f;
+ gridColors[i * 3 + 1] = 0.7f;
+ gridColors[i * 3 + 2] = 0.7f;
+ }
+
+ // Combine positions and colors
+ int gridTotalVertices = gridVertexCount / 3;
+ int gridTotalSize = gridTotalVertices * 6 * sizeof(GLfloat);
+ GLfloat* gridData = malloc(gridTotalSize);
+ for (int i = 0; i < gridTotalVertices; ++i) {
+ gridData[i * 6 + 0] = gridVertices[i * 3 + 0];
+ gridData[i * 6 + 1] = gridVertices[i * 3 + 1];
+ gridData[i * 6 + 2] = gridVertices[i * 3 + 2];
+ gridData[i * 6 + 3] = gridColors[i * 3 + 0];
+ gridData[i * 6 + 4] = gridColors[i * 3 + 1];
+ gridData[i * 6 + 5] = gridColors[i * 3 + 2];
+ }
+
+ GLuint gridVAO, gridVBO;
+ glGenVertexArrays(1, &gridVAO);
+ glGenBuffers(1, &gridVBO);
+
+ glBindVertexArray(gridVAO);
+
+ glBindBuffer(GL_ARRAY_BUFFER, gridVBO);
+ glBufferData(GL_ARRAY_BUFFER, gridTotalSize, gridData, GL_STATIC_DRAW);
+
+ // Position attribute
+ glEnableVertexAttribArray(posAttrib);
+ glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
+
+ // Color attribute
+ glEnableVertexAttribArray(colAttrib);
+ glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
+
+ glBindVertexArray(0);
+
+ // Clean up temporary buffers
+ free(gridVertices);
+ free(gridColors);
+ free(gridData);
+
+ // Initialize projection matrix
+ int width, height;
+ glfwGetFramebufferSize(window, &width, &height);
+ float ratio = (float)width / (float)height;
+ glm_perspective(glm_rad(45.0f), ratio, 0.1f, 100.0f, projection);
+
+ // Enable depth test
+ glEnable(GL_DEPTH_TEST);
+
+ // Set point size
+ glPointSize(10.0f);
+
+ // Main loop
+ while (!glfwWindowShouldClose(window)) {
+ // Process input
+ processInput(window);
+
+ // Clear buffers
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+ // View matrix
+ mat4 view;
+ glm_lookat(cameraPos, (vec3){cameraPos[0] + cameraFront[0], cameraPos[1] + cameraFront[1], cameraPos[2] + cameraFront[2]}, cameraUp, view);
+
+ // Use shader program
+ glUseProgram(shaderProgram);
+
+ // Set projection and view matrices
+ glUniformMatrix4fv(projLoc, 1, GL_FALSE, (const GLfloat*)projection);
+ glUniformMatrix4fv(viewLoc, 1, GL_FALSE, (const GLfloat*)view);
+
+ // Draw axes
+ mat4 model;
+ glm_mat4_identity(model);
+ glUniformMatrix4fv(modelLoc, 1, GL_FALSE, (const GLfloat*)model);
+
+ glBindVertexArray(axesVAO);
+ glDrawArrays(GL_LINES, 0, 6);
+ glBindVertexArray(0);
+
+ // Draw grid
+ glBindVertexArray(gridVAO);
+ glDrawArrays(GL_LINES, 0, gridVertexCount / 3);
+ glBindVertexArray(0);
+
+ // Draw dot
+ glm_mat4_identity(model);
+ glm_translate(model, (vec3){dotX, dotY, dotZ});
+ glUniformMatrix4fv(modelLoc, 1, GL_FALSE, (const GLfloat*)model);
+
+ glBindVertexArray(dotVAO);
+ glDrawArrays(GL_POINTS, 0, 1);
+ glBindVertexArray(0);
+
+ // Swap buffers and poll events
+ glfwSwapBuffers(window);
+ glfwPollEvents();
+ }
+
+ // Clean up
+ glDeleteVertexArrays(1, &dotVAO);
+ glDeleteBuffers(1, &dotVBO);
+ glDeleteVertexArrays(1, &axesVAO);
+ glDeleteBuffers(1, &axesVBO);
+ glDeleteVertexArrays(1, &gridVAO);
+ glDeleteBuffers(1, &gridVBO);
+
+ glfwDestroyWindow(window);
+ glfwTerminate();
+
+ return 0;
+ }