Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Code off

Name: Anonymous 2009-07-28 13:59

Welcome to the first annual /prog/ Code off !

The language will be C.

Compeptitors include (in no particular order):

Mr. Ribs
Xarn
FV
Anonymous
W. T. Snacks

Rumor has it, that there may be a special guest appearance from either The Sussman or
Leah Culver.

What's the point of this, you ask?

It's to determine who is an EXPERT PROGRAMMER.

Marking criteria:

Optimization
Elegance

Bonus marks:
indentation

Good luck!

Name: Anonymous 2009-07-30 8:38

Okay, this should work.  The squares don't "appear" where you click because I couldn't solve the equation ;_;  Add -lglut and -lGL when linking.  Compilation might be tricky.

#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SQUARE 1024
static unsigned int square_count;
static struct {
    GLfloat data[16];
} square_data[MAX_SQUARE];

static GLint uniformTime;

static void
display()
{
    GLfloat t = glutGet(GLUT_ELAPSED_TIME) * 0.001f;
    glUniform1f(uniformTime, t);
    glClear(GL_COLOR_BUFFER_BIT);
    glVertexPointer(4, GL_FLOAT, 0, square_data);
    glDrawArrays(GL_QUADS, 0, square_count * 4);
    glutSwapBuffers();
}

static void
display_callback(int value)
{
    display();
    glutTimerFunc(16, display_callback, 0);
}

static void
square_add(int x, int y, int vx, int vy)
{
    if (square_count >= MAX_SQUARE)
        return;

    GLfloat data[16] = { x - 8, y - 8, vx, vy, x + 8, y - 8, vx, vy, x + 8, y + 8, vx, vy, x - 8, y + 8, vx, vy };
    memcpy(square_data[square_count++].data, data, sizeof(GLfloat) * 16);
}

static void
mouse(int button, int state, int x, int y)
{
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
        square_add(x, y, (rand() % 400) - 200, (rand() % 400) - 200);
    } else if (button == GLUT_RIGHT_BUTTON && state == GLUT_UP) {
        unsigned int i;
        for (i = 0; i < MAX_SQUARE; ++i) {
            square_add(rand() % 640, rand() % 480, (rand() % 400) - 200, (rand() % 400) - 200);
        }
    }
}

static const char* vert =
    "#version 120\n"
    "const mat4 orthoMatrix = "
    "mat4(2.0 / 640.0, 0.0,          0.0, 0.0,"
         "0.0,         2.0 / -480.0, 0.0, 0.0,"
         "0.0,         0.0,         -1.0, 0.0,"
        "-1.0,         1.0,         -0.0, 1.0);\n"
    "uniform float t;\n"
    "const vec2 windowSize = vec2(640.0f, 480.0f);\n"
    "void main() {\n"
        "vec2 dist = gl_Vertex.xy + gl_Vertex.zw * t;\n"
        "ivec2 signs = ivec2(mod(dist / windowSize, 2));\n"
        "gl_Position = orthoMatrix * vec4((2 * signs - 1) * ((signs * windowSize) - mod(dist, windowSize)), 0.0f, 1.0f);\n"
    "}\n";

static const char* frag =
    "#version 120\n"
    "void main() {\n"
        "gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
    "}\n";

int
main(int argc, char* argv[])
{
    GLuint pr;
    GLuint sh;

    glutInit(&argc, argv);
    glutInitWindowSize(640, 480);
    glutInitDisplayMode(0);

    glutCreateWindow("/prog/");

    pr = glCreateProgram();

    sh = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(sh, 1, &vert, NULL);
    glCompileShader(sh);
    glAttachShader(pr, sh);

    sh = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(sh, 1, &frag, NULL);
    glCompileShader(sh);
    glAttachShader(pr, sh);

    glLinkProgram(pr);
    glUseProgram(pr);
    uniformTime = glGetUniformLocation(pr, "t");

    glEnableClientState(GL_VERTEX_ARRAY);

    glutDisplayFunc(display);
    glutMouseFunc(mouse);

    glutTimerFunc(16, display_callback, 0);

    glutMainLoop();

    return 0;
}

Name: Anonymous 2009-07-30 9:17

>>157
Oops, forgot I was going to use a VBO.  Also enabled double buffering to avoid flickering.
9,11d8
< static struct {
<     GLfloat data[16];
< } square_data[MAX_SQUARE];
21c18
<     glVertexPointer(4, GL_FLOAT, 0, square_data);
---
    glVertexPointer(4, GL_FLOAT, 0, NULL);
40c37
<     memcpy(square_data[square_count++].data, data, sizeof(GLfloat) * 16);
---
    glBufferSubData(GL_ARRAY_BUFFER, square_count++ * sizeof(GLfloat) * 16, sizeof(GLfloat) * 16, data);
81a79
    GLuint vbo;
85c83
<     glutInitDisplayMode(0);
---
    glutInitDisplayMode(GLUT_DOUBLE);
104a103,106
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 16 * MAX_SQUARE, NULL, GL_STATIC_DRAW);

Name: Anonymous 2009-07-30 12:47

ed(1) to the rescue!
104a
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 16 * MAX_SQUARE, NULL, GL_STATIC_DRAW);

.
85c
    glutInitDisplayMode(GLUT_DOUBLE);
.
81a
    GLuint vbo;
.
40c
    glBufferSubData(GL_ARRAY_BUFFER, square_count++ * sizeof(GLfloat) * 16, sizeof(GLfloat) * 16, data);
.
21c
    glVertexPointer(4, GL_FLOAT, 0, NULL);
.
9,11d

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List