void Renderer::setBuffers( Mesh& mesh ) { UInt32 stride = sizeof( Vertex ); UInt32 offset = 0; setVertexBuffers( &mesh._vertexBuffer, &stride, &offset ); setIndexBuffer( &mesh._indexBuffer ); }
//------------------------------------------------------------------------------ //## Basic TEST_F(Test_Shader_Shader, IndependentSamplerState) { auto shader1 = Shader::create(LN_ASSETFILE("Shader/IndependentSamplerState.fx")); auto vertexDecl1 = newObject<VertexLayout>(); vertexDecl1->addElement(0, VertexElementType::Float3, VertexElementUsage::Position, 0); vertexDecl1->addElement(0, VertexElementType::Float2, VertexElementUsage::TexCoord, 0); struct Vertex { Vector3 pos; Vector2 uv; }; Vertex v[] = { { { -1, 1, 0 }, { 0, 0 }, }, // 0.5 で中央の face からサンプリングする { { 0, 1, 0 }, { 1, 0 }, }, { { -1, 0, 0 }, { 0, 1 }, }, { { 0, 0, 0 }, { 1, 1 }, }, }; auto vb1 = newObject<VertexBuffer>(sizeof(v), v, GraphicsResourceUsage::Static); auto tex1 = newObject<Texture2D>(2, 2, TextureFormat::RGBA8); auto bmp1 = tex1->map(MapMode::Write); bmp1->setPixel32(0, 0, ColorI(255, 0, 0, 255)); bmp1->setPixel32(1, 0, ColorI(255, 0, 255, 255)); bmp1->setPixel32(0, 1, ColorI(0, 255, 0, 255)); bmp1->setPixel32(1, 1, ColorI(0, 0, 255, 255)); // TODO: まだ SamplerState 直接指定をサポートしていないので Texture に対してセットする方法でテストケースだけ用意しておく。 // 後でサポートするとき、shader1->findParameter("mySamplerState")->setSamplerState(samplerState); とかに書き換える。 auto samplerState = newObject<SamplerState>(); samplerState->setFilterMode(TextureFilterMode::Linear); tex1->setSamplerState(samplerState); shader1->findParameter("_Texture")->setTexture(tex1); auto ctx = Engine::graphicsContext(); TestEnv::resetGraphicsContext(ctx); ctx->setVertexLayout(vertexDecl1); ctx->setVertexBuffer(0, vb1); ctx->setIndexBuffer(nullptr); ctx->setShaderPass(shader1->techniques()[0]->passes()[0]); // * [ ] default { ctx->clear(ClearFlags::All, Color::White, 1.0f, 0); ctx->setPrimitiveTopology(PrimitiveTopology::TriangleStrip); ctx->drawPrimitive(0, 2); ASSERT_SCREEN(LN_ASSETFILE("Shader/Result/Test_Shader_Shader-IndependentSamplerState-1.png")); } }
void mirror::setData(ID3D11Device *_m_d3d11Device,float _width, float _height, float _left, float _top) { m_d3d11Device = _m_d3d11Device; left = _left; top = _top; width = _width; height = _height; float cameraZ = 0; //********************m_backMirror_VERTEX m_backMirror_VERTEX[0].xyz.x = left; m_backMirror_VERTEX[0].xyz.y = top; m_backMirror_VERTEX[0].xyz.z = cameraZ; m_backMirror_VERTEX[0].uvw.x = 0; m_backMirror_VERTEX[0].uvw.y = 0; m_backMirror_VERTEX[0].uvw.z = 0; m_backMirror_VERTEX[1].xyz.x = m_backMirror_VERTEX[0].xyz.x + width; m_backMirror_VERTEX[1].xyz.y = m_backMirror_VERTEX[0].xyz.y; m_backMirror_VERTEX[1].xyz.z = cameraZ; m_backMirror_VERTEX[1].uvw.x = 1; m_backMirror_VERTEX[1].uvw.y = 0; m_backMirror_VERTEX[1].uvw.z = 0; m_backMirror_VERTEX[2].xyz.x = m_backMirror_VERTEX[0].xyz.x + width; m_backMirror_VERTEX[2].xyz.y = m_backMirror_VERTEX[0].xyz.y - height; m_backMirror_VERTEX[2].xyz.z = cameraZ; m_backMirror_VERTEX[2].uvw.x = 1; m_backMirror_VERTEX[2].uvw.y = 1; m_backMirror_VERTEX[2].uvw.z = 0; m_backMirror_VERTEX[3].xyz.x = m_backMirror_VERTEX[0].xyz.x; m_backMirror_VERTEX[3].xyz.y = m_backMirror_VERTEX[0].xyz.y - height; m_backMirror_VERTEX[3].xyz.z = cameraZ; m_backMirror_VERTEX[3].uvw.x = 0; m_backMirror_VERTEX[3].uvw.y = 1; m_backMirror_VERTEX[3].uvw.z = 0; for (int i = 0; i < 4; i++) { m_backMirror_VERTEX[i].nrm.x = 0; m_backMirror_VERTEX[i].nrm.y = 0; m_backMirror_VERTEX[i].nrm.z = -1; } unsigned int mirror_indicies[6] = { 0, 2, 3, 0, 1, 2 }; setVertexBuffer(m_backMirror_VERTEX, rectNumber); setIndexBuffer(mirror_indicies, rectIndexNumber); }
void mirror::setPos(ID3D11Device *_m_d3d11Device, const XMFLOAT2& topleft, const XMFLOAT2& topright, const XMFLOAT2& botright, const XMFLOAT2& botleft) { m_d3d11Device = _m_d3d11Device; float cameraZ = 0; //********************m_backMirror_VERTEX //topleft m_backMirror_VERTEX[0].xyz.x = topleft.x; m_backMirror_VERTEX[0].xyz.y = topleft.y; m_backMirror_VERTEX[0].xyz.z = cameraZ; m_backMirror_VERTEX[0].uvw.x = 0; m_backMirror_VERTEX[0].uvw.y = 0; m_backMirror_VERTEX[0].uvw.z = 0; //top right m_backMirror_VERTEX[1].xyz.x = topright.x; m_backMirror_VERTEX[1].xyz.y = topright.y; m_backMirror_VERTEX[1].xyz.z = cameraZ; m_backMirror_VERTEX[1].uvw.x = 1; m_backMirror_VERTEX[1].uvw.y = 0; m_backMirror_VERTEX[1].uvw.z = 0; //bot right m_backMirror_VERTEX[2].xyz.x = botright.x; m_backMirror_VERTEX[2].xyz.y = botright.y; m_backMirror_VERTEX[2].xyz.z = cameraZ; m_backMirror_VERTEX[2].uvw.x = 1; m_backMirror_VERTEX[2].uvw.y = 1; m_backMirror_VERTEX[2].uvw.z = 0; //bot left m_backMirror_VERTEX[3].xyz.x = botleft.x; m_backMirror_VERTEX[3].xyz.y = botleft.y; m_backMirror_VERTEX[3].xyz.z = cameraZ; m_backMirror_VERTEX[3].uvw.x = 0; m_backMirror_VERTEX[3].uvw.y = 1; m_backMirror_VERTEX[3].uvw.z = 0; for (int i = 0; i < 4; i++) { m_backMirror_VERTEX[i].nrm.x = 0; m_backMirror_VERTEX[i].nrm.y = 0; m_backMirror_VERTEX[i].nrm.z = -1; } unsigned int mirror_indicies[6] = { 0, 2, 3, 0, 1, 2 }; setVertexBuffer(m_backMirror_VERTEX, rectNumber); setIndexBuffer(mirror_indicies, rectIndexNumber); }
void ShadowsDemo::onDraw() { App::onDraw(); const auto device = graphicsDevice(); device->setClearColor(0.0f, 0.0f, 0.0f, 0.0f); device->clear(ciri::ClearFlags::Color | ciri::ClearFlags::Depth); device->setRasterizerState(_rasterState); device->restoreDefaultBlendState(); if( _spotlightShader->isValid() && _directionalShader->isValid() ) { const cc::Mat4f& cameraViewProj = _camera.getProj() * _camera.getView(); bool firstLight = true; Light::Type boundLightType = Light::Type::Invalid; for( auto& light : _lights ) { // compute light matrices //const cc::Mat4f& lightView = light.view(); //const cc::Mat4f& lightProj = light.proj();//cc::math::perspectiveRH(45.0f, 1.0f, 0.1f, light.range());//light.proj(); //const cc::Mat4f lightViewProj = lightProj * lightView; if( light.type() == Light::Type::Directional ) { //light.computeViewProjFromFrustum(BoundingFrustum(cameraViewProj)); light.computeViewProjFromFrustum(BoundingFrustum(_camera.getFov(), _camera.getAspect(), _camera.getNearPlane(), _camera.getFarPlane(), _camera.getPosition(), _camera.getFpsFront(), _camera.getUp())); //light.computeViewProjOrtho(_camera.getView(), _camera.getFov(), _camera.getAspect(), _camera.getNearPlane(), _camera.getFarPlane()); } const cc::Mat4f lightViewProj = light.proj() * light.view(); if( light.castShadows() ) { device->setDepthStencilState(device->getDefaultDepthStencilDefault()); // set and clear render target ciri::IRenderTarget2D* depthTarget = _shadowTarget.get(); device->setRenderTargets(&depthTarget, 1); device->setClearColor(0.0f, 0.0f, 0.0f, 0.0f); device->clear(ciri::ClearFlags::Color | ciri::ClearFlags::Depth); // apply depth shader device->applyShader(_depthShader); // set viewport to depth size device->setViewport(ciri::Viewport(0, 0, _shadowTarget->getDepth()->getWidth(), _shadowTarget->getDepth()->getHeight())); // render all models for( auto& mdl : _models ) { _depthConstants.xform = lightViewProj * mdl->getXform().getWorld(); _depthConstantsBuffer->setData(sizeof(DepthConstants), &_depthConstants); device->setVertexBuffer(mdl->getVertexBuffer()); if( mdl->getIndexBuffer() != nullptr ) { device->setIndexBuffer(mdl->getIndexBuffer()); device->drawIndexed(ciri::PrimitiveTopology::TriangleList, mdl->getIndexBuffer()->getIndexCount()); } else { device->drawArrays(ciri::PrimitiveTopology::TriangleList, mdl->getVertexBuffer()->getVertexCount(), 0); } } // reser viewport to screen device->setViewport(ciri::Viewport(0, 0, window()->getWidth(), window()->getHeight())); // restore default render targets device->restoreDefaultRenderTargets(); } switch( light.type() ) { case Light::Type::Directional: { if( boundLightType != Light::Type::Directional || light.castShadows() ) { boundLightType = Light::Type::Directional; device->applyShader(_directionalShader); device->setTexture2D(0, _shadowTarget->getDepth(), ciri::ShaderStage::Pixel); device->setSamplerState(0, _shadowSampler, ciri::ShaderStage::Pixel); } _directionalConstants.LightDirection = light.direction(); _directionalConstants.LightColor = light.diffuseColor(); _directionalConstants.LightIntensity = light.diffuseIntensity(); _directionalConstants.campos = _camera.getPosition(); _directionalConstants.CastShadows = light.castShadows(); _directionalConstants.lightViewProj = lightViewProj; for( auto& mdl : _models ) { if( !mdl->isValid() ) { continue; } _directionalConstants.world = mdl->getXform().getWorld(); _directionalConstants.xform = cameraViewProj * _directionalConstants.world; _directionalConstantsBuffer->setData(sizeof(DirectionalConstants), &_directionalConstants); device->setVertexBuffer(mdl->getVertexBuffer()); if( mdl->getIndexBuffer() != nullptr ) { device->setIndexBuffer(mdl->getIndexBuffer()); device->drawIndexed(ciri::PrimitiveTopology::TriangleList, mdl->getIndexBuffer()->getIndexCount()); } else { device->drawArrays(ciri::PrimitiveTopology::TriangleList, mdl->getVertexBuffer()->getVertexCount(), 0); } } break; } case Light::Type::Spot: { if( boundLightType != Light::Type::Spot || light.castShadows() ) { boundLightType = Light::Type::Spot; device->applyShader(_spotlightShader); device->setTexture2D(0, _shadowTarget->getDepth(), ciri::ShaderStage::Pixel); device->setSamplerState(0, _shadowSampler, ciri::ShaderStage::Pixel); } _spotlightConstants.LightPosition = light.position(); _spotlightConstants.LightDirection = light.direction(); _spotlightConstants.LightColor = light.diffuseColor(); _spotlightConstants.LightCosInner = light.cosConeInnerAngle(true); _spotlightConstants.LightCosOuter = light.cosConeOuterAngle(true); _spotlightConstants.LightIntensity = light.diffuseIntensity(); _spotlightConstants.LightRange = light.range(); _spotlightConstants.CastShadows = light.castShadows(); _spotlightConstants.lightViewProj = lightViewProj; for( auto& mdl : _models ) { _spotlightConstants.world = mdl->getXform().getWorld(); _spotlightConstants.xform = cameraViewProj * _spotlightConstants.world; _spotlightConstantsBuffer->setData(sizeof(SpotlightConstants), &_spotlightConstants); device->setVertexBuffer(mdl->getVertexBuffer()); if( mdl->getIndexBuffer() != nullptr ) { device->setIndexBuffer(mdl->getIndexBuffer()); device->drawIndexed(ciri::PrimitiveTopology::TriangleList, mdl->getIndexBuffer()->getIndexCount()); } else { device->drawArrays(ciri::PrimitiveTopology::TriangleList, mdl->getVertexBuffer()->getVertexCount(), 0); } } break; } } if( firstLight ) { firstLight = false; device->setBlendState(_additiveBlendState); } } } device->present(); }
BoxGeometry::BoxGeometry( String name, const Vector3D& halfExtends, bool addTangents, bool patchRepresentation, const Vector4D& texcoordScale) : VertexBasedGeometry(name, patchRepresentation ? VERTEX_BASED_TRIANGLE_PATCHES : VERTEX_BASED_TRIANGLES), mHalfExtends(halfExtends) { BufferInfo bufferi( name + String("BoxGeometryPositionBuffer"), ContextTypeFlags(HOST_CONTEXT_TYPE_FLAG | OPEN_GL_CONTEXT_TYPE_FLAG), POSITION_SEMANTICS, TYPE_VEC4F, 6* 4 , //because of the normals, the vertices cannot be shared ;( BufferElementInfo(4,GPU_DATA_TYPE_FLOAT,32,false), VERTEX_ATTRIBUTE_BUFFER_TYPE, NO_CONTEXT_TYPE ); setAttributeBuffer(new Buffer(bufferi,false,0)); bufferi.name = name + String("BoxGeometryNormalBuffer"); bufferi.bufferSemantics = NORMAL_SEMANTICS; setAttributeBuffer(new Buffer(bufferi,false,0)); if(addTangents) { bufferi.name = name + String("BoxGeometryTangentBuffer"); bufferi.bufferSemantics = TANGENT_SEMANTICS; setAttributeBuffer( new Buffer( bufferi, false, 0 ) ); } bufferi.name = name + String( "BoxGeometryTexCoordBuffer"); bufferi.bufferSemantics = TEXCOORD_SEMANTICS; setAttributeBuffer(new Buffer(bufferi,false,0)); setIndexBuffer( new Buffer( BufferInfo( name + String("BoxGeometryIndexBuffer"), ContextTypeFlags(HOST_CONTEXT_TYPE_FLAG | OPEN_GL_CONTEXT_TYPE_FLAG), INDEX_SEMANTICS, TYPE_UINT32, 6 * 6, //6 faces * 2* triangles @ 3 verts BufferElementInfo(1,GPU_DATA_TYPE_UINT,32,false), VERTEX_INDEX_BUFFER_TYPE, NO_CONTEXT_TYPE ) ) ); Vector4D* posBuffer = reinterpret_cast<Vector4D*>(getAttributeBuffer(POSITION_SEMANTICS)->getCPUBufferHandle()); Vector4D* normalBuffer =reinterpret_cast<Vector4D*>(getAttributeBuffer(NORMAL_SEMANTICS)->getCPUBufferHandle()); Vector4D* tangentBuffer = 0; if(addTangents) { tangentBuffer = reinterpret_cast<Vector4D*>(getAttributeBuffer(TANGENT_SEMANTICS)->getCPUBufferHandle()); } Vector4D* tcBuffer = reinterpret_cast<Vector4D*>(getAttributeBuffer(TEXCOORD_SEMANTICS)->getCPUBufferHandle()); unsigned int* indexBuffer = reinterpret_cast<unsigned int*>(getIndexBuffer()->getCPUBufferHandle()); //iterate over the six faces: for(int axis=0; axis<3; axis++) { for(int side = 1; side >= -1; side -= 2) { Vector3D normal = Vector3D(axis==0?side:0,axis==1?side:0, axis==2?side:0); Vector3D left = Vector3D( ((axis==2) || (axis ==1)) ? -side : 0, 0, axis==0?side:0 ); Vector3D down= glm::cross(normal,left); Vector3D tangent = left * (-1.0f); int vertexIndexBase = 8* axis + 4*(1-(side+1)/2); int indexIndexBase = 12* axis + 6*(1-(side+1)/2); //lower left posBuffer[vertexIndexBase + 0] = Vector4D( (normal + left + down)* halfExtends, 1 ); normalBuffer[vertexIndexBase + 0] = Vector4D( normal, 0 ); if(addTangents){ tangentBuffer[vertexIndexBase+0]= Vector4D( tangent, 0 ); } tcBuffer[vertexIndexBase + 0] = Vector4D(0,0,0, 0 ) * texcoordScale; //lower right posBuffer[vertexIndexBase + 1] = Vector4D( (normal - left + down)* halfExtends , 1 ); normalBuffer[vertexIndexBase + 1] = Vector4D( normal, 0 ); if(addTangents){ tangentBuffer[vertexIndexBase+1]= Vector4D( tangent, 0 ); } tcBuffer[vertexIndexBase + 1] = Vector4D(1,0,0, 0 )* texcoordScale; //upper right posBuffer[vertexIndexBase + 2] = Vector4D( (normal - left - down)* halfExtends , 1 ); normalBuffer[vertexIndexBase + 2] = Vector4D( normal, 0 ); if(addTangents){ tangentBuffer[vertexIndexBase+2]= Vector4D( tangent, 0 ); } tcBuffer[vertexIndexBase + 2] = Vector4D(1,1,0, 0 )* texcoordScale; //upper left posBuffer[vertexIndexBase + 3] = Vector4D( (normal + left - down)* halfExtends , 1 ); normalBuffer[vertexIndexBase + 3] = Vector4D( normal, 0 ); if(addTangents){ tangentBuffer[vertexIndexBase+3]= Vector4D( tangent, 0 ); } tcBuffer[vertexIndexBase + 3] = Vector4D(0,1,0, 0 )* texcoordScale; indexBuffer[indexIndexBase + 0]= vertexIndexBase + 0; indexBuffer[indexIndexBase + 1]= vertexIndexBase + 1; indexBuffer[indexIndexBase + 2]= vertexIndexBase + 2; indexBuffer[indexIndexBase + 3]= vertexIndexBase + 0; indexBuffer[indexIndexBase + 4]= vertexIndexBase + 2; indexBuffer[indexIndexBase + 5]= vertexIndexBase + 3; // LOG<<DEBUG_LOG_LEVEL<<"VERTICES OF BOX, face "<< // ((side>0)?"positive":"negative") << // ( (axis==0) // ?" X" // :( (axis==1) // ?" Y" // :" Z" ) ) // // << ": \n"; // for(int i=0;i<4;i++) // { // LOG<<DEBUG_LOG_LEVEL // <<"Index of vertex :"<<vertexIndexBase+i // <<"; Vertex coords: "<<posBuffer[vertexIndexBase + i] // <<"; Normal: "<<normalBuffer[vertexIndexBase + i] // <<"; start index for face in index buffer: "<<indexIndexBase<<";\n" // ; // } } } getAttributeBuffer(POSITION_SEMANTICS)->copyFromHostToGPU(); getAttributeBuffer(NORMAL_SEMANTICS)->copyFromHostToGPU(); if(addTangents){getAttributeBuffer(TANGENT_SEMANTICS)->copyFromHostToGPU();} getAttributeBuffer(TEXCOORD_SEMANTICS)->copyFromHostToGPU(); getIndexBuffer()->copyFromHostToGPU(); }
void Mesh::setIndexBuffer(const HardwareIndexBuffer& indexbuffer) { auto ptr = lock(); if ( ptr ) ptr->setIndexBuffer(indexbuffer); }