Skip to content

Instantly share code, notes, and snippets.

@micromeeeter
Created May 29, 2017 04:23
Show Gist options
  • Save micromeeeter/3ea2b3119819f2f2026d048b3ac542d2 to your computer and use it in GitHub Desktop.
Save micromeeeter/3ea2b3119819f2f2026d048b3ac542d2 to your computer and use it in GitHub Desktop.
sinWave
//sfc17「グラフィックスプログラミング」で提出した課題
//三次元メッシュのように見えてそうではない
//
//This program requires you to import two libraries, GLUT.framework and OpenGL.framework.
#include <iostream>
#include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
#include <math.h>
float phase = 0.0; //位相
void display(void){
glClear(GL_COLOR_BUFFER_BIT); //ウィンドウの初期化
int n = 40; //メッシュの行数
int m = 8; //メッシュの列数
float xs[n][m], ys[n][m]; //メッシュの頂点の座標
for(int y = 0; y < m; y++){ //メッシュの頂点座標に値を代入
for(int x = 0; x < n; x++){
xs[x][y] = - 1.2 + 2.4 / n * x + 0.01 * y;
ys[x][y] = - 0.3 + 0.6 / m * y + 0.1 * sin(0.5*x - 0.5*y + phase);
}
}
for(int y = 0; y < m-1; y++){ //メッシュの頂点を順番に呼んで図形を決める
glBegin(GL_LINE_STRIP);
for(int x = 0; x < n; x++){
glVertex2f(xs[x][y], ys[x][y]);
glColor4f((217.0 - 66.0/(n+m) * (x+y)) / 256.0, (175.0 + 42.0/(n+m) * (x+y)) / 256.0, (217 - 8.0/(n+m)*(x+y))/256.0, 1.0);
glVertex2f(xs[x][y+1], ys[x][y+1]);
glColor4f((217.0 - 66.0/(n+m) * (x+y+1)) / 256.0, (175.0 + 42.0/(n+m) * (x+y+1)) / 256.0, (217 - 8.0/(n+m)*(x+y+1))/256.0, 1.0);
}
glEnd();
}
glFlush();
}
void timer(int value){ //10ミリ秒ごとに実行される関数
phase += 0.05; //位相を増加
glutPostRedisplay(); //画面を再描画
glutTimerFunc(10, timer, 0); //10ミリ秒ごとに再実行
}
int main(int argc, char * argv[]) {
glutInit(&argc, argv); //GLUTの初期化
glutInitWindowSize(1500, 500); //ウィンドウのサイズを指定
glutInitDisplayMode(GLUT_RGBA); //ウィンドウの色モードを設定
glutCreateWindow("assingment_01"); //ウィンドウを生成
glutDisplayFunc(display); //画面を再描画するときに実行される関数を指定
glClearColor(0.9, 0.9, 0.9, 1.0); //背景色を設定:明るい灰色
glLineWidth(1.5); //lineの太さを変更
glEnable(GL_LINE_SMOOTH); //lineにアンチエイリアスをかける(効用が感じられない気もする)
glutTimerFunc(100, timer, 0); //10ミリ病後にtimer()を実行
glutMainLoop(); //ウィンドウが閉じられるまでループ
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment