From a082eeb1aa6ba89a650c65d5f1df16cae626b3e5 Mon Sep 17 00:00:00 2001 From: Simon Krauter Date: Sat, 4 Oct 2014 00:58:46 +0200 Subject: [PATCH 1/3] Fixed some problems and added an example program --- examples/glut_example.nim | 95 +++++++++++++++++++++++++++++++++++++++ src/glu.nim | 8 ++-- 2 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 examples/glut_example.nim diff --git a/examples/glut_example.nim b/examples/glut_example.nim new file mode 100644 index 0000000..18ff5d1 --- /dev/null +++ b/examples/glut_example.nim @@ -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() diff --git a/src/glu.nim b/src/glu.nim index 98d69cd..3f377ef 100644 --- a/src/glu.nim +++ b/src/glu.nim @@ -55,11 +55,13 @@ type TGLUquadricObj* = GLUquadricObj TGLUtesselatorObj* = GLUtesselatorObj TGLUtriangulatorObj* = GLUtriangulatorObj - + PGLfloat* = float32 + PGLdouble* = float64 + proc gluErrorString*(errCode: TGLenum): cstring{.dynlib: dllname, importc: "gluErrorString".} -proc gluErrorUnicodeStringEXT*(errCode: TGLenum): ptr int16{.dynlib: dllname, - importc: "gluErrorUnicodeStringEXT".} +#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, From 1e638076192af487e967f61017a45b7624b00879 Mon Sep 17 00:00:00 2001 From: Simon Krauter Date: Sat, 4 Oct 2014 01:15:54 +0200 Subject: [PATCH 2/3] changes like suggested --- src/glu.nim | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/glu.nim b/src/glu.nim index 3f377ef..46d43a7 100644 --- a/src/glu.nim +++ b/src/glu.nim @@ -55,13 +55,14 @@ type TGLUquadricObj* = GLUquadricObj TGLUtesselatorObj* = GLUtesselatorObj TGLUtriangulatorObj* = GLUtriangulatorObj - PGLfloat* = float32 - PGLdouble* = float64 + PGLfloat* = ptr TGLfloat + PGLdouble* = ptr TGLdouble 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, From 769d34473ec3897b1e0e83121dc2f736083745c1 Mon Sep 17 00:00:00 2001 From: Simon Krauter Date: Sat, 4 Oct 2014 01:23:46 +0200 Subject: [PATCH 3/3] Replaced PGLfloat and PGLdouble by ptr .. --- src/glu.nim | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/glu.nim b/src/glu.nim index 46d43a7..9a6b9b9 100644 --- a/src/glu.nim +++ b/src/glu.nim @@ -55,8 +55,6 @@ type TGLUquadricObj* = GLUquadricObj TGLUtesselatorObj* = GLUtesselatorObj TGLUtriangulatorObj* = GLUtriangulatorObj - PGLfloat* = ptr TGLfloat - PGLdouble* = ptr TGLdouble proc gluErrorString*(errCode: TGLenum): cstring{.dynlib: dllname, importc: "gluErrorString".} @@ -75,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, @@ -133,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".} @@ -146,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, @@ -163,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".}