示例#1
0
int main(int argc, char** argv)
{
    // If no arguments passed, print usage and exit
    if (argc == 1) {
        printf("Usage: %s scenefile.glsf filename.obj [filename.obj, ...]\n\n", argv[0]);
        exit(0);
    }

    // Print the help message each time the program is run.
    //   This provides users with a list of keyboard commands to use.
    printf("%s", help_msg);

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
    glutInitContextVersion (3, 2);
    glutInitContextFlags (GLUT_FORWARD_COMPATIBLE);
    glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    glutInitWindowPosition(500, 300);
    glutCreateWindow("Simple Open GL Program");
    printf("%s\n%s\n", glGetString(GL_RENDERER), glGetString(GL_VERSION));

    glewExperimental = GL_TRUE;
    glewInit();

    SceneState camera;
    ss = &camera;

    // Read and parse the scene file
    readSceneFilename(argv);
    // Read and parse each object file passed in
    readObjFilenames(argc, argv);
    // Add plane to list of objects.
    Mesh plane("plane.obj", vec4(0.3, 0.3, 0.3, 1.0));
    plane.ss = ss;
    objects.push_back(plane);

    // Initialize Shaders
    uniform_color = InitShader("vshader.glsl", "singlecolor.glsl");
    glUniform1i(glGetUniformLocation(uniform_color, "disks" ), disks);
    cel_shading = InitShader("celshader.glsl", "celfragment.glsl");
    shadow_shader = InitShader("vshadow.glsl", "fshadow.glsl");
    phong_illumination = InitShader("vshader.glsl", "fshader.glsl");
    // TODO: Add simple depth shader for rendering shadow texture.

    cur_program = phong_illumination;

    // Create manipulators
    Mesh unit_x("unit_cube.obj", vec4(1.0, 0.0, 0.0, 1.0));
    Mesh unit_y(UNIT_CUBE, vec4(0.0, 1.0, 0.0, 1.0));
    Mesh unit_z(UNIT_CUBE, vec4(0.0, 0.0, 1.0, 1.0));

    // Scale
    unit_x.scale = unit_y.scale = unit_z.scale = Angel::Scale(0.5, 0.5, 0.5);

    // Translate
    unit_x.offset *= Angel::Translate(vec4(0.75, 0.0, 0.0, 0.0));
    unit_y.offset *= Angel::Translate(vec4(0.0, 0.75, 0.0, 0.0));
    unit_z.offset *= Angel::Translate(vec4(0.0, 0.0, 0.75, 0.0));

    // Camera
    unit_x.ss = ss;
    unit_y.ss = ss;
    unit_z.ss = ss;

    manipulator.push_back(unit_x);
    manipulator.push_back(unit_y);
    manipulator.push_back(unit_z);

    glEnable(GL_DEPTH_TEST);

    // TODO: Start Shadow Buffers

    glGenTextures(1, &shadow_texture);
    glBindTexture(GL_TEXTURE_2D, shadow_texture);

    // Reserve texture memory
    glTexImage2D(
        GL_TEXTURE_2D, 0,
        GL_DEPTH_COMPONENT,
        1024, 1024, 0,
        GL_DEPTH_COMPONENT, GL_FLOAT, NULL
    );

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glGenFramebuffers(1, &shadow_buffer);
    glBindFramebuffer(GL_FRAMEBUFFER, shadow_buffer);

    glFramebufferTexture2D(
        GL_FRAMEBUFFER,
        GL_DEPTH_ATTACHMENT,
        GL_TEXTURE_2D,
        shadow_texture,
        0
    );
    glDrawBuffer(GL_NONE);

    // Make sure the frame buffer is good-to-go
    if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
        printf("Framebuffer not OK!\n");
    }

    // Unbind framebuffer until we need to render to it.
    glBindTexture(GL_TEXTURE_2D, 0);
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

    // TODO: End Shadow Buffers

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glClearColor( 1.0, 1.0, 1.0, 1.0 );

    // Print the number of objects 'init-ed'
    printf("Initialized: %ld objects\n\n", objects.size());

    //NOTE:  callbacks must go after window is created!!!
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(mouseMotion);
    glutDisplayFunc(display);
    if (ss->lens.size() == 4) {
        glutReshapeFunc(myPerspectiveReshape);
    } else {
        glutReshapeFunc(myOrthoReshape);
    }

    glutMainLoop();

    return(0);
}