//----------------------------------------------------------------------------- // Desc: 初始化Direct3D //----------------------------------------------------------------------------- HRESULT InitD3D( HWND hWnd ) { //创建Direct3D对象, 该对象用于创建Direct3D设备对象 if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) return E_FAIL; //设置D3DPRESENT_PARAMETERS结构, 准备创建Direct3D设备对象 D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.AutoDepthStencilFormat = D3DFMT_D16; //创建Direct3D设备对象 if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) ) { return E_FAIL; } //设置剔出模式为不剔出任何面(正面和方面) g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE ); //启用深度测试 g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE ); //设置变换矩阵 SetMatrices(); return S_OK; }
Transform::Transform(const glm::vec3 &t, const glm::vec3 &r, const glm::vec3 &s): translation(t), rotation(r), scale(s) { SetMatrices(); }
void XSkinnedMesh::LoadModel( const string& fileName ) { BoneHierarchyLoader boneHierarchy; BoneFrame* frame = nullptr; // .x 파일로부터 본 계층 구조를 로드한다. auto hr = D3DXLoadMeshHierarchyFromX( fileName.c_str(), D3DXMESH_MANAGED, D3D9_DEVICE, &boneHierarchy, NULL, &frame, &_animation ); if (FAILED( hr )) { SAFE_DELETE( frame ); // assert } _rootBone = static_cast<Bone*>(frame); // bone matrices update SetMatrices(); UpdateMatrices( _rootBone, &_world ); SetupBoneMatrixPtr( _rootBone ); // render sphere //D3DXCreateSphere( D3D9_DEVICE, 2.0f, 10, 10, &_sphereMesh, NULL ); //LoadTexture( DEFAULT_TEX ); }
void XSkinnedMesh::Render( const BoneFrame* bone /* = nullptr */ ) { SetMatrices(); if (!bone) { bone = _rootBone; } if (bone->pMeshContainer) { BoneMesh* boneMesh = static_cast<BoneMesh*>(_rootBone->pMeshContainer); if (boneMesh->pSkinInfo) { int numBones = boneMesh->pSkinInfo->GetNumBones(); for (int i = 0; i < numBones; i++) { D3DXMatrixMultiply( &boneMesh->_currentBoneMatrices[i], &boneMesh->_boneOffsetMatrices[i], boneMesh->_boneMatrixPtrs[i] ); } BYTE* src = nullptr; BYTE* dest = nullptr; boneMesh->_originalMesh->LockVertexBuffer( D3DLOCK_READONLY, (void**)&src ); boneMesh->MeshData.pMesh->LockVertexBuffer( 0, (void**)&dest ); boneMesh->pSkinInfo->UpdateSkinnedMesh( boneMesh->_currentBoneMatrices, nullptr, src, dest ); boneMesh->MeshData.pMesh->UnlockVertexBuffer(); boneMesh->_originalMesh->UnlockVertexBuffer(); for (int i = 0; i < boneMesh->_numAttributeGroups; i++) { int mtrl = boneMesh->_attributeTable[i].AttribId; D3D9_DEVICE->SetMaterial(&(boneMesh->_materials[mtrl])); D3D9_DEVICE->SetTexture( 0, _texture ); boneMesh->MeshData.pMesh->DrawSubset( mtrl ); } } } if (bone->pFrameSibling) { Render( bone->pFrameSibling ); } if (bone->pFrameFirstChild) { Render( bone->pFrameFirstChild ); } }
void RenderTerrain(ID3D11DeviceContext* pContext, const D3DXMATRIX& mProj, const D3D11_VIEWPORT& vp, const char* passOverride=NULL) { SetMatrices(GetApp()->ActiveCam_, mProj); g_HwTessellation = false; if (g_HwTessellation) { pContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_4_CONTROL_POINT_PATCHLIST); pContext->IASetIndexBuffer(g_TileQuadListIB, DXGI_FORMAT_R32_UINT, 0); } else { pContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); pContext->IASetIndexBuffer(g_TileTriStripIB, DXGI_FORMAT_R32_UINT, 0); } const char* passName = "ShadedTriStrip"; const bool wire = true; if (wire) passName = "Wireframe"; if (g_HwTessellation) { if (wire) passName = "HwTessellatedWireframe"; else passName = "HwTessellated"; } if (passOverride != NULL) passName = passOverride; ID3DX11EffectPass* pPass = g_pTesselationTechnique->GetPassByName(passName); if (!pPass) return; // Shouldn't happen unless the FX file is broken (like wrong pass name). SetViewport(pContext, vp); for (int i=0; i!=g_nRings ; ++i) { const TileRing* pRing = g_pTileRings[i]; pRing->SetRenderingState(pContext); g_HeightMapVar->SetResource(g_pHeightMapSRV); g_GradientMapVar->SetResource(g_pGradientMapSRV); g_pTileSizeVar->SetFloat(pRing->tileSize()); // Need to apply the pass after setting its vars. pPass->Apply(0, pContext); // Instancing is used: one tiles is one instance and the index buffer describes all the // NxN patches within one tile. const int nIndices = (g_HwTessellation)? QUAD_LIST_INDEX_COUNT: TRI_STRIP_INDEX_COUNT; pContext->DrawIndexedInstanced(nIndices, pRing->nTiles(), 0, 0, 0); } }
void Paint::DrawScene() { _model = mat4(1.0f); _model = glm::rotate(_model, glm::radians(-15.0f), vec3(0.0f, 1.0f, 0.0f)); _model = glm::rotate(_model, glm::radians(-90.0f), vec3(1.0f, 0.0f, 0.0f)); SetMatrices(); _shader.SetUniform("Material.Kd", 0.7f, 0.5f, 0.3f); _shader.SetUniform("Material.Ks", 1.0f, 1.0f, 1.0f); _shader.SetUniform("Material.Shininess", 100.0f); _shader.SetUniform("PaintColor", 0.0, 0.4, 0.7); _teapot->Render(); }
void Render() { g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0); if (SUCCEEDED(g_pd3dDevice->BeginScene())) { g_pd3dDevice->SetRenderState(D3DRS_AMBIENT, 0xffffffff); SetMatrices(); D3DXMATRIXA16 matWorld1, matWorld2; D3DXMatrixTranslation(&matWorld1, 0.0f, 0.0f, 1.0f); D3DXMatrixRotationY(&matWorld2, D3DX_PI); matWorld2 *= matWorld1; g_pd3dDevice->GetTransform(D3DTS_WORLD, &matWorld2); RenderModel(modelindex); g_pd3dDevice->EndScene(); } g_pd3dDevice->Present(NULL, NULL, NULL, NULL); }
void QuadBackground::Render(OpenGL * openGL) { struct timespec start,end; SET_TIME(&start); OpenGLSettings(); //Draw object SetMatrices(openGL); //glEnable(GL_TEXTURE_2D); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textureID); glUniform1i(openGL->renderData.textureLocation,0); //Debugging - Draw a solid color to texture if (ENABLE_TEXTURE_COLOR) { u_int32_t * pxData = new u_int32_t[textureWidth*textureHeight]; for (int i=0;i<(textureWidth*textureHeight);i++) { pxData[i] = 123912048; } glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, textureWidth, textureHeight, GL_RGBA, GL_UNSIGNED_BYTE, pxData); delete[] pxData; } //Update the texture glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,imageWidth,imageHeight, GL_RGBA, GL_UNSIGNED_BYTE, imagePixels); openGL->DrawGLObject(testCube); openGL->DrawGLObject(texturedQuad); ResetGLSettings(); SET_TIME(&end); LOG_TIME("QuadBG Render", start, end); }
void Renderer::Render( Context& context, SceneGraph::TextureCache& textureCache, BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, SceneGraph::Shader& defaultShader, const Matrix& modelViewMatrix, const Matrix& viewMatrix, const Matrix& projectionMatrix, bool cull, bool blend ) { NewRenderer* renderer = GetNewRenderer(); // avoid a dynamic cast per item per frame if( renderer ) { // Get the shader from the material: mShader = &renderer->mRenderDataProvider->GetShader(); } // if mShader is NULL it means we're set to default if( !mShader ) { mShader = &defaultShader; } if( !CheckResources() ) { // CheckResources() is overriden in derived classes. // Prevents modify the GL state if resources are not ready and nothing is to be rendered. return; } // Get the program to use: Program* program = mShader->GetProgram(); if( !program ) { // if program is NULL it means this is a custom shader with non matching geometry type so we need to use default shaders program program = defaultShader.GetProgram(); DALI_ASSERT_DEBUG( program && "Default shader should always have a program available." ); if( !program ) { DALI_LOG_ERROR( "Failed to get program for shader at address %p.", (void*) &*mShader ); return; } } // Take the program into use so we can send uniforms to it program->Use(); DoSetCullFaceMode( context, bufferIndex ); DoSetBlending( context, bufferIndex, blend ); // Ignore missing uniforms - custom shaders and flat color shaders don't have SAMPLER // set projection and view matrix if program has not yet received them yet this frame const Matrix& modelMatrix = node.GetModelMatrix( bufferIndex ); SetMatrices( *program, modelMatrix, viewMatrix, projectionMatrix, modelViewMatrix ); // set color uniform GLint loc = program->GetUniformLocation( Program::UNIFORM_COLOR ); if( Program::UNIFORM_UNKNOWN != loc ) { const Vector4& color = node.GetRenderColor( bufferIndex ); program->SetUniform4f( loc, color.r, color.g, color.b, color.a ); } //@todo MESH_REWORK Remove after removing ImageRenderer DoSetUniforms(context, bufferIndex, mShader, program ); // subclass rendering and actual draw call DoRender( context, textureCache, node, bufferIndex, *program, modelViewMatrix, viewMatrix ); }
void DrawOOBM() { uint tt = timeGetTime(); renderer->BeginBatchDrawing(); SetTransformMatrix(&vpmatrix); mshbatch->begin(); for(int t = 0; t < strMaterials.len; t++) { int txset = 0; for(int a = 0; a < 2; a++) { int afset = 0; GrowList<GameObject*> *l = 0; // = &((oobm[a])[t]); for(int i = 0; i < oobm[a].len; i++) if(oobm[a].getpnt(i)->tid == t) {l = oobm[a].getpnt(i)->objs; break;} if(!l) continue; for(int i = 0; i < l->len; i++) { GameObject *o = l->get(i); int dif = -1; if((o->flags & FGO_SELECTED) || (currentSelection == o)) { if((o->flags & FGO_SELECTED) && (currentSelection == o)) dif = 0xFFFF00FF; else dif = (currentSelection==o)?0xFFFF0000:0xFF0000FF; } //Model *md = o->objdef->subtypes[o->subtype].appear[o->appearance].def; Model *md = GetObjectModel(o); if(showrepresentations && o->objdef->representation) md = o->objdef->representation; md->prepare(); md->mesh->prepare(); Mesh *msh = md->mesh; // If md != md->mesh, this means that md is an Anim! SetMatrices(o->scale, -o->orientation, o->position); Matrix mWorldCopy = mWorld; uint tm = (int)(current_time*1000.0f) - o->animtimeref; // If the current task has SYNCH_ANIMATION_TO_FRACTION, use it. if(md != md->mesh) if(o->ordercfg.order.len) { STask *st = &o->ordercfg.order.first->value.task.first->value; if(st->type->satf) { SequenceEnv env; env.self = o; tm = st->type->satf->get(&env) * ((Anim*)md)->dur; tm = st->lastsatf + (tm - st->lastsatf) * elapsed_time * 0.5f; if(tm >= ((Anim*)md)->dur) tm = ((Anim*)md)->dur - 1; st->lastsatf = tm; } } if(o->animlooping) if(md != md->mesh) tm %= ((Anim*)md)->dur; for(int g = 0; g < msh->ngrp; g++) if((msh->lstmatflags[g] == a) && (msh->lstmattid[g] == t)) { if(!txset) {txset = 1; SetTexture(0, msh->lstmattex[g]);} if(!afset) {afset = 1; if(a) renderer->EnableAlphaTest(); else renderer->DisableAlphaTest();} (animsEnabled?md:md->mesh)->drawInBatch(mshbatch, g, o->color, dif, tm); } for(int j = 0; j < msh->nAttachPnts; j++) if(msh->attachPnts[j].model) if(md->isAttachPointOn(j, tm)) { Model *apmd = msh->attachPnts[j].model; Mesh *apms = apmd->mesh; for(int g = 0; g < apms->ngrp; g++) if((apms->lstmatflags[g] == a) && (apms->lstmattid[g] == t)) { if(!txset) {txset = 1; SetTexture(0, apms->lstmattex[g]);} if(!afset) {afset = 1; if(a) renderer->EnableAlphaTest(); else renderer->DisableAlphaTest();} uint aptm = current_time * 1000; if(apmd != apmd->mesh) aptm %= ((Anim*)apmd)->dur; Vector3 p, s; md->getAttachPointPos(&s, j, tm); TransformVector3(&p, &s, &mWorldCopy); SetMatrices(Vector3(1,1,1), Vector3(0,0,0), p); (animsEnabled?apmd:apms)->drawInBatch(mshbatch, g, o->color, dif, aptm); } } } l->clear(); mshbatch->flush(); } mshbatch->flush(); } mshbatch->end(); }
void DrawScene() { BeginMeshDrawing(); if(fogenabled) SetFog(); SetConstantMatrices(); newSelection = -1; SetMatrices(onevector, nullvector, nullvector); CalcRay(); newSelZ = -1; DrawObj(levelobj); if(currentSelection.get() != newSelection.get()) { currentSelection = newSelection; //printf("New selection: %i\n", newSelection); } if(meshbatching) DrawOOBM(); if(experimentalKeys) { // Stampdown CalcStampdownPos(); /* int od; if((od = FindObjDef(CLASS_CHARACTER, "Archer")) != -1) { if(stdownvalid) { renderer->BeginMeshDrawing(); SetMatrices(onevector, nullvector, stdownpos); SetTransformMatrix(&matrix); objdef[od].subtypes[0].appear[0].def->draw(0); //sprintf(statustextbuf, "stdownpos = (%f, %f, %f)", stdownpos.x, stdownpos.y, stdownpos.z); //sprintf(statustextbuf, "raydir = (%f, %f, %f)", raydir.x, raydir.y, raydir.z); //sprintf(statustextbuf, "raystart = (%f, %f, %f)", raystart.x, raystart.y, raystart.z); } //else strcpy(statustextbuf, "Invalid stampdown position."); //statustext = statustextbuf; } */ if(objtypeToStampdown) if(stdownvalid) if(playerToGiveStampdownObj.valid()) { // Align buildings in the grid. AlignObjPosToGrid(objtypeToStampdown, stdownpos, stampdownRot); Model *m = GetObjTypeDefaultModel(objtypeToStampdown); if(m) { renderer->BeginMeshDrawing(); SetMatrices(objtypeToStampdown->scale, Vector3(0,-stampdownRot,0), stdownpos); SetTransformMatrix(&matrix); m->draw(playerToGiveStampdownObj->color); } } int moti; if(mousetool == 7) if(stdownvalid) if(newmanorplayer.valid()) if((moti = FindObjDef(CLASS_BUILDING, "Manor")) != -1) { CObjectDefinition *cod = &(objdef[moti]); // Align buildings in the grid. stdownpos.x = (int)(stdownpos.x / 5) * 5.0f + 2.5f; stdownpos.z = (int)(stdownpos.z / 5) * 5.0f + 2.5f; stdownpos.y = GetHeight(stdownpos.x, stdownpos.z); Model *m = GetObjTypeDefaultModel(cod); if(m) { renderer->BeginMeshDrawing(); SetMatrices(cod->scale, Vector3(0,-stampdownRot,0), stdownpos); SetTransformMatrix(&matrix); m->draw(newmanorplayer->color); } } } if(enableMap) { SetMatrices(Vector3(5.0f, 1.0f, -5.0f), nullvector, Vector3(-mapedge*5,0,+mapheight*5-mapedge*5)); SetTransformMatrix(&matrix); DrawMap(); DrawTileHighlights(); DrawLakes(); } drawdebug = 0; if(fogenabled) DisableFog(); }
void DrawObj(GameObject *o) { float pntz; if((o->renderable && o->objdef->subtypes[o->subtype].appear[o->appearance].def) || (showrepresentations && o->objdef->representation)) { if(IsPointOnScreen(o->position, &pntz)) { //Model *md = o->objdef->subtypes[o->subtype].appear[o->appearance].def; Model *md = GetObjectModel(o); if(showrepresentations && o->objdef->representation) md = o->objdef->representation; md->prepare(); md->mesh->prepare(); Mesh *msh = md->mesh; objsdrawn++; SetMatrices(o->scale, -o->orientation, o->position); Vector3 sphPos = o->position; sphPos.y += msh->sphere[1] * o->scale.y; if((newSelZ == -1) || (pntz < newSelZ)) if(SphereIntersectsRay(&sphPos, msh->sphere[3]*o->scale.y/2.0f, &raystart, &raydir)) {newSelection = o; newSelZ = pntz;} if(multiSel) { Vector3 tdp; TransformCoord3(&tdp, &nullvector, &matrix); float ba = (float)((mselx<mouseX)?mselx:mouseX) * 2.0f / scrw - 1; float bb = (float)((msely<mouseY)?msely:mouseY) * 2.0f / scrh - 1; float bc = (float)((mselx>mouseX)?mselx:mouseX) * 2.0f / scrw - 1; float bd = (float)((msely>mouseY)?msely:mouseY) * 2.0f / scrh - 1; if((tdp.x >= ba) && (tdp.x <= bc) && (-tdp.y >= bb) && (-tdp.y <= bd)) msellist.add(o); } if(!meshbatching) { if((o->flags & FGO_SELECTED) || (currentSelection == o)) { renderer->EnableColorBlend(); if((o->flags & FGO_SELECTED) && (currentSelection == o)) renderer->SetBlendColor(0xFFFF00FF); else renderer->SetBlendColor((currentSelection==o)?0xFFFF0000:0xFF0000FF); } SetTransformMatrix(&matrix); msh->draw(o->color); if((o->flags & FGO_SELECTED) || (currentSelection == o)) renderer->DisableColorBlend(); } else { auto f = [o](Mesh *msh) { msh->prepare(); for(int i = 0; i < msh->ngrp; i++) { OOBMTex *ot; int f = msh->lstmatflags[i]&1; int t = msh->lstmattid[i]; for(int j = 0; j < oobm[f].len; j++) { ot = oobm[f].getpnt(j); if(ot->tid == t) {ot->objs->add(o); goto nextgrp;} } ot = oobm[f].addp(); ot->tid = t; ot->objs = new GrowList<GameObject*>; ot->objs->add(o); nextgrp: ; } }; f(msh); for(int i = 0; i < msh->nAttachPnts; i++) if(msh->attachPnts[i].model) f(msh->attachPnts[i].model->mesh); } } } for(DynListEntry<GameObject> *e = o->children.first; e; e = e->next) DrawObj(&e->value); }