From f63289903b37781a57d7ad6b3c3a8f312ccae25d Mon Sep 17 00:00:00 2001 From: Rakarake Date: Mon, 8 Dec 2025 05:08:01 +0100 Subject: [PATCH] good baseline working --- Makefile | 2 +- main.c | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 108 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 4ef9a13..b087ba7 100644 --- a/Makefile +++ b/Makefile @@ -6,4 +6,4 @@ all: $(CC) $(CFLAGS) $(LDFLAGS) -o glonkers main.c run: all - ./glonkers + SDL_VIDEODRIVER="wayland,x11" ./glonkers diff --git a/main.c b/main.c index 26914de..1fde8b2 100644 --- a/main.c +++ b/main.c @@ -2,6 +2,88 @@ #include #include +float tanf(float in) { + return in; +} + +void perspective(float fovY, float aspect, float zNear, float zFar, float* matrix) { + float f = 1.0f / tanf(fovY * 0.5f * (3.14159265358979323846f / 180.0f)); + matrix[0] = f / aspect; + matrix[1] = 0; + matrix[2] = 0; + matrix[3] = 0; + + matrix[4] = 0; + matrix[5] = f; + matrix[6] = 0; + matrix[7] = 0; + + matrix[8] = 0; + matrix[9] = 0; + matrix[10] = (zFar + zNear) / (zNear - zFar); + matrix[11] = -1; + + matrix[12] = 0; + matrix[13] = 0; + matrix[14] = (2 * zFar * zNear) / (zNear - zFar); + matrix[15] = 0; +} + +void drawCube() { + glBegin(GL_QUADS); + + // Front + glColor3f(1, 0, 0); + glVertex3f(-1, -1, 1); + glVertex3f( 1, -1, 1); + glVertex3f( 1, 1, 1); + glVertex3f(-1, 1, 1); + + // Back + glColor3f(0, 1, 0); + glVertex3f(-1, -1, -1); + glVertex3f(-1, 1, -1); + glVertex3f( 1, 1, -1); + glVertex3f( 1, -1, -1); + + // Top + glColor3f(0, 0, 1); + glVertex3f(-1, 1, -1); + glVertex3f(-1, 1, 1); + glVertex3f( 1, 1, 1); + glVertex3f( 1, 1, -1); + + // Bottom + glColor3f(1, 1, 0); + glVertex3f(-1, -1, -1); + glVertex3f( 1, -1, -1); + glVertex3f( 1, -1, 1); + glVertex3f(-1, -1, 1); + + // Right + glColor3f(1, 0, 1); + glVertex3f(1, -1, -1); + glVertex3f(1, 1, -1); + glVertex3f(1, 1, 1); + glVertex3f(1, -1, 1); + + // Left + glColor3f(0, 1, 1); + glVertex3f(-1, -1, -1); + glVertex3f(-1, -1, 1); + glVertex3f(-1, 1, 1); + glVertex3f(-1, 1, -1); + + glEnd(); +} + +void checkGlError() { + const char *err = SDL_GetError(); + if (*err != 0) { + printf("OpenGL error: %s", err); + } +} + int main() { printf("Good Morning 🍵\n"); SDL_Init(SDL_INIT_VIDEO); @@ -24,13 +106,34 @@ int main() { SDL_Event e; int running = 1; - while (SDL_PollEvent(&e)) { - printf("INSgkldjsj\n"); - if (e.type == SDL_EVENT_QUIT) running = false; + float angle = 0.0; + while (running) { + while (SDL_PollEvent(&e)) { + if (e.type == SDL_EVENT_QUIT) running = false; + } + checkGlError(); + // Rendorrrr glViewport(0, 0, 800, 600); glClearColor(0.1f, 0.1f, 0.1f, 1.0f); // Dark gray background glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); -//SDL_GL_SwapWindow(window); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + float proj[16]; + perspective(60.0f, 800.0f / 600.0f, 1.0f, 100.0f, proj); + glLoadMatrixf(proj); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0.0f, 0.0f, -5.0f); + glRotatef(angle, 1.0f, 1.0f, 0.0f); + + drawCube(); + SDL_GL_SwapWindow(window); + angle += 0.5; } + + checkGlError(); }