Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

cleanup and updating readme

  • Loading branch information...
commit d8fdc3a312155fe4b363f4561c669388b1390475 1 parent 556b8f6
Nickolay Schwarz authored
3  README.markdown
Source Rendered
@@ -23,4 +23,5 @@
23 23 * lesson06 - texture mapping
24 24 * lesson07 - texture filters, lighting and keyboard control
25 25 * lesson08 - texture blending, transparency
26   - * lesson09 - moving bitmaps in 3D space
  26 + * lesson09 - moving bitmaps in 3D space
  27 + * lesson10 - loading and moving through 3D World
303 using_nehe_lessons/bla.rb
... ... @@ -0,0 +1,303 @@
  1 +# Nehe Lesson 36 Code
  2 +# modified from immediate mode to use vertex arrays for helix drawing
  3 +require 'opengl'
  4 +include Gl,Glu,Glut
  5 +include Math
  6 +
  7 +def emptyTexture
  8 + # Create Storage Space For Texture Data (128x128x4)
  9 + data = ([0]*4*128*128).pack("f*")
  10 + txtnumber = glGenTextures(1) # Create 1 Texture
  11 + glBindTexture(GL_TEXTURE_2D, txtnumber[0]) # Bind The Texture
  12 + glTexImage2D(GL_TEXTURE_2D, 0, 4, 128, 128, 0,
  13 + GL_RGBA, GL_FLOAT, data) # Build Texture Using Information In data
  14 + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR)
  15 + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR)
  16 + return txtnumber[0] # Return The Texture ID
  17 +end
  18 +
  19 +def init
  20 + global_ambient = [0.2, 0.2, 0.2, 1.0] # Set Ambient Lighting To Fairly Dark Light (No Color)
  21 + light0pos = [0.0, 5.0, 10.0, 1.0] # Set The Light Position
  22 + light0ambient = [0.2, 0.2, 0.2, 1.0] # More Ambient Light
  23 + light0diffuse = [0.3, 0.3, 0.3, 1.0] # Set The Diffuse Light A Bit Brighter
  24 + light0specular = [0.8, 0.8, 0.8, 1.0] # Fairly Bright Specular Lighting
  25 +
  26 + lmodel_ambient = [0.2,0.2,0.2,1.0] # And More Ambient Light
  27 +
  28 + $angle = 0.0 # Set Starting Angle To Zero
  29 +
  30 + $lasttime = 0
  31 +
  32 + $blurTexture = emptyTexture() # Create Our Empty Texture
  33 +
  34 + $helix_v,$helix_n = createHelix()
  35 + glVertexPointer(3,GL_FLOAT,0,$helix_v.flatten.pack("f*"))
  36 + glNormalPointer(GL_FLOAT,0,$helix_n.flatten.pack("f*"))
  37 +
  38 + glLoadIdentity() # Reset The Modelview Matrix
  39 +
  40 + glEnable(GL_DEPTH_TEST) # Enable Depth Testing
  41 +
  42 + glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lmodel_ambient) # Set The Ambient Light Model
  43 +
  44 + glLightModelfv(GL_LIGHT_MODEL_AMBIENT,global_ambient) # Set The Global Ambient Light Model
  45 + glLightfv(GL_LIGHT0, GL_POSITION, light0pos) # Set The Lights Position
  46 + glLightfv(GL_LIGHT0, GL_AMBIENT, light0ambient) # Set The Ambient Light
  47 + glLightfv(GL_LIGHT0, GL_DIFFUSE, light0diffuse) # Set The Diffuse Light
  48 + glLightfv(GL_LIGHT0, GL_SPECULAR, light0specular) # Set Up Specular Lighting
  49 + glEnable(GL_LIGHTING) # Enable Lighting
  50 + glEnable(GL_LIGHT0) # Enable Light0
  51 +
  52 + glShadeModel(GL_SMOOTH) # Select Smooth Shading
  53 +
  54 + glMateriali(GL_FRONT, GL_SHININESS, 128)
  55 + glClearColor(0.0, 0.0, 0.0, 0.5) # Set The Clear Color To Black
  56 +end
  57 +
  58 +# Keyboard handler to exit when ESC is typed
  59 +keyboard = lambda do |key, x, y|
  60 + case(key)
  61 + when ?\e
  62 + exit(0)
  63 + end
  64 + glutPostRedisplay
  65 +end
  66 +
  67 +reshape = lambda do |w,h|
  68 + glMatrixMode(GL_PROJECTION)
  69 + glViewport(0,0,w,h)
  70 + glLoadIdentity()
  71 + width = 0.5
  72 + height = 0.5 * h/w;
  73 + glFrustum(-width,width,-height,height,1.0,2000.0)
  74 + glMatrixMode(GL_MODELVIEW)
  75 + glViewport(0,0,w,h)
  76 +end
  77 +
  78 +def viewOrtho
  79 + glMatrixMode(GL_PROJECTION) # Select Projection
  80 + glPushMatrix() # Push The Matrix
  81 + glLoadIdentity() # Reset The Matrix
  82 + width = glutGet(GLUT_WINDOW_WIDTH)
  83 + height = glutGet(GLUT_WINDOW_HEIGHT)
  84 + glOrtho( 0, width , height , 0, -1, 1 ) # Select Ortho Mode (widthxheight)
  85 + glMatrixMode(GL_MODELVIEW) # Select Modelview Matrix
  86 + glPushMatrix() # Push The Matrix
  87 + glLoadIdentity() # Reset The Matrix
  88 +end
  89 +
  90 +def viewPerspective # Set Up A Perspective View
  91 + glMatrixMode( GL_PROJECTION ) # Select Projection
  92 + glPopMatrix() # Pop The Matrix
  93 + glMatrixMode( GL_MODELVIEW ) # Select Modelview
  94 + glPopMatrix() # Pop The Matrix
  95 +end
  96 +
  97 +def normalize(v)
  98 + len = sqrt( v[0]*v[0] + v[1]*v[1] + v[2]*v[2])
  99 + return v if len==0
  100 + [ v[0] / len, v[1] / len, v[2] / len ]
  101 +end
  102 +
  103 +def calcNormal(v) # Calculates Normal For A Quad Using 3 Points
  104 + # Finds The Vector Between 2 Points By Subtracting
  105 + # The x,y,z Coordinates From One Point To Another.
  106 + # Calculate The Vector From Point 1 To Point 0
  107 + v1, v2, out = [], [], []
  108 + x,y,z = 0,1,2
  109 +
  110 + v1[x] = v[0][x] - v[1][x] # Vector 1.x=Vertex[0].x-Vertex[1].x
  111 + v1[y] = v[0][y] - v[1][y] # Vector 1.y=Vertex[0].y-Vertex[1].y
  112 + v1[z] = v[0][z] - v[1][z] # Vector 1.z=Vertex[0].y-Vertex[1].z
  113 + # Calculate The Vector From Point 2 To Point 1
  114 + v2[x] = v[1][x] - v[2][x] # Vector 2.x=Vertex[0].x-Vertex[1].x
  115 + v2[y] = v[1][y] - v[2][y] # Vector 2.y=Vertex[0].y-Vertex[1].y
  116 + v2[z] = v[1][z] - v[2][z] # Vector 2.z=Vertex[0].z-Vertex[1].z
  117 + # Compute The Cross Product To Give Us A Surface Normal
  118 + out[x] = v1[y]*v2[z] - v1[z]*v2[y] # Cross Product For Y - Z
  119 + out[y] = v1[z]*v2[x] - v1[x]*v2[z] # Cross Product For X - Z
  120 + out[z] = v1[x]*v2[y] - v1[y]*v2[x] # Cross Product For X - Y
  121 +
  122 + normalize(out)
  123 +end
  124 +
  125 +def createHelix() # creates helix VA
  126 + twists = 5
  127 + r = 1.5
  128 +
  129 + helix_v = []
  130 + helix_n = []
  131 +
  132 + 0.step(360,20) do |phi| # 360 Degrees In Steps Of 20
  133 + 0.step(360*twists,20) do |theta| # 360 Degrees * Number Of Twists In Steps Of 20
  134 + v= phi/180.0*PI # Calculate Angle Of First Point ( 0 )
  135 + u= theta/180.0*PI # Calculate Angle Of First Point ( 0 )
  136 +
  137 + x= cos(u)*(2.0+cos(v))*r # Calculate x Position (1st Point)
  138 + y= sin(u)*(2.0+cos(v))*r # Calculate y Position (1st Point)
  139 + z=((u-(2.0*PI)) + sin(v))*r # Calculate z Position (1st Point)
  140 +
  141 + v0 = [x,y,z]
  142 +
  143 + v= phi/180.0*PI # Calculate Angle Of Second Point ( 0 )
  144 + u= (theta+20)/180.0*PI # Calculate Angle Of Second Point ( 20 )
  145 +
  146 + x= cos(u)*(2.0+cos(v))*r # Calculate x Position (2nd Point)
  147 + y= sin(u)*(2.0+cos(v))*r # Calculate y Position (2nd Point)
  148 + z= ((u-(2.0*PI)) + sin(v))*r # Calculate z Position (2nd Point)
  149 +
  150 + v1 = [x,y,z]
  151 +
  152 + v= (phi+20)/180.0*PI # Calculate Angle Of Third Point ( 20 )
  153 + u= (theta+20)/180.0*PI # Calculate Angle Of Third Point ( 20 )
  154 +
  155 + x= cos(u)*(2.0+cos(v))*r # Calculate x Position (3rd Point)
  156 + y= sin(u)*(2.0+cos(v))*r # Calculate y Position (3rd Point)
  157 + z= ((u-(2.0*PI)) + sin(v))*r # Calculate z Position (3rd Point)
  158 +
  159 + v2 = [x,y,z]
  160 +
  161 + v= (phi+20)/180.0*PI # Calculate Angle Of Fourth Point ( 20 )
  162 + u= (theta)/180.0*PI # Calculate Angle Of Fourth Point ( 0 )
  163 +
  164 + x= cos(u)*(2.0+cos(v))*r # Calculate x Position (4th Point)
  165 + y= sin(u)*(2.0+cos(v))*r # Calculate y Position (4th Point)
  166 + z= ((u-(2.0*PI)) + sin(v))*r # Calculate z Position (4th Point)
  167 +
  168 + v3 = [x,y,z]
  169 +
  170 + normal = calcNormal([v0,v1,v2,v3]) # Calculate The Quad Normal
  171 + helix_v << v0 << v1 << v2 << v3
  172 + helix_n << normal << normal << normal << normal
  173 + end
  174 + end
  175 + [helix_v,helix_n]
  176 +end
  177 +
  178 +def processHelix() # Draws A Helix
  179 + glfMaterialColor = [0.4,0.2,0.8,1.0] # Set The Material Color
  180 + specular = [1.0,1.0,1.0,1.0] # Sets Up Specular Lighting
  181 +
  182 + glLoadIdentity() # Reset The Modelview Matrix
  183 + gluLookAt(0, 5, 50, 0, 0, 0, 0, 1, 0) # Eye Position (0,5,50) Center Of Scene (0,0,0), Up On Y Axis
  184 +
  185 + glPushMatrix() # Push The Modelview Matrix
  186 +
  187 + glTranslatef(0,0,-50) # Translate 50 Units Into The Screen
  188 + glRotatef($angle/2.0,1,0,0) # Rotate By angle/2 On The X-Axis
  189 + glRotatef($angle/3.0,0,1,0) # Rotate By angle/3 On The Y-Axis
  190 +
  191 + glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,glfMaterialColor)
  192 + glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,specular)
  193 +
  194 + glEnableClientState(GL_VERTEX_ARRAY)
  195 + glEnableClientState(GL_NORMAL_ARRAY)
  196 + glDrawArrays(GL_QUADS,0,$helix_v.size)
  197 + glDisableClientState(GL_VERTEX_ARRAY)
  198 + glDisableClientState(GL_NORMAL_ARRAY)
  199 +
  200 + glPopMatrix() # Pop The Matrix
  201 +end
  202 +
  203 +def drawBlur(times,inc)
  204 + spost = 0.0 # Starting Texture Coordinate Offset
  205 + alphainc = 0.9 / times # Fade Speed For Alpha Blending
  206 + alpha = 0.2 # Starting Alpha Value
  207 +
  208 + width = glutGet(GLUT_WINDOW_WIDTH)
  209 + height = glutGet(GLUT_WINDOW_HEIGHT)
  210 + # Disable AutoTexture Coordinates
  211 + glDisable(GL_TEXTURE_GEN_S)
  212 + glDisable(GL_TEXTURE_GEN_T)
  213 +
  214 + glEnable(GL_TEXTURE_2D) # Enable 2D Texture Mapping
  215 + glDisable(GL_DEPTH_TEST) # Disable Depth Testing
  216 + glBlendFunc(GL_SRC_ALPHA,GL_ONE) # Set Blending Mode
  217 + glEnable(GL_BLEND) # Enable Blending
  218 + glBindTexture(GL_TEXTURE_2D,$blurTexture) # Bind To The Blur Texture
  219 + viewOrtho() # Switch To An Ortho View
  220 +
  221 + alphainc = alpha / times # alphainc=0.2 / Times To Render Blur
  222 +
  223 + glBegin(GL_QUADS) # Begin Drawing Quads
  224 + 0.upto(times-1) do |num| # Number Of Times To Render Blur
  225 + glColor4f(1.0, 1.0, 1.0, alpha) # Set The Alpha Value (Starts At 0.2)
  226 + glTexCoord2f(0+spost,1-spost) # Texture Coordinate ( 0, 1 )
  227 + glVertex2f(0,0) # First Vertex ( 0, 0 )
  228 +
  229 + glTexCoord2f(0+spost,0+spost) # Texture Coordinate ( 0, 0 )
  230 + glVertex2f(0,height) # Second Vertex ( 0, height )
  231 +
  232 + glTexCoord2f(1-spost,0+spost) # Texture Coordinate ( 1, 0 )
  233 + glVertex2f(width,height) # Third Vertex ( width, height )
  234 +
  235 + glTexCoord2f(1-spost,1-spost) # Texture Coordinate ( 1, 1 )
  236 + glVertex2f(width,0) # Fourth Vertex ( width, 0 )
  237 +
  238 + spost += inc # Gradually Increase spost (Zooming Closer To Texture Center)
  239 + alpha = alpha - alphainc # Gradually Decrease alpha (Gradually Fading Image Out)
  240 + end
  241 + glEnd() # Done Drawing Quads
  242 +
  243 + viewPerspective() # Switch To A Perspective View
  244 +
  245 + glEnable(GL_DEPTH_TEST) # Enable Depth Testing
  246 + glDisable(GL_TEXTURE_2D) # Disable 2D Texture Mapping
  247 + glDisable(GL_BLEND) # Disable Blending
  248 + glBindTexture(GL_TEXTURE_2D,0) # Unbind The Blur Texture
  249 +end
  250 +
  251 +
  252 +def renderToTexture
  253 + glViewport(0,0,128,128); # Set Our Viewport (Match Texture Size)
  254 +
  255 + processHelix() # Render The Helix
  256 +
  257 + glBindTexture(GL_TEXTURE_2D,$blurTexture) # Bind To The Blur Texture
  258 +
  259 + # Copy Our ViewPort To The Blur Texture (From 0,0 To 128,128... No Border)
  260 + glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 0, 0, 128, 128, 0)
  261 +
  262 + glClearColor(0.0, 0.0, 0.5, 0.5) # Set The Clear Color To Medium Blue
  263 + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Clear The Screen And Depth Buffer
  264 + width = glutGet(GLUT_WINDOW_WIDTH)
  265 + height = glutGet(GLUT_WINDOW_HEIGHT)
  266 + glViewport(0 , 0,width,height) # Set Viewport (0,0 to widthxheight)
  267 +end
  268 +
  269 +drawGLScene = lambda do # Draw The Scene
  270 + glClearColor(0.0, 0.0, 0.0, 0.5) # Set The Clear Color To Black
  271 + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # Clear Screen And Depth Buffer
  272 + glLoadIdentity() # Reset The View
  273 + renderToTexture() # Render To A Texture
  274 + processHelix() # Draw Our Helix
  275 + drawBlur(25,0.02) # Draw The Blur Effect
  276 + glFlush() # Flush The GL Rendering Pipeline
  277 + glutSwapBuffers()
  278 + sleep(0.001) # don't hog all cpu time
  279 +end
  280 +
  281 +idle = lambda do
  282 + now = glutGet(GLUT_ELAPSED_TIME)
  283 + elapsed = now - $lasttime
  284 + $angle += (elapsed * 0.03) # Update angle Based On The Clock
  285 + $lasttime = now
  286 +
  287 + glutPostRedisplay()
  288 +end
  289 +
  290 +# Main
  291 +glutInit()
  292 +glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
  293 +glutInitWindowPosition(100,100)
  294 +glutInitWindowSize(640,480)
  295 +glutCreateWindow("NeHe's Lesson 36")
  296 +glutDisplayFunc(drawGLScene)
  297 +glutIdleFunc(idle)
  298 +glutReshapeFunc(reshape)
  299 +glutKeyboardFunc(keyboard)
  300 +
  301 +init()
  302 +
  303 +glutMainLoop()
2  using_nehe_lessons/lesson10.rb
@@ -3,7 +3,6 @@
3 3 require 'gl'
4 4 require 'glu'
5 5 require 'glut'
6   -require 'ruby-debug'
7 6 include Gl
8 7 include Glu
9 8
@@ -44,6 +43,7 @@ def initialize
44 43 init_lights
45 44 init_textures
46 45 end
  46 +
47 47 def setup_world
48 48 triangle_data = JSON.parse(File.read('resources/nehe/lesson10/world.json'))
49 49 @triangles = triangle_data.inject([]) do |acc, vertexes|

0 comments on commit d8fdc3a

Please sign in to comment.
Something went wrong with that request. Please try again.