Skip to content

Instantly share code, notes, and snippets.

@roxlu
Created September 15, 2012 12:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save roxlu/3727599 to your computer and use it in GitHub Desktop.
Save roxlu/3727599 to your computer and use it in GitHub Desktop.
Generating vertices for textured arcs

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">

#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();
}
#ifndef APOLLO_RAINBOWARCH
#define APOLLO_RAINBOWARCH
#include <roxlu/Roxlu.h>
class RainbowArc {
public:
RainbowArc(VerticesPT& vertices);
~RainbowArc();
void setup(const float radius, const float width, const char* fgImage, const char* bgImage);
void update();
void draw();
void debugDraw();
int fg_start_dx;
int fg_num_vertices;
int bg_start_dx;
int bg_num_vertices;
Mat4 model_matrix;
Texture fg_tex;
Texture bg_tex;
float x;
float y;
float angle;
private:
int createArc(const float x, const float y, const float rad, const float size, bool isBackground);
void debugDrawArc(int start, int end);
float radius;
float width;
VerticesPT& vertices;
};
#endif
@roxlu
Copy link
Author

roxlu commented Sep 15, 2012

test

@roxlu
Copy link
Author

roxlu commented Sep 15, 2012

test

@roxlu
Copy link
Author

roxlu commented Sep 15, 2012

dsdf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment