Skip to content

Instantly share code, notes, and snippets.

@tinycrops
Created May 17, 2024 18:51
Show Gist options
  • Save tinycrops/42238602150ea212923ec7e3f276471b to your computer and use it in GitHub Desktop.
Save tinycrops/42238602150ea212923ec7e3f276471b to your computer and use it in GitHub Desktop.
gcc -o sdl_opengl_shapes sdl_opengl_shapes.c -lSDL2 -lGL -lGLU -lm
#include <SDL2/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdio.h>
#include <math.h>
//link to conversation that lead up to this code
//https://chatgpt.com/share/c201bd1b-5024-4b6a-9a65-2ae286db4fe3
//
// Function to initialize SDL and OpenGL
int initSDL(SDL_Window **window, SDL_GLContext *context, int width, int height) {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
return 0;
}
*window = SDL_CreateWindow("SDL OpenGL Shapes", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
if (*window == NULL) {
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
return 0;
}
*context = SDL_GL_CreateContext(*window);
if (*context == NULL) {
printf("OpenGL context could not be created! SDL_Error: %s\n", SDL_GetError());
return 0;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
if (SDL_GL_SetSwapInterval(1) < 0) {
printf("Warning: Unable to set VSync! SDL_Error: %s\n", SDL_GetError());
}
glEnable(GL_DEPTH_TEST); // Enable depth testing for 3D rendering
return 1;
}
// Function to draw a rectangle
void drawRectangle() {
glColor3f(1.0f, 0.0f, 0.0f); // Red color
glBegin(GL_QUADS);
glVertex2f(-0.5f, -0.5f);
glVertex2f(0.5f, -0.5f);
glVertex2f(0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
}
// Function to draw a triangle
void drawTriangle() {
glColor3f(0.0f, 1.0f, 0.0f); // Green color
glBegin(GL_TRIANGLES);
glVertex2f(0.0f, 0.5f);
glVertex2f(-0.5f, -0.5f);
glVertex2f(0.5f, -0.5f);
glEnd();
}
// Function to draw a 3D cone
void drawCone() {
glColor3f(0.0f, 0.0f, 1.0f); // Blue color
GLUquadric *quad = gluNewQuadric();
glPushMatrix();
glTranslatef(0.0f, -0.5f, 0.0f);
gluCylinder(quad, 0.5f, 0.0f, 1.0f, 32, 32);
glPopMatrix();
gluDeleteQuadric(quad);
}
// Main function
int main(int argc, char* args[]) {
SDL_Window *window = NULL;
SDL_GLContext context;
int width = 800, height = 600;
if (!initSDL(&window, &context, width, height)) {
printf("Failed to initialize SDL.\n");
return -1;
}
int running = 1;
SDL_Event event;
float angle = 0.0f;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)width / (double)height, 1.0, 500.0);
glMatrixMode(GL_MODELVIEW);
while (running) {
while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_QUIT) {
running = 0;
}
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// Move the camera back
glTranslatef(0.0f, 0.0f, -10.0f);
// Increment the angle for the orbit
angle += 0.01f;
// Calculate positions for each shape
float radius = 3.0f;
float x, z;
// Draw rectangle
glPushMatrix();
x = radius * cos(angle);
z = radius * sin(angle);
glTranslatef(x, 0.0f, z);
glRotatef(angle * 57.2958f, 0.0f, 1.0f, 0.0f); // Convert radians to degrees
drawRectangle();
glPopMatrix();
// Draw triangle
glPushMatrix();
x = radius * cos(angle + 2.0f);
z = radius * sin(angle + 2.0f);
glTranslatef(x, 0.0f, z);
glRotatef((angle + 2.0f) * 57.2958f, 0.0f, 1.0f, 0.0f);
drawTriangle();
glPopMatrix();
// Draw cone
glPushMatrix();
x = radius * cos(angle + 4.0f);
z = radius * sin(angle + 4.0f);
glTranslatef(x, 0.0f, z);
glRotatef((angle + 4.0f) * 57.2958f, 0.0f, 1.0f, 0.0f);
drawCone();
glPopMatrix();
SDL_GL_SwapWindow(window);
}
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment