예제 #1
0
//------------------------------------------------------
void MaterialService::prepareMaterialInstance(MaterialPtr &mat,
                                              unsigned int idx, int tag) {
    if (tag < 0) // Should not be here if the polygon is sky textured
        OPDE_EXCEPT("Non-instanced material instance requested");

    mat->setReceiveShadows(true);

    Ogre::StringStream lightmapName;
    lightmapName << "@lightmap" << tag;

    Pass *shadPass = mat->getTechnique(0)->getPass(0);

    if (shadPass->getNumTextureUnitStates() <= 1) {
        // Lightmap texture is added here
        TextureUnitState *tex = shadPass->createTextureUnitState();
        tex->setTexture(mLightService->getAtlasTexture(tag));

        // Blend
        tex->setColourOperation(LBO_MODULATE);
        // Use 2nd texture co-ordinate set
        tex->setTextureCoordSet(1);

        // Clamp
        tex->setTextureAddressingMode(TextureUnitState::TAM_CLAMP);

        // Switch filtering off to see lmap pixels: TFO_NONE
        tex->setTextureFiltering(TFO_BILINEAR);
    } else {
        // There is a definition of the lightmapping pass already, we only
        // update that definition
        TextureUnitState *tex = shadPass->getTextureUnitState(1);
        tex->setTextureName(lightmapName.str());
        tex->setTextureCoordSet(1);
    }
}
예제 #2
0
파일: texture.cpp 프로젝트: kimziv/toy3d
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;
}