Exemplo n.º 1
0
int main()
{

    int width = 500;
    int height = 500;

    sf::RenderWindow window(sf::VideoMode(width,height),"soft-renderer");

    if(glewInit()!=GLEW_OK){
        cout << "error in glew";
        return 0;
    }

    initialize();

    VertexShader *vs = new VertexShader ;
    vs->setIterationCompute(&iterationCompute);
    vs->setIterationTimes(times);

    FragShader *fs = new FragShader;
    fs->setIterationCompute(&FragShaderIterCompute);

    Pipeline::Config config;
    config.height = height;
    config.width = width;
    config.primitiveType = TRIANGLES;
    config.clearColor = glm::vec3(0.5f,0.0f,0.0f);

    Pipeline pl;
    pl.setConfig(config);
    pl.attachVertShader(vs);
    pl.attachFragShader(fs);
    pl.render();

//    sf::Clock clock;
//    clock.restart();
    while(window.isOpen()){
        sf::Event event;
        while(window.pollEvent(event)){
            if(event.type == sf::Event::MouseButtonPressed){

                int x = event.mouseButton.x;
                int y = event.mouseButton.y;

                vector< unsigned char > pixels( 1 * 1 * 4 );
                glReadPixels(x,height-y,1,1,GL_RGBA,
                             GL_UNSIGNED_BYTE,
                             &pixels[0]);
                cout << "r: " << (int)pixels[0] << endl;
                cout << "g: " << (int)pixels[1] << endl;
                cout << "b: " << (int)pixels[2] << endl;
                cout << "a: " << (int)pixels[3] << endl;
                cout << endl;
                cout << "x: " << x << endl;
                cout << "y: " << 800-y << endl;
                cout << endl;

            }

            if (event.type == sf::Event::Closed) {
                window.close();
            }

            if (event.type == sf::Event::Resized)
            {
                // adjust the viewport when the window is resized
                glViewport(0, 0, event.size.width, event.size.height);
            }

            if (event.type == sf::Event::KeyPressed) {

                glm::vec4 col(0);
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                    col.x -= 0.05f;
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                   col.x += 0.05f;
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                    col.y += 0.05f;
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                {
                    col.y -= 0.05f;
                }

                glm::mat4 translate = glm::translate(glm::mat4(1),vec3(col));
                MVP = translate * MVP;
                pl.update();
            }
            glDrawPixels(width,height,GL_RGB,GL_FLOAT,pl.getColorBuffer());
            window.display();
        }

//        float fps = 1.f / clock.getElapsedTime().asSeconds();
//        std::cout << "fps" << fps << std::endl;
//        clock.restart();
    }


    delete vs;
    delete fs;

    return 0;
}