You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
147 lines
4.2 KiB
C
147 lines
4.2 KiB
C
// $ gcc -o glitch_cube glitch_cube.c -lglfw -lGL -lGLU
|
|
#include <GLFW/glfw3.h>
|
|
#include <GL/gl.h>
|
|
#include <GL/glu.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <math.h>
|
|
|
|
void cubeFaceX(float side) {
|
|
glNormal3f(side, 0.0f, 0.0f);
|
|
glTexCoord2f(0.0f, 0.0f); glVertex3f(side, -1.0f, -1.0f);
|
|
glTexCoord2f(1.0f, 0.0f); glVertex3f(side, 1.0f, -1.0f);
|
|
glTexCoord2f(1.0f, 1.0f); glVertex3f(side, 1.0f, 1.0f);
|
|
glTexCoord2f(0.0f, 1.0f); glVertex3f(side, -1.0f, 1.0f);
|
|
}
|
|
void cubeFaceY(float side) {
|
|
glNormal3f(0.0f, side, 0.0f);
|
|
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, side, -1.0f);
|
|
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, side, -1.0f);
|
|
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, side, 1.0f);
|
|
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, side, 1.0f);
|
|
}
|
|
void cubeFaceZ(float side) {
|
|
glNormal3f(0.0f, 0.0f, side);
|
|
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, side);
|
|
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, side);
|
|
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, side);
|
|
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, side);
|
|
}
|
|
|
|
int main(int argc, const char** argv) {
|
|
if (!glfwInit()) exit(1);
|
|
|
|
glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE);
|
|
glfwWindowHint(GLFW_FLOATING, GLFW_TRUE);
|
|
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_API);
|
|
glfwWindowHint(GLFW_RED_BITS, 8);
|
|
glfwWindowHint(GLFW_BLUE_BITS, 8);
|
|
glfwWindowHint(GLFW_GREEN_BITS, 8);
|
|
glfwWindowHint(GLFW_ALPHA_BITS, 0);
|
|
glfwWindowHint(GLFW_STENCIL_BITS, 0);
|
|
glfwWindowHint(GLFW_DEPTH_BITS, 16);
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
|
|
|
|
GLFWwindow* window = glfwCreateWindow(640, 480, "glitch_cube", NULL, NULL);
|
|
glfwMakeContextCurrent(window);
|
|
|
|
glClearDepth(1.0);
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
glEnable(GL_TEXTURE_2D);
|
|
glDisable(GL_CULL_FACE);
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
GLfloat aspect = (GLfloat) 640 / 480;
|
|
gluPerspective(45.0, aspect, 3.0, 1000.0);
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
/* load texture */
|
|
GLuint texture;
|
|
glGenTextures(1, &texture);
|
|
glBindTexture(GL_TEXTURE_2D, texture);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
int width = 64;
|
|
int height = 64;
|
|
|
|
FILE* file = fopen(argv[0], "rb");
|
|
fseek(file, 0L, SEEK_END);
|
|
int size = ftell(file);
|
|
rewind(file);
|
|
|
|
GLubyte* data = (GLubyte*) malloc(size);
|
|
fread(data, 1, size, file);
|
|
fclose(file);
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
|
|
|
|
int frameCount = 0;
|
|
while (!glfwWindowShouldClose(window)) {
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
glPushMatrix();
|
|
|
|
glTranslatef(0.0f, 0.0f, -30.0f);
|
|
glRotatef(glfwGetTime() * 180.0f, 0.3f, 1.0f, 0.0f);
|
|
glRotatef(glfwGetTime() * 79.0f, 0.7f, 0.0f, 1.7f);
|
|
float scale = 6.0f - ((frameCount % 30) / 15.0f);
|
|
glScalef(scale, scale, scale);
|
|
|
|
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
|
|
glBindTexture(GL_TEXTURE_2D, texture);
|
|
|
|
if (frameCount % 30 == 0) {
|
|
const GLuint formats[] = {
|
|
GL_LUMINANCE,
|
|
GL_RGBA,
|
|
GL_RGB,
|
|
};
|
|
const GLuint bytes[] = {
|
|
GL_UNSIGNED_BYTE,
|
|
GL_UNSIGNED_SHORT,
|
|
GL_BYTE,
|
|
GL_UNSIGNED_BYTE_3_3_2,
|
|
GL_FLOAT,
|
|
};
|
|
const GLuint filters[] = {
|
|
GL_NEAREST,
|
|
GL_NEAREST,
|
|
};
|
|
|
|
GLuint fmt = formats[rand() % 3];
|
|
GLubyte* ptr = data + (rand() % size);
|
|
GLuint byt = bytes[rand() % 5];
|
|
GLuint fil = filters[rand() % 2];
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, fmt, byt, ptr);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, fil);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, fil);
|
|
}
|
|
|
|
glColor3f(1.0, 1.0, 1.0);
|
|
|
|
glBegin(GL_QUADS);
|
|
cubeFaceX( 1.0f);
|
|
cubeFaceY( 1.0f);
|
|
cubeFaceZ( 1.0f);
|
|
cubeFaceX(-1.0f);
|
|
cubeFaceY(-1.0f);
|
|
cubeFaceZ(-1.0f);
|
|
glEnd();
|
|
|
|
glPopMatrix();
|
|
|
|
glfwSwapBuffers(window);
|
|
glfwPollEvents();
|
|
frameCount++;
|
|
}
|
|
|
|
free(data);
|
|
|
|
glfwDestroyWindow(window);
|
|
glfwTerminate();
|
|
} |