139 lines
3.1 KiB
C
139 lines
3.1 KiB
C
#include <stdio.h>
|
|
#include <GL/gl.h>
|
|
#include <SDL3/SDL.h>
|
|
|
|
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);
|
|
SDL_Window* window = SDL_CreateWindow("SDL3 OpenGL Cube",
|
|
800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
|
|
if (!window) {
|
|
printf("CreateWindow Error: %s\n", SDL_GetError());
|
|
SDL_Quit();
|
|
return 1;
|
|
}
|
|
|
|
SDL_GLContext glContext = SDL_GL_CreateContext(window);
|
|
if (!glContext) {
|
|
printf("GL Context Error: %s\n", SDL_GetError());
|
|
SDL_DestroyWindow(window);
|
|
SDL_Quit();
|
|
return 1;
|
|
}
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
SDL_Event e;
|
|
int running = 1;
|
|
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);
|
|
|
|
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();
|
|
}
|