Skip to content

Commit

Permalink
Use RenderState in most of the views
Browse files Browse the repository at this point in the history
  • Loading branch information
kko committed Jan 27, 2014
1 parent 3aaadef commit 57ecb00
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 52 deletions.
7 changes: 1 addition & 6 deletions src/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@ void Camera::Draw(Graphics::Renderer *renderer, const Body *excludeBody, ShipCoc

m_renderer = renderer;

m_renderer->SetDepthWrite(true);
m_renderer->SetDepthTest(true);

glPushAttrib(GL_ALL_ATTRIB_BITS & (~GL_POINT_BIT));

m_renderer->SetPerspectiveProjection(m_fovAng, m_width/m_height, m_zNear, m_zFar);
Expand Down Expand Up @@ -232,9 +229,6 @@ void Camera::DrawSpike(double rad, const vector3d &viewCoords, const matrix4x4d
matrix4x4d rot = matrix4x4d::MakeInvRotMatrix(xaxis, yaxis, zaxis);
trans = trans * rot;

m_renderer->SetDepthTest(false);
m_renderer->SetBlendMode(BLEND_ALPHA_ONE);

// XXX this is supposed to pick a correct light colour for the object twinkle.
// Not quite correct, since it always uses the first light
GLfloat col[4];
Expand Down Expand Up @@ -282,6 +276,7 @@ void Camera::DrawSpike(double rad, const vector3d &viewCoords, const matrix4x4d
}

m_renderer->SetTransform(trans);
m_renderer->SetRenderState(Sfx::additiveAlphaState);
m_renderer->DrawTriangles(&va, Graphics::vtxColorMaterial, TRIANGLE_FAN);
}

Expand Down
10 changes: 6 additions & 4 deletions src/GalacticView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ GalacticView::GalacticView() :

m_onMouseWheelCon =
Pi::onMouseWheel.connect(sigc::mem_fun(this, &GalacticView::MouseWheel));

Graphics::RenderStateDesc rsd;
rsd.depthTest = false;
rsd.depthWrite = false;
m_renderState = Gui::Screen::GetRenderer()->CreateRenderState(rsd);
}

GalacticView::~GalacticView()
Expand Down Expand Up @@ -108,8 +113,7 @@ void GalacticView::Draw3D()
const float aspect = m_renderer->GetDisplayAspect();
m_renderer->SetOrthographicProjection(-aspect, aspect, 1.f, -1.f, -1.f, 1.f);
m_renderer->ClearScreen();
m_renderer->SetDepthTest(false);
m_renderer->SetBlendMode(BLEND_SOLID);
m_renderer->SetRenderState(m_renderState);

//apply zoom
m_renderer->SetTransform(
Expand Down Expand Up @@ -138,8 +142,6 @@ void GalacticView::Draw3D()

m_labels->Clear();
PutLabels(-vector3d(offset_x, offset_y, 0.0));

m_renderer->SetDepthTest(true);
}

void GalacticView::Update()
Expand Down
4 changes: 4 additions & 0 deletions src/GalacticView.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <vector>
#include <string>
#include "View.h"
#include "graphics/RenderState.h"

class GalacticView: public View {
public:
Expand All @@ -19,8 +20,10 @@ class GalacticView: public View {
virtual void Draw3D();
virtual void Save(Serializer::Writer &wr);
virtual void Load(Serializer::Reader &rd);

protected:
virtual void OnSwitchTo() {}

private:
void OnClickGalacticView();
void PutLabels(vector3d offset);
Expand All @@ -32,6 +35,7 @@ class GalacticView: public View {
float m_zoom, m_zoomTo;
Gui::TexturedQuad m_quad;
sigc::connection m_onMouseWheelCon;
Graphics::RenderState *m_renderState;
};

#endif /* _GALACTICVIEW_H */
11 changes: 8 additions & 3 deletions src/HudTrail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt

#include "HudTrail.h"
#include "Pi.h"
#include "graphics/RenderState.h"

const float UPDATE_INTERVAL = 0.1f;
const Uint16 MAX_POINTS = 100;
Expand All @@ -12,6 +14,11 @@ HudTrail::HudTrail(Body *b, const Color& c)
, m_color(c)
{
m_currentFrame = b->GetFrame();

Graphics::RenderStateDesc rsd;
rsd.blendMode = Graphics::BLEND_ALPHA_ONE;
rsd.depthWrite = false;
m_renderState = Pi::renderer->CreateRenderState(rsd);
}

void HudTrail::Update(float time)
Expand Down Expand Up @@ -57,9 +64,7 @@ void HudTrail::Render(Graphics::Renderer *r)
}

r->SetTransform(m_transform);
r->SetBlendMode(Graphics::BLEND_ALPHA_ONE);
r->SetDepthWrite(false);
r->SetDepthTest(true);
r->SetRenderState(m_renderState);
r->DrawLines(tvts.size(), &tvts[0], &colors[0], Graphics::LINE_STRIP);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/HudTrail.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class HudTrail
Color m_color;
matrix4x4d m_transform;
std::deque<vector3d> m_trailPoints;
Graphics::RenderState *m_renderState;
};

#endif
34 changes: 18 additions & 16 deletions src/SectorView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,14 @@ void SectorView::InitObject()
m_searchBox->onKeyPress.connect(sigc::mem_fun(this, &SectorView::OnSearchBoxKeyPress));
Add(m_searchBox, 700, 500);

m_disk.reset(new Graphics::Drawables::Disk(Pi::renderer, Color::WHITE, 0.2f));
Graphics::RenderStateDesc rsd;
m_solidState = Pi::renderer->CreateRenderState(rsd);

rsd.blendMode = Graphics::BLEND_ALPHA;
rsd.depthWrite = false;
m_alphaBlendState = Pi::renderer->CreateRenderState(rsd);

m_disk.reset(new Graphics::Drawables::Disk(Pi::renderer, m_solidState, Color::WHITE, 0.2f));

m_infoBox = new Gui::VBox();
m_infoBox->SetTransparency(false);
Expand Down Expand Up @@ -447,14 +454,13 @@ void SectorView::Draw3D()
modelview.Translate(-FFRAC(m_pos.x)*Sector::SIZE, -FFRAC(m_pos.y)*Sector::SIZE, -FFRAC(m_pos.z)*Sector::SIZE);
m_renderer->SetTransform(modelview);

m_renderer->SetBlendMode(BLEND_ALPHA);

if (m_zoomClamped <= FAR_THRESHOLD)
DrawNearSectors(modelview);
else
DrawFarSectors(modelview);

//draw sector legs in one go
m_renderer->SetRenderState(m_alphaBlendState);
m_renderer->SetTransform(matrix4x4f::Identity());
if (m_lineVerts->GetNumVerts() > 2)
m_renderer->DrawLines(m_lineVerts->GetNumVerts(), &m_lineVerts->position[0], &m_lineVerts->diffuse[0]);
Expand All @@ -463,8 +469,6 @@ void SectorView::Draw3D()
m_renderer->DrawLines(m_secLineVerts->GetNumVerts(), &m_secLineVerts->position[0], &m_secLineVerts->diffuse[0]);

UpdateFactionToggles();

m_renderer->SetBlendMode(BLEND_SOLID);
}

void SectorView::SetHyperspaceTarget(const SystemPath &path)
Expand Down Expand Up @@ -976,19 +980,11 @@ void SectorView::DrawNearSector(const int sx, const int sy, const int sz, const
m_disk->Draw(m_renderer);
}
if(bIsCurrentSystem && m_jumpSphere && m_playerHyperspaceRange>0.0f) {
// not sure I should do these here on when applying the material?
m_renderer->SetDepthWrite(false);
m_renderer->SetDepthTest(false);
m_renderer->SetBlendMode(BLEND_ALPHA);

m_renderer->SetRenderState(m_jumpSphereState);
const matrix4x4f sphTrans = trans * matrix4x4f::Translation((*i).p.x, (*i).p.y, (*i).p.z);
m_renderer->SetTransform(sphTrans * matrix4x4f::ScaleMatrix(m_playerHyperspaceRange));
m_jumpSphere->Draw(m_renderer);
m_jumpDisk->Draw(m_renderer);

m_renderer->SetDepthWrite(true);
m_renderer->SetBlendMode(BLEND_SOLID);
m_renderer->SetDepthTest(true);
}
}
}
Expand Down Expand Up @@ -1285,11 +1281,17 @@ void SectorView::Update()

if(!m_jumpSphere)
{
Graphics::RenderStateDesc rsd;
rsd.blendMode = Graphics::BLEND_ALPHA;
rsd.depthTest = false;
rsd.depthWrite = false;
m_jumpSphereState = m_renderer->CreateRenderState(rsd);

Graphics::MaterialDescriptor matdesc;
matdesc.effect = EFFECT_FRESNEL_SPHERE;
RefCountedPtr<Graphics::Material> fresnelMat(Pi::renderer->CreateMaterial(matdesc));
RefCountedPtr<Graphics::Material> fresnelMat(m_renderer->CreateMaterial(matdesc));
m_jumpSphere.reset( new Graphics::Drawables::Sphere3D(fresnelMat, 3, 1.0f) );
m_jumpDisk.reset( new Graphics::Drawables::Disk(fresnelMat, 72, 1.0f) );
m_jumpDisk.reset( new Graphics::Drawables::Disk(fresnelMat, m_jumpSphereState, 72, 1.0f) );
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/SectorView.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
#include "libs.h"
#include "gui/Gui.h"
#include "View.h"
#include <vector>
#include <set>
#include <string>
#include "View.h"
#include "galaxy/Sector.h"
#include "galaxy/SystemPath.h"
#include "graphics/Drawables.h"
#include "graphics/RenderState.h"
#include <set>

class SectorView: public View {
public:
Expand Down Expand Up @@ -149,6 +147,9 @@ class SectorView: public View {
Graphics::Drawables::Line3D m_secondLine;
Graphics::Drawables::Line3D m_jumpLine;

Graphics::RenderState *m_solidState;
Graphics::RenderState *m_alphaBlendState;
Graphics::RenderState *m_jumpSphereState;
RefCountedPtr<Graphics::Material> m_material;

std::vector<vector3f> m_farstars;
Expand Down
2 changes: 1 addition & 1 deletion src/Ship.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,7 @@ void Ship::Render(Graphics::Renderer *renderer, const Camera *camera, const vect
}

Sfx::ecmParticle->diffuse = c;
renderer->SetBlendMode(Graphics::BLEND_ALPHA_ONE);
renderer->SetRenderState(Sfx::additiveAlphaState);
renderer->DrawPointSprites(100, v, Sfx::ecmParticle, 50.f);
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/ShipCpanelMultiFuncDisplays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,6 @@ void ScannerWidget::Draw()
// objects above
if (!m_contacts.empty()) DrawBlobs(false);

m_renderer->SetBlendMode(BLEND_SOLID);

SetScissor(false);
}

Expand Down
4 changes: 3 additions & 1 deletion src/SystemView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ void SystemView::PutBody(const SystemBody *b, const vector3d &offset, const matr
if (b->type != SystemBody::TYPE_GRAVPOINT) {

if (!m_bodyIcon) {
m_bodyIcon.reset(new Graphics::Drawables::Disk(m_renderer, Color::WHITE, 1.0f));
Graphics::RenderStateDesc rsd;
auto solidState = m_renderer->CreateRenderState(rsd);
m_bodyIcon.reset(new Graphics::Drawables::Disk(m_renderer, solidState, Color::WHITE, 1.0f));
}

const double radius = b->GetRadius() * m_zoom;
Expand Down
2 changes: 0 additions & 2 deletions src/WorldView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1683,8 +1683,6 @@ void WorldView::Draw()
}

glPopAttrib();

m_renderer->SetBlendMode(Graphics::BLEND_SOLID);
}

void WorldView::DrawCrosshair(float px, float py, float sz, const Color &c)
Expand Down
1 change: 0 additions & 1 deletion src/gameui/ModelSpinner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ void ModelSpinner::Draw()
r->SetPerspectiveProjection(fov, 1.f, 1.f, 10000.f);
r->SetTransform(matrix4x4f::Identity());

r->SetDepthTest(true);
r->ClearDepthBuffer();

r->SetLights(1, &m_light);
Expand Down
10 changes: 7 additions & 3 deletions src/graphics/Drawables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace Graphics {

namespace Drawables {

Disk::Disk(Graphics::Renderer *r, const Color &c, float rad)
Disk::Disk(Graphics::Renderer *r, Graphics::RenderState *state, const Color &c, float rad)
: m_renderState(state)
{
m_vertices.reset(new VertexArray(ATTRIB_POSITION));
m_material.Reset(r->CreateMaterial(MaterialDescriptor()));
Expand All @@ -23,7 +24,9 @@ Disk::Disk(Graphics::Renderer *r, const Color &c, float rad)
}
}

Disk::Disk(RefCountedPtr<Material> material, const int numEdges/*=72*/, const float radius/*=1.0f*/) : m_material(material)
Disk::Disk(RefCountedPtr<Material> material, Graphics::RenderState *state, const int numEdges/*=72*/, const float radius/*=1.0f*/)
: m_material(material)
, m_renderState(state)
{
m_vertices.reset(new VertexArray(ATTRIB_POSITION));

Expand All @@ -39,6 +42,7 @@ Disk::Disk(RefCountedPtr<Material> material, const int numEdges/*=72*/, const fl

void Disk::Draw(Renderer *r)
{
r->SetRenderState(m_renderState);
r->DrawTriangles(m_vertices.get(), m_material.Get(), TRIANGLE_FAN);
}

Expand Down Expand Up @@ -169,7 +173,7 @@ void Sphere3D::Subdivide(const matrix4x4f &trans, const vector3f &v1, const vect
}

// a textured quad with reversed winding
TexturedQuad::TexturedQuad(Graphics::Renderer *r, Graphics::Texture *texture, const vector2f &pos, const vector2f &size) : m_texture(RefCountedPtr<Graphics::Texture>(texture))
TexturedQuad::TexturedQuad(Graphics::Renderer *r, Graphics::Texture *texture, const vector2f &pos, const vector2f &size) : m_texture(RefCountedPtr<Graphics::Texture>(texture))
{
m_vertices.reset(new VertexArray(ATTRIB_POSITION | ATTRIB_UV0));
Graphics::MaterialDescriptor desc;
Expand Down
16 changes: 7 additions & 9 deletions src/graphics/Drawables.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "graphics/Renderer.h"
#include "graphics/Surface.h"
#include "graphics/VertexArray.h"
#include "graphics/RenderState.h"

namespace Graphics {

Expand All @@ -18,6 +19,7 @@ namespace Drawables {
class Drawable {
protected:
virtual void Draw(Renderer *r) = 0;
virtual ~Drawable() { }
};

class Circle : public Drawable {
Expand All @@ -37,7 +39,6 @@ class Circle : public Drawable {
m_verts.push_back(vector3f(radius*sin(theta) + center.x, radius*cos(theta) + center.y, center.z));
}
}
virtual ~Circle() {}
virtual void Draw(Renderer *renderer) {
renderer->DrawLines(m_verts.size(), &m_verts[0], m_color, LINE_LOOP);
}
Expand All @@ -50,23 +51,22 @@ class Circle : public Drawable {
// Two-dimensional filled circle
class Disk : public Drawable {
public:
Disk(Graphics::Renderer *r, const Color &c, float radius);
Disk(RefCountedPtr<Material> material, const int numEdges=72, const float radius=1.0f);
virtual ~Disk() { }
Disk(Graphics::Renderer *r, Graphics::RenderState*, const Color &c, float radius);
Disk(RefCountedPtr<Material> material, Graphics::RenderState*, const int numEdges=72, const float radius=1.0f);
virtual void Draw(Graphics::Renderer *r);

void SetColor(const Color&);

private:
std::unique_ptr<Graphics::VertexArray> m_vertices;
RefCountedPtr<Material> m_material;
Graphics::RenderState *m_renderState;
};

//A three dimensional line between two points
class Line3D : public Drawable {
public:
Line3D();
virtual ~Line3D() {}
void SetStart(const vector3f &);
void SetEnd(const vector3f &);
void SetColor(const Color &);
Expand All @@ -83,7 +83,6 @@ class Sphere3D : public Drawable {
public:
//subdivisions must be 0-4
Sphere3D(RefCountedPtr<Material> material, int subdivisions=0, float scale=1.f);
virtual ~Sphere3D() {}
virtual void Draw(Renderer *r);

RefCountedPtr<Material> GetMaterial() const { return m_surface->GetMaterial(); }
Expand All @@ -99,11 +98,10 @@ class Sphere3D : public Drawable {
};

// a textured quad with reversed winding
class TexturedQuad : public Graphics::Drawables::Drawable {
class TexturedQuad : public Drawable {
public:
TexturedQuad(Graphics::Renderer *r, Graphics::Texture *texture, const vector2f &pos, const vector2f &size);
virtual ~TexturedQuad() {}
virtual void Draw(Graphics::Renderer *r) {
virtual void Draw(Graphics::Renderer *r) {
r->DrawTriangles(m_vertices.get(), m_material.get(), TRIANGLE_STRIP);
}

Expand Down

0 comments on commit 57ecb00

Please sign in to comment.