void Form::setNode(Node* node) { // If the user wants a custom node then we need to create a 3D quad if (node && node != _node) { // Set this Form up to be 3D by initializing a quad. float x2 = _bounds.width; float y2 = _bounds.height; float vertices[] = { 0, y2, 0, 0, _v1, 0, 0, 0, 0, 0, x2, y2, 0, _u2, _v1, x2, 0, 0, _u2, 0 }; VertexFormat::Element elements[] = { VertexFormat::Element(VertexFormat::POSITION, 3), VertexFormat::Element(VertexFormat::TEXCOORD0, 2) }; Mesh* mesh = Mesh::createMesh(VertexFormat(elements, 2), 4, false); GP_ASSERT(mesh); mesh->setPrimitiveType(Mesh::TRIANGLE_STRIP); mesh->setVertexData(vertices, 0, 4); _nodeQuad = Model::create(mesh); SAFE_RELEASE(mesh); GP_ASSERT(_nodeQuad); // Create the effect and material Effect* effect = createEffect(); GP_ASSERT(effect); _nodeMaterial = Material::create(effect); GP_ASSERT(_nodeMaterial); _nodeQuad->setMaterial(_nodeMaterial); _nodeMaterial->release(); node->setModel(_nodeQuad); _nodeQuad->release(); // Bind the WorldViewProjection matrix. _nodeMaterial->setParameterAutoBinding("u_worldViewProjectionMatrix", RenderState::WORLD_VIEW_PROJECTION_MATRIX); // Bind the texture from the framebuffer and set the texture to clamp Texture::Sampler* sampler = Texture::Sampler::create(_frameBuffer->getRenderTarget()->getTexture()); GP_ASSERT(sampler); sampler->setWrapMode(Texture::CLAMP, Texture::CLAMP); _nodeMaterial->getParameter("u_texture")->setValue(sampler); sampler->release(); RenderState::StateBlock* rsBlock = _nodeMaterial->getStateBlock(); rsBlock->setDepthWrite(true); rsBlock->setBlend(true); rsBlock->setBlendSrc(RenderState::BLEND_SRC_ALPHA); rsBlock->setBlendDst(RenderState::BLEND_ONE_MINUS_SRC_ALPHA); } _node = node; }
void Form::initializeQuad(Mesh* mesh) { // Release current model. SAFE_RELEASE(_quad); // Create the model _quad = Model::create(mesh); // Create the material Material* material = _quad->setMaterial("res/shaders/textured.vsh", "res/shaders/textured.fsh"); // Set the common render state block for the material RenderState::StateBlock* stateBlock = _theme->getSpriteBatch()->getStateBlock(); stateBlock->setDepthWrite(true); material->setStateBlock(stateBlock); // Bind the WorldViewProjection matrix material->setParameterAutoBinding("u_worldViewProjectionMatrix", RenderState::WORLD_VIEW_PROJECTION_MATRIX); // Create a FrameBuffer if necessary. if (!_frameBuffer) { _frameBuffer = FrameBuffer::create(_id.c_str()); } // Use the FrameBuffer to texture the quad. if (!_frameBuffer->getRenderTarget()) { RenderTarget* rt = RenderTarget::create(_id.c_str(), _bounds.width, _bounds.height); _frameBuffer->setRenderTarget(rt); SAFE_RELEASE(rt); } Texture::Sampler* sampler = Texture::Sampler::create(_frameBuffer->getRenderTarget()->getTexture()); sampler->setWrapMode(Texture::CLAMP, Texture::CLAMP); material->getParameter("u_texture")->setValue(sampler); material->getParameter("u_textureRepeat")->setValue(Vector2::one()); material->getParameter("u_textureTransform")->setValue(Vector2::zero()); SAFE_RELEASE(sampler); }
int lua_TextureSampler_setWrapMode(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::Wrap param1 = (Texture::Wrap)lua_enumFromString_TextureWrap(luaL_checkstring(state, 2)); // Get parameter 2 off the stack. Texture::Wrap param2 = (Texture::Wrap)lua_enumFromString_TextureWrap(luaL_checkstring(state, 3)); Texture::Sampler* instance = getInstance(state); instance->setWrapMode(param1, param2); return 0; } else { lua_pushstring(state, "lua_TextureSampler_setWrapMode - 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 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); }
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()); } } } }