Merge pull request #6 from trustable-code/master
Fixed some problems and added an example program
This commit is contained in:
commit
5ee6d6a1e2
2 changed files with 109 additions and 13 deletions
95
examples/glut_example.nim
Normal file
95
examples/glut_example.nim
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
# OpenGL example using glut
|
||||
|
||||
import glut
|
||||
import opengl
|
||||
import glu
|
||||
|
||||
proc display() {.cdecl.} =
|
||||
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT) # Clear color and depth buffers
|
||||
glMatrixMode(GL_MODELVIEW) # To operate on model-view matrix
|
||||
|
||||
# Render a color-cube consisting of 6 quads with different colors
|
||||
glLoadIdentity() # Reset the model-view matrix
|
||||
glTranslatef(1.5, 0.0, -7.0) # Move right and into the screen
|
||||
|
||||
glBegin(GL_QUADS) # Begin drawing the color cube with 6 quads
|
||||
|
||||
# Top face (y = 1.0f)
|
||||
# Define vertices in counter-clockwise (CCW) order with normal pointing out
|
||||
glColor3f(0.0, 1.0, 0.0) # Green
|
||||
glVertex3f( 1.0, 1.0, -1.0)
|
||||
glVertex3f(-1.0, 1.0, -1.0)
|
||||
glVertex3f(-1.0, 1.0, 1.0)
|
||||
glVertex3f( 1.0, 1.0, 1.0)
|
||||
|
||||
# Bottom face (y = -1.0f)
|
||||
glColor3f(1.0, 0.5, 0.0) # Orange
|
||||
glVertex3f( 1.0, -1.0, 1.0)
|
||||
glVertex3f(-1.0, -1.0, 1.0)
|
||||
glVertex3f(-1.0, -1.0, -1.0)
|
||||
glVertex3f( 1.0, -1.0, -1.0)
|
||||
|
||||
# Front face (z = 1.0f)
|
||||
glColor3f(1.0, 0.0, 0.0) # Red
|
||||
glVertex3f( 1.0, 1.0, 1.0)
|
||||
glVertex3f(-1.0, 1.0, 1.0)
|
||||
glVertex3f(-1.0, -1.0, 1.0)
|
||||
glVertex3f( 1.0, -1.0, 1.0)
|
||||
|
||||
# Back face (z = -1.0f)
|
||||
glColor3f(1.0, 1.0, 0.0) # Yellow
|
||||
glVertex3f( 1.0, -1.0, -1.0)
|
||||
glVertex3f(-1.0, -1.0, -1.0)
|
||||
glVertex3f(-1.0, 1.0, -1.0)
|
||||
glVertex3f( 1.0, 1.0, -1.0)
|
||||
|
||||
# Left face (x = -1.0f)
|
||||
glColor3f(0.0, 0.0, 1.0) # Blue
|
||||
glVertex3f(-1.0, 1.0, 1.0)
|
||||
glVertex3f(-1.0, 1.0, -1.0)
|
||||
glVertex3f(-1.0, -1.0, -1.0)
|
||||
glVertex3f(-1.0, -1.0, 1.0)
|
||||
|
||||
# Right face (x = 1.0f)
|
||||
glColor3f(1.0, 0.0, 1.0) # Magenta
|
||||
glVertex3f(1.0, 1.0, -1.0)
|
||||
glVertex3f(1.0, 1.0, 1.0)
|
||||
glVertex3f(1.0, -1.0, 1.0)
|
||||
glVertex3f(1.0, -1.0, -1.0)
|
||||
|
||||
glEnd() # End of drawing
|
||||
|
||||
glutSwapBuffers() # Swap the front and back frame buffers (double buffering)
|
||||
|
||||
proc reshape(width: GLsizei, height: GLsizei) {.cdecl.} =
|
||||
# Compute aspect ratio of the new window
|
||||
if height == 0:
|
||||
return # To prevent divide by 0
|
||||
|
||||
# Set the viewport to cover the new window
|
||||
glViewport(0, 0, width, height)
|
||||
|
||||
# Set the aspect ratio of the clipping volume to match the viewport
|
||||
glMatrixMode(GL_PROJECTION) # To operate on the Projection matrix
|
||||
glLoadIdentity() # Reset
|
||||
# Enable perspective projection with fovy, aspect, zNear and zFar
|
||||
gluPerspective(45.0, width / height, 0.1, 100.0)
|
||||
|
||||
loadExtensions()
|
||||
glutInit()
|
||||
glutInitDisplayMode(GLUT_DOUBLE)
|
||||
glutInitWindowSize(640, 480)
|
||||
glutInitWindowPosition(50, 50)
|
||||
discard glutCreateWindow("OpenGL Example")
|
||||
|
||||
glutDisplayFunc(display)
|
||||
glutReshapeFunc(reshape)
|
||||
|
||||
glClearColor(0.0, 0.0, 0.0, 1.0) # Set background color to black and opaque
|
||||
glClearDepth(1.0) # Set background depth to farthest
|
||||
glEnable(GL_DEPTH_TEST) # Enable depth testing for z-culling
|
||||
glDepthFunc(GL_LEQUAL) # Set the type of depth-test
|
||||
glShadeModel(GL_SMOOTH) # Enable smooth shading
|
||||
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) # Nice perspective corrections
|
||||
|
||||
glutMainLoop()
|
||||
27
src/glu.nim
27
src/glu.nim
|
|
@ -55,11 +55,12 @@ type
|
|||
TGLUquadricObj* = GLUquadricObj
|
||||
TGLUtesselatorObj* = GLUtesselatorObj
|
||||
TGLUtriangulatorObj* = GLUtriangulatorObj
|
||||
|
||||
|
||||
proc gluErrorString*(errCode: TGLenum): cstring{.dynlib: dllname,
|
||||
importc: "gluErrorString".}
|
||||
proc gluErrorUnicodeStringEXT*(errCode: TGLenum): ptr int16{.dynlib: dllname,
|
||||
importc: "gluErrorUnicodeStringEXT".}
|
||||
when defined(Windows):
|
||||
proc gluErrorUnicodeStringEXT*(errCode: TGLenum): ptr int16{.dynlib: dllname,
|
||||
importc: "gluErrorUnicodeStringEXT".}
|
||||
proc gluGetString*(name: TGLenum): cstring{.dynlib: dllname,
|
||||
importc: "gluGetString".}
|
||||
proc gluOrtho2D*(left, right, bottom, top: TGLdouble){.dynlib: dllname,
|
||||
|
|
@ -72,11 +73,11 @@ proc gluLookAt*(eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz: TGLd
|
|||
dynlib: dllname, importc: "gluLookAt".}
|
||||
proc gluProject*(objx, objy, objz: TGLdouble,
|
||||
modelMatrix, projMatrix: var T16dArray,
|
||||
viewport: var TViewPortArray, winx, winy, winz: PGLdouble): int{.
|
||||
viewport: var TViewPortArray, winx, winy, winz: ptr TGLdouble): int{.
|
||||
dynlib: dllname, importc: "gluProject".}
|
||||
proc gluUnProject*(winx, winy, winz: TGLdouble,
|
||||
modelMatrix, projMatrix: var T16dArray,
|
||||
viewport: var TViewPortArray, objx, objy, objz: PGLdouble): int{.
|
||||
viewport: var TViewPortArray, objx, objy, objz: ptr TGLdouble): int{.
|
||||
dynlib: dllname, importc: "gluUnProject".}
|
||||
proc gluScaleImage*(format: TGLenum, widthin, heightin: TGLint, typein: TGLenum,
|
||||
datain: Pointer, widthout, heightout: TGLint,
|
||||
|
|
@ -130,7 +131,7 @@ proc gluTessNormal*(tess: PGLUtesselator, x, y, z: TGLdouble){.dynlib: dllname,
|
|||
importc: "gluTessNormal".}
|
||||
proc gluTessCallback*(tess: PGLUtesselator, which: TGLenum, fn: TCallBack){.
|
||||
dynlib: dllname, importc: "gluTessCallback".}
|
||||
proc gluGetTessProperty*(tess: PGLUtesselator, which: TGLenum, value: PGLdouble){.
|
||||
proc gluGetTessProperty*(tess: PGLUtesselator, which: TGLenum, value: ptr TGLdouble){.
|
||||
dynlib: dllname, importc: "gluGetTessProperty".}
|
||||
proc gluNewNurbsRenderer*(): PGLUnurbs{.dynlib: dllname,
|
||||
importc: "gluNewNurbsRenderer".}
|
||||
|
|
@ -143,15 +144,15 @@ proc gluEndCurve*(nobj: PGLUnurbs){.dynlib: dllname, importc: "gluEndCurve".}
|
|||
proc gluEndSurface*(nobj: PGLUnurbs){.dynlib: dllname, importc: "gluEndSurface".}
|
||||
proc gluBeginTrim*(nobj: PGLUnurbs){.dynlib: dllname, importc: "gluBeginTrim".}
|
||||
proc gluEndTrim*(nobj: PGLUnurbs){.dynlib: dllname, importc: "gluEndTrim".}
|
||||
proc gluPwlCurve*(nobj: PGLUnurbs, count: TGLint, aarray: PGLfloat,
|
||||
proc gluPwlCurve*(nobj: PGLUnurbs, count: TGLint, aarray: ptr TGLfloat,
|
||||
stride: TGLint, atype: TGLenum){.dynlib: dllname,
|
||||
importc: "gluPwlCurve".}
|
||||
proc gluNurbsCurve*(nobj: PGLUnurbs, nknots: TGLint, knot: PGLfloat,
|
||||
stride: TGLint, ctlarray: PGLfloat, order: TGLint,
|
||||
proc gluNurbsCurve*(nobj: PGLUnurbs, nknots: TGLint, knot: ptr TGLfloat,
|
||||
stride: TGLint, ctlarray: ptr TGLfloat, order: TGLint,
|
||||
atype: TGLenum){.dynlib: dllname, importc: "gluNurbsCurve".}
|
||||
proc gluNurbsSurface*(nobj: PGLUnurbs, sknot_count: TGLint, sknot: PGLfloat,
|
||||
tknot_count: TGLint, tknot: PGLfloat,
|
||||
s_stride, t_stride: TGLint, ctlarray: PGLfloat,
|
||||
proc gluNurbsSurface*(nobj: PGLUnurbs, sknot_count: TGLint, sknot: ptr TGLfloat,
|
||||
tknot_count: TGLint, tknot: ptr TGLfloat,
|
||||
s_stride, t_stride: TGLint, ctlarray: ptr TGLfloat,
|
||||
sorder, torder: TGLint, atype: TGLenum){.dynlib: dllname,
|
||||
importc: "gluNurbsSurface".}
|
||||
proc gluLoadSamplingMatrices*(nobj: PGLUnurbs,
|
||||
|
|
@ -160,7 +161,7 @@ proc gluLoadSamplingMatrices*(nobj: PGLUnurbs,
|
|||
importc: "gluLoadSamplingMatrices".}
|
||||
proc gluNurbsProperty*(nobj: PGLUnurbs, aproperty: TGLenum, value: TGLfloat){.
|
||||
dynlib: dllname, importc: "gluNurbsProperty".}
|
||||
proc gluGetNurbsProperty*(nobj: PGLUnurbs, aproperty: TGLenum, value: PGLfloat){.
|
||||
proc gluGetNurbsProperty*(nobj: PGLUnurbs, aproperty: TGLenum, value: ptr TGLfloat){.
|
||||
dynlib: dllname, importc: "gluGetNurbsProperty".}
|
||||
proc gluNurbsCallback*(nobj: PGLUnurbs, which: TGLenum, fn: TCallBack){.
|
||||
dynlib: dllname, importc: "gluNurbsCallback".}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue