[OpenGL]
OpenGL学習
三角形を描画
- RubyとBundlerを使ってやってみる
- GL::GL_TRIANGLES
- GL.Begin, GL.Endを使って素朴な3D描画 begin, end は yield を使っている
- 色指定を GL.Color3f(1, 0, 0) だけにすると赤色の三角形が描画される
- コメントアウトを外すと3色混じった三角形が描画される
#!/usr/bin/env ruby # coding: utf-8 require "bundler/setup" require "opengl" require "glut" require "glu" class Triangle def block(c) GL.Begin(c) yield GL.End end def initialize() GLUT.InitWindowPosition(400, 400) GLUT.InitWindowSize(400, 400) GLUT.Init GLUT.InitDisplayMode(GLUT::GLUT_RGB) GLUT.CreateWindow("sample") GLUT.DisplayFunc(method(:display).to_proc()) GL.ClearColor(1,1,1,1) end def display() GL.Clear(GL::GL_COLOR_BUFFER_BIT) block(GL::GL_TRIANGLES) do GL.Color3f(1, 0, 0) GL.Vertex2f(0, 0.5) # GL.Color3f(0, 1, 0) GL.Vertex2f(-0.5, -0.5) # GL.Color3f(0, 0, 1) GL.Vertex2f(0.5, -0.5) end GL.Flush() end def start() GLUT.MainLoop() end end Triangle.new().start()
triangle1.png
triangle2.png