Beispiel #1
0
void PezUpdate(unsigned int elapsedMicroseconds)
{
    const float RadiansPerMicrosecond = 0.0000005f;
    static float Theta = 0;
    Theta += elapsedMicroseconds * RadiansPerMicrosecond;
    
    Vector3 offset = V3MakeFromElems(0, 0, 0);
    Matrix4 model = M4MakeRotationZ(Theta);
    model = M4Mul(M4MakeTranslation(offset), model);
    model = M4Mul(model, M4MakeTranslation(V3Neg(offset)));

    Point3 eyePosition = P3MakeFromElems(0, 10, 0);
    Point3 targetPosition = P3MakeFromElems(0, 0, 0);
    Vector3 upVector = V3MakeFromElems(0, 0, 1);
    Matrix4 view = M4MakeLookAt(eyePosition, targetPosition, upVector);
    
    ModelviewMatrix = M4Mul(view, model);
}
Beispiel #2
0
void draw()
{
    int mesh = 0, multi = 0, meshcolor = 0;
    switch (state) {
    case STATE_GRAY_SOURCE:
        parg_shader_bind(P_GRAY);
        parg_texture_bind(graytex, 0);
        parg_uniform1f(U_ZSCALE, 1);
        break;
    case STATE_COLOR_IH:
        mesh = 1;
        parg_shader_bind(P_GRAYMESH);
        parg_uniform1f(U_ZSCALE, 0.3);
        break;
    case STATE_COLOR_DHSCSI:
        mesh = multi = 1;
        parg_shader_bind(P_GRAYMESH);
        parg_uniform1f(U_ZSCALE, 0.3);
        break;
    case STATE_MULTI_RGBA:
    case STATE_MULTI_RGB:
    case STATE_MULTI_DIAGRAM:
        meshcolor = mesh = multi = 1;
        parg_shader_bind(P_GRAYMESH);
        parg_uniform1f(U_ZSCALE, 0.25);
        break;
    case STATE_COLOR_DEFAULT:
    case STATE_GRAY_DEFAULT:
    case STATE_GRAY_SIMPLIFY:
    case STATE_GRAY_INVERT:
    case STATE_GRAY_HEIGHTS:
        mesh = 1;
        parg_shader_bind(P_GRAYMESH);
        parg_uniform1f(U_ZSCALE, 1);
        break;
    case STATE_GRAY_MULTI:
        mesh = multi = 1;
        parg_shader_bind(P_GRAYMESH);
        parg_texture_bind(colortex, 0);
        parg_uniform1f(U_ZSCALE, 0.3);
        break;
    case STATE_GRAY_DUAL:
        mesh = multi = 1;
        parg_shader_bind(P_GRAYMESH);
        parg_texture_bind(colortex, 0);
        parg_uniform1f(U_ZSCALE, 1);
        break;
    case STATE_GRAY_DHS:
    case STATE_GRAY_DHSC:
        mesh = multi = 1;
        parg_shader_bind(P_GRAYMESH);
        parg_texture_bind(colortex, 0);
        parg_uniform1f(U_ZSCALE, 0.5);
        break;
    case STATE_COLOR_SOURCE:
        parg_texture_bind(colortex, 0);
        parg_shader_bind(P_COLOR);
        parg_uniform1f(U_ZSCALE, 1);
        break;
    default:
        break;
    }

    if (mesh) {
        for (int i = 0; i < sizeof(trimesh) / sizeof(trimesh[0]); i++) {
            parg_mesh_free(trimesh[i]);
        }
        memset(trimesh, 0, sizeof(trimesh));
        create_mesh();
    }

    Matrix4 model;
    if (mesh) {
        model = M4MakeScale(V3MakeFromElems(20, 20, 10));
        model = M4Mul(M4MakeTranslation(V3MakeFromElems(-10, -10, 0)), model);
    } else {
        model = M4MakeIdentity();
    }

    Matrix4 modelview = M4Mul(view, model);
    Matrix4 mvp = M4Mul(projection, modelview);
    parg_uniform_matrix4f(U_MVP, &mvp);
    parg_draw_clear();
    if (mesh) {
        Vector4 colors[3];
        colors[0] = (Vector4){0, 0.6, 0.9, 1};
        colors[1] = (Vector4){0, 0.9, 0.6, 1};
        colors[2] = (Vector4){0.9, 0.6, 0, 1};
        Vector4 black = {0, 0, 0, 1.0};

        for (int imesh = 0; imesh < nmeshes; imesh++) {
            parg_varray_enable(parg_mesh_coord(trimesh[imesh]), A_POSITION, 3,
                PARG_FLOAT, 0, 0);
            parg_varray_bind(parg_mesh_index(trimesh[imesh]));
            if (meshcolor) {
                unsigned int b = meshcolors[imesh] & 0xff;
                unsigned int g = (meshcolors[imesh] >> 8) & 0xff;
                unsigned int r = (meshcolors[imesh] >> 16) & 0xff;
                unsigned int a = (meshcolors[imesh] >> 24) & 0xff;
                Vector4 color;
                color.x = r / 255.0f;
                color.y = g / 255.0f;
                color.z = b / 255.0f;
                color.w = a / 255.0f;
                parg_uniform4f(U_COLOR, &color);
            } else {
                parg_uniform4f(U_COLOR, &colors[imesh]);
            }
            parg_draw_triangles_u16(0, parg_mesh_ntriangles(trimesh[imesh]));
            parg_uniform4f(U_COLOR, &black);
            parg_draw_wireframe_triangles_u16(
                0, parg_mesh_ntriangles(trimesh[imesh]));
        }

    } else {
void PezRender()
{
    #define Instances 7

    Matrix4 Model[Instances];
    Model[0] = M4MakeRotationY(Globals.Theta);
    Model[1] = M4Mul(M4Mul(
                     M4MakeTranslation((Vector3){0, 0, 0.6}),
                     M4MakeScale(V3MakeFromScalar(0.25))),
                     M4MakeRotationX(Pi/2)
    );
    Model[2] = Model[3] = Model[4] = Model[1];
    Model[1] = M4Mul(M4MakeRotationY(Globals.Theta), Model[1]);
    Model[2] = M4Mul(M4MakeRotationY(Globals.Theta + Pi/2), Model[2]);
    Model[3] = M4Mul(M4MakeRotationY(Globals.Theta - Pi/2), Model[3]);
    Model[4] = M4Mul(M4MakeRotationY(Globals.Theta + Pi), Model[4]);
    Model[5] = M4Mul(M4Mul(
                     M4MakeScale(V3MakeFromScalar(0.5)),
                     M4MakeTranslation((Vector3){0, 1.25, 0})),
                     M4MakeRotationY(-Globals.Theta)
    );
    Model[6] = M4Mul(M4Mul(
                     M4MakeScale(V3MakeFromScalar(0.5)),
                     M4MakeTranslation((Vector3){0, -1.25, 0})),
                     M4MakeRotationY(-Globals.Theta)
    );

    Vector3 LightPosition = {0.5, 0.25, 1.0}; // world space
    Vector3 EyePosition = {0, 0, 1};          // world space

    Matrix4 MVP[Instances];
    Vector3 Lhat[Instances];
    Vector3 Hhat[Instances];
    for (int i = 0; i < Instances; i++) {
        Matrix4 mv = M4Mul(Globals.View, Model[i]);
        MVP[i] = M4Mul(Globals.Projection, mv);
        Matrix3 m = M3Transpose(M4GetUpper3x3(Model[i]));
        Lhat[i] = M3MulV3(m, V3Normalize(LightPosition));    // object space
        Vector3 Eye =  M3MulV3(m, V3Normalize(EyePosition)); // object space
        Hhat[i] = V3Normalize(V3Add(Lhat[i], Eye));
    }

    int instanceCount = Instances;
    MeshPod* mesh = &Globals.Cylinder;

    glBindFramebuffer(GL_FRAMEBUFFER, Globals.FboHandle);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
  
    glUseProgram(Globals.LitProgram);
    glUniform3f(u("SpecularMaterial"), 0.4, 0.4, 0.4);
    glUniform4f(u("FrontMaterial"), 0, 0, 1, 1);
    glUniform4f(u("BackMaterial"), 0.5, 0.5, 0, 1);
    glUniform3fv(u("Hhat"), Instances, &Hhat[0].x);
    glUniform3fv(u("Lhat"), Instances, &Lhat[0].x);
    glUniformMatrix4fv(u("ModelviewProjection"), Instances, 0, (float*) &MVP[0]);

    glBindVertexArray(mesh->FillVao);
    glDrawElementsInstanced(GL_TRIANGLES, mesh->FillIndexCount, GL_UNSIGNED_SHORT, 0, instanceCount);

    glUseProgram(Globals.SimpleProgram);
    glUniform4f(u("Color"), 0, 0, 0, 1);
    glUniformMatrix4fv(u("ModelviewProjection"), Instances, 0, (float*) &MVP[0]);

    glDepthMask(GL_FALSE);
    glBindVertexArray(mesh->LineVao);
    glDrawElementsInstanced(GL_LINES, mesh->LineIndexCount, GL_UNSIGNED_SHORT, 0, instanceCount);
    glDepthMask(GL_TRUE);

    glDisable(GL_DEPTH_TEST);

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glUseProgram(Globals.QuadProgram);
    glBindTexture(GL_TEXTURE_2D, Globals.FboTexture);
    glBindVertexArray(Globals.Grid.FillVao);
    glDrawElements(GL_TRIANGLES, Globals.Grid.FillIndexCount, GL_UNSIGNED_SHORT, 0);

    if (1) {
        glUseProgram(Globals.GridProgram);
        glBindVertexArray(Globals.Grid.LineVao);
        glDrawElements(GL_LINES, Globals.Grid.LineIndexCount, GL_UNSIGNED_SHORT, 0);
    }

    glBindTexture(GL_TEXTURE_2D, 0);
}