首先要gem install opengl gl glu glut
不过也有说只用opengl就行,使用的时候直接include Gl Glu Glut 但是我的不行
程序很好运行起来,主要是那个讨厌的控制台,于是使用了win32的ShowWindow将其隐藏。我参考的网站都已经给出,我觉得还是很有帮助的,他们见证了我遇到问题的思考过程。
# -*- coding:utf-8 -*-
#参考网站
#http://www.oschina.net/code/snippet_12_499 主要opengl程序参考
#http://blog.csdn.net/glorin/article/details/6568364 隐藏控制台启发处
#http://www.cnblogs.com/sky_online/archive/2009/11/13/1602515.html 使用ruby win32api 必备
#http://blog.csdn.net/cocolong2002/article/details/3897820 ruby win32api小示例
require 'Win32API'
require "opengl"
require "glut"
require "glu"
FindWindow=Win32API.new("user32", "FindWindow", ['P','P'], 'L')
ShowWindow=Win32API.new("user32", "ShowWindow", ['L','I'], 'B')
#title="chj_opengl"
#system "title #{title}" 方式使用名字了,不知道为什么 明明spy++都是对的但是FindWindow却不行
IntPtr_a=FindWindow.Call("ConsoleWindowClass",nil)
#puts IntPtr_a.class 是Fixnum
# IntPtr_a=gets.to_i(16) 这个是通过控制台直接输入的形式
if IntPtr_a!=0
puts IntPtr_a
ShowWindow.Call(IntPtr_a,0) #隐藏窗口
end
$light_diffuse = [1.0, 0.7, 0.7, 1.0]
$light_position = [1.0, 1.0, 1.0, 0.0]
$n = [
[-1.0, 0.0, 0.0], [0.2, 1.0, 0.0], [1.0, 0.0, 0.0],
[0.0, -1.0, 0.0], [0.0, 0.3, 1.0], [0.0, 0.0, -1.0] ]
$faces = [
[0, 1, 2, 3], [3, 2, 6, 2], [7, 6, 5, 4],
[4, 2, 1, 2], [5, 6, 2, 1], [7, 4, 0, 3] ]
def drawBox
for i in (0..5)
GL.Begin(GL::QUADS)
GL.Normal(*($n[i]))
GL.Vertex($v[$faces[i][0]])
GL.Vertex($v[$faces[i][1]])
GL.Vertex($v[$faces[i][2]])
GL.Vertex($v[$faces[i][3]])
GL.End()
end
end
display = Proc.new {
GL.Clear(GL::COLOR_BUFFER_BIT | GL::DEPTH_BUFFER_BIT)
drawBox
GLUT.SwapBuffers
}
def myinit
$v = [[-1, -1,1],[-1, -1,-1], [-1,1,-1], [-1,1,1], [1, -1,1],
[1, -1,-1], [1, 1,-1], [1,1,1]]
GL.Light(GL::LIGHT0, GL::DIFFUSE, $light_diffuse)
GL.Light(GL::LIGHT0, GL::POSITION, $light_position)
GL.Enable(GL::LIGHT0)
GL.Enable(GL::LIGHTING)
GL.Enable(GL::DEPTH_TEST)
GL.MatrixMode(GL::PROJECTION)
GLU.Perspective(40.0, 1.0, 1.0, 10.0)
GL.MatrixMode(GL::MODELVIEW)
GLU.LookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
GL.Translate(0.0, 0.0, -1.0)
GL.Rotate(60, 1.0, 0.0, 0.0)
GL.Rotate(-20, 0.0, 0.0, 1.0)
end
GLUT.Init
GLUT.InitDisplayMode(GLUT::DOUBLE | GLUT::RGB | GLUT::DEPTH)
GLUT.CreateWindow("red 3D lighted cube")
GLUT.DisplayFunc(display)
myinit
GLUT.MainLoop()