void LightSample::setUnlitMaterialTexture(Model* model, const char* texturePath, bool mipmap) { Material* material = model->setMaterial("res/shaders/textured.vert", "res/shaders/textured.frag", "DIRECTIONAL_LIGHT_COUNT 1"); material->setParameterAutoBinding("u_worldViewProjectionMatrix", "WORLD_VIEW_PROJECTION_MATRIX"); // Load the texture from file. Texture::Sampler* sampler = material->getParameter("u_diffuseTexture")->setValue(texturePath, mipmap); if (mipmap) { sampler->setFilterMode(Texture::LINEAR_MIPMAP_LINEAR, Texture::LINEAR); } else { sampler->setFilterMode(Texture::LINEAR, Texture::LINEAR); } sampler->setWrapMode(Texture::CLAMP, Texture::CLAMP); material->getStateBlock()->setCullFace(true); material->getStateBlock()->setDepthTest(true); material->getStateBlock()->setDepthWrite(true); material->getStateBlock()->setBlend(true); material->getStateBlock()->setBlendSrc(RenderState::BLEND_SRC_ALPHA); material->getStateBlock()->setBlendDst(RenderState::BLEND_ONE_MINUS_SRC_ALPHA); }
int lua_TextureSampler_setFilterMode(lua_State* state) { // Get the number of parameters. int paramCount = lua_gettop(state); // Attempt to match the parameters to a valid binding. switch (paramCount) { case 3: { if ((lua_type(state, 1) == LUA_TUSERDATA) && (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) && (lua_type(state, 3) == LUA_TSTRING || lua_type(state, 3) == LUA_TNIL)) { // Get parameter 1 off the stack. Texture::Filter param1 = (Texture::Filter)lua_enumFromString_TextureFilter(luaL_checkstring(state, 2)); // Get parameter 2 off the stack. Texture::Filter param2 = (Texture::Filter)lua_enumFromString_TextureFilter(luaL_checkstring(state, 3)); Texture::Sampler* instance = getInstance(state); instance->setFilterMode(param1, param2); return 0; } else { lua_pushstring(state, "lua_TextureSampler_setFilterMode - Failed to match the given parameters to a valid function signature."); lua_error(state); } break; } default: { lua_pushstring(state, "Invalid number of parameters (expected 3)."); lua_error(state); break; } } return 0; }
void BillboardSample::loadGround() { // Just a basic plane for this sample Mesh* mesh = Mesh::createQuad(-(GROUND_WIDTH / 2.0f), -(GROUND_HEIGHT / 2.0f), GROUND_WIDTH, GROUND_HEIGHT); Node* node = Node::create(); _ground = Model::create(mesh); SAFE_RELEASE(mesh); node->setDrawable(_ground); _scene->addNode(node); node->rotateX(MATH_DEG_TO_RAD(90)); Effect* effect = Effect::createFromFile("res/shaders/textured.vert", "res/shaders/textured.frag", "TEXTURE_REPEAT"); Material* material = Material::create(effect); material->getStateBlock()->setDepthTest(true); material->getStateBlock()->setBlend(false); Texture::Sampler* sampler = material->getParameter("u_diffuseTexture")->setValue("res/png/dirt.png", true); sampler->setFilterMode(Texture::LINEAR_MIPMAP_LINEAR, Texture::LINEAR); material->getParameter("u_textureRepeat")->setValue(Vector2(GROUND_REPEAT_TEXTURE, GROUND_REPEAT_TEXTURE)); material->setParameterAutoBinding("u_worldViewProjectionMatrix", RenderState::WORLD_VIEW_PROJECTION_MATRIX); _ground->setMaterial(material); SAFE_RELEASE(material); SAFE_RELEASE(effect) SAFE_RELEASE(node); }
void CreateSceneSample::initialize() { // Create the font for drawing the framerate. _font = Font::create("res/ui/arial.gpb"); // Create a new empty scene. _scene = Scene::create(); // Create the camera. Camera* camera = Camera::createPerspective(45.0f, getAspectRatio(), 1.0f, 10.0f); Node* cameraNode = _scene->addNode("camera"); // Attach the camera to a node. This determines the position of the camera. cameraNode->setCamera(camera); // Make this the active camera of the scene. _scene->setActiveCamera(camera); SAFE_RELEASE(camera); // Move the camera to look at the origin. cameraNode->translate(0, 1, 5); cameraNode->rotateX(MATH_DEG_TO_RAD(-11.25f)); // Create a white light. Light* light = Light::createDirectional(0.75f, 0.75f, 0.75f); Node* lightNode = _scene->addNode("light"); lightNode->setLight(light); // Release the light because the node now holds a reference to it. SAFE_RELEASE(light); lightNode->rotateX(MATH_DEG_TO_RAD(-45.0f)); // Create the cube mesh and model. Mesh* cubeMesh = createCubeMesh(); Model* cubeModel = Model::create(cubeMesh); // Release the mesh because the model now holds a reference to it. SAFE_RELEASE(cubeMesh); // Create the material for the cube model and assign it to the first mesh part. Material* material = cubeModel->setMaterial("res/shaders/textured.vert", "res/shaders/textured.frag", "DIRECTIONAL_LIGHT_COUNT 1"); // These parameters are normally set in a .material file but this example sets them programmatically. // Bind the uniform "u_worldViewProjectionMatrix" to use the WORLD_VIEW_PROJECTION_MATRIX from the scene's active camera and the node that the model belongs to. material->setParameterAutoBinding("u_worldViewProjectionMatrix", "WORLD_VIEW_PROJECTION_MATRIX"); material->setParameterAutoBinding("u_inverseTransposeWorldViewMatrix", "INVERSE_TRANSPOSE_WORLD_VIEW_MATRIX"); // Set the ambient color of the material. material->getParameter("u_ambientColor")->setValue(Vector3(0.2f, 0.2f, 0.2f)); // Bind the light's color and direction to the material. material->getParameter("u_directionalLightColor[0]")->setValue(lightNode->getLight()->getColor()); material->getParameter("u_directionalLightDirection[0]")->bindValue(lightNode, &Node::getForwardVectorWorld); // Load the texture from file. Texture::Sampler* sampler = material->getParameter("u_diffuseTexture")->setValue("res/png/crate.png", true); sampler->setFilterMode(Texture::LINEAR_MIPMAP_LINEAR, Texture::LINEAR); material->getStateBlock()->setCullFace(true); material->getStateBlock()->setDepthTest(true); material->getStateBlock()->setDepthWrite(true); _cubeNode = _scene->addNode("cube"); _cubeNode->setModel(cubeModel); _cubeNode->rotateY(MATH_PIOVER4); SAFE_RELEASE(cubeModel); }
void Material::loadRenderState(RenderState* renderState, Properties* properties) { GP_ASSERT(renderState); GP_ASSERT(properties); // Rewind the properties to start reading from the start. properties->rewind(); const char* name; while ((name = properties->getNextProperty())) { if (isMaterialKeyword(name)) continue; // keyword - skip switch (properties->getType()) { case Properties::NUMBER: GP_ASSERT(renderState->getParameter(name)); renderState->getParameter(name)->setValue(properties->getFloat()); break; case Properties::VECTOR2: { Vector2 vector2; if (properties->getVector2(NULL, &vector2)) { GP_ASSERT(renderState->getParameter(name)); renderState->getParameter(name)->setValue(vector2); } } break; case Properties::VECTOR3: { Vector3 vector3; if (properties->getVector3(NULL, &vector3)) { GP_ASSERT(renderState->getParameter(name)); renderState->getParameter(name)->setValue(vector3); } } break; case Properties::VECTOR4: { Vector4 vector4; if (properties->getVector4(NULL, &vector4)) { GP_ASSERT(renderState->getParameter(name)); renderState->getParameter(name)->setValue(vector4); } } break; case Properties::MATRIX: { Matrix matrix; if (properties->getMatrix(NULL, &matrix)) { GP_ASSERT(renderState->getParameter(name)); renderState->getParameter(name)->setValue(matrix); } } break; default: { // Assume this is a parameter auto-binding. renderState->setParameterAutoBinding(name, properties->getString()); } break; } } // Iterate through all child namespaces searching for samplers and render state blocks. Properties* ns; while ((ns = properties->getNextNamespace())) { if (strcmp(ns->getNamespace(), "sampler") == 0) { // Read the texture uniform name. name = ns->getId(); if (strlen(name) == 0) { GP_ERROR("Texture sampler is missing required uniform name."); continue; } // Get the texture path. const char* path = ns->getString("path"); if (path == NULL || strlen(path) == 0) { GP_ERROR("Texture sampler '%s' is missing required image file path.", name); continue; } // Read texture state (booleans default to 'false' if not present). bool mipmap = ns->getBool("mipmap"); Texture::Wrap wrapS = parseTextureWrapMode(ns->getString("wrapS"), Texture::REPEAT); Texture::Wrap wrapT = parseTextureWrapMode(ns->getString("wrapT"), Texture::REPEAT); Texture::Filter minFilter = parseTextureFilterMode(ns->getString("minFilter"), mipmap ? Texture::NEAREST_MIPMAP_LINEAR : Texture::LINEAR); Texture::Filter magFilter = parseTextureFilterMode(ns->getString("magFilter"), Texture::LINEAR); // Set the sampler parameter. GP_ASSERT(renderState->getParameter(name)); Texture::Sampler* sampler = renderState->getParameter(name)->setValue(path, mipmap); if (sampler) { sampler->setWrapMode(wrapS, wrapT); sampler->setFilterMode(minFilter, magFilter); } } else if (strcmp(ns->getNamespace(), "renderState") == 0) { while ((name = ns->getNextProperty())) { GP_ASSERT(renderState->getStateBlock()); renderState->getStateBlock()->setState(name, ns->getString()); } } } }