2011-04-24 67 views
0

我剛開始用Ruby-OpenGL編程。 我將Nehe教程翻譯成渲染器類,它可以很好地顯示一些簡單的3D對象。 現在我想在這些對象上添加一些紋理。 我有一個「texture.bmp」,但如何將它放在我的對象? 這是我的渲染器類:Ruby OpenGL:如何加載位圖紋理並將其放置在3D對象上?

module RGLEngine 
    require "rubygems" 
    require "gl" 
    require "glu" 
    require "glut" 
    require "mathn" 
    require "yaml" 

    class Renderer 
     include Gl 
     include Glu 
     include Glut 

     attr_reader :width, :height 

     def initialize 
      @width = 640 # to be loaded from config.yaml 
      @height = 480 
     end 
     def render 
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) 
      glMatrixMode(GL_MODELVIEW) 
      glLoadIdentity 
      glTranslatef(-1.5, 0.0, -6.0) 
      # Draw Poylgons 
      # ... 
     # uncomment this for a simple example 
      #glBegin(GL_POLYGON) 
      # glVertex3f(0.0, 1.0, 0.0) 
      # glVertex3f(1.0, -1.0, 0.0) 
      # glVertex3f(-1.0, -1.0, 0.0) 
      #glEnd 
      glutSwapBuffers 
     end 
     def idle 
      glutPostRedisplay 
     end 
     def reshape(width, height) 
      if height == 0 
       height = 1 
      end 
      glViewport(0, 0, width, height) 
      glMatrixMode(GL_PROJECTION) 
      glLoadIdentity 
      gluPerspective(45.0, width/height, 0.1, 100.0) 
     end 
     def realize! 
      glutInit 
      glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH) 
      glutInitWindowSize(@width, @height) 
      glutInitWindowPosition(0, 0) 
      window = glutCreateWindow("RGLEngine alpha") 
      glutDisplayFunc(method(:render).to_proc) 
      glutReshapeFunc(method(:reshape).to_proc) 
      glutIdleFunc(method(:idle).to_proc) 
      glClearColor(0.0, 0.0, 0.0, 0) 
      glClearDepth(1.0) 
      glDepthFunc(GL_LEQUAL) 
      glEnable(GL_DEPTH_TEST) 
      glEnable(GL_TEXTURE_2D) 
      glShadeModel(GL_SMOOTH) 
      glMatrixMode(GL_PROJECTION) 
      glLoadIdentity 
      gluPerspective(45.0, @width/@height, 0.1, 100.0) 
      glMatrixMode(GL_MODELVIEW) 
      glutMainLoop() 
     end 
    end 
end 

RGLEngine::Renderer.new.realize! 

回答

1

的OpenGL不知道有關文件格式的東西,所以你會需要的東西,你的BMP加載到內存中後,才能調用glTexImage2D。

編輯:這裏有一些資源:textures也許image loading

相關問題