float4 operator *(const float4 &lhs, const float3x4 &rhs) { ///\todo SSE. return float4(DOT3STRIDED(lhs, rhs.ptr(), 4), DOT3STRIDED(lhs, rhs.ptr()+1, 4), DOT3STRIDED(lhs, rhs.ptr()+2, 4), DOT3STRIDED(lhs, rhs.ptr()+3, 4) + lhs.w); }
float4x4 MUST_USE_RESULT Quat::ToFloat4x4(const float3 &translation) const { #if defined(MATH_AUTOMATIC_SSE) && defined(MATH_SSE) return ToFloat4x4(float4(translation, 1.f)); #else return float4x4(*this, translation); #endif }
float3 MUST_USE_RESULT Quat::Transform(float X, float Y, float Z) const { #if defined(MATH_AUTOMATIC_SSE) && defined(MATH_SIMD) return float4(quat_transform_vec4(q, set_ps(0.f, Z, Y, X))).xyz(); #else return Transform(float3(X, Y, Z)); #endif }
float4 characteristic_poly(mat4 p) { float2 v1 = (p.v[0].xy * p.v[1].yx) + (p.v[0].xz * p.v[2].zx) + (p.v[0].xw * p.v[3].wx) + (p.v[1].yz * p.v[2].zy) + (p.v[1].yw * p.v[3].wy) + (p.v[2].zw * p.v[3].wz); return float4(determinant(p), -dot(p.v[1].yzw, cross(p.v[2].yzw, p.v[3].yzw)) - dot(p.v[0].xzw, cross(p.v[2].xzw, p.v[3].xzw)) - dot(p.v[0].xyw, cross(p.v[1].xyw, p.v[3].xyw)) - dot(p.v[0].xyz, cross(p.v[1].xyz, p.v[2].xyz)), v1.x - v1.y, -trace(p)); }
void Scene::Mesh::Primitive::Init( Mesh& _Owner, const U8*& _pData ) { int MaterialID = ReadU16( _pData, true ); ASSERT( MaterialID < _Owner.m_Owner.m_MaterialsCount, "Material ID out of range!" ); m_pMaterial = _Owner.m_Owner.m_ppMaterials[MaterialID]; m_FacesCount = ReadU32( _pData ); m_VerticesCount = ReadU32( _pData ); // Read BBox in local space m_LocalBBoxMin.x = ReadF32( _pData ); m_LocalBBoxMin.y = ReadF32( _pData ); m_LocalBBoxMin.z = ReadF32( _pData ); m_LocalBBoxMax.x = ReadF32( _pData ); m_LocalBBoxMax.y = ReadF32( _pData ); m_LocalBBoxMax.z = ReadF32( _pData ); // Read indices m_pFaces = new U32[3*m_FacesCount]; if ( m_VerticesCount <= 65536 ) { for ( U32 FaceIndex=0; FaceIndex < m_FacesCount; FaceIndex++ ) { m_pFaces[3*FaceIndex+0] = ReadU16( _pData ); m_pFaces[3*FaceIndex+1] = ReadU16( _pData ); m_pFaces[3*FaceIndex+2] = ReadU16( _pData ); } } else { int IndexBufferSize = 3*m_FacesCount*sizeof(U32); memcpy( m_pFaces, _pData, IndexBufferSize ); _pData += IndexBufferSize; } // Read vertices m_VertexFormat = (VERTEX_FORMAT) *_pData++; int VertexSize = 0; switch ( m_VertexFormat ) { case P3N3G3B3T2: VertexSize = (3+3+3+3+2) * sizeof(float); break; } int VertexBufferSize = m_VerticesCount * VertexSize; m_pVertices = new U8[VertexBufferSize]; memcpy( m_pVertices, _pData, VertexBufferSize ); _pData += VertexBufferSize; // Compute global bounding box m_GlobalBBoxMin = float3::MaxFlt; m_GlobalBBoxMax = -float3::MaxFlt; for ( U32 VertexIndex=0; VertexIndex < m_VerticesCount; VertexIndex++ ) { float3 LocalPosition = *((float3*) ((U8*) m_pVertices + VertexIndex * VertexSize)); float3 WorldPosition = float4( LocalPosition, 1 ) * _Owner.m_Local2World; m_GlobalBBoxMin = m_GlobalBBoxMin.Min( WorldPosition ); m_GlobalBBoxMax = m_GlobalBBoxMax.Max( WorldPosition ); } }
matrix4 matrix4::rotation(float4 axis, float angle) { matrix4 result; float sin = std::sin(angle); float cos = std::cos(angle); axis.w = 0.0f; float4 u(axis.normalized()); /* * According to Redbook: * * u = axis/||axis|| * * | 0 -z y | * S = | z 0 -x | * | -y x 0 | * * M = uu^t + cos(a)(I - uu^t) + sin(a)*S * * That is: M.x = (uu^t).x + cos(a)((1 0 0)^t - uu^t.x) + sin(a) (0 -z y)^t * uu^t.x = u.x * u * And so on for the others */ result.x = u.x*u + cos*(float4(1, 0, 0, 0) - u.x*u) + sin * float4(0.0, u.z, -u.y, 0); result.y = u.y*u + cos*(float4(0, 1, 0, 0) - u.y*u) + sin * float4(-u.z, 0.0, u.x, 0); result.z = u.z*u + cos*(float4(0, 0, 1, 0) - u.z*u) + sin * float4(u.y, -u.x, 0.0, 0); result.w = float4(0, 0, 0, 1); return result; }
void particle_debug_draw(particle_system* ps, rtt::fbo* pos_normal_fbo, rtt::fbo* border_fbo) { if(ps == nullptr || pos_normal_fbo == nullptr || border_fbo == nullptr) { return; } r->start_draw(particle_debug_fbo); r->clear(); r->start_2d_draw(); // draw particle_system::internal_particle_data* pdata = ps->get_internal_particle_data(); glEnable(GL_BLEND); egfx->set_blend_mode(gfx::BLEND_MODE::PRE_MUL); glDepthMask(GL_FALSE); // point -> gs: quad gl3shader particle_draw = s->get_gl3shader("PARTICLE DEBUG"); particle_draw->use(); particle_draw->uniform("in_color", float4(0.0f, 0.0f, 1.0f, 1.0f)); particle_draw->uniform("mvpm", matrix4f().ortho(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f)); particle_draw->attribute_array("in_vertex", pdata->ocl_gl_dir_vbo, 4); if(!ps->is_sorting() || (ps->is_sorting() && !ps->is_reentrant_sorting()) || (ps->is_reentrant_sorting() && ps->is_render_intermediate_sorted_buffer())) { // std: use active indices buffer glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pdata->particle_indices_vbo[pdata->particle_indices_swap]); } else if(ps->is_reentrant_sorting() && !ps->is_render_intermediate_sorted_buffer()) { // use previously sorted indices buffer glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, pdata->particle_indices_vbo[1 - pdata->particle_indices_swap]); } else { assert(false && "invalid particle system state"); } glDisable(GL_PROGRAM_POINT_SIZE); glPointSize(2.0f); glDrawElements(GL_POINTS, (GLsizei)pdata->particle_count, GL_UNSIGNED_INT, nullptr); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glEnable(GL_PROGRAM_POINT_SIZE); particle_draw->disable(); glDepthMask(GL_TRUE); egfx->set_blend_mode(gfx::BLEND_MODE::DEFAULT); glDisable(GL_BLEND); r->stop_2d_draw(); r->stop_draw(); //debug_tex->tex_num = particle_debug_fbo->tex_id[0]; }
void AABB::ToLineList(VertexBuffer &vb) const { Array<float3> pos; pos.Resize_pod(NumVerticesInEdgeList()); ToEdgeList(&pos[0]); int startIndex = vb.AppendVertices((int)pos.size()); for(int i = 0; i < (int)pos.size(); ++i) vb.Set(startIndex+i, VDPosition, float4(pos[i], 1.f)); }
StateManager::StateManager ( ) :m_renderScene( false ) ,m_renderFullscreenQuad( false ) ,m_clearColor( float4( 0.0f, 0.0f, 0.0f, 0.0f ) ) ,m_clear( false ) ,m_newRenderTarget( true ) ,m_newClearColor( true ) { }
RBaseDevice::RBaseDevice() { FrameCounter = 0; QueuedDrawCallCounter = 0; QueueCounter = 0; DoDrawcalls = true; SetMainClearValues(float4(0, 0, 0, 0), 1.0f); }
VS_Output VS(VS_Input In) { VS_Output Out; float4 crd = float4(In.vsCoord + In.aiPosition, 1.0); Out.Pos = mul(crd, VP_Matrix); Out.Normal = mul(In.vsNormal, (float3x3)V_Matrix); Out.ViewPos = mul(crd, V_Matrix).xyz; Out.Color = In.aiColor; return Out; }
void TAnimContainer::AnimSetVMD(double fNewSpeed) { if (!this) return; // wywo�ywane z eventu, gdy brak modelu // skala do ustalenia, "cal" japo�ski (sun) to nieco ponad 3cm // X-w lewo, Y-w g�r�, Z-do ty�u // minimalna wysoko�� to -7.66, a nadal musi by� ponad pod�og� // if (pMovementData->iFrame>0) return; //tylko pierwsza ramka vTranslateTo = vector3(0.1 * pMovementData->f3Vector.x, 0.1 * pMovementData->f3Vector.z, 0.1 * pMovementData->f3Vector.y); if (LengthSquared3(vTranslateTo) > 0.0 ? true : LengthSquared3(vTranslation) > 0.0) { // je�li ma by� przesuni�te albo jest przesuni�cie iAnim |= 2; // wy��czy si� samo if (fNewSpeed > 0.0) fTranslateSpeed = fNewSpeed; // pr�dko�� jest mno�nikiem, nie podlega skalowaniu else // za p�no na animacje, trzeba przestawi� vTranslation = vTranslateTo; } // if ((qCurrent.w<1.0)||(pMovementData->qAngle.w<1.0)) { // je�li jest jaki� obr�t if (!mAnim) { mAnim = new float4x4(); // b�dzie potrzebna macierz animacji mAnim->Identity(); // jedynkowanie na pocz�tek } iAnim |= 4; // animacja kwaternionowa qStart = qCurrent; // potrzebna pocz�tkowa do interpolacji //---+ - te� niby dobrze, ale nie tak tr�ca w�osy na pocz�tku (macha w d�) //-+-+ - d�o� ma w g�rze zamiast na pasie w pozycji pocz�tkowej //+--+ - g�owa do ty�u (broda w g�r�) w pozycji pocz�tkowej //--++ - pozycja pocz�tkowa dobra, tr�ca u g�ry, ale z r�kami jako� nie tak, k�ko w // przeciwn� stron� //++++ - k�adzie si� brzuchem do g�ry //-+++ - r�ce w g�rze na pocz�tku, zamiast w d�, �okie� jakby w przeciwn� stron� //+-++ - nie podnosi r�ki do g�owy //++-+ - d�o� ma w g�rze zamiast na pasie qDesired = Normalize(float4(-pMovementData->qAngle.x, -pMovementData->qAngle.z, -pMovementData->qAngle.y, pMovementData->qAngle.w)); // tu trzeba b�dzie osie zamieni� if (fNewSpeed > 0.0) { fAngleSpeed = fNewSpeed; // wtedy animowa� za pomoc� interpolacji fAngleCurrent = 0.0; // pocz�tek interpolacji } else { // za p�no na animacj�, mo�na tylko przestawi� w docelowe miejsce fAngleSpeed = 0.0; fAngleCurrent = 1.0; // interpolacja zako�czona qCurrent = qDesired; } } // if (!strcmp(pSubModel->pName,"?Z?�?^?[")) //jak g��wna ko�� // if (!strcmp(pSubModel->pName,"����?�h�j")) //IK lewej stopy // WriteLog(AnsiString(pMovementData->iFrame)+": "+AnsiString(pMovementData->f3Vector.x)+" // "+AnsiString(pMovementData->f3Vector.y)+" "+AnsiString(pMovementData->f3Vector.z)); }
void LightSource::Color(float3 const & clr) { color_ = float4(clr.x(), clr.y(), clr.z(), MathLib::dot(clr, float3(0.2126f, 0.7152f, 0.0722f))); if (range_ < 0) { this->Range(-1); } }
float4 MUST_USE_RESULT Quat::Transform(const float4 &vec) const { assume(vec.IsWZeroOrOne()); #if defined(MATH_AUTOMATIC_SSE) && defined(MATH_SSE) return quat_transform_vec4(q, vec); #else return float4(Transform(vec.x, vec.y, vec.z), vec.w); #endif }
float PlaneCost(const std::vector<Face> &inputfaces,const float4 &split,const WingMesh &space,int onbrep) { count[COPLANAR] = 0; count[UNDER] = 0; count[OVER] = 0; count[SPLIT] = 0; for(unsigned int i=0;i<inputfaces.size();i++) { count[FaceSplitTest(inputfaces[i],split,FUZZYWIDTH)]++; } if (space.verts.size() == 0) { // The following formula isn't that great. // Better to use volume as well eh. return (float)(abs(count[OVER]-count[UNDER]) + count[SPLIT] - count[COPLANAR]); } float volumeover =(float)1.0; float volumeunder=(float)1.0; float volumetotal=WingMeshVolume(space); WingMesh spaceunder= WingMeshCrop(space,float4( split.xyz(), split.w)); WingMesh spaceover = WingMeshCrop(space,float4(-split.xyz(),-split.w)); if(usevolcalc==1) { volumeunder = WingMeshVolume(spaceunder); volumeover = WingMeshVolume(spaceover ); } else if (usevolcalc==2) { volumeunder = sumbboxdim(spaceunder); volumeover = sumbboxdim(spaceover ); } assert(volumeover/volumetotal>=-0.01); assert(volumeunder/volumetotal>=-0.01); if(fabs((volumeover+volumeunder-volumetotal)/volumetotal)>0.01) { // ok our volume equations are starting to break down here // lets hope that we dont have too many polys to deal with at this point. volumetotal=volumeover+volumeunder; } if(solidbias && onbrep && count[OVER]==0 && count[SPLIT]==0) { return volumeunder; } return volumeover *powf(count[OVER] +1.5f*count[SPLIT],0.9f) + volumeunder*powf(count[UNDER]+1.5f*count[SPLIT],0.9f); }
void OpenGLFrameStart(const int2 &screensize) { glViewport(0, 0, screensize.x(), screensize.y()); SetBlendMode(BLEND_ALPHA); curcolor = float4(1); lights.clear(); }
void Set3DMode(float fovy, float ratio, float znear, float zfar) { glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); otransforms = objecttransforms(); view2clip = perspectiveFov(fovy, ratio, znear, zfar, 1); view2clip *= float4x4(float4(1, -1, 1, 1)); // FIXME? }
virtual float4 getReflectance(const Vector &_outDir, const Vector &_inDir) const { float4 t = float4(m_position) * scale; int x = (int)floor(t.x), y = (int)floor(t.y), z = (int)floor(t.z); x %= 2; y %= 2; z %= 2; float col = (float)((6 + x + 0 + z) % 2); return float4::rep(col); }
//----------------------------------------------------------------------------- void CPUT_OGL::UpdatePerFrameConstantBuffer( CPUTRenderParameters &renderParams, double totalSeconds ) { //NOTE: Issue with using the value of the resultant Uniform Block in shader if( mpPerFrameConstantBuffer ) { float4x4 view, projection, inverseView, viewProjection; float4 eyePosition, lightDir; if( renderParams.mpCamera ) { view = *renderParams.mpCamera->GetViewMatrix(); projection = *renderParams.mpCamera->GetProjectionMatrix(); inverseView = inverse(*renderParams.mpCamera->GetViewMatrix()); eyePosition = float4(renderParams.mpCamera->GetPosition(), 0.0f); viewProjection = view * projection; } if( renderParams.mpShadowCamera ) { lightDir = float4(normalize(renderParams.mpShadowCamera->GetLook()), 0); } CPUTFrameConstantBuffer cb; cb.View = view; cb.InverseView = inverseView; cb.Projection = projection; cb.ViewProjection = viewProjection; cb.AmbientColor = float4(mAmbientColor, 0.0f); cb.LightColor = float4(mLightColor, 0.0f); cb.LightDirection = lightDir; cb.EyePosition = eyePosition; cb.TotalSeconds = float4((float)totalSeconds); #ifndef CPUT_FOR_OGLES2 GLuint BufferID = mpPerFrameConstantBuffer->GetBufferID(); GL_CHECK(glBindBuffer(GL_UNIFORM_BUFFER,BufferID )); GL_CHECK(glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(CPUTFrameConstantBuffer), &cb)); GL_CHECK(glBindBuffer(GL_UNIFORM_BUFFER, 0)); #else #warning "Need to do something with uniform buffers here" #endif } }
float3 CCamera::CalcWindowCoordinates(const float3& objPos) const { // does same as gluProject() const float4 v = viewProjectionMatrix * float4(objPos, 1.0f); float3 winPos; winPos.x = viewport[0] + viewport[2] * (v.x / v.w + 1.0f) * 0.5f; winPos.y = viewport[1] + viewport[3] * (v.y / v.w + 1.0f) * 0.5f; winPos.z = (v.z / v.w + 1.0f) * 0.5f; return winPos; }
void Sphere::Triangulate(VertexBuffer &vb, int numVertices, bool ccwIsFrontFacing) const { Array<float3> pos; Array<float3> normal; Array<float2> uv; pos.Resize_pod(numVertices); normal.Resize_pod(numVertices); uv.Resize_pod(numVertices); Triangulate(pos.beginptr(), normal.beginptr(), uv.beginptr(), numVertices, ccwIsFrontFacing); int startIndex = vb.AppendVertices(numVertices); for(int i = 0; i < (int)pos.size(); ++i) { vb.Set(startIndex+i, VDPosition, float4(pos[i],1.f)); if (vb.Declaration()->TypeOffset(VDNormal) >= 0) vb.Set(startIndex+i, VDNormal, float4(normal[i],0.f)); if (vb.Declaration()->TypeOffset(VDUV) >= 0) vb.SetFloat2(startIndex+i, VDUV, 0, uv[i]); } }
void MicroManager::frameNew(matrix CamView, float time) { perViewData data = { CamView, float4(time,0,0,0) }; mQue.begin(); mQue.clearRenderTargerts(); ConstantBufferManager::editBuffer( md3d, mPerFrame, &data, sizeof( perViewData ) ); }
// ********************************************************************************************************\n\ float4 PSMainNoTexture( PS_INPUT_NO_TEX input ) : SV_Target\n\ {\n\ float3 normal = normalize(input.Norm);\n\ float nDotL = saturate( dot( normal, -normalize(LightDirection.xyz) ) );\n\ float3 eyeDirection = normalize(EyePosition.xyz - input.Position);\n\ float3 HalfVector = normalize( eyeDirection + (-LightDirection.xyz) );\n\ float nDotH = saturate( dot(normal, HalfVector) );\n\ float3 specular = 0.3f * pow(nDotH, 50.0f );\n\ return float4( (nDotL + specular).xxx, 1.0f);\n\ }\n\
void RenderLine2D(Shader *sh, Primitive prim, const float3 &v1, const float3 &v2, float thickness) { auto v = (v2 - v1) / 2; auto len = length(v); auto vnorm = v / len; auto trans = translation(v1 + v) * rotationZ(vnorm.xy()) * float4x4(float4(len, thickness / 2, 1, 1)); RenderQuad(sh, prim, true, trans); }
friend float4 operator*(float4 v, const M44& m) { // 0b00000000 = 0x00 // 0b01010101 = 0x55 // 0b10101010 = 0xAA // 0b11111111 = 0xFF float4 hvec = m.m_rows[0]*float4(_mm_shuffle_ps(v.get(), v.get(), 0x00)) + m.m_rows[1]*float4(_mm_shuffle_ps(v.get(), v.get(), 0x55)) + m.m_rows[2]*float4(_mm_shuffle_ps(v.get(), v.get(), 0xAA)) + m.m_rows[3]*float4(_mm_shuffle_ps(v.get(), v.get(), 0xFF)); // return hvec * _mm_div_ps(_mm_set1_ps(1), _mm_shuffle_ps(hvec.get(), hvec.get(), 0xFF)); // calculate approximate reciprocal of last element of hvec using // rcp and one iteration of Newton's method. This is faster than // using direct division. float4 w = _mm_shuffle_ps(hvec.get(), hvec.get(), 0xFF); float4 rcp = _mm_rcp_ps(w.get()); rcp *= (2 - rcp*w); return hvec * rcp; }
float2 eigenvector(mat2 p, float eigvl) { // for a mat2, we first reverse-subtract the eigenvalue from the matrix diagonal, // then return whichever row had the larger sum-of-absolute-values. float4 v = float4(p.v[0], p.v[1]); v.xw = eigvl - v.xw; if (fabs(v.x) + fabs(v.y) > fabs(v.z) + fabs(v.w)) return v.yx; else return v.wz; }
aabbox D3D9Mesh::CaculateSubMeshBBox(std::vector<float>& vertices ) { aabbox bbox; bbox.GetMax() = float4(vertices[0],vertices[1],vertices[2],1.f); bbox.GetMin() = float4(vertices[0],vertices[1],vertices[2],1.f); for (unsigned int i = 0;i<vertices.size();i = i+3) { bbox.GetMax().X() = Max(bbox.GetMax().X(),vertices[i]); bbox.GetMin().X() = Min(bbox.GetMin().X(),vertices[i]); bbox.GetMax().Y() = Max(bbox.GetMax().Y(),vertices[i]); bbox.GetMin().Y() = Min(bbox.GetMin().Y(),vertices[i]); bbox.GetMax().Z() = Max(bbox.GetMax().Z(),vertices[i]); bbox.GetMin().Z() = Min(bbox.GetMin().Z(),vertices[i]); } return bbox; }
RenderSurface::RenderSurface() : m_pRenderTargetView(NULL), m_pDepthStencilView(NULL), m_pDepthStencilViewFg(NULL), m_pDepthStencilBuffer(NULL), m_pColorBuffer(NULL), m_width(0), m_height(0) { m_bkgColor = float4( 0.62745f, 0.62745f, 0.62745f, 1.0f ); }
float3 MUST_USE_RESULT Quat::Transform(const float3 &vec) const { assume2(this->IsNormalized(), *this, this->LengthSq()); #if defined(MATH_AUTOMATIC_SSE) && defined(MATH_SSE) return float4(quat_transform_vec4(q, load_vec3(vec.ptr(), 0.f))).xyz(); #else ///\todo Optimize/benchmark the scalar path not to generate a matrix! float3x3 mat = this->ToFloat3x3(); return mat * vec; #endif }
void AABB::Triangulate(VertexBuffer &vb, int numFacesX, int numFacesY, int numFacesZ, bool ccwIsFrontFacing) const { Array<float3> pos; Array<float3> normal; Array<float2> uv; int numVertices = (numFacesX*numFacesY + numFacesY*numFacesZ + numFacesX*numFacesZ)*2*6; pos.Resize_pod(numVertices); normal.Resize_pod(numVertices); uv.Resize_pod(numVertices); Triangulate(numFacesX, numFacesY, numFacesZ, &pos[0], &normal[0], &uv[0], ccwIsFrontFacing); int startIndex = vb.AppendVertices(numVertices); for(int i = 0; i < (int)pos.size(); ++i) { vb.Set(startIndex+i, VDPosition, float4(pos[i],1.f)); if (vb.Declaration()->TypeOffset(VDNormal) >= 0) vb.Set(startIndex+i, VDNormal, float4(normal[i],0.f)); if (vb.Declaration()->TypeOffset(VDUV) >= 0) vb.SetFloat2(startIndex+i, VDUV, 0, uv[i]); } }