Author Topic: NeHe OpenGL Tutorials  (Read 683 times)

Marcus

  • Administrator
  • Hero Member
  • *****
  • Posts: 541
    • View Profile
NeHe OpenGL Tutorials
« on: April 02, 2015 »
Here's the naalaa code for the "3D Shapes" tutorial on NeHe. A link for the tutorial is included in the code. I wrote this one after work yesterday, and I promise to port many more of the tutorials to naalaa this weekend.

Code: [Select]
rem ==================================================================
rem 3D Shapes
rem
rem Visit the webside for full explanation of the code:
rem    http://nehe.gamedev.net/tutorial/3d_shapes/10035/
rem
rem ==================================================================

rem Import libraries.
import "OpenGL.lib"
import "gl_h.lib"
import "Speed.lib"

rem Open gl window. Last parameter to glInit should be 'true' if you
rem wish to hide the default naalaa window. The naalaa window can be
rem used for debug output.
if not glInit("3D Shapes", 64, 64, 800, 600, false) then end

rem Background color, set when calling glClear.
_glClearColor 0.0, 0.0, 0.0, 1.0

rem Triangle angle.
rtri#
rem Cube angle.
rquad#

do
rem Call this once per frame, else window messages (keyboard, mouse
rem et.c. won't be forwarded to naalaa).
_glUpdate

rem Increase the rotation variable for the triangle.
rtri = rtri + 0.2
rem Decrease the rotation variable for the quad.
rquad = rquad - 0.15

rem Clear the screen and the depth buffer.
_glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT

rem Reset the view.
_glLoadIdentity
rem Move left and into the screen.
_glTranslatef -1.5, 0.0, -6.0
rem Rotate the pyramid on its y axis.
_glRotatef rtri, 0.0, 1.0, 0.0

rem Start drawing the pyramid.
_glBegin GL_TRIANGLES

rem Red.
_glColor3f 1.0, 0.0, 0.0
rem Top of triangle (front).
_glVertex3f 0.0, 1.0, 0.0
rem Green
_glColor3f 0.0, 1.0, 0.0
rem Left of triangle (front).
_glVertex3f -1.0, -1.0, 1.0
rem Blue.
_glColor3f 0.0, 0.0, 1.0
rem Right of triangle (front).
_glVertex3f 1.0, -1.0, 1.0

rem Red.
_glColor3f 1.0, 0.0, 0.0
rem Top of triangle (right).
_glVertex3f 0.0, 1.0, 0.0
rem Blue.
_glColor3f 0.0, 0.0, 1.0
rem Left of triangle (right).
_glVertex3f 1.0, -1.0, 1.0
rem Green.
_glColor3f 0.0, 1.0, 0.0
rem Right of triangle (right).
_glVertex3f 1.0,-1.0, -1.0

rem Red.
_glColor3f 1.0, 0.0, 0.0
rem Top of triangle (back).
_glVertex3f 0.0, 1.0, 0.0
rem Green.
_glColor3f 0.0, 1.0, 0.0
rem Left of triangle (back).
_glVertex3f 1.0, -1.0, -1.0
rem Blue.
_glColor3f 0.0, 0.0, 1.0
rem Right of triangle (back).
_glVertex3f -1.0, -1.0, -1.0

rem Red.
_glColor3f 1.0, 0.0, 0.0
rem Top of triangle (left).
_glVertex3f 0.0, 1.0, 0.0
rem Blue.
_glColor3f 0.0, 0.0, 1.0
rem Left of triangle (left).
_glVertex3f -1.0, -1.0, -1.0
rem Green.
_glColor3f 0.0, 1.0, 0.0
rem Right of triangle (left).
_glVertex3f -1.0, -1.0, 1.0

_glEnd

_glLoadIdentity
rem Move right and into the screen.
_glTranslatef 1.5, 0.0, -7.0
rem Rotate the cube on x, y and z.
_glRotatef rquad, 1.0, 1.0, 1.0

rem Start drawing the cube.
_glBegin GL_QUADS

rem Set the color to green.
_glColor3f 0.0, 1.0, 0.0
rem Top right of the quad (top).
_glVertex3f 1.0, 1.0, -1.0
rem Top left of the quad (top).
_glVertex3f -1.0, 1.0, -1.0
rem Bottom left of the quad (top).
_glVertex3f -1.0, 1.0, 1.0
rem Bottom right of the quad (top).
_glVertex3f 1.0, 1.0, 1.0

rem Set the color to orange.
_glColor3f 1.0, 0.5, 0.0
rem Top right of the quad (bottom).
_glVertex3f 1.0, -1.0, 1.0
rem Top left of the quad (bottom).
_glVertex3f -1.0, -1.0, 1.0
rem Bottom left of the quad (bottom).
_glVertex3f -1.0, -1.0, -1.0
rem Bottom right of the quad (bottom).
_glVertex3f 1.0, -1.0, -1.0

rem Set the color to red.
_glColor3f 1.0, 0.0, 0.0
rem Top right of the quad (front).
_glVertex3f 1.0, 1.0, 1.0
rem Top left of the quad (front).
_glVertex3f -1.0, 1.0, 1.0
rem Bottom left of the quad (front).
_glVertex3f -1.0, -1.0, 1.0
rem Bottom right of the quad (front).
_glVertex3f 1.0, -1.0, 1.0

rem Set the color to yellow.
_glColor3f 1.0, 1.0, 0.0
rem Bottom left of the quad (back).
_glVertex3f 1.0, -1.0, -1.0
rem Bottom right of the quad (back).
_glVertex3f -1.0, -1.0, -1.0
rem Top right of the quad (back).
_glVertex3f -1.0, 1.0, -1.0
rem Top left of the quad (back).
_glVertex3f 1.0, 1.0, -1.0

rem Set the color to blue.
_glColor3f 0.0, 0.0, 1.0
rem top right of the quad (left).
_glVertex3f -1.0, 1.0, 1.0
rem Top left of the quad (left).
_glVertex3f -1.0, 1.0, -1.0
rem Bottom left of the quad (left).
_glVertex3f -1.0, -1.0, -1.0
rem Bottom right of the quad (left).
_glVertex3f -1.0, -1.0, 1.0

rem Set the color to violet.
_glColor3f 1.0, 0.0, 1.0
rem Top right of the quad (right).
_glVertex3f 1.0, 1.0, -1.0
rem Top left of the quad (right).
_glVertex3f 1.0, 1.0, 1.0
rem Bottom left of the quad (right).
_glVertex3f 1.0, -1.0, 1.0
rem Bottom right of the quad (right).
_glVertex3f 1.0, -1.0, -1.0

_glEnd


rem Flush graphics to window.
_glRender
rem Run at a maximum of 60 fps.
_SPD_HoldFrame 60
until keydown(27, true)
« Last Edit: April 02, 2015 by Marcus »
.\\\opz

Peter

  • Guest
Re: NeHe OpenGL Tutorials
« Reply #1 on: April 02, 2015 »
good, Marcus!

I will take some tests.  :D

Rick3137

  • Sr. Member
  • ****
  • Posts: 341
  • May the Force be with You
    • View Profile
    • Rick's Programs
Re: NeHe OpenGL Tutorials
« Reply #2 on: April 02, 2015 »
 Awesome!!  :-* :-X

johnno56

  • Guest
Re: NeHe OpenGL Tutorials
« Reply #3 on: April 02, 2015 »
Strong is he, with the force, yes!

Peter

  • Guest
Re: NeHe OpenGL Tutorials
« Reply #4 on: April 02, 2015 »
Hello,
first opengl test.
Code: [Select]
import "OpenGL.lib"
import "gl_h.lib"
import "Speed.lib"

if not glInit("3D First Points", 64, 64, 640, 480, true) then end
_glClearColor 0.0, 0.0, 0.0, 1.0
_glPointSize 10.0
_glEnable GL_POINT_SMOOTH

a#

while keydown(27)=0
_glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT
_glLoadIdentity
_glTranslatef 0.0, 0.0, -3.0
_glRotatef a,1.0, 1.0, 0.0

_glBegin GL_POINTS
   _colour 255,0,255,255
   _vec3 sin(a), cos(a), 0.0
   _colour 0,255,255,255
   _vec3 sin(a), 0.0, cos(a)
_glEnd

a = a+1.0
if a >360.0 then a =-a

_glRender
_SPD_HoldFrame 60
_glUpdate
wend

procedure vec3(x#,y#,z#)
    _glVertex3f x,y,z
endproc

procedure vec2(x#,y#)
    _glVertex2f x,y
endproc

procedure colour(r,g,b,a)
    _glColor4ub r,g,b,a
endproc




Peter

  • Guest
Re: NeHe OpenGL Tutorials
« Reply #5 on: April 03, 2015 »
Hi Marcus,
It seems something is wrong with your opengl library!
Code: [Select]
import "OpenGL.lib"
import "gl_h.lib"
import "Speed.lib"
if not glInit("3D First Points", 64, 64, 640, 480, true) then end

visible:
angle#
legAngle#[2]
armAngle#[2] 
leg1 = 1
leg2 = 0
arm1 = 1
arm2 = 0

_glEnable GL_DEPTH_TEST
_glClearColor 0.0, 0.0, 0.0, 0.0

while keydown(27)=0
_glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT
_glLoadIdentity

angle = angle + 1.0
if angle >= 360.0 then angle = 0.0

_glPushMatrix
_glLoadIdentity
_glTranslatef 0.0, 0.0, -30.0
_glRotatef angle, 0.0, 1.0, 0.0
_DrawRobot 0.0, 0.0, 0.0
_glPopMatrix
_glRender
_SPD_HoldFrame 60
_glUpdate
wend

procedure DrawCube(xPos#, yPos#, zPos#)
    _glPushMatrix
    _glBegin GL_POLYGON
    _glVertex3f  0.0, 0.0, 0.0
    _glVertex3f  0.0, 0.0,-1.0
    _glVertex3f -1.0, 0.0,-1.0
    _glVertex3f -1.0, 0.0, 0.0
    _glVertex3f  0.0, 0.0, 0.0
    _glVertex3f -1.0, 0.0, 0.0
    _glVertex3f -1.0,-1.0, 0.0
    _glVertex3f  0.0,-1.0, 0.0
    _glVertex3f  0.0, 0.0, 0.0
    _glVertex3f  0.0,-1.0, 0.0
    _glVertex3f  0.0,-1.0,-1.0
    _glVertex3f  0.0, 0.0,-1.0
    _glVertex3f -1.0, 0.0, 0.0
    _glVertex3f -1.0, 0.0,-1.0
    _glVertex3f -1.0,-1.0,-1.0
    _glVertex3f -1.0,-1.0, 0.0
    _glVertex3f  0.0, 0.0, 0.0
    _glVertex3f  0.0,-1.0,-1.0
    _glVertex3f -1.0,-1.0,-1.0
    _glVertex3f -1.0,-1.0, 0.0
    _glVertex3f  0.0, 0.0, 0.0
    _glVertex3f -1.0, 0.0,-1.0
    _glVertex3f -1.0,-1.0,-1.0
    _glVertex3f  0.0,-1.0,-1.0
    _glEnd
    _glPopMatrix
endproc

procedure DrawArm(xPos#, yPos#, zPos#)
    _glPushMatrix
    _glColor3f 1.0, 0.0, 0.0
    _glTranslatef xPos, yPos, zPos
    _glScalef 1.0, 4.0, 1.0
    _DrawCube 0.0, 0.0, 0.0
    _glPopMatrix
endproc

procedure DrawHead(xPos#, yPos#, zPos#)
   _glPushMatrix
   _glColor3f 1.0, 1.0, 1.0
   _glTranslatef xPos, yPos, zPos
   _glScalef 2.0, 2.0, 2.0
   _DrawCube 0.0, 0.0, 0.0
   _glPopMatrix
endproc

procedure DrawTorso(xPos#, yPos#, zPos#)
    _glPushMatrix
    _glColor3f 0.0, 0.0, 1.0
    _glTranslatef xPos, yPos, zPos
    _glScalef 3.0, 5.0, 1.0
    _DrawCube 0.0, 0.0, 0.0
    _glPopMatrix
endproc

procedure DrawLeg(xPos#, yPos#, zPos#)
    _glPushMatrix
    _glColor3f 1.0, 1.0, 0.0
    _glTranslatef xPos, yPos, zPos
    _glScalef 1.0, 5.0, 1.0
    _DrawCube 0.0, 0.0, 0.0
    _glPopMatrix
endproc

procedure DrawRobot(xPos#, yPos#, zPos#)
    _glPushMatrix
    _glTranslatef xPos, yPos, zPos
    _DrawHead  1.0, 2.0, 0.0
    _DrawTorso 1.5, 0.0, 0.0
    _glPushMatrix
    if arm1
       armAngle[0] = armAngle[0] + 1.0
    else
       armAngle[0] = armAngle[0] - 1.0
    endif
    if armAngle[0] >= 15.0
       arm1 = 0
    endif           
    if armAngle[0] <= 15.0
       arm1 = 1
    endif           
    _glTranslatef 0.0, -0.5, 0.0
    _glRotatef armAngle[0], 1.0, 0.0, 0.0
    _DrawArm 2.5, 0.0, -0.5
    _glPopMatrix
    _glPushMatrix
    if arm2
       armAngle[1] = armAngle[1] + 1.0
    else
       armAngle[1] = armAngle[1] - 1.0
    endif           
    _glTranslatef 0.0, -0.5, 0.0
    _glRotatef armAngle[1], 1.0, 0.0, 0.0
    _DrawArm -1.5, 0.0, -0.5
    _glPopMatrix
    _glPushMatrix
    if leg1
       legAngle[0] = legAngle[0] + 1.0
    else
       legAngle[0] = legAngle[0] - 1.0
    endif                 
    if legAngle[0] >= 15.0
       leg1 = 0
    endif           
    if legAngle[0] <= -15.0
       leg1 = 1
    endif   
    _glTranslatef 0.0, -0.5, 0.0
    _glRotatef legAngle[0], 1.0, 0.0, 0.0
    _DrawLeg -0.5, -5.0, -0.5
    _glPopMatrix
    _glPushMatrix
    if leg2
       legAngle[1] = legAngle[1] + 1.0
    else
       legAngle[1] = legAngle[1] - 1.0
    endif         
    if legAngle[1] >= 15.0
       leg2 = 0
    endif           
    if legAngle[1] <= -15.0
       leg2 = 1
    endif           
    _glTranslatef 0.0, -0.5, 0.0
    _glRotatef legAngle[1], 1.0, 0.0, 0.0
    _DrawLeg 1.5, -5.0, -0.5
    _glPopMatrix
    _glPopMatrix
endproc

Rick3137

  • Sr. Member
  • ****
  • Posts: 341
  • May the Force be with You
    • View Profile
    • Rick's Programs
Re: NeHe OpenGL Tutorials
« Reply #6 on: April 03, 2015 »
  I almost fixed it. This stuff takes practice.
Code: [Select]
import "OpenGL.lib"
import "gl_h.lib"
import "Speed.lib"
if not glInit("3D First Points", 64, 64, 640, 480, true) then end

visible:
angle#
legAngle#[2]
armAngle#[2] 
leg1 = 1
leg2 = 0
arm1 = 1
arm2 = 0

_glEnable GL_DEPTH_TEST
_glClearColor 0.0, 0.0, 0.0, 0.0

while keydown(27)=0
_glUpdate

_glClear GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT
_glLoadIdentity

angle = angle + 1.0
if angle >= 360.0 then angle = 0.0
_glUpdate

_glPushMatrix
_glLoadIdentity
_glTranslatef 0.0, 0.0, -30.0
_glRotatef angle, 0.0, 1.0, 0.0
_DrawRobot 0.0, 0.0, 0.0
_glPopMatrix
_glRender
_SPD_HoldFrame 30

wend
procedure DrawCube(xPos#, yPos#, zPos#)
 _glPushMatrix

_glBegin GL_QUADS

rem Top right of the quad (top).
_glVertex3f 1.0, 1.0, -1.0
rem Top left of the quad (top).
_glVertex3f -1.0, 1.0, -1.0
rem Bottom left of the quad (top).
_glVertex3f -1.0, 1.0, 1.0
rem Bottom right of the quad (top).
_glVertex3f 1.0, 1.0, 1.0

rem Top right of the quad (bottom).
_glVertex3f 1.0, -1.0, 1.0
rem Top left of the quad (bottom).
_glVertex3f -1.0, -1.0, 1.0
rem Bottom left of the quad (bottom).
_glVertex3f -1.0, -1.0, -1.0
rem Bottom right of the quad (bottom).
_glVertex3f 1.0, -1.0, -1.0

rem Top right of the quad (front).
_glVertex3f 1.0, 1.0, 1.0
rem Top left of the quad (front).
_glVertex3f -1.0, 1.0, 1.0
rem Bottom left of the quad (front).
_glVertex3f -1.0, -1.0, 1.0
rem Bottom right of the quad (front).
_glVertex3f 1.0, -1.0, 1.0

rem Bottom left of the quad (back).
_glVertex3f 1.0, -1.0, -1.0
rem Bottom right of the quad (back).
_glVertex3f -1.0, -1.0, -1.0
rem Top right of the quad (back).
_glVertex3f -1.0, 1.0, -1.0
rem Top left of the quad (back).
_glVertex3f 1.0, 1.0, -1.0

rem top right of the quad (left).
_glVertex3f -1.0, 1.0, 1.0
rem Top left of the quad (left).
_glVertex3f -1.0, 1.0, -1.0
rem Bottom left of the quad (left).
_glVertex3f -1.0, -1.0, -1.0
rem Bottom right of the quad (left).
_glVertex3f -1.0, -1.0, 1.0

rem Top right of the quad (right).
_glVertex3f 1.0, 1.0, -1.0
rem Top left of the quad (right).
_glVertex3f 1.0, 1.0, 1.0
rem Bottom left of the quad (right).
_glVertex3f 1.0, -1.0, 1.0
rem Bottom right of the quad (right).
_glVertex3f 1.0, -1.0, -1.0

_glEnd
  _glPopMatrix

endproc


procedure DrawArm(xPos#, yPos#, zPos#)
    _glPushMatrix
    _glColor3f 1.0, 0.0, 0.0
    _glTranslatef xPos, yPos, zPos
    _glScalef 1.0, 4.0, 1.0
    _DrawCube 0.0, 0.0, 0.0
    _glPopMatrix
endproc

procedure DrawHead(xPos#, yPos#, zPos#)
   _glPushMatrix
   _glColor3f 1.0, 1.0, 1.0
   _glTranslatef xPos, yPos, zPos
   _glScalef 2.0, 2.0, 2.0
   _DrawCube 0.0, 0.0, 0.0
   _glPopMatrix
endproc

procedure DrawTorso(xPos#, yPos#, zPos#)
    _glPushMatrix
    _glColor3f 0.0, 0.0, 1.0
    _glTranslatef xPos, yPos, zPos
    _glScalef 2.0, 4.0, 1.0
    _DrawCube 0.0, 0.0, 0.0
    _glPopMatrix
endproc

procedure DrawLeg(xPos#, yPos#, zPos#)
    _glPushMatrix
    _glColor3f 1.0, 1.0, 0.0
    _glTranslatef xPos, yPos, zPos
    _glScalef 1.0, 5.0, 1.0
    _DrawCube 0.0, 0.0, 0.0
    _glPopMatrix
endproc

procedure DrawRobot(xPos#, yPos#, zPos#)
    _glPushMatrix
    _glTranslatef xPos, yPos, zPos
    _DrawHead  1.0, 6.0, 0.0
    _DrawTorso 1.0, 0.0, 0.0
    _glPushMatrix
    if arm1
       armAngle[0] = armAngle[0] + 1.0
    else
       armAngle[0] = armAngle[0] - 1.0
    endif
    if armAngle[0] >= 15.0
       arm1 = 0
    endif           
    if armAngle[0] <= 15.0
       arm1 = 1
    endif           
    _glTranslatef 0.0, -0.5, 0.0
    _glRotatef armAngle[0], 1.0, 0.0, 0.0
    _DrawArm 2.5, 0.0, -0.5
    _glPopMatrix
    _glPushMatrix
    if arm2
       armAngle[1] = armAngle[1] + 1.0
    else
       armAngle[1] = armAngle[1] - 1.0
    endif           
    _glTranslatef 0.0, -0.5, 0.0
    _glRotatef armAngle[1], 1.0, 0.0, 0.0
    _DrawArm -2.0, 0.0, -0.5
    _glPopMatrix
    _glPushMatrix
    if leg1
       legAngle[0] = legAngle[0] + 1.0
    else
       legAngle[0] = legAngle[0] - 1.0
    endif                 
    if legAngle[0] >= 15.0
       leg1 = 0
    endif           
    if legAngle[0] <= -15.0
       leg1 = 1
    endif   
    _glTranslatef 0.0, -0.5, 0.0
    _glRotatef legAngle[0], 1.0, 0.0, 0.0
    _DrawLeg -0.5, -8.0, -0.5
    _glPopMatrix
    _glPushMatrix
    if leg2
       legAngle[1] = legAngle[1] + 1.0
    else
       legAngle[1] = legAngle[1] - 1.0
    endif         
    if legAngle[1] >= 15.0
       leg2 = 0
    endif           
    if legAngle[1] <= -15.0
       leg2 = 1
    endif           
    _glTranslatef 0.0, -0.5, 0.0
    _glRotatef legAngle[1], 1.0, 0.0, 0.0
    _DrawLeg 1.5, -8.0, -0.5
    _glPopMatrix
    _glPopMatrix
endproc



Peter

  • Guest
Re: NeHe OpenGL Tutorials
« Reply #7 on: April 04, 2015 »
Hi Rick,

Not the same!

Marcus

  • Administrator
  • Hero Member
  • *****
  • Posts: 541
    • View Profile
Re: NeHe OpenGL Tutorials
« Reply #8 on: April 08, 2015 »
Sorry for not responding, I'll have a look at it.

Although I have not checked the problem out yet, know that all GL drawing and state commands are queued and executed first when you call _glRender (or was the command named  _glDisplay? Don't remember.).

Also, for faster execution use display lists whenever possible.

.\\\opz