Пример #1
0
Bool init()
{
    int   width = WINDOW_W, height = WINDOW_H;
    //int   texid;
    Entity *entity;
    int texUnitId1 = 0;
    int texUnitId2 = 1;

    world = new World ();
    printf("pointer world: %d.\n", world);
    world->setSize(width, height);
    world->setBackColor (1.0, 1.0, 1.0, 1.0);

    camera = world->createCamera ("camera1");
    win = world->createRenderWindow ();
    win->addViewport (camera, 0, 0, width, height);

    //shader
    ShaderProgram *shaderProgram = ShaderProgramManager::getInstance()->createShaderProgram();
    if( !shaderProgram->loadShaderSource (SHADER_VERT_FILE, SHADER_FRAG_FILE) )
    {
        TOY3D_TIPS("Error: loadShaderSource failed.\n");
        return FALSE;
    }
    //printf("shaderProgram id: %d\n", shaderProgram->getShaderProgramID());


    ShaderProgramParams *params = ShaderProgramManager::getInstance()->createShaderProgramParams();

    //shader auto constant
    params->setNamedAutoConstant (TOY3D_ACT_PROJECTION_MATRIX, "proj_mat");
    params->setNamedAutoConstant (TOY3D_ACT_VIEW_MATRIX, "view_mat");
    params->setNamedAutoConstant (TOY3D_ACT_WORLD_MATRIX, "world_mat");

    //shader attributes
    params->setNamedAttrConstant(TOY3D_ATTR_VERTEX, "a_position");
    params->setNamedAttrConstant(TOY3D_ATTR_UV, "a_texCoord");

    //shader custom constant
    params->setNamedCustUniformConstant(TOY3D_CUST_SAMPLER2D, "s_baseMap", texUnitId1);
    params->setNamedCustUniformConstant(TOY3D_CUST_SAMPLER2D, "s_lightMap", texUnitId2);

    shaderProgram->bindShaderParameters(params);

    Texture *tex_back = TextureManager::getInstance()->createTextureByFile(TEXTURE_FILE_BACK);
    Texture *tex_light = TextureManager::getInstance()->createTextureByFile(TEXTURE_FILE_LIGHT);

    Material *mat = MaterialManager::getInstance()->createMaterial();
    mat->setShaderProgram (shaderProgram);
    //mat->setSceneBlending(T3D_SRC_ALPHA, T3D_ONE_MINUS_SRC_ALPHA, T3D_ADD);

    //create tex unit 1 for back.tga
    TextureUnitState *texUS_back;
    texUS_back = mat->createTextureUnitState("TexUnit_back");
    if(!texUS_back)
    {
        TOY3D_TIPS("Error: createTextureUnitState failed.\n");
        return FALSE;
    }

    texUS_back->setID (texUnitId1);
    texUS_back->setTexture(tex_back);
    texUS_back->setTextureType(T3D_TEXTURE_2D);
    texUS_back->setTextureParameter(T3D_LINEAR, T3D_LINEAR, T3D_CLAMP_TO_EDGE, T3D_CLAMP_TO_EDGE);


    //create tex unit 2 for light.tga

    TextureUnitState *texUS_light;
    texUS_light = mat->createTextureUnitState("TexUnit_light");
    if(!texUS_light)
    {
        TOY3D_TIPS("Error: createTextureUnitState failed.\n");
        return FALSE;
    }

    texUS_light->setID (texUnitId2);
    texUS_light->setTexture(tex_light);
    texUS_light->setTextureType(T3D_TEXTURE_2D);
    texUS_light->setTextureParameter(T3D_LINEAR, T3D_LINEAR, T3D_CLAMP_TO_EDGE, T3D_CLAMP_TO_EDGE);

    //mat->addTextureUnitState (texUS_back);
    //mat->addTextureUnitState (texUS_light);

    Mesh* mesh = MeshManager::getInstance()->createMesh();
    mesh->setRenderMode (TOY3D_TRIANGLE_STRIP);
    mesh->setVertices (vertices, VERTEX_COUNT);
    mesh->setUVs( uvs, VERTEX_COUNT);

    entity = world->createEntity();
    entity->setMesh(mesh);
    entity->setMaterial (mat);

    return true;
}
Пример #2
0
bool init()
{
    int   width = WINDOW_W, height = WINDOW_H;
    //int   texid;
    int   texUnit = 0;
    Real  limit;
    Entity *entity;

    world = new World ();
    printf("pointer world: %d.\n", world);
    world->setSize(width, height);
    world->setBackColor (1.0, 1.0, 1.0, 1.0);

    camera = world->createCamera ("camera1");
    win = world->createRenderWindow ();
    win->addViewport (camera, 0, 0, width, height);

    //shader
    ShaderProgram *shaderProgram = ShaderProgramManager::getInstance()->createShaderProgram();
    shaderProgram->loadShaderSource (SHADER_VERT_FILE, SHADER_FRAG_FILE);
    printf("shaderProgram id: %d\n", shaderProgram->getShaderProgramID());


    ShaderProgramParams *params = ShaderProgramManager::getInstance()->createShaderProgramParams();

    //shader auto constant
    params->setNamedAutoConstant (TOY3D_ACT_PROJECTION_MATRIX, "proj_mat");
    params->setNamedAutoConstant (TOY3D_ACT_VIEW_MATRIX, "view_mat");
    params->setNamedAutoConstant (TOY3D_ACT_WORLD_MATRIX, "world_mat");

    //shader attributes
    params->setNamedAttrConstant(TOY3D_ATTR_VERTEX, "vPosition");
    params->setNamedAttrConstant(TOY3D_ATTR_UV, "vTexture");

    //shader custom constant
    limit = 0.1f;
    printf("limit = %f.\n", limit);
    params->setNamedCustUniformConstant(TOY3D_CUST_REAL1, "limit", limit);
    params->setNamedCustUniformConstant(TOY3D_CUST_SAMPLER2D, "sampler2d", texUnit);

    shaderProgram->bindShaderParameters(params);

    Texture *tex = TextureManager::getInstance()->createTextureByFile(TEXTURE_FILE);
    //unsigned char *temp = generateColorData(64, 64,4);
    //Texture *tex = TextureManager::getInstance()->createTexture(temp, 64, 64, 4);

    Material *mat = MaterialManager::getInstance()->createMaterial();
    mat->setShaderProgram (shaderProgram);
    //mat->setSceneBlending(T3D_SRC_ALPHA, T3D_ONE_MINUS_SRC_ALPHA, T3D_ADD);
    TextureUnitState *texUS;
    texUS = mat->createTextureUnitState("TexUnit1");
    if(!texUS)
    {
        TOY3D_TIPS("Error: createTextureUnitState failed.\n");
        return FALSE;
    }

    texUS->setID(texUnit);
    texUS->setTexture(tex);
    texUS->setTextureType(T3D_TEXTURE_2D);
    texUS->setTextureParameter(T3D_LINEAR, T3D_LINEAR, T3D_CLAMP_TO_EDGE, T3D_CLAMP_TO_EDGE);
    //mat->setTexture(tex);

    Mesh* mesh = MeshManager::getInstance()->createMesh();
    mesh->setRenderMode (TOY3D_TRIANGLE_STRIP);
    mesh->setVertices (vertices, VERTEX_COUNT);
    mesh->setUVs( uvs, VERTEX_COUNT);

    entity = world->createEntity();
    entity->setMesh(mesh);
    entity->setMaterial (mat);

    return true;
}