Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
OpenGL で正多面体を回転させる
require './miniopengl'
require 'parallel'
filenames = []
1.upto(5) {|i| filenames << "./polyhedrons_obj/r" + ("%02d" % i) + ".obj"}
data = []
filenames.each do |fn|
open(fn, "r") do |io|
ar = [[], []]
io.each_line do |l|
l.chomp!
a = l[2..-1].split(" ")
(l[0] == "v") ? ar[0] << a.map(&:to_f) : ar[1] << a.map {|i| i.to_i - 1}
end
data << ar
end
end
Parallel.each(data, in_processes: 5) do |vertex, apexes|
MiniOpenGL.app width: 400, height: 400, depth_buffer: :on do #depth_buffer を on に(隠面消去のため)
clear_color(0, 0, 0)
num = apexes.size
col = []
num.times {|i| col[i] = [rand, rand, rand]}
draw do
clear
glEnable(GL_DEPTH_TEST) #隠面消去に必要
glEnableClientState(GL_VERTEX_ARRAY)
glVertexPointer(3, GL_DOUBLE, 0, vertex.flatten)
num.times do |i|
color(col[i][0], col[i][1], col[i][2])
glDrawElements(GL_POLYGON, apexes[i].size, GL_UNSIGNED_BYTE, apexes[i])
end
display
end
reshape do |w, h|
viewport(0, 0, w, h)
init_projection
perspective(30, w / h.to_f, 3, 10)
init_modelview
look_at(3 * 0.8, 4 * 0.8, 5 *0.8, 0, 0, 0, 0, 1, 0)
end
repeat(30) do
modelview
rotate(5, 0, 1, 0)
redisplay
end
end
end
# miniopengl.rb
# http://d.hatena.ne.jp/obelisk2+marginalia/20170414/1492139406
#polyhedrons_obj
# http://mitani.cs.tsukuba.ac.jp/polyhedron/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.