void Context::Draw() { Graphics::Renderer *r = GetRenderer(); // Ticket for the viewport mostly Graphics::Renderer::StateTicket ticket(r); r->SetViewport(0, 0, m_width, m_height); // reset renderer for each layer for (std::vector<Layer*>::iterator i = m_layers.begin(); i != m_layers.end(); ++i) { r->SetOrthographicProjection(0, m_width, m_height, 0, -1, 1); r->SetTransform(matrix4x4f::Identity()); r->SetClearColor(Color4f::BLACK); DrawWidget(*i); r->SetScissor(false); } if (m_mousePointer && m_mousePointerEnabled) { r->SetOrthographicProjection(0, m_width, m_height, 0, -1, 1); r->SetTransform(matrix4x4f::Identity()); r->SetClearColor(Color4f::BLACK); DrawWidget(m_mousePointer); r->SetScissor(false); } }
void Billboard::Render(const matrix4x4f &trans, const RenderData *rd) { Graphics::Renderer *r = GetRenderer(); Graphics::VertexArray va(Graphics::ATTRIB_POSITION | Graphics::ATTRIB_UV0, 6); const matrix3x3f rot = trans.GetOrient().Transpose(); //some hand-tweaked scaling, to make the lights seem larger from distance const float size = m_size * Graphics::GetFovFactor() * Clamp(trans.GetTranslate().Length() / 500.f, 0.25f, 15.f); const vector3f rotv1 = rot * vector3f(size/2.f, -size/2.f, 0.0f); const vector3f rotv2 = rot * vector3f(size/2.f, size/2.f, 0.0f); va.Add(m_offset-rotv1, vector2f(0.f, 0.f)); //top left va.Add(m_offset-rotv2, vector2f(0.f, 1.f)); //bottom left va.Add(m_offset+rotv2, vector2f(1.f, 0.f)); //top right va.Add(m_offset+rotv2, vector2f(1.f, 0.f)); //top right va.Add(m_offset-rotv2, vector2f(0.f, 1.f)); //bottom left va.Add(m_offset+rotv1, vector2f(1.f, 1.f)); //bottom right r->SetTransform(trans); r->DrawTriangles(&va, m_renderState, m_material.Get()); }
void ShipSpinner::Draw() { Graphics::Renderer *r = GetContext()->GetRenderer(); Graphics::Renderer::StateTicket ticket(r); r->SetPerspectiveProjection(45.f, 1.f, 1.f, 10000.f); r->SetTransform(matrix4x4f::Identity()); r->SetDepthTest(true); r->ClearDepthBuffer(); r->SetLights(1, &m_light); Point pos(GetAbsolutePosition() + GetActiveOffset()); Point size(GetActiveArea()); r->SetViewport(pos.x, GetContext()->GetSize().y - pos.y - size.y, size.x, size.y); matrix4x4f rot = matrix4x4f::RotateXMatrix(m_rotX); rot.RotateY(m_rotY); rot[14] = -1.5f * m_model->GetDrawClipRadius(); m_model->Render(r, rot, &m_params); }
void Label3D::Render(const matrix4x4f &trans, const RenderData *rd) { //needs alpha test Graphics::Renderer *r = GetRenderer(); r->SetTransform(trans); r->DrawTriangles(m_geometry.Get(), m_material.Get()); }
void ModelSpinner::Draw() { Graphics::Renderer *r = GetContext()->GetRenderer(); Graphics::Renderer::StateTicket ticket(r); const float fov = 45.f; r->SetPerspectiveProjection(fov, 1.f, 1.f, 10000.f); r->SetTransform(matrix4x4f::Identity()); r->SetDepthWrite(true); r->SetDepthTest(true); r->ClearDepthBuffer(); r->SetLights(1, &m_light); Point pos(GetAbsolutePosition() + GetActiveOffset()); Point size(GetActiveArea()); r->SetViewport(pos.x, GetContext()->GetSize().y - pos.y - size.y, size.x, size.y); matrix4x4f rot = matrix4x4f::RotateXMatrix(m_rotX); rot.RotateY(m_rotY); const float dist = m_model->GetDrawClipRadius() / sinf(DEG2RAD(fov*0.5f)); rot[14] = -dist; m_model->Render(rot); }
int main(int argc, char **argv) { FileSystem::Init(); if (SDL_Init(SDL_INIT_VIDEO) < 0) { Output("sdl init failed: %s\n", SDL_GetError()); exit(-1); } Graphics::Settings videoSettings; videoSettings.width = WIDTH; videoSettings.height = HEIGHT; videoSettings.fullscreen = false; videoSettings.hidden = false; videoSettings.requestedSamples = 0; videoSettings.vsync = false; videoSettings.useTextureCompression = false; videoSettings.enableDebugMessages = false; videoSettings.iconFile = OS::GetIconFilename(); videoSettings.title = "textstress"; Graphics::Renderer *r = Graphics::Init(videoSettings); r->SetOrthographicProjection(0, WIDTH, HEIGHT, 0, -1, 1); r->SetTransform(matrix4x4f::Identity()); r->SetClearColor(Color::BLACK); r->SetBlendMode(Graphics::BLEND_ALPHA); r->SetDepthTest(false); const Text::FontDescriptor fontDesc(Text::FontDescriptor::Load(FileSystem::gameDataFiles, "fonts/UIFont.ini", "en")); Text::TextureFont *font = new Text::TextureFont(fontDesc, r); std::string str; for (int i = 33; i < 127; i++) str.push_back(i); while (1) { bool done = false; SDL_Event event; while (SDL_PollEvent(&event)) { if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) done = true; } if (done) break; font->RenderString(str.c_str(), rand()%(WIDTH*2)-WIDTH, rand()%HEIGHT, Color::WHITE); r->SwapBuffers(); } delete font; delete r; SDL_Quit(); exit(0); }
void Screen::LeaveOrtho() { PROFILE_SCOPED() Graphics::Renderer *r = GetRenderer(); r->SetProjection(projMatrix); r->SetTransform(modelMatrix); }
void StaticGeometry::Render(const matrix4x4f &trans, const RenderData *rd) { PROFILE_SCOPED() SDL_assert(m_renderState); Graphics::Renderer *r = GetRenderer(); r->SetTransform(trans); for (auto& it : m_meshes) r->DrawBufferIndexed(it.vertexBuffer.Get(), it.indexBuffer.Get(), m_renderState, it.material.Get()); //DrawBoundingBox(m_boundingBox); }
void StaticGeometry::Render(const std::vector<matrix4x4f> &trans, const RenderData *rd) { PROFILE_SCOPED() SDL_assert(m_renderState); Graphics::Renderer *r = GetRenderer(); const size_t numTrans = trans.size(); if (!m_instBuffer.Valid() || (numTrans > m_instBuffer->GetSize())) { // create the InstanceBuffer with the maximum number of transformations we might use within it. m_instBuffer.Reset( r->CreateInstanceBuffer(numTrans, Graphics::BUFFER_USAGE_DYNAMIC) ); } // Update the InstanceBuffer data for(Uint32 i=0; i<numTrans; i++) { Graphics::InstanceBuffer* ib = m_instBuffer.Get(); matrix4x4f *pBuffer = ib->Map(Graphics::BUFFER_MAP_WRITE); // Copy the transforms into the buffer for(auto mt : trans) { (*pBuffer) = mt; ++pBuffer; } ib->Unmap(); ib->SetInstanceCount(numTrans); } // we'll set the transformation within the vertex shader so identity the global one r->SetTransform(matrix4x4f::Identity()); // process each mesh for (auto& it : m_meshes) { // Due to the shader needing to change we have to get the material and force it to the instanced variant Graphics::MaterialDescriptor mdesc = it.material->GetDescriptor(); mdesc.instanced = true; // create the "new" material with the instanced description RefCountedPtr<Graphics::Material> mat(r->CreateMaterial(mdesc)); // copy over all of the other details mat->texture0 = it.material->texture0; mat->texture1 = it.material->texture1; mat->texture2 = it.material->texture2; mat->texture3 = it.material->texture3; mat->texture4 = it.material->texture4; mat->texture5 = it.material->texture5; mat->texture6 = it.material->texture5; mat->heatGradient = it.material->heatGradient; mat->diffuse = it.material->diffuse; mat->specular = it.material->specular; mat->emissive = it.material->emissive; mat->shininess = it.material->shininess; mat->specialParameter0 = it.material->specialParameter0; // finally render using the instance material r->DrawBufferIndexedInstanced(it.vertexBuffer.Get(), it.indexBuffer.Get(), m_renderState, mat.Get(), m_instBuffer.Get()); } }
void Screen::EnterOrtho() { PROFILE_SCOPED() Graphics::Renderer *r = GetRenderer(); modelMatrix = r->GetCurrentModelView(); projMatrix = r->GetCurrentProjection(); r->GetCurrentViewport(&viewport[0]); r->SetOrthographicProjection(0, width, height, 0, -1, 1); r->SetTransform(matrix4x4f::Identity()); }
void Context::Draw() { Graphics::Renderer *r = GetRenderer(); // reset renderer for each layer for (std::vector<Layer*>::iterator i = m_layers.begin(); i != m_layers.end(); ++i) { r->SetOrthographicProjection(0, m_width, m_height, 0, -1, 1); r->SetTransform(matrix4x4f::Identity()); r->SetClearColor(Color::BLACK); (*i)->Draw(); r->SetScissor(false); } }
void Context::Draw() { Graphics::Renderer *r = GetRenderer(); r->SetOrthographicProjection(0, m_width, m_height, 0, -1, 1); r->SetTransform(matrix4x4f::Identity()); r->SetClearColor(Color::BLACK); r->SetBlendMode(Graphics::BLEND_ALPHA); r->SetDepthTest(false); Single::Draw(); m_float->Draw(); r->SetScissor(false); }
void Thruster::Render(const matrix4x4f &trans, const RenderData *rd) { float power = 0.f; power = -dir.Dot(vector3f(rd->linthrust)); if (!linearOnly) { // pitch X // yaw Y // roll Z //model center is at 0,0,0, no need for invSubModelMat stuff const vector3f at = vector3f(rd->angthrust); const vector3f angdir = pos.Cross(dir); const float xp = angdir.x * at.x; const float yp = angdir.y * at.y; const float zp = angdir.z * at.z; if (xp+yp+zp > 0) { if (xp > yp && xp > zp && fabs(at.x) > power) power = fabs(at.x); else if (yp > xp && yp > zp && fabs(at.y) > power) power = fabs(at.y); else if (zp > xp && zp > yp && fabs(at.z) > power) power = fabs(at.z); } } if (power < 0.001f) return; Graphics::Renderer *r = GetRenderer(); r->SetTransform(trans); r->SetBlendMode(Graphics::BLEND_ALPHA_ONE); r->SetDepthWrite(false); m_tMat->diffuse = m_glowMat->diffuse = baseColor * power; //directional fade vector3f cdir = vector3f(trans * -dir).Normalized(); vector3f vdir = vector3f(trans[2], trans[6], -trans[10]).Normalized(); // XXX check this for transition to new colors. m_glowMat->diffuse.a = Easing::Circ::EaseIn(Clamp(vdir.Dot(cdir), 0.f, 1.f), 0.f, 1.f, 1.f) * 255; m_tMat->diffuse.a = 255 - m_glowMat->diffuse.a; r->DrawTriangles(m_tVerts.get(), m_tMat.Get()); r->DrawTriangles(m_glowVerts.get(), m_glowMat.Get()); r->SetBlendMode(Graphics::BLEND_SOLID); r->SetDepthWrite(true); }
void Billboard::Render(const matrix4x4f &trans, const RenderData *rd) { PROFILE_SCOPED() Graphics::Renderer *r = GetRenderer(); const matrix3x3f rot = trans.GetOrient().Transpose(); //some hand-tweaked scaling, to make the lights seem larger from distance const float size = m_size * Graphics::GetFovFactor() * Clamp(trans.GetTranslate().Length() / 500.f, 0.25f, 15.f); const vector3f rotv1 = rot * vector3f(size*0.5f, -size*0.5f, 0.0f); const vector3f rotv2 = rot * vector3f(size*0.5f, size*0.5f, 0.0f); if( !m_vbuffer.Valid() ) { //create buffer and upload data Graphics::VertexBufferDesc vbd; vbd.attrib[0].semantic = Graphics::ATTRIB_POSITION; vbd.attrib[0].format = Graphics::ATTRIB_FORMAT_FLOAT3; vbd.attrib[1].semantic = Graphics::ATTRIB_UV0; vbd.attrib[1].format = Graphics::ATTRIB_FORMAT_FLOAT2; vbd.numVertices = 6; vbd.usage = Graphics::BUFFER_USAGE_DYNAMIC; // we could be updating this per-frame m_vbuffer.Reset( r->CreateVertexBuffer(vbd) ); } #pragma pack(push, 4) struct PosUVVert { vector3f pos; vector2f uv; }; #pragma pack(pop) PosUVVert* vtxPtr = m_vbuffer->Map<PosUVVert>(Graphics::BUFFER_MAP_WRITE); vtxPtr[0].pos = (m_offset - rotv1); vtxPtr[0].uv = vector2f(0.f, 0.f); //top left vtxPtr[1].pos = (m_offset - rotv2); vtxPtr[1].uv = vector2f(0.f, 1.f); //bottom left vtxPtr[2].pos = (m_offset + rotv2); vtxPtr[2].uv = vector2f(1.f, 0.f); //top right vtxPtr[3].pos = (m_offset + rotv2); vtxPtr[3].uv = vector2f(1.f, 0.f); //top right vtxPtr[4].pos = (m_offset - rotv2); vtxPtr[4].uv = vector2f(0.f, 1.f); //bottom left vtxPtr[5].pos = (m_offset + rotv1); vtxPtr[5].uv = vector2f(1.f, 1.f); //bottom right m_vbuffer->Unmap(); r->SetTransform(trans); r->DrawBuffer(m_vbuffer.Get(), m_renderState, m_material.Get()); r->GetStats().AddToStatCount(Graphics::Stats::STAT_BILLBOARD, 1); }
void Thruster::Render(const matrix4x4f &trans, const RenderData *rd) { float power = 0.f; power = -dir.Dot(vector3f(rd->linthrust)); if (!linearOnly) { // pitch X // yaw Y // roll Z //model center is at 0,0,0, no need for invSubModelMat stuff const vector3f at = vector3f(rd->angthrust); const vector3f angdir = pos.Cross(dir); const float xp = angdir.x * at.x; const float yp = angdir.y * at.y; const float zp = angdir.z * at.z; if (xp+yp+zp > 0) { if (xp > yp && xp > zp && fabs(at.x) > power) power = fabs(at.x); else if (yp > xp && yp > zp && fabs(at.y) > power) power = fabs(at.y); else if (zp > xp && zp > yp && fabs(at.z) > power) power = fabs(at.z); } } if (power < 0.001f) return; Graphics::Renderer *r = GetRenderer(); r->SetBlendMode(Graphics::BLEND_ADDITIVE); r->SetDepthWrite(false); r->SetTransform(trans); m_tMat->diffuse = baseColor * power; //directional fade /*vector3f cdir(0.f, 0.f, -1.f); vector3f vdir(-trans[2], -trans[6], -trans[10]); m_tMat->diffuse.a = 1.f - Clamp(vdir.Dot(cdir), 0.f, 1.f);*/ r->DrawTriangles(m_tVerts.Get(), m_tMat.Get()); r->SetBlendMode(Graphics::BLEND_SOLID); r->SetDepthWrite(true); }
int main(int argc, char **argv) { FileSystem::Init(); if (SDL_Init(SDL_INIT_VIDEO) < 0) { fprintf(stderr, "sdl init failed: %s\n", SDL_GetError()); exit(-1); } const SDL_VideoInfo *info = SDL_GetVideoInfo(); switch (info->vfmt->BitsPerPixel) { case 16: SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5); break; case 24: case 32: SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); break; default: fprintf(stderr, "invalid pixel depth: %d bpp\n", info->vfmt->BitsPerPixel); exit(-1); } SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1); SDL_Surface *surface = SDL_SetVideoMode(WIDTH, HEIGHT, info->vfmt->BitsPerPixel, SDL_OPENGL); if (!surface) { fprintf(stderr, "sdl video mode init failed: %s\n", SDL_GetError()); SDL_Quit(); exit(-1); } SDL_WM_SetCaption("textstress", "textstress"); Graphics::Settings videoSettings; videoSettings.width = WIDTH; videoSettings.height = HEIGHT; videoSettings.fullscreen = false; videoSettings.shaders = false; videoSettings.requestedSamples = 0; videoSettings.vsync = false; videoSettings.useTextureCompression = false; Graphics::Renderer *r = Graphics::Init(videoSettings); r->SetOrthographicProjection(0, WIDTH, HEIGHT, 0, -1, 1); r->SetTransform(matrix4x4f::Identity()); r->SetClearColor(Color::BLACK); r->SetBlendMode(Graphics::BLEND_ALPHA); r->SetDepthTest(false); const Text::FontDescriptor fontDesc(Text::FontDescriptor::Load(FileSystem::gameDataFiles, "fonts/UIFont.ini", "English")); Text::TextureFont *font = new Text::TextureFont(fontDesc, r); std::string str; for (int i = 33; i < 127; i++) str.push_back(i); while (1) { bool done = false; SDL_Event event; while (SDL_PollEvent(&event)) { if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) done = true; } if (done) break; font->RenderString(str.c_str(), rand()%(WIDTH*2)-WIDTH, rand()%HEIGHT, Color::WHITE); r->SwapBuffers(); } delete font; delete r; SDL_Quit(); exit(0); }