Last active
October 17, 2015 11:50
-
-
Save wararyo/b48c42eec2e1ff4aa6af to your computer and use it in GitHub Desktop.
オリンピックの図形を描く
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define GLFW_INCLUDE_GLU | |
#define _USE_MATH_DEFINES //M_PIを使うためのおまじない | |
#include <gl/glfw3.h> //某大の教授が開発したっぽいChinouGLとやらの代わりにGLFWを使う(ChinouGLはググっても情報が出てこなかった) | |
#include <iostream> | |
#include <cstdlib> | |
#include <cmath> | |
#include <string> | |
#define LENGTH_OF(TS) (sizeof(TS) / sizeof(TS[0]))//配列のサイズを求める | |
GLFWwindow* window; | |
const int HEIGHT = 480; | |
const int WIDTH = 640; | |
typedef struct hCircle{ | |
int x;int y;int z; | |
int outer_radius;int inner_radius; | |
int start_degree;int end_degree; | |
float red;float green;float blue; | |
} hCircle_t; | |
const hCircle_t OLYMPIC[] = { | |
{448,272,-40,60,50,-45,135,1.0f,0.0f,0.0f},//赤うえ | |
{384,208,-30,60,50,225,405,0.0f,1.0f,0.0f},//緑した | |
{448,272,-20,60,50,135,315,1.0f,0.0f,0.0f},//赤した | |
{256,208,-10,60,50,225,405,1.0f,1.0f,0.0f},//黄した | |
{320,272,0,60,50,135,315,1.0f,1.0f,1.0f},//白した | |
{384,208,10,60,50,45,225,0.0f,1.0f,0.0f},//緑うえ | |
{320,272,20,60,50,-45,135,1.0f,1.0f,1.0f},//白うえ | |
{192,272,30,60,50,135,315,0.0f,0.0f,1.0f},//青した | |
{256,208,40,60,50,45,225,1.0f,1.0f,0.0f},//黄うえ | |
{192,272,50,60,50,-45,135,0.0f,0.0f,1.0f},//青うえ | |
}; | |
//OpenGL周りの初期化 | |
bool Initialize(){ | |
if (!glfwInit()) return false; | |
window = glfwCreateWindow(640, 480, "OpenGL Window", nullptr, nullptr); | |
if (!window) { | |
glfwTerminate(); | |
return false; | |
} | |
glfwMakeContextCurrent(window); | |
return true; | |
} | |
//ウィンドウ左下が原点 | |
void glStart(){ | |
int width, height; | |
glfwGetFramebufferSize(window, &width, &height); | |
float ratio = width / (float) height; | |
glViewport(0, 0, width, height); | |
glClear(GL_COLOR_BUFFER_BIT); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
glOrtho(0.0f, width, 0.0f, height, 1000.0f, -1000.0f); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
} | |
//かまぼこの赤い部分みたいな図形を描くよ | |
//xとyは円の中心の座標だよ。 | |
void drawHollowCircle(int x,int y,int z, int outer_radius,int inner_radius, int start_degree,int end_degree, float red,float green,float blue){ | |
if(start_degree > end_degree) return;//start_degreeの方が大きいのはダメ | |
glBegin(GL_TRIANGLE_STRIP); | |
glColor3f(red,green,blue); | |
int diff_degree = (end_degree - start_degree); | |
//円の外側の頂点 | |
for(int i=0;i <= diff_degree;i++){ | |
float angle = (float)(M_PI*2.0*(i+start_degree))/360.0f; //degreeをradianに変換 | |
glVertex3f(outer_radius * cos(angle) + x, | |
outer_radius * sin(angle) + y, z); | |
glVertex3f(inner_radius * cos(angle) + x, | |
inner_radius * sin(angle) + y, z); | |
} | |
glEnd(); | |
} | |
int main() | |
{ | |
if(!Initialize()){ | |
return -1;//初期化に失敗したのでプログラム終了 | |
} | |
while(!glfwWindowShouldClose(window)){ | |
glStart(); | |
glTranslatef(WIDTH/2,0.f,0.f); | |
glRotatef((float) sin(glfwGetTime()*2)*60, 0.f, 1.0f, 0.f);//何となく回転させてみる | |
glTranslatef(-WIDTH/2,0.f,0.f); | |
for(int i=0;i < LENGTH_OF(OLYMPIC);i++){ | |
hCircle_t c = OLYMPIC[i]; | |
drawHollowCircle(c.x,c.y,c.z, c.outer_radius,c.inner_radius, c.start_degree,c.end_degree, c.red,c.green,c.blue); | |
} | |
glfwSwapBuffers(window); | |
glfwPollEvents(); | |
} | |
glfwTerminate(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment