Ruby OpenGL:如何加载位图纹理并将它们放在3D对象上



我刚开始用Ruby-OpenGL编程。我将Nehe教程翻译成渲染器类,它可以很好地显示一些简单的3D对象。现在我想在这些物体上添加一些纹理。我有一个"纹理。bmp",但如何把它放在我的对象?这是我的渲染器类:

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!

OpenGL对文件格式一无所知,所以在调用glTexImage2D之前,你需要一些东西将bmp加载到内存中。

编辑:这里有一些资源:纹理和图像加载

最新更新