|
#include "RainbowArc.h" |
|
|
|
RainbowArc::RainbowArc(VerticesPT& vertices) |
|
:radius(100) |
|
,width(5) |
|
,fg_start_dx(0) |
|
,fg_num_vertices(0) |
|
,bg_start_dx(0) |
|
,bg_num_vertices(0) |
|
,vertices(vertices) |
|
,x(0) |
|
,y(0) |
|
,angle(0) // in radians |
|
{ |
|
} |
|
|
|
RainbowArc::~RainbowArc() { |
|
} |
|
|
|
void RainbowArc::setup(const float radius, const float width, const char* fgImage, const char* bgImage) { |
|
this->radius = radius; |
|
this->width = width; |
|
bg_start_dx = vertices.size(); |
|
bg_num_vertices = createArc(512, 600, radius-width, radius+width, true); |
|
fg_start_dx = vertices.size(); |
|
fg_num_vertices = createArc(512, 600, radius-width*0.5, radius+width*0.5, false); |
|
|
|
Image img; |
|
img.load(File::toDataPath(bgImage).c_str()); |
|
bg_tex.setPixels(img.getPixels(), img.getWidth(), img.getHeight(), GL_RGBA); |
|
img.load(File::toDataPath(fgImage).c_str()); |
|
fg_tex.setPixels(img.getPixels(), img.getWidth(), img.getHeight(), GL_RGBA); |
|
} |
|
|
|
int RainbowArc::createArc(const float x, const float y, const float innerRadius, const float outerRadius, bool isBackground) { |
|
this->x = x; |
|
this->y = y; |
|
int start = vertices.size(); |
|
int num_segments = 25; |
|
float part_angle = PI / num_segments; |
|
float angle = 0; |
|
float tex_w = 1024; |
|
float tex_h = 768; |
|
for(int i = 0; i <= num_segments; ++i) { |
|
float cosa = cosf(PI + angle); |
|
float sina = sinf(PI + angle); |
|
Vec2 inner_point(cosa * innerRadius,sina * innerRadius); |
|
Vec2 outer_point(cosa * outerRadius, sina * outerRadius); |
|
if(isBackground) { |
|
float ap = angle / PI; |
|
vertices.add(VertexPT(inner_point, ap, 0.0f)); |
|
vertices.add(VertexPT(outer_point, ap, 1.0f)); |
|
} |
|
else { |
|
vertices.add(VertexPT(inner_point, (x+inner_point.x) / tex_w, (y+inner_point.y) / tex_h)); |
|
vertices.add(VertexPT(outer_point, (x+outer_point.x) / tex_w, (y+outer_point.y) / tex_h)); |
|
} |
|
angle += part_angle; |
|
} |
|
return vertices.size() - start; |
|
} |
|
|
|
void RainbowArc::update() { |
|
} |
|
|
|
void RainbowArc::draw() { |
|
} |
|
|
|
void RainbowArc::debugDraw() { |
|
glColor3f(1.0f, 1.0f, 1.0f); |
|
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); |
|
debugDrawArc(bg_start_dx, bg_start_dx+bg_num_vertices); |
|
glColor3f(1.0f, 0.0f, 0.0f); |
|
debugDrawArc(fg_start_dx, fg_start_dx+fg_num_vertices); |
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); |
|
} |
|
|
|
void RainbowArc::debugDrawArc(int start, int end) { |
|
glBegin(GL_TRIANGLE_STRIP); |
|
for(int i = start; i < end; ++i) { |
|
glVertex3fv(vertices[i].getPtr()); |
|
} |
|
glEnd(); |
|
|
|
} |
Code snippet demonstrating how to render a simple rainbow arc. This code
is created for a project where I need to generate different kind of rainbow arcs.
See createArc for the code that calculates the vertices for the rainbow. We simply
create two half parts of a circle. See http://en.wikipedia.org/wiki/Polar_coordinate_system for
more info on this.
Rainbow background (shadow) texture
Rainbow foreground texture
Test render 1
Test render 2
Test render 3
<img src='http://upload.roxlu.com/server/php/files/rainbow3.png">