2024-04-16 18:10:39 +02:00
|
|
|
#define GLFW_INCLUDE_NONE
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#include <GL/gl.h>
|
|
|
|
#include <GL/glu.h>
|
|
|
|
#include <math.h>
|
2024-04-08 00:41:29 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2024-04-16 18:10:39 +02:00
|
|
|
#define WIDTH 800
|
|
|
|
#define HEIGHT 600
|
|
|
|
|
|
|
|
// Function to draw the cube
|
|
|
|
void drawCube() {
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
|
|
|
|
// Front face
|
|
|
|
glColor3f(1.0f, 0.0f, 0.0f);
|
|
|
|
glVertex3f(-1.0f, -1.0f, 1.0f);
|
|
|
|
glVertex3f(1.0f, -1.0f, 1.0f);
|
|
|
|
glVertex3f(1.0f, 1.0f, 1.0f);
|
|
|
|
glVertex3f(-1.0f, 1.0f, 1.0f);
|
|
|
|
|
|
|
|
// Back face
|
|
|
|
glColor3f(0.0f, 1.0f, 0.0f);
|
|
|
|
glVertex3f(-1.0f, -1.0f, -1.0f);
|
|
|
|
glVertex3f(-1.0f, 1.0f, -1.0f);
|
|
|
|
glVertex3f(1.0f, 1.0f, -1.0f);
|
|
|
|
glVertex3f(1.0f, -1.0f, -1.0f);
|
|
|
|
|
|
|
|
// Top face
|
|
|
|
glColor3f(0.0f, 0.0f, 1.0f);
|
|
|
|
glVertex3f(-1.0f, 1.0f, -1.0f);
|
|
|
|
glVertex3f(-1.0f, 1.0f, 1.0f);
|
|
|
|
glVertex3f(1.0f, 1.0f, 1.0f);
|
|
|
|
glVertex3f(1.0f, 1.0f, -1.0f);
|
|
|
|
|
|
|
|
// Bottom face
|
|
|
|
glColor3f(1.0f, 1.0f, 0.0f);
|
|
|
|
glVertex3f(-1.0f, -1.0f, -1.0f);
|
|
|
|
glVertex3f(1.0f, -1.0f, -1.0f);
|
|
|
|
glVertex3f(1.0f, -1.0f, 1.0f);
|
|
|
|
glVertex3f(-1.0f, -1.0f, 1.0f);
|
|
|
|
|
|
|
|
// Right face
|
|
|
|
glColor3f(1.0f, 0.0f, 1.0f);
|
|
|
|
glVertex3f(1.0f, -1.0f, -1.0f);
|
|
|
|
glVertex3f(1.0f, 1.0f, -1.0f);
|
|
|
|
glVertex3f(1.0f, 1.0f, 1.0f);
|
|
|
|
glVertex3f(1.0f, -1.0f, 1.0f);
|
|
|
|
|
|
|
|
// Left face
|
|
|
|
glColor3f(0.0f, 1.0f, 1.0f);
|
|
|
|
glVertex3f(-1.0f, -1.0f, -1.0f);
|
|
|
|
glVertex3f(-1.0f, -1.0f, 1.0f);
|
|
|
|
glVertex3f(-1.0f, 1.0f, 1.0f);
|
|
|
|
glVertex3f(-1.0f, 1.0f, -1.0f);
|
|
|
|
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
// Initialize GLFW
|
|
|
|
if (!glfwInit()) {
|
|
|
|
fprintf(stderr, "Failed to initialize GLFW\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a windowed mode window and its OpenGL context
|
|
|
|
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Rotating Cube", NULL, NULL);
|
|
|
|
if (!window) {
|
|
|
|
glfwTerminate();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the window's context current
|
|
|
|
glfwMakeContextCurrent(window);
|
|
|
|
|
|
|
|
// Loop until the user closes the window
|
|
|
|
while (!glfwWindowShouldClose(window)) {
|
|
|
|
// Clear the framebuffer
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
|
|
|
// Set up the view
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
|
|
|
gluPerspective(45.0f, (float)WIDTH / (float)HEIGHT, 0.1f, 100.0f);
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glLoadIdentity();
|
|
|
|
gluLookAt(3, 3, 3, 0, 0, 0, 0, 1, 0); // Eye position, look at position, up direction
|
|
|
|
|
|
|
|
// Rotate the cube
|
|
|
|
static double angle = 0;
|
|
|
|
glRotated(angle, 0, 1, 1); // Rotate around x, y, and z axes
|
|
|
|
angle += 0.5;
|
|
|
|
|
|
|
|
// Draw the cube
|
|
|
|
drawCube();
|
|
|
|
|
|
|
|
// Swap front and back buffers
|
|
|
|
glfwSwapBuffers(window);
|
|
|
|
|
|
|
|
// Poll for and process events
|
|
|
|
glfwPollEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
glfwTerminate();
|
|
|
|
return 0;
|
2024-04-08 00:41:29 +02:00
|
|
|
}
|