// ---------------------------------------------------------------- // Name: renderRectangle // Description: 根据参数TRectanglef提供的参数绘制矩形 // Return Value: void // ---------------------------------------------------------------- void Helper::renderRectangle( const TRectanglef& rect, Color4 color, bool isEmpty ) { float left = rect.m_Left; float right = rect.m_Right; float bottom = rect.m_Bottom; float top = rect.m_Top; glPushMatrix(); { glColor4f( color.r(), color.g(), color.b(), color.a() ); glLineWidth(2.0); if( isEmpty ) { glBegin(GL_LINE_STRIP); glVertex3f(left, bottom, 0.0); glVertex3f(right, bottom, 0.0); glVertex3f(right, top, 0.0); glVertex3f(left, top, 0.0); glVertex3f(left, bottom, 0.0); glEnd(); } else { glBegin(GL_QUADS); glVertex3f(left, bottom, 0.0); glVertex3f(right, bottom, 0.0); glVertex3f(right, top, 0.0); glVertex3f(left, top, 0.0); glEnd(); } glLineWidth(1.0); } glPopMatrix(); }
// ---------------------------------------------------------------- // Name: renderBox // Description: 根据参数Box提供的参数绘制box线框 // Return Value: void // ---------------------------------------------------------------- void Helper::renderBox(const Box3f& box, Color4 color) { glPushMatrix(); { glColor4f( color.r(), color.g(), color.b(), color.a() ); ColorCube::renderColorCube( box ); } glPopMatrix(); }
/* ------------------------------------------------------- */ D3DCOLORVALUE DX::Convert(const Color4& color) { D3DCOLORVALUE result; result.a = color.A(); result.r = color.R(); result.g = color.G(); result.b = color.B(); return result; }
Color4ub toColor4ub(const Color4& _color) { using gex::color::clamp; return Color4ub( clamp(int(255*_color.r()),0,255), clamp(int(255*_color.g()),0,255), clamp(int(255*_color.b()),0,255), clamp(int(255*_color.a()),0,255)); }
void D3D10System::ClearColorBuffer(DWORD color) { Color4 floatColor; floatColor.MakeFromRGBA(color); D3D10Texture *d3dTex = static_cast<D3D10Texture*>(curRenderTarget); if(d3dTex) d3d->ClearRenderTargetView(d3dTex->renderTarget, floatColor.ptr); else d3d->ClearRenderTargetView(swapRenderView, floatColor.ptr); }
void Board::draw() { //doing this without textures just to make this work. //this should be chopped into squares for tiling the texture //or there's a gl function for doing so. whatevs Color4 myColor = ColorScheme::GREEN; glBegin(GL_QUADS); glColor4fv(myColor.toArray()); glVertex2f(_center.x - Game::HBOUND/2.0f, _center.y - Game::VBOUND/2.0f); glVertex2f(_center.x + Game::HBOUND/2.0f, _center.y - Game::VBOUND/2.0f); glVertex2f(_center.x + Game::HBOUND/2.0f, _center.y + Game::VBOUND/2.0f); glVertex2f(_center.x - Game::HBOUND/2.0f, _center.y + Game::VBOUND/2.0f); glEnd(); }
void CurvePreview::Redraw() { if (!mCurve) return; TextureRef texture = mSprite->GetTexture(); if (!texture || texture->GetSize() != layout->GetSize()) { texture = TextureRef(layout->GetSize(), PixelFormat::R8G8B8A8, Texture::Usage::RenderTarget); mSprite->SetTexture(texture); mSprite->SetTextureSrcRect(RectI(Vec2I(), texture->GetSize())); } const Color4 backColor(120, 120, 120, 255); const Color4 curveColor(0, 255, 0, 255); Camera prevCamera = o2Render.GetCamera(); Camera currCamera; currCamera.SetRect(mCurve->GetRect()); currCamera.SetScale(currCamera.GetScale().InvertedY()); o2Render.SetRenderTexture(texture); o2Render.SetCamera(currCamera); o2Render.Clear(backColor); static Vector<Vertex2> buffer; buffer.Clear(); auto curveColorHex = curveColor.ARGB(); auto& keys = mCurve->GetKeys(); for (auto& key : keys) { buffer.Add(Vertex2(key.position, key.value, curveColorHex, 0, 0)); auto points = key.GetApproximatedPoints(); for (int i = 0; i < key.GetApproximatedPointsCount(); i++) buffer.Add(Vertex2(points[i], curveColorHex, 0, 0)); o2Render.DrawAAPolyLine(buffer.Data(), buffer.Count(), 2); } o2Render.UnbindRenderTexture(); o2Render.SetCamera(prevCamera); }
// ---------------------------------------------------------------- // Name: renderElipse // Description: 根据参数TRectanglef提供的参数绘制椭圆 // Return Value: void // ---------------------------------------------------------------- void Helper::renderElipse( const TRectanglef& rect, Color4 color, bool isEmpty ) { float xpos = rect.GetCenter().x(); float ypos = rect.GetCenter().y(); float xradius = rect.GetSize().x() / 2; float yradius = rect.GetSize().y() / 2; const float DEG2RAD = 3.14159/180; glPushMatrix(); { glColor4f( color.r(), color.g(), color.b(), color.a() ); glLineWidth(5.0); if( isEmpty ) { glBegin(GL_LINE_LOOP); for (int i=0; i<360; i++) { //convert degrees into radians float degInRad = i*DEG2RAD; glVertex2f( xpos + cos(degInRad) * xradius, ypos + sin(degInRad) * yradius ); } glEnd(); } else { glBegin(GL_TRIANGLE_FAN); glVertex2f(xpos, ypos); //draw the vertex on the contour of the circle for(int i = 0; i < 360; i++) { float degInRad = i*DEG2RAD; glVertex2f( xpos + cos(degInRad) * xradius, ypos + sin(degInRad) * yradius ); } glVertex2f( 1.0 * xradius + xpos, 0.0 *yradius + ypos ); glEnd(); } glLineWidth(1.0); }glPopMatrix(); }
GLvoid Material::SetSpecularColor(GLenum face, const Color4& c) { switch (face) { case GL_FRONT: _front_specular.r() = c.r(); _front_specular.g() = c.g(); _front_specular.b() = c.b(); _front_specular.a() = c.a(); break; case GL_BACK: _back_specular.r() = c.r(); _back_specular.g() = c.g(); _back_specular.b() = c.b(); _back_specular.a() = c.a(); break; case GL_FRONT_AND_BACK: _front_specular.r() = c.r(); _front_specular.g() = c.g(); _front_specular.b() = c.b(); _front_specular.a() = c.a(); _back_specular.r() = c.r(); _back_specular.g() = c.g(); _back_specular.b() = c.b(); _back_specular.a() = c.a(); break; } }
void Light(float colorOut[4], const float colorIn[4], Vec3 pos, Vec3 normal, float dots[4]) { // could cache a lot of stuff, such as ambient, across vertices... bool doShadeMapping = (gstate.texmapmode & 0x3) == 2; if (!doShadeMapping && !(gstate.lightEnable[0]&1) && !(gstate.lightEnable[1]&1) && !(gstate.lightEnable[2]&1) && !(gstate.lightEnable[3]&1)) { memcpy(colorOut, colorIn, sizeof(float) * 4); return; } Color4 emissive; emissive.GetFromRGB(gstate.materialemissive); Color4 globalAmbient; globalAmbient.GetFromRGB(gstate.ambientcolor); globalAmbient.GetFromA(gstate.ambientalpha); Vec3 norm = normal.Normalized(); Color4 in(colorIn); Color4 ambient; if (gstate.materialupdate & 1) { ambient = in; } else { ambient.GetFromRGB(gstate.materialambient); ambient.a=1.0f; } Color4 diffuse; if (gstate.materialupdate & 2) { diffuse = in; } else { diffuse.GetFromRGB(gstate.materialdiffuse); diffuse.a=1.0f; } Color4 specular; if (gstate.materialupdate & 4) { specular = in; } else { specular.GetFromRGB(gstate.materialspecular); specular.a=1.0f; } float specCoef = getFloat24(gstate.materialspecularcoef); norm.Normalize(); Vec3 viewer(gstate.viewMatrix[8], gstate.viewMatrix[9], gstate.viewMatrix[10]); Color4 lightSum = globalAmbient * ambient + emissive; // Try lights.elf - there's something wrong with the lighting for (int l = 0; l < 4; l++) { // can we skip this light? if ((gstate.lightEnable[l] & 1) == 0) // && !doShadeMapping) continue; GELightComputation comp = (GELightComputation)(gstate.ltype[l]&3); GELightType type = (GELightType)((gstate.ltype[l]>>8)&3); Vec3 toLight; if (type == GE_LIGHTTYPE_DIRECTIONAL) toLight = Vec3(gstate.lightpos[l]); else toLight = Vec3(gstate.lightpos[l]) - pos; Vec3 dir = Vec3(gstate.lightdir[l]); bool doSpecular = (comp != GE_LIGHTCOMP_ONLYDIFFUSE); bool poweredDiffuse = comp == GE_LIGHTCOMP_BOTHWITHPOWDIFFUSE; float distance = toLight.Normalize(); float lightScale = 1.0f; if (type != GE_LIGHTTYPE_DIRECTIONAL) { lightScale = 1.0f / (gstate.lightatt[l][0] + gstate.lightatt[l][1]*distance + gstate.lightatt[l][2]*distance*distance); if (lightScale>1.0f) lightScale=1.0f; } float dot = toLight * norm; // Clamp dot to zero. if (dot < 0.0f) dot = 0.0f; if (poweredDiffuse) dot = powf(dot, specCoef); Color4 diff = (gstate.lightColor[1][l] * diffuse) * (dot*lightScale); Color4 spec(0,0,0,0); if (doSpecular) { Vec3 halfVec = toLight; halfVec += viewer.Normalized(); halfVec.Normalize(); dot = halfVec * norm; if (dot >= 0) { spec += (gstate.lightColor[2][l] * specular * (powf(dot, specCoef)*lightScale)); } } dots[l] = dot; if (gstate.lightEnable[l] & 1) { lightSum += gstate.lightColor[0][l]*ambient + diff + spec; } } for (int i = 0; i < 3; i++) colorOut[i] = lightSum[i]; }
void ParticleSystem::spawnFace (const FaceScatter& faceScatter, const ParseOBJ& parseObj, const Color4& color, const Matrix4& transform, int materialIndex) { const Color4unorm8 c(color.pow(1.0f / SmokeVolumeSet::PARTICLE_GAMMA)); Random rnd(10000, false); m_particle.resize(0); m_physicsData.resize(0); AABox bounds; for (ParseOBJ::GroupTable::Iterator git = parseObj.groupTable.begin(); git.isValid(); ++git) { const shared_ptr<ParseOBJ::Group>& group = git->value; for (ParseOBJ::MeshTable::Iterator mit = group->meshTable.begin(); mit.isValid(); ++mit) { const shared_ptr<ParseOBJ::Mesh>& mesh = mit->value; for (int f = 0; f < mesh->faceArray.size(); ++f) { if (rnd.uniform() <= faceScatter.particlesPerFace) { const ParseOBJ::Face& face = mesh->faceArray[f]; Particle& particle = m_particle.next(); PhysicsData& physicsData = m_physicsData.next(); // Use the average vertex as the position Point3 vrt[10]; particle.position = Point3::zero(); for (int v = 0; v < face.size(); ++v) { vrt[v] = (transform * Vector4(parseObj.vertexArray[face[v].vertex], 1.0f)).xyz(); particle.position += vrt[v]; } particle.position /= float(face.size()); const Vector3& normal = (vrt[1] - vrt[0]).cross(vrt[2] - vrt[0]).direction(); particle.position += faceScatter.explodeDistance * normal; particle.normal = normal; particle.materialIndex = materialIndex; particle.color = c; particle.angle = rnd.uniform(0.0f, 2.0f) * pif(); particle.radius = min(faceScatter.maxRadius, faceScatter.radiusScaleFactor * pow((particle.position - vrt[0]).length(), faceScatter.radiusExponent)); particle.emissive = 0; physicsData.angularVelocity = rnd.uniform(-1.0f, 1.0f) * (360.0f * units::degrees()) / (20.0f * units::seconds()); bounds.merge(particle.position); } } // face } // mesh } // group // Center if (faceScatter.moveCenterToOrigin) { const Vector3& offset = -bounds.center(); for (int i = 0; i < m_particle.size(); ++i) { m_particle[i].position += offset; } m_lastObjectSpaceAABoxBounds = bounds + offset; } else { m_lastObjectSpaceAABoxBounds = bounds; } }
static tuple getinitargs(Color4 const& c) { return make_tuple(c.getRed(),c.getGreen(),c.getBlue(),c.getAlpha()); }
GLvoid Material::SetAmbientColor(GLenum face, const Color4& c) { SetAmbientColor(face, c.r(), c.g(), c.b(), c.a()); }
/* ------------------------------------------------------- */ unsigned int DX::ConvertUInt(const Color4& color) { return D3DCOLOR_ARGB(int(color.A()*255.0f), int(color.R()*255.0f), int(color.G()*255.0f), int(color.B()*255.0f)); }
Color3::Color3( const Color4& c4) : Tuple3<uchar_t>(c4.getRed(),c4.getGreen(),c4.getBlue()) { }
//[-------------------------------------------------------] //[ Private virtual SRPDirectionalLighting functions ] //[-------------------------------------------------------] void SRPDirectionalLightingFixedFunctions::DrawMesh(Renderer &cRenderer, const SQCull &cCullQuery, const VisNode &cVisNode, SceneNode &cSceneNode, const MeshHandler &cMeshHandler, const Mesh &cMesh, const MeshLODLevel &cMeshLODLevel, VertexBuffer &cVertexBuffer) { // Get the fixed functions interface (when we're in here, we know that it must exist!) FixedFunctions *pFixedFunctions = cRenderer.GetFixedFunctions(); // Set the current world matrix pFixedFunctions->SetTransformState(FixedFunctions::Transform::World, cVisNode.GetWorldMatrix()); // Get buffers IndexBuffer *pIndexBuffer = cMeshLODLevel.GetIndexBuffer(); const Array<Geometry> &lstGeometries = *cMeshLODLevel.GetGeometries(); // Bind buffers cRenderer.SetIndexBuffer(pIndexBuffer); pFixedFunctions->SetVertexBuffer(&cVertexBuffer); // Draw mesh for (uint32 nMat=0; nMat<cMeshHandler.GetNumOfMaterials(); nMat++) { // Get mesh material const Material *pMaterial = cMeshHandler.GetMaterial(nMat); if (pMaterial && !pMaterial->GetEffect()) { // Draw geometries for (uint32 nGeo=0; nGeo<lstGeometries.GetNumOfElements(); nGeo++) { // Is this geometry active and is it using the current used mesh material? const Geometry &cGeometry = lstGeometries[nGeo]; if (cGeometry.IsActive() && nMat == cGeometry.GetMaterial()) { // Transparent material? static const String sOpacity = "Opacity"; const Parameter *pParameter = pMaterial->GetParameter(sOpacity); if ((GetFlags() & TransparentPass) ? (pParameter && pParameter->GetValue1f() < 1.0f) : (!pParameter || pParameter->GetValue1f() >= 1.0f)) { // Material change? if (m_pCurrentMaterial != pMaterial) { // Update current material m_nMaterialChanges++; m_pCurrentMaterial = pMaterial; // Get opacity const float fOpacity = pParameter ? pParameter->GetValue1f() : 1.0f; if (fOpacity < 1) { // Get and set source blend function uint32 nValue = BlendFunc::SrcAlpha; static const String sSrcBlendFunc = "SrcBlendFunc"; pParameter = pMaterial->GetParameter(sSrcBlendFunc); if (pParameter) { m_pRenderStates->SetAttribute("SrcBlendFunc", pParameter->GetParameterString()); nValue = m_pRenderStates->Get(RenderState::SrcBlendFunc); } cRenderer.SetRenderState(RenderState::SrcBlendFunc, nValue); // Get and set destination blend function nValue = BlendFunc::InvSrcAlpha; static const String sDstBlendFunc = "DstBlendFunc"; pParameter = pMaterial->GetParameter(sDstBlendFunc); if (pParameter) { m_pRenderStates->SetAttribute("DstBlendFunc", pParameter->GetParameterString()); nValue = m_pRenderStates->Get(RenderState::DstBlendFunc); } cRenderer.SetRenderState(RenderState::DstBlendFunc, nValue); } // Setup cull mode static const String sTwoSided = "TwoSided"; pParameter = pMaterial->GetParameter(sTwoSided); cRenderer.SetRenderState(RenderState::CullMode, (pParameter && pParameter->GetValue1f()) == 1.0f ? Cull::None : Cull::CCW); // Setup transparency and diffuse color static const String sDiffuseColor = "DiffuseColor"; pParameter = pMaterial->GetParameter(sDiffuseColor); if (pParameter) { float fDiffuseColor[3] = { 1.0f, 1.0f, 1.0f }; pParameter->GetValue3f(fDiffuseColor[0], fDiffuseColor[1], fDiffuseColor[2]); pFixedFunctions->SetColor(Color4(fDiffuseColor[0], fDiffuseColor[1], fDiffuseColor[2], fOpacity)); } else { pFixedFunctions->SetColor(Color4(1.0f, 1.0f, 1.0f, fOpacity)); } // Specular highlight if (GetFlags() & NoSpecular) { pFixedFunctions->SetMaterialState(FixedFunctions::MaterialState::Specular, Color4::Black.ToUInt32()); pFixedFunctions->SetMaterialState(FixedFunctions::MaterialState::Shininess, 0); } else { static const String sSpecularColor = "SpecularColor"; static const String sSpecularExponent = "SpecularExponent"; float fSpecularExponent = 45.0f; Color4 cSpecularColor = Color4::White; // First, get specular color - if it's 0, we don't have any specular at all pParameter = pMaterial->GetParameter(sSpecularColor); if (pParameter) cSpecularColor = pParameter->GetValue3fv(); if (cSpecularColor != 0.0f) { // Get specular exponent pParameter = pMaterial->GetParameter(sSpecularExponent); if (pParameter) pParameter->GetValue1f(fSpecularExponent); } // Set material states pFixedFunctions->SetMaterialState(FixedFunctions::MaterialState::Specular, cSpecularColor.ToUInt32()); pFixedFunctions->SetMaterialState(FixedFunctions::MaterialState::Shininess, Tools::FloatToUInt32(fSpecularExponent)); } // Bind textures // Diffuse map (stage 0) const Texture *pTexture = nullptr; if (GetFlags() & NoDiffuseMap) cRenderer.SetTextureBuffer(0, nullptr); else { pParameter = pMaterial->GetParameter(Material::DiffuseMap); if (pParameter) pTexture = pParameter->GetValueTexture(); if (pTexture && pTexture->GetTextureBuffer()) { pTexture->Bind(0); SetupTextureFiltering(cRenderer, 0); // Enable/disable alpha test - but only if this material is not transparent, else the result may look odd if (pTexture->GetTextureBuffer()->GetComponentsPerPixel() == 4 && fOpacity >= 1) { // Get alpha reference static const String sAlphaReference = "AlphaReference"; pParameter = pMaterial->GetParameter(sAlphaReference); float fAlphaReference = pParameter ? pParameter->GetValue1f() : 0.5f; // Set alpha render states if (fAlphaReference) { pFixedFunctions->SetRenderState(FixedFunctions::RenderState::AlphaTestEnable, true); pFixedFunctions->SetRenderState(FixedFunctions::RenderState::AlphaTestReference, Tools::FloatToUInt32(fAlphaReference)); } else { pFixedFunctions->SetRenderState(FixedFunctions::RenderState::AlphaTestEnable, false); } } else { pFixedFunctions->SetRenderState(FixedFunctions::RenderState::AlphaTestEnable, false); } } else { cRenderer.SetTextureBuffer(0, nullptr); // Disable alpha test pFixedFunctions->SetRenderState(FixedFunctions::RenderState::AlphaTestEnable, false); } } // Light map (stage 1) bool bLightMapUsed = false; if (GetFlags() & NoLightMap) cRenderer.SetTextureBuffer(1, nullptr); else { pTexture = nullptr; pParameter = pMaterial->GetParameter(Material::LightMap); if (pParameter) pTexture = pParameter->GetValueTexture(); if (pTexture) { pTexture->Bind(1); SetupTextureFiltering(cRenderer, 1); bLightMapUsed = true; } else { cRenderer.SetTextureBuffer(1, nullptr); } } // Ambient occlusion map (stage 1) if (!bLightMapUsed) { if (GetFlags() & NoAmbientOcclusionMap) cRenderer.SetTextureBuffer(1, nullptr); else { pTexture = nullptr; pParameter = pMaterial->GetParameter(Material::AmbientOcclusionMap); if (pParameter) pTexture = pParameter->GetValueTexture(); if (pTexture) { pTexture->Bind(1); SetupTextureFiltering(cRenderer, 1); } else { cRenderer.SetTextureBuffer(1, nullptr); } } } // Reflection map (stage 2) if (GetFlags() & NoReflectionMap) cRenderer.SetTextureBuffer(2, nullptr); else { pTexture = nullptr; pParameter = pMaterial->GetParameter(Material::ReflectionMap); if (pParameter) pTexture = pParameter->GetValueTexture(); if (pTexture) { pTexture->Bind(2); SetupTextureFiltering(cRenderer, 2); // 2D or cube ? const TextureBuffer *pTextureBuffer = pTexture->GetTextureBuffer(); if (pTextureBuffer) { if (pTextureBuffer->GetType() == TextureBuffer::TypeTextureBufferCube) { pFixedFunctions->SetTextureStageState(2, FixedFunctions::TextureStage::TexGen, FixedFunctions::TexCoordGen::ReflectionMap); cRenderer.SetSamplerState(2, Sampler::AddressU, TextureAddressing::Clamp); cRenderer.SetSamplerState(2, Sampler::AddressV, TextureAddressing::Clamp); cRenderer.SetSamplerState(2, Sampler::AddressW, TextureAddressing::Clamp); } else if (pTextureBuffer->GetType() == TextureBuffer::TypeTextureBuffer2D) { pFixedFunctions->SetTextureStageState(2, FixedFunctions::TextureStage::TexGen, FixedFunctions::TexCoordGen::SphereMap); cRenderer.SetSamplerState(2, Sampler::AddressU, TextureAddressing::Wrap); cRenderer.SetSamplerState(2, Sampler::AddressV, TextureAddressing::Wrap); cRenderer.SetSamplerState(2, Sampler::AddressW, TextureAddressing::Wrap); } } } else { cRenderer.SetTextureBuffer(2, nullptr); } } } // Draw geometry cRenderer.DrawIndexedPrimitives( cGeometry.GetPrimitiveType(), 0, cVertexBuffer.GetNumOfElements()-1, cGeometry.GetStartIndex(), cGeometry.GetIndexSize() ); } } } } } }
GLvoid Material::SetDiffuseColor(GLenum face, const Color4& c) { switch (face) { case GL_FRONT: _front_diffuse.r() = c.r(); _front_diffuse.g() = c.g(); _front_diffuse.b() = c.b(); _front_diffuse.a() = c.a(); break; case GL_BACK: _back_diffuse.r() = c.r(); _back_diffuse.g() = c.g(); _back_diffuse.b() = c.b(); _back_diffuse.a() = c.a(); break; case GL_FRONT_AND_BACK: _front_diffuse.r() = c.r(); _front_diffuse.g() = c.g(); _front_diffuse.b() = c.b(); _front_diffuse.a() = c.a(); _back_diffuse.r() = c.r(); _back_diffuse.g() = c.g(); _back_diffuse.b() = c.b(); _back_diffuse.a() = c.a(); break; } }
static std::string Color_str(const Color4& c) { return stringf("<rgba: %.2f %.2f %.2f %.2f>", c.r(), c.g(), c.b(), c.a()); }