ruby 使用opengl,用Win32api隐藏控制台

首先要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()




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值