Join 300,462 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,664 people online right now. Registration is fast and FREE... Join Now!
--OpenGL on Ruby-- HI im TJ. I want to add a string of text in a specified x and y coordinate to this sample code.either in 2D or 3D. Both is good for my learning and comparison. eventually im gona be wanting to scroll it at a specified rate. i dont know where to start. i searched sites but all i could find is code in other languages.
Thanks..
CODE
# This code was created by Jeff Molofee '99 # Conversion to Ruby by Manolo Padron Martinez (manolopm@cip.es)
require "opengl" require "glut"
# A general OpenGL initialization function. Sets all of the initial parameters
def InitGL(width, height) # We call this right after our OpenGL window # is created.
GL.ClearColor(0.0, 0.0, 0.0, 0.0) # This Will Clear The Background # Color To Black GL.ClearDepth(1.0) # Enables Clearing Of The Depth Buffer GL.DepthFunc(GL::LESS) # The Type Of Depth Test To Do GL.Enable(GL::DEPTH_TEST) # Enables Depth Testing GL.ShadeModel(GL::SMOOTH) # Enables Smooth Color Shading GL.MatrixMode(GL::PROJECTION) GL.LoadIdentity() # Reset The Projection Matrix GLU.Perspective(45.0,Float(width)/Float(height),0.1,100.0) # Calculate The Aspect Ratio # Of The Window GL.MatrixMode(GL::MODELVIEW) end
# The function called when our window is resized (which shouldn't happen, # because we're fullscreen) ReSizeGLScene = Proc.new {|width, height| if (height==0) # Prevent A Divide By Zero If The Window Is Too Small height=1 end GL.Viewport(0,0,width,height) # Reset The Current Viewport And # Perspective Transformation GL.MatrixMode(GL::PROJECTION) GL.LoadIdentity() GLU.Perspective(45.0,Float(width)/Float(height),0.1,100.0) GL.MatrixMode(GL::MODELVIEW) }
# The main drawing function. DrawGLScene = Proc.new { GL.Clear(GL::COLOR_BUFFER_BIT | GL::DEPTH_BUFFER_BIT) # Clear The Screen And # The Depth Buffer GL.LoadIdentity() # Reset The View GL.Translate(-1.5, 0.0, -6.0) # Move Left 1.5 Units And Into The # Screen 6.0 GL.Rotate($rtri,0.0,1.0,0.0) # Rotate the triangle on the Y Axis
# draw a triangle (in smooth coloring mode) GL.Begin(GL::POLYGON) # start drawing a polygon
# front face of pyramid GL.Color3f( 1.0, 0.0, 0.0) # Set The Color To Red GL.Vertex3f( 0.0, 1.0, 0.0) # Top GL.Color3f( 0.0, 1.0, 0.0) # Set The Color To Green GL.Vertex3f( 1.0,-1.0, 0.0) # Bottom Right GL.Color3f( 0.0, 0.0, 1.0) # Set The Color To Blue GL.Vertex3f(-1.0,-1.0, 0.0) # Bottom Left
# right face of pyramid GL.Color3f( 1.0, 0.0, 0.0) # Red GL.Vertex3f( 0.0, 1.0, 0.0) # Top of triangle (Right) GL.Color3f( 0.0, 0.0, 1.0) # Blue GL.Vertex3f( 1.0,-1.0, 1.0) # Left of triangle (Right) GL.Color3f( 1.0, 0.0, 0.0) # Green GL.Vertex3f( 1.0,-1.0,-1.0) # Right of triangle (Right)
# back face of pyramid GL.Color3f( 1.0, 0.0, 0.0) # Red GL.Vertex3f( 0.0, 1.0, 0.0) # Top of triangle (Back) GL.Color3f( 0.0, 1.0, 0.0) # Green GL.Vertex3f( 1.0,-1.0,-1.0) # Left of triangle (Back) GL.Color3f( 0.0, 0.0, 1.0) # Blue GL.Vertex3f(-1.0,-1.0,-1.0) # Right of triangle (Back
# left face of pyramid GL.Color3f( 1.0, 0.0, 0.0) # Red GL.Vertex3f( 0.0, 1.0, 0.0) # Top of triangle (Left) GL.Color3f( 0.0, 0.0, 1.0) # Blue GL.Vertex3f(-1.0,-1.0,-1.0) # Left of triangle (Left) GL.Color3f( 1.0, 0.0, 0.0) # Green GL.Vertex3f(-1.0,-1.0, 1.0) # Right of triangle (Left)
GL.End() # Done drawing the pyramid
GL.LoadIdentity() # make sure we're no longer rotated. GL.Translate(1.5,0.0,-7.0) # Move Right 3 Units, and back into # the screen 7.0 GL.Rotate($rquad,1.0,1.0,1.0) # Rotate the quad on the X Axis # draw a cube (6 quadrilateral) GL.Begin(GL::QUADS) # start drawing the cube
# top of cube GL.Color3f(0.0,1.0,0.0) # Set The Color To Blue GL.Vertex3f( 1.0, 1.0,-1.0) # Top Right Of The Quad (Top) GL.Vertex3f(-1.0, 1.0,-1.0) # Top Left Of The Quad (Top) GL.Vertex3f(-1.0, 1.0, 1.0) # Bottom Left Of The Quad (Top) GL.Vertex3f( 1.0, 1.0, 1.0) # Bottom Right Of The Quad (Top)
# bottom of cube GL.Color3f(1.0,0.5,0.0) # Set The Color To Orange GL.Vertex3f( 1.0,-1.0, 1.0) # Top Right Of The Quad (Bottom) GL.Vertex3f(-1.0,-1.0, 1.0) # Top Left Of The Quad (Bottom) GL.Vertex3f(-1.0,-1.0,-1.0) # Bottom Left Of The Quad (Bottom) GL.Vertex3f( 1.0,-1.0,-1.0) # Bottom Right Of The Quad (Bottom)
# front of cube GL.Color3f(1.0,0.0,0.0) # Set The Color To Red GL.Vertex3f( 1.0, 1.0, 1.0) # Top Right Of The Quad (Front) GL.Vertex3f(-1.0, 1.0, 1.0) # Top Left Of The Quad (Front) GL.Vertex3f(-1.0,-1.0, 1.0) # Bottom Left Of The Quad (Front) GL.Vertex3f( 1.0,-1.0, 1.0) # Bottom Right Of The Quad (Front)
# back of cube. GL.Color3f(1.0,1.0,0.0) # Set The Color To Yellow GL.Vertex3f( 1.0,-1.0,-1.0) # Top Right Of The Quad (Back) GL.Vertex3f(-1.0,-1.0,-1.0) # Top Left Of The Quad (Back) GL.Vertex3f(-1.0, 1.0,-1.0) # Bottom Left Of The Quad (Back) GL.Vertex3f( 1.0, 1.0,-1.0) # Bottom Right Of The Quad (Back)
# left of cube GL.Color3f(0.0,0.0,1.0) # Blue GL.Vertex3f(-1.0, 1.0, 1.0) # Top Right Of The Quad (Left) GL.Vertex3f(-1.0, 1.0,-1.0) # Top Left Of The Quad (Left) GL.Vertex3f(-1.0,-1.0,-1.0) # Bottom Left Of The Quad (Left) GL.Vertex3f(-1.0,-1.0, 1.0) # Bottom Right Of The Quad (Left)
# Right of cube GL.Color3f(1.0,0.0,1.0) # Set The Color To Violet GL.Vertex3f( 1.0, 1.0,-1.0); # Top Right Of The Quad (Right) GL.Vertex3f( 1.0, 1.0, 1.0) # Top Left Of The Quad (Right) GL.Vertex3f( 1.0,-1.0, 1.0) # Bottom Left Of The Quad (Right) GL.Vertex3f( 1.0,-1.0,-1.0) # Bottom Right Of The Quad (Right) GL.End(); # done with the polygon
$rtri=$rtri+1.0 # Increase the rotation variable for # the Triangle $rquad=$rquad-1.1 # Decrease the rotation variable for # the Quad # we need to swap the buffer to display our drawing. GLUT.SwapBuffers(); }
# The function called whenever a key is pressed. keyPressed = Proc.new {|key, x, y|
# If escape is pressed, kill everything. if (key == 27) # shut down our window GLUT.DestroyWindow($window) # exit the program...normal termination. exit(0) end }
# Rotation angle for the triangle. $rtri=0.0
#Rotation angle for the quadrilateral. $rquad=0.0
#Initialize GLUT state - glut will take any command line arguments that pertain # to it or X Windows - look at its documentation at # http://reality.sgi.com/mjk/spec3/spec3.html GLUT.Init
#Select type of Display mode: # Double buffer # RGBA color # Alpha components supported # Depth buffer GLUT.InitDisplayMode(GLUT::RGBA|GLUT::DOUBLE|GLUT::ALPHA|GLUT::DEPTH)
# get a 640x480 window GLUT.InitWindowSize(640,480)
# the window starts at the upper left corner of the screen GLUT.InitWindowPosition(0,0)
# Open a window $window=GLUT.CreateWindow("Jeff Molofee's GL Code Tutorial ... NeHe '99")
# Register the function to do all our OpenGL drawing. GLUT.DisplayFunc(DrawGLScene)
# Go fullscreen. This is as soon as possible. GLUT.FullScreen()
# Even if there are no events, redraw our gl scene. GLUT.IdleFunc(DrawGLScene)
# Register the function called when our window is resized. GLUT.ReshapeFunc(ReSizeGLScene)
# Register the function called when the keyboard is pressed. GLUT.KeyboardFunc(keyPressed)
# Initialize our window. InitGL(640, 480)
# Start Event Processing Engine GLUT.MainLoop()
This post has been edited by tjcool: 14 Nov, 2008 - 03:05 AM
There are several ways to do this, as you should saw it. You can build your text in several ways (http://nehe.gamedev.net/lesson.asp?index=03) but as you said I don't know if there any example in ruby. If I have time this weekend I'll try to port that lessons of Nehe into ruby. If I get it I told you
Regards from Canary Islands
Manuel Padrón Martínez
QUOTE(tjcool @ 14 Nov, 2008 - 12:18 AM)
--OpenGL on Ruby-- HI im TJ. I want to add a string of text in a specified x and y coordinate to this sample code.either in 2D or 3D. Both is good for my learning and comparison. eventually im gona be wanting to scroll it at a specified rate. i dont know where to start. i searched sites but all i could find is code in other languages.
Thanks..
CODE
# This code was created by Jeff Molofee '99 # Conversion to Ruby by Manolo Padron Martinez (manolopm@cip.es)
require "opengl" require "glut"
# A general OpenGL initialization function. Sets all of the initial parameters
def InitGL(width, height) # We call this right after our OpenGL window # is created.
GL.ClearColor(0.0, 0.0, 0.0, 0.0) # This Will Clear The Background # Color To Black GL.ClearDepth(1.0) # Enables Clearing Of The Depth Buffer GL.DepthFunc(GL::LESS) # The Type Of Depth Test To Do GL.Enable(GL::DEPTH_TEST) # Enables Depth Testing GL.ShadeModel(GL::SMOOTH) # Enables Smooth Color Shading GL.MatrixMode(GL::PROJECTION) GL.LoadIdentity() # Reset The Projection Matrix GLU.Perspective(45.0,Float(width)/Float(height),0.1,100.0) # Calculate The Aspect Ratio # Of The Window GL.MatrixMode(GL::MODELVIEW) end
# The function called when our window is resized (which shouldn't happen, # because we're fullscreen) ReSizeGLScene = Proc.new {|width, height| if (height==0) # Prevent A Divide By Zero If The Window Is Too Small height=1 end GL.Viewport(0,0,width,height) # Reset The Current Viewport And # Perspective Transformation GL.MatrixMode(GL::PROJECTION) GL.LoadIdentity() GLU.Perspective(45.0,Float(width)/Float(height),0.1,100.0) GL.MatrixMode(GL::MODELVIEW) }
# The main drawing function. DrawGLScene = Proc.new { GL.Clear(GL::COLOR_BUFFER_BIT | GL::DEPTH_BUFFER_BIT) # Clear The Screen And # The Depth Buffer GL.LoadIdentity() # Reset The View GL.Translate(-1.5, 0.0, -6.0) # Move Left 1.5 Units And Into The # Screen 6.0 GL.Rotate($rtri,0.0,1.0,0.0) # Rotate the triangle on the Y Axis
# draw a triangle (in smooth coloring mode) GL.Begin(GL::POLYGON) # start drawing a polygon
# front face of pyramid GL.Color3f( 1.0, 0.0, 0.0) # Set The Color To Red GL.Vertex3f( 0.0, 1.0, 0.0) # Top GL.Color3f( 0.0, 1.0, 0.0) # Set The Color To Green GL.Vertex3f( 1.0,-1.0, 0.0) # Bottom Right GL.Color3f( 0.0, 0.0, 1.0) # Set The Color To Blue GL.Vertex3f(-1.0,-1.0, 0.0) # Bottom Left
# right face of pyramid GL.Color3f( 1.0, 0.0, 0.0) # Red GL.Vertex3f( 0.0, 1.0, 0.0) # Top of triangle (Right) GL.Color3f( 0.0, 0.0, 1.0) # Blue GL.Vertex3f( 1.0,-1.0, 1.0) # Left of triangle (Right) GL.Color3f( 1.0, 0.0, 0.0) # Green GL.Vertex3f( 1.0,-1.0,-1.0) # Right of triangle (Right)
# back face of pyramid GL.Color3f( 1.0, 0.0, 0.0) # Red GL.Vertex3f( 0.0, 1.0, 0.0) # Top of triangle (Back) GL.Color3f( 0.0, 1.0, 0.0) # Green GL.Vertex3f( 1.0,-1.0,-1.0) # Left of triangle (Back) GL.Color3f( 0.0, 0.0, 1.0) # Blue GL.Vertex3f(-1.0,-1.0,-1.0) # Right of triangle (Back
# left face of pyramid GL.Color3f( 1.0, 0.0, 0.0) # Red GL.Vertex3f( 0.0, 1.0, 0.0) # Top of triangle (Left) GL.Color3f( 0.0, 0.0, 1.0) # Blue GL.Vertex3f(-1.0,-1.0,-1.0) # Left of triangle (Left) GL.Color3f( 1.0, 0.0, 0.0) # Green GL.Vertex3f(-1.0,-1.0, 1.0) # Right of triangle (Left)
GL.End() # Done drawing the pyramid
GL.LoadIdentity() # make sure we're no longer rotated. GL.Translate(1.5,0.0,-7.0) # Move Right 3 Units, and back into # the screen 7.0 GL.Rotate($rquad,1.0,1.0,1.0) # Rotate the quad on the X Axis # draw a cube (6 quadrilateral) GL.Begin(GL::QUADS) # start drawing the cube
# top of cube GL.Color3f(0.0,1.0,0.0) # Set The Color To Blue GL.Vertex3f( 1.0, 1.0,-1.0) # Top Right Of The Quad (Top) GL.Vertex3f(-1.0, 1.0,-1.0) # Top Left Of The Quad (Top) GL.Vertex3f(-1.0, 1.0, 1.0) # Bottom Left Of The Quad (Top) GL.Vertex3f( 1.0, 1.0, 1.0) # Bottom Right Of The Quad (Top)
# bottom of cube GL.Color3f(1.0,0.5,0.0) # Set The Color To Orange GL.Vertex3f( 1.0,-1.0, 1.0) # Top Right Of The Quad (Bottom) GL.Vertex3f(-1.0,-1.0, 1.0) # Top Left Of The Quad (Bottom) GL.Vertex3f(-1.0,-1.0,-1.0) # Bottom Left Of The Quad (Bottom) GL.Vertex3f( 1.0,-1.0,-1.0) # Bottom Right Of The Quad (Bottom)
# front of cube GL.Color3f(1.0,0.0,0.0) # Set The Color To Red GL.Vertex3f( 1.0, 1.0, 1.0) # Top Right Of The Quad (Front) GL.Vertex3f(-1.0, 1.0, 1.0) # Top Left Of The Quad (Front) GL.Vertex3f(-1.0,-1.0, 1.0) # Bottom Left Of The Quad (Front) GL.Vertex3f( 1.0,-1.0, 1.0) # Bottom Right Of The Quad (Front)
# back of cube. GL.Color3f(1.0,1.0,0.0) # Set The Color To Yellow GL.Vertex3f( 1.0,-1.0,-1.0) # Top Right Of The Quad (Back) GL.Vertex3f(-1.0,-1.0,-1.0) # Top Left Of The Quad (Back) GL.Vertex3f(-1.0, 1.0,-1.0) # Bottom Left Of The Quad (Back) GL.Vertex3f( 1.0, 1.0,-1.0) # Bottom Right Of The Quad (Back)
# left of cube GL.Color3f(0.0,0.0,1.0) # Blue GL.Vertex3f(-1.0, 1.0, 1.0) # Top Right Of The Quad (Left) GL.Vertex3f(-1.0, 1.0,-1.0) # Top Left Of The Quad (Left) GL.Vertex3f(-1.0,-1.0,-1.0) # Bottom Left Of The Quad (Left) GL.Vertex3f(-1.0,-1.0, 1.0) # Bottom Right Of The Quad (Left)
# Right of cube GL.Color3f(1.0,0.0,1.0) # Set The Color To Violet GL.Vertex3f( 1.0, 1.0,-1.0); # Top Right Of The Quad (Right) GL.Vertex3f( 1.0, 1.0, 1.0) # Top Left Of The Quad (Right) GL.Vertex3f( 1.0,-1.0, 1.0) # Bottom Left Of The Quad (Right) GL.Vertex3f( 1.0,-1.0,-1.0) # Bottom Right Of The Quad (Right) GL.End(); # done with the polygon
$rtri=$rtri+1.0 # Increase the rotation variable for # the Triangle $rquad=$rquad-1.1 # Decrease the rotation variable for # the Quad # we need to swap the buffer to display our drawing. GLUT.SwapBuffers(); }
# The function called whenever a key is pressed. keyPressed = Proc.new {|key, x, y|
# If escape is pressed, kill everything. if (key == 27) # shut down our window GLUT.DestroyWindow($window) # exit the program...normal termination. exit(0) end }
# Rotation angle for the triangle. $rtri=0.0
#Rotation angle for the quadrilateral. $rquad=0.0
#Initialize GLUT state - glut will take any command line arguments that pertain # to it or X Windows - look at its documentation at # http://reality.sgi.com/mjk/spec3/spec3.html GLUT.Init
#Select type of Display mode: # Double buffer # RGBA color # Alpha components supported # Depth buffer GLUT.InitDisplayMode(GLUT::RGBA|GLUT::DOUBLE|GLUT::ALPHA|GLUT::DEPTH)
# get a 640x480 window GLUT.InitWindowSize(640,480)
# the window starts at the upper left corner of the screen GLUT.InitWindowPosition(0,0)
# Open a window $window=GLUT.CreateWindow("Jeff Molofee's GL Code Tutorial ... NeHe '99")
# Register the function to do all our OpenGL drawing. GLUT.DisplayFunc(DrawGLScene)
# Go fullscreen. This is as soon as possible. GLUT.FullScreen()
# Even if there are no events, redraw our gl scene. GLUT.IdleFunc(DrawGLScene)
# Register the function called when our window is resized. GLUT.ReshapeFunc(ReSizeGLScene)
# Register the function called when the keyboard is pressed. GLUT.KeyboardFunc(keyPressed)
Thank you so much, i have been to that site, and its where i learned that you could use opengl in ruby, but i saw that all the examples are in other languages and none in ruby. i dont have any background in opengl programming but i downloaded links that could help me.
another question, do you have some sort of quick reference about opengl and ruby?
There only a few translated into ruby (I did it) the example code that you are using is one of them. (It's seems that now are bad linked into nehe website but there are the 9 first nehe tutorials ported to ruby in my own server (http://rendevouz.homeunix.org/ruby/))
This is the main project about opengl and ruby (http://ruby-opengl.rubyforge.org/)
Regards from Canary Islands
Manuel Padrón Martínez
QUOTE(tjcool @ 14 Nov, 2008 - 03:39 AM)
Thank you so much, i have been to that site, and its where i learned that you could use opengl in ruby, but i saw that all the examples are in other languages and none in ruby. i dont have any background in opengl programming but i downloaded links that could help me.
another question, do you have some sort of quick reference about opengl and ruby?
I think i have a problem compiling, this is the error
>ruby lesson7.rb lesson7.rb:63:in `ImageLoad': undefined method `reverse' for nil:NilClass (NoMethodError) from lesson7.rb:83:in `LoadGLTextures' from lesson7.rb:140:in `InitGL' from lesson7.rb:385 >Exit code: 1
i think its a library problem. i am using SciTE as a compiler. im still getting the hang of libraries and compiling in ruby. do you know what this error is all about? what compiler do you recommend me using? im still a noob in ruby though i could understand the code. my major problem right now is a good compiler.
CODE
# This code was created by Jeff Molofee '99 # Conversion to Ruby by Manolo Padron Martinez (manolopm@cip.es)
require "opengl" require "glut"
#Define the struct image Image=Struct.new("Image", :sizeX, :sizeY, :data)
#----------------------------------------------------------- # quick and dirty bitmap loader... for 24 bit bitmaps with 1 plane only. # See http://www.dcs.ed.ac.uk/~mxr/gfx/2d/BMP.txt for more info.
def ImageLoad(filename, image) begin
file=File.open(filename,"r") # Seek through the bmp header, up to the width/height: file.seek(18,IO::SEEK_CUR)
# Read the width tmp=file.read(4).unpack('I').to_s.to_i image[:sizeX]=tmp if (tmp.nil?) $stderr.print "Error reading Width from "+ filename +"\n" return false end
# Read the height tmp=file.read(4).unpack('I').to_s.to_i image[:sizeY]=tmp if (tmp.nil?) $stderr.print "Error reading Height from "+ filename +"\n" return false end
# Calculate the size of the bmp assuming 3 bytes per pixel size=image[:sizeX]*image[:sizeY]*3
# Read the planes tmp=file.read(2).unpack('s').to_s.to_i if (tmp!=1) $stderr.print "Planes from "+ filename +" is not 1:"+ tmp.to_s() +"\n" return false end
# Read the bpp tmp=file.read(2).unpack('s').to_s.to_i if (tmp!=24) $stderr.print "Bpp from "+ filename +" is not 24: "+ tmp.to_s() + "\n" return false end
# Seek past the rest of the bitmap header. file.seek(24,IO::SEEK_CUR)
# Read the data. image[:data]=file.read(size)
# Reverse all components of the colors (bgr->rgb) i=0 while i+2<size image[:data][i..i+2]= image[:data][i..i+2].reverse i=i+3 end
# we're done true rescue SystemCallError $stderr.print "File Not Found: "+ filename + "\n" false ensure #Close the file if exists file.close unless file.nil? end
def LoadGLTextures # Load Texture image1=Image.new if !ImageLoad("crate.bmp",image1) exit(1) end
# Create Texture # The ruby-opengl don't have yet GenTextures like in the standard # The solution is 3 calls to GenTextures by the moment. GL.GenTextures($texture[0]) GL.GenTextures($texture[1]) GL.GenTextures($texture[2])
# Texture 1 (poor quality scaling) GL.BindTexture(GL::TEXTURE_2D,$texture[0]) # 2d texture (x and y size) # Cheap scale when image bigger than texture GL.TexParameteri(GL::TEXTURE_2D,GL::TEXTURE_MAG_FILTER,GL::NEAREST) # Cheap scale when image smalled than texture GL.TexParameteri(GL::TEXTURE_2D,GL::TEXTURE_MIN_FILTER,GL::NEAREST)
# 2D texture, level of detail 0(normal, 3 components(red,green,blue) # x size from image, y size from image, border 0 (normal), rgb format, # format of the data, and finally the data itself GL.TexImage2D(GL::TEXTURE_2D,0,3,image1[:sizeX],image1[:sizeY],0,GL::RGB,GL::UNSIGNED_BYTE,image1[:data])
# Texture 2 (linear scaling) GL.BindTexture(GL::TEXTURE_2D,$texture[1]) # 2d texture (x and y size) # Scale linearly when image bigger than texture GL.TexParameteri(GL::TEXTURE_2D,GL::TEXTURE_MAG_FILTER,GL::LINEAR) # Scale linearly when image smalled than texture GL.TexParameteri(GL::TEXTURE_2D,GL::TEXTURE_MIN_FILTER,GL::LINEAR)
# 2D texture, level of detail 0(normal, 3 components(red,green,blue) # x size from image, y size from image, border 0 (normal), rgb format, # format of the data, and finally the data itself GL.TexImage2D(GL::TEXTURE_2D,0,3,image1[:sizeX],image1[:sizeY],0,GL::RGB,GL::UNSIGNED_BYTE,image1[:data])
# Texture 3 (mipmapped scaling) GL.BindTexture(GL::TEXTURE_2D,$texture[2]) # 2d texture (x and y size) # Scale linearly when image bigger than texture GL.TexParameteri(GL::TEXTURE_2D,GL::TEXTURE_MAG_FILTER,GL::LINEAR) # Scale linearly + mipmap when image smalled than texture GL.TexParameteri(GL::TEXTURE_2D,GL::TEXTURE_MIN_FILTER,GL::LINEAR_MIPMAP_NEAREST)
# 2D texture, level of detail 0(normal, 3 components(red,green,blue) # x size from image, y size from image, border 0 (normal), rgb format, # format of the data, and finally the data itself GL.TexImage2D(GL::TEXTURE_2D,0,3,image1[:sizeX],image1[:sizeY],0,GL::RGB,GL::UNSIGNED_BYTE,image1[:data])
GLU.Build2DMipmaps(GL::TEXTURE_2D,3,image1[:sizeX],image1[:sizeY],GL::RGB,GL::UNSIGNED_BYTE, image1[:data]) end
def InitGL(width, height) # We call this right after our OpenGL window # is created.
LoadGLTextures() # Load the texture(s) GL.Enable(GL::TEXTURE_2D) # Enable texture mapping GL.ClearColor(0.0, 0.0, 0.0, 0.0) # This Will Clear The Background # Color To Black GL.ClearDepth(1.0) # Enables Clearing Of The Depth Buffer GL.DepthFunc(GL::LESS) # The Type Of Depth Test To Do GL.Enable(GL::DEPTH_TEST) # Enables Depth Testing GL.ShadeModel(GL::SMOOTH) # Enables Smooth Color Shading GL.MatrixMode(GL::PROJECTION) GL.LoadIdentity() # Reset The Projection Matrix GLU.Perspective(45.0,Float(width)/Float(height),0.1,100.0) # Calculate The # Aspect Ratio # Of The Window GL.MatrixMode(GL::MODELVIEW)
# Set up light number 0 lightAmbient=[0.5,0.5,0.5,1.0] lightDiffuse=[1.0,1.0,1.0,1.0] lightPosition=[0.0,0.0,2.0,1.0] GL.Lightfv(GL::LIGHT0, GL::AMBIENT, lightAmbient) GL.Lightfv(GL::LIGHT0, GL::DIFFUSE, lightDiffuse) GL.Lightfv(GL::LIGHT0, GL::POSITION, lightPosition) GL.Enable(GL::LIGHT0)
# The function called when our window is resized (which shouldn't happen, # because we're fullscreen) ReSizeGLScene = Proc.new {|width, height| if (height==0) # Prevent A Divide By Zero If The Window Is Too Small height=1 end GL.Viewport(0,0,width,height) # Reset The Current Viewport And # Perspective Transformation GL.MatrixMode(GL::PROJECTION) GL.LoadIdentity() GLU.Perspective(45.0,Float(width)/Float(height),0.1,100.0) GL.MatrixMode(GL::MODELVIEW) }
GL.Clear(GL::COLOR_BUFFER_BIT | GL::DEPTH_BUFFER_BIT) # Clear The Screen And # The Depth Buffer GL.LoadIdentity() # Reset The View GL.Translate( 0.0, 0.0,$z) # Move Z units into the screen. GL.Rotate($xrot,1.0,0.0,0.0) # Rotate on the X Axis GL.Rotate($yrot,0.0,1.0,0.0) # Rotate on the Y Axis
GL.BindTexture(GL::TEXTURE_2D, $texture[$filter]) # Chose the texture to use.
GL.Begin(GL::QUADS) # Begin drawing a cube
# Front Face (note that the texture's corners have to match the quad's # corners) GL.TexCoord2f(0.0, 0.0) GL.Vertex3f(-1.0, -1.0, 1.0) # Bottom Left Of The Texture and Quad GL.TexCoord2f(1.0, 0.0) GL.Vertex3f( 1.0, -1.0, 1.0) # Bottom Right Of The Texture and Quad GL.TexCoord2f(1.0, 1.0) GL.Vertex3f( 1.0, 1.0, 1.0) # Top Right Of The Texture and Quad GL.TexCoord2f(0.0, 1.0) GL.Vertex3f(-1.0, 1.0, 1.0) # Top Left Of The Texture and Quad
# Back Face GL.TexCoord2f(1.0, 0.0) GL.Vertex3f(-1.0, -1.0, -1.0) # Bottom Right Of The Texture and Quad GL.TexCoord2f(1.0, 1.0) GL.Vertex3f(-1.0, 1.0, -1.0) # Top Right Of The Texture and Quad GL.TexCoord2f(0.0, 1.0) GL.Vertex3f( 1.0, 1.0, -1.0) # Top Left Of The Texture and Quad GL.TexCoord2f(0.0, 0.0) GL.Vertex3f( 1.0, -1.0, -1.0) # Bottom Left Of The Texture and Quad
# Top Face GL.TexCoord2f(0.0, 1.0) GL.Vertex3f(-1.0, 1.0, -1.0) # Top Left Of The Texture and Quad GL.TexCoord2f(0.0, 0.0) GL.Vertex3f(-1.0, 1.0, 1.0) # Bottom Left Of The Texture and Quad GL.TexCoord2f(1.0, 0.0) GL.Vertex3f( 1.0, 1.0, 1.0) # Bottom Right Of The Texture and Quad GL.TexCoord2f(1.0, 1.0) GL.Vertex3f( 1.0, 1.0, -1.0) # Top Right Of The Texture and Quad
# Bottom Face GL.TexCoord2f(1.0, 1.0) GL.Vertex3f(-1.0, -1.0, -1.0) # Top Right Of The Texture and Quad GL.TexCoord2f(0.0, 1.0) GL.Vertex3f( 1.0, -1.0, -1.0) # Top Left Of The Texture and Quad GL.TexCoord2f(0.0, 0.0) GL.Vertex3f( 1.0, -1.0, 1.0) # Bottom Left Of The Texture and Quad GL.TexCoord2f(1.0, 0.0) GL.Vertex3f(-1.0, -1.0, 1.0) # Bottom Right Of The Texture and Quad
# Right face GL.TexCoord2f(1.0, 0.0) GL.Vertex3f( 1.0, -1.0, -1.0) # Bottom Right Of The Texture and Quad GL.TexCoord2f(1.0, 1.0) GL.Vertex3f( 1.0, 1.0, -1.0) # Top Right Of The Texture and Quad GL.TexCoord2f(0.0, 1.0) GL.Vertex3f( 1.0, 1.0, 1.0) # Top Left Of The Texture and Quad GL.TexCoord2f(0.0, 0.0) GL.Vertex3f( 1.0, -1.0, 1.0) # Bottom Left Of The Texture and Quad
# Left Face GL.TexCoord2f(0.0, 0.0) GL.Vertex3f(-1.0, -1.0, -1.0) # Bottom Left Of The Texture and Quad GL.TexCoord2f(1.0, 0.0) GL.Vertex3f(-1.0, -1.0, 1.0) # Bottom Right Of The Texture and Quad GL.TexCoord2f(1.0, 1.0) GL.Vertex3f(-1.0, 1.0, 1.0) # Top Right Of The Texture and Quad GL.TexCoord2f(0.0, 1.0) GL.Vertex3f(-1.0, 1.0, -1.0) # Top Left Of The Texture and Quad
# The function called whenever a key is pressed. keyPressed = Proc.new {|key, x, y|
case key when 27 # If escape is pressed, kill everything. GLUT.DestroyWindow($window) # shut down our window # exit the program...normal termination. exit(0) when 76,108 #If L/l is pressed switch the lighting puts "L/l pressed; Is the light on? "+$light.to_s+"! \n" $light=!$light puts "And now, Is the light on? "+$light.to_s+"! \n" if $light GL.Enable(GL::LIGHTING) else GL.Disable(GL::LIGHTING) end when 70,102 #If F/f is pressed switch the filter puts "F/f pressed; Is the filter on? "+$filter.to_s+"! \n" $filter=($filter+1).divmod(3)[1] puts "And now, Is the filter on? "+$filter.to_s+"! \n" end }
# The function called whenever a special key is pressed specialKeyPressed = Proc.new {|key,x,y| case key when GLUT::KEY_PAGE_UP # Move the cube into the distance $z=$z-0.02 when GLUT::KEY_PAGE_DOWN # Move the cube closer $z=$z+0.02 when GLUT::KEY_UP # Decrease x rotation speed $xspeed=$xspeed-0.01 when GLUT::KEY_DOWN # Increase x rotation speed $xspeed=$xspeed+0.01 when GLUT::KEY_LEFT # Decrease y rotation speed $yspeed=$yspeed-0.01 when GLUT::KEY_RIGHT # Increase y rotation speed $yspeed=$yspeed+0.01 end }
#Initialize GLUT state - glut will take any command line arguments that pertain # to it or X Windows - look at its documentation at # http://reality.sgi.com/mjk/spec3/spec3.html GLUT.Init
#Select type of Display mode: # Double buffer # RGBA color # Alpha components supported # Depth buffer GLUT.InitDisplayMode(GLUT::RGBA|GLUT::DOUBLE|GLUT::ALPHA|GLUT::DEPTH)
# get a 640x480 window GLUT.InitWindowSize(640,480)
# the window starts at the upper left corner of the screen GLUT.InitWindowPosition(0,0)
# Open a window $window=GLUT.CreateWindow("Jeff Molofee's GL Code Tutorial ... NeHe '99")
# Register the function to do all our OpenGL drawing. GLUT.DisplayFunc(DrawGLScene)
# Go fullscreen. This is as soon as possible. GLUT.FullScreen()
# Even if there are no events, redraw our gl scene. GLUT.IdleFunc(DrawGLScene)
# Register the function called when our window is resized. GLUT.ReshapeFunc(ReSizeGLScene)
# Register the function called when the keyboard is pressed. GLUT.KeyboardFunc(keyPressed)
# Register the function called when special keys # (arrows, pagedown, etc) are pressed. GLUT.SpecialFunc(specialKeyPressed)
# Initialize our window. InitGL(640, 480)
# Start Event Processing Engine GLUT.MainLoop()
thanks for your replies
Regards from Philippines
This post has been edited by tjcool: 15 Nov, 2008 - 06:10 AM
I checked the file crate.bmp. i put it in the same folder as of lesson7.rb. that is why i took off the extension name in your example. the file is also accessible. did i do the right thing?