Commit c16ac092 authored by bbguimaraes's avatar bbguimaraes
Browse files

sms: make texture rendering a little less hack-y

parent b26095be
......@@ -17,7 +17,9 @@ class Rendering {
static void draw_axis(GLUquadric * q, unsigned int slices);
static void draw_axes(GLUquadric * q, unsigned int slices);
static void draw_springs_non_textured(const SpringMassSystem * system);
static void draw_springs_textured(SpringMassSystem * system);
static void draw_springs_textured(
SpringMassSystem * system,
unsigned int texture_width, unsigned int texture_height);
static void draw_masses(
const SpringMassSystem * system,
GLenum mode,
......
......@@ -12,13 +12,17 @@ class Simulation {
bool m_draw_masses;
bool m_textured;
bool m_texturable;
unsigned int m_texture_width;
unsigned int m_texture_height;
public:
Simulation() :
m_paused(false),
m_draw_masses(true),
m_textured(false),
m_texturable(false) {}
m_texturable(false),
m_texture_width(0),
m_texture_height(0) {}
const std::vector<SpringMassSystem *> * systems() const
{return &this->m_systems;}
......@@ -27,6 +31,8 @@ class Simulation {
bool draw_masses() const {return this->m_draw_masses;}
bool textured() const {return this->m_textured;}
bool texturable() const {return this->m_texturable;}
unsigned int texture_width() const {return this->m_texture_width;}
unsigned int texture_height() const {return this->m_texture_height;}
void set_systems(const std::vector<SpringMassSystem *> & v)
{this->m_systems = v;}
......@@ -35,6 +41,8 @@ class Simulation {
void set_draw_masses(bool d) {this->m_draw_masses = d;}
void set_textured(bool t) {this->m_textured = t;}
void set_texturable(bool t) {this->m_texturable = t;}
void set_texture_width(unsigned int w) {this->m_texture_width = w;}
void set_texture_height(unsigned int h) {this->m_texture_height = h;}
void update();
};
......
......@@ -118,7 +118,8 @@ void Display::draw_simulation(Simulation * s) {
} else {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, this->m_texture);
Rendering::draw_springs_textured(system);
Rendering::draw_springs_textured(
system, s->texture_width(), s->texture_height());
glDisable(GL_TEXTURE_2D);
}
if(s->draw_masses())
......
......@@ -26,6 +26,10 @@ int main(int argc, char ** argv) {
simulations[1].set_update_rate(UPDATE_RATE);
simulations[0].set_texturable(true);
simulations[1].set_texturable(true);
simulations[0].set_texture_width(10);
simulations[1].set_texture_width(10);
simulations[0].set_texture_height(5);
simulations[1].set_texture_height(5);
for(auto & mass : *sms0.masses())
mass.set_position(mass.position() + Vector(-10.0f));
for(auto & mass : *sms1.masses())
......
......@@ -90,20 +90,24 @@ void Rendering::draw_springs_non_textured(const SpringMassSystem * system) {
}
/*static*/
void Rendering::draw_springs_textured(SpringMassSystem * system) {
void Rendering::draw_springs_textured(
SpringMassSystem * system,
unsigned int texture_width, unsigned int texture_height) {
auto masses = system->masses();
const unsigned int WIDTH = 10;
const float WIDTH_F = static_cast<float>(WIDTH);
auto point = [WIDTH, WIDTH_F, masses](int x, int y) {
glTexCoord2f(x / WIDTH_F, y / WIDTH_F);;
auto p = (*masses)[y * WIDTH + x].position();
glVertex3f(p.x(), p.y(), p.z());;
};
const float WIDTH_F = static_cast<float>(texture_width - 1);
const float HEIGHT_F = static_cast<float>(texture_height - 1);
auto point =
[WIDTH_F, HEIGHT_F, texture_width, texture_height, masses]
(int x, int y) {
glTexCoord2f(x / WIDTH_F, y / HEIGHT_F);;
auto p = (*masses)[y * texture_width + x].position();
glVertex3f(p.x(), p.y(), p.z());;
};
glPushAttrib(GL_CURRENT_BIT);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_TRIANGLES);
for(unsigned int y = 0; y < masses->size() / WIDTH - 1; ++y)
for(unsigned int x = 0; x < WIDTH - 1; ++x) {
for(unsigned int y = 0; y < masses->size() / texture_width - 1; ++y)
for(unsigned int x = 0; x < texture_width - 1; ++x) {
point(x , y );
point(x , y + 1);
point(x + 1, y + 1);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment