Skip to content

Instantly share code, notes, and snippets.

@genya0407
Created April 28, 2014 09:14
Show Gist options
  • Save genya0407/11366359 to your computer and use it in GitHub Desktop.
Save genya0407/11366359 to your computer and use it in GitHub Desktop.
report02
require 'opengl'
require 'glut'
Width = 200
Height = 200
__num = 0
#######################
##縦棒
#######################
def vert(vect)
vectors = gen_vectors_vert(vect[0],vect[1])
vert_(vectors)
end
def vert_inv(vect)
vectors = gen_vectors_vert(vect[0],vect[1])
vectors.map!{|item| [item[0], -item[1]]}
vert_(vectors)
end
def gen_vectors_vert(x,y)
vectors = [
[x+ 0.0,y+ 0.0],
[x+ 1.0/12,y+ 1.0/12],
[x- 1.0/12,y+ 1.0/12],
[x+ 1.0/12,y+ 9.0/12],
[x- 1.0/12,y+ 9.0/12],
[x+ 0.0,y+ 5.0/6]
]
return vectors
end
def vert_(vectors)
GL.Begin(GL::TRIANGLE_STRIP)
GL.Color(1.0,1.0,1.0)
for item in vectors
GL.Vertex(item[0], item[1])
end
GL.End()
end
############################
##横棒
############################
def horz(vect)
vectors = gen_vectors_horz(vect[0],vect[1])
horz_(vectors)
end
def gen_vectors_horz(x,y)
vectors = [
[x+ 0.0,y+ 0.0],
[x+ 1.0/12,y+ 1.0/12],
[x+ 1.0/12,y- 1.0/12],
[x+ 7.0/12,y+ 1.0/12],
[x+ 7.0/12,y- 1.0/12],
[x+ 4.0/6,y+ 0.0]
]
return vectors
end
def horz_(vectors)
GL.Color(1.0,1.0,1.0)
GL.Begin(GL::TRIANGLE_STRIP)
for item in vectors
GL.Vertex(item[0],item[1])
end
GL.End()
end
###############################
##マウス
###############################
mouse = Proc.new {|button, state, x, y|
if button == (GLUT::LEFT_BUTTON) and state == (GLUT::DOWN)
__num = (__num + 1) % 10
elsif button == (GLUT::RIGHT_BUTTON) and state == (GLUT::DOWN)
__num = (__num - 1) % 10
end
GLUT.PostRedisplay()
}
keyboard = Proc.new{|key,x,y|
if key == ?f
__num = (__num + 1) % 10
elsif key == ?j
__num = (__num - 1) % 10
elsif key == ?q
exit 0
end
GLUT.PostRedisplay()
}
#########################
##実働
#########################
V1 = [1.0/6, 5.0/6]
V2 = [1.0/6, 0.0]
V3 = [1.0/6, -5.0/6]
V4 = [5.0/6, 0.0]
display = Proc.new {
GL.Clear(GL::COLOR_BUFFER_BIT)
case __num
when 0
vert(V2)
vert_inv(V2)
vert(V4)
vert_inv(V4)
for i in [V1,V3]
horz(i)
end
when 1
vert(V4)
vert_inv(V4)
when 2
vert(V4)
vert_inv(V2)
for i in [V1,V2,V3]
horz(i)
end
when 3
vert(V4)
vert_inv(V4)
for i in [V1,V2,V3]
horz(i)
end
when 4
vert(V2)
vert(V4)
vert_inv(V4)
horz(V2)
when 5
vert(V2)
vert_inv(V4)
for i in [V1,V2,V3]
horz(i)
end
when 6
vert(V2)
vert_inv(V2)
vert_inv(V4)
for i in [V1,V2,V3]
horz(i)
end
when 7
vert(V4)
vert_inv(V4)
horz(V1)
when 8
vert(V2)
vert_inv(V2)
vert(V4)
vert_inv(V4)
for i in [V1,V2,V3]
horz(i)
end
when 9
vert(V2)
vert(V4)
vert_inv(V4)
for i in [V1,V2,V3]
horz(i)
end
else
exit 0
end
GL.Flush()
}
GLUT.Init()
GLUT.InitWindowSize(Width,Height) # ウインドウサイズ (適切に設定すること)
GLUT.CreateWindow("10進カウンタ") # ウインドウタイトル (適切に設定すること)
GLUT.DisplayFunc(display) # 描画コールバックの登録
GLUT.KeyboardFunc(keyboard) # キーボード入力コールバックの登録
GLUT.MouseFunc(mouse) # マウス入力コールバックの登録
GL.ClearColor(0.0,0.0,0.0,1.0) # 背景色の設定 (適切に設定すること)
GLUT.MainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment