Author Topic: OpenGL Glut32 example  (Read 1675 times)

HSE

  • Member
  • *****
  • Posts: 1304
  • <AMD>< 7-32>
OpenGL Glut32 example
« on: January 02, 2019, 03:29:20 AM »
Here is an example width Glut32 for Windows (binary package with dll and lib), wich is used in most OpenGL tutorials.

Because tutorials are for "C", AsmC it's very well fitted for that and you only have to copy and paste examples.

Apparently there is a problem closing the window with the icon. Instead use "x" from keyboard or middle button from mouse. Left mouse button is run and Right button is  stop.

« Last Edit: January 02, 2019, 04:33:43 AM by HSE »

HSE

  • Member
  • *****
  • Posts: 1304
  • <AMD>< 7-32>
Re: OpenGL Glut32 example
« Reply #1 on: October 31, 2019, 10:12:24 AM »
Apparently there is a problem closing the window with the icon. Instead use "x" from keyboard or middle button from mouse. Left mouse button is run and Right button is  stop.
I don't see that problem anymore, perhaps some OS issue.
Still there the problem  :cool:

nidud

  • Member
  • *****
  • Posts: 1928
    • https://github.com/nidud/asmc
Re: OpenGL Glut32 example
« Reply #2 on: November 06, 2019, 03:10:35 AM »
I added a glut sample.

include gl/glut.inc

.code

renderScene proc

    glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)

    glBegin(GL_TRIANGLES)
    glVertex3f(-0.5, -0.5, 0.0)
    glVertex3f(0.5, 0.0, 0.0)
    glVertex3f(0.0, 0.5, 0.0)
    glEnd()

    glutSwapBuffers()
    ret

renderScene endp

main proc argc:int_t, argv:array_t

    ;; init GLUT and create Window
    glutInit(&argc, argv)

    glutInitDisplayMode(GLUT_DEPTH or GLUT_DOUBLE or GLUT_RGBA)
    glutInitWindowPosition(100, 100)
    glutInitWindowSize(320, 320)

    glutCreateWindow("Simple GLUT")

    ;; register callbacks
    glutDisplayFunc(&renderScene)

    ;; enter GLUT event processing cycle
    glutMainLoop()

    mov eax,1
    ret

main endp

    end

The problem appear to be glutInit() and a few other functions.

if defined(_WIN32) AND NOT defined(GLUT_DISABLE_ATEXIT_HACK)
__glutInitWithExit proto WINAPI :ptr int_t, :array_t, :ptr
ifndef GLUT_BUILDING_LIB
glutInit macro argcp, argv
    exitm<__glutInitWithExit(argcp, argv, &exit)>
    endm
endif
else
glutInit proto WINAPI :ptr int_t, :array_t
endif

The library (glut32.lib) is also added to asmc/lib.
« Last Edit: December 04, 2019, 07:03:12 AM by nidud »

HSE

  • Member
  • *****
  • Posts: 1304
  • <AMD>< 7-32>
Re: OpenGL Glut32 example
« Reply #3 on: November 06, 2019, 05:33:22 AM »
 :biggrin: Yes, there is some problems.

nidud

  • Member
  • *****
  • Posts: 1928
    • https://github.com/nidud/asmc
Re: OpenGL Glut32 example
« Reply #4 on: May 16, 2020, 11:50:50 PM »
:biggrin: Yes, there is some problems.

I vent another round with this, converted some of the Redbook samples. From the two first with anti-aliased lines the one using color index (aaindex) fails. It appear the color array in the DLL is NULL. The second one (aargb) works but it suppose to rotate hitting R.

The accanti sample suppose to display this:

It draws something but not that.

The bezcurve sample actually draws a line but the curve is off and the dots are missing. The dof sample:


So this seems to actually work but the teapots are way to small compare to the image above. The version is 4.4.0 so maybe that's the problem or some hardware limitations (the idea that there's [was] something wrong with the code is of course very unlikely :biggrin:).

The binaries and sources are attached.

« Last Edit: May 23, 2020, 11:21:16 AM by nidud »

nidud

  • Member
  • *****
  • Posts: 1928
    • https://github.com/nidud/asmc
Re: OpenGL Glut32 example
« Reply #5 on: May 18, 2020, 12:59:00 AM »
The current version is 4.4.0 and the color index mode is apparently obsolete. There was a mixup with floats and doubles in the code so the samples above now works.

The samples are added to ./source/test/glut directory and the ZIP files above contain some of the binaries. The C source are translated as written and this assumes a console application (using main) so a new startup is added to the header file linking them as gui. However, some use the console for output so there is a mix of options.

This one is a GUI app using mouse input:
Code: [Select]
;
; https://www.opengl.org/archives/resources/code/samples/redbook/colormat.c
; build: asmc64 -pe -gui -D_GLPE colormat.asm
;
include GL/glut.inc

.data
diffuseMaterial GLfloat 0.5, 0.5, 0.5, 1.0

.code

init proc

    .data
    mat_specular   GLfloat 1.0, 1.0, 1.0, 1.0
    light_position GLfloat 1.0, 1.0, 1.0, 0.0
    .code

    glClearColor(0.0, 0.0, 0.0, 0.0)
    glShadeModel(GL_SMOOTH)
    glEnable(GL_DEPTH_TEST)
    glMaterialfv(GL_FRONT, GL_DIFFUSE, &diffuseMaterial)
    glMaterialfv(GL_FRONT, GL_SPECULAR, &mat_specular)
    glMaterialf(GL_FRONT, GL_SHININESS, 25.0)
    glLightfv(GL_LIGHT0, GL_POSITION, &light_position)
    glEnable(GL_LIGHTING)
    glEnable(GL_LIGHT0)

    glColorMaterial(GL_FRONT, GL_DIFFUSE)
    glEnable(GL_COLOR_MATERIAL)
    ret

init endp

display proc

    glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
    glutSolidSphere(1.0, 20, 16)
    glFlush()
    ret

display endp

reshape proc w:int_t, h:int_t

    glViewport (0, 0, ecx, edx)
    glMatrixMode (GL_PROJECTION)
    glLoadIdentity()
    cvtsi2sd xmm0,w
    .if w <= h
        cvtsi2sd xmm2,h
        divsd xmm2,xmm0
        movsd xmm3,xmm2
        mulsd xmm2,-1.5
        mulsd xmm3,1.5
        glOrtho(-1.5, 1.5, xmm2, xmm3, -10.0, 10.0)
    .else
        cvtsi2sd xmm1,h
        divsd xmm0,xmm1
        movsd xmm1,xmm0
        mulsd xmm0,-1.5
        mulsd xmm1,1.5
        glOrtho(xmm0, xmm1, -1.5, 1.5, -10.0, 10.0)
    .endif
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    ret

reshape endp

mouse proc button:int_t, state:int_t, x:int_t, y:int_t

    .switch ecx
      .case GLUT_LEFT_BUTTON
         .if edx == GLUT_DOWN
            movss xmm0,diffuseMaterial
            addss xmm0,0.1
            comiss xmm0,1.0
            .ifa
               xorps xmm0,xmm0
            .endif
            movss diffuseMaterial,xmm0
            glColor4fv(&diffuseMaterial)
            glutPostRedisplay()
         .endif
         .endc
      .case GLUT_MIDDLE_BUTTON
         .if edx == GLUT_DOWN
            movss xmm0,diffuseMaterial[4]
            addss xmm0,0.1
            comiss xmm0,1.0
            .ifa
               xorps xmm0,xmm0
            .endif
            movss diffuseMaterial[4],xmm0
            glColor4fv(&diffuseMaterial)
            glutPostRedisplay()
         .endif
         .endc
      .case GLUT_RIGHT_BUTTON
         .if edx == GLUT_DOWN
            movss xmm0,diffuseMaterial[8]
            addss xmm0,0.1
            comiss xmm0,1.0
            .ifa
               xorps xmm0,xmm0
            .endif
            movss diffuseMaterial[8],xmm0
            glColor4fv(&diffuseMaterial)
            glutPostRedisplay()
         .endif
         .endc
    .endsw
    ret

mouse endp

keyboard proc key:byte, x:int_t, y:int_t

    .if key == 27
        exit(0)
    .endif
    ret

keyboard endp

main proc argc:int_t, argv:array_t

    glutInit(&argc, argv)
    glutInitDisplayMode(GLUT_SINGLE or GLUT_RGB or GLUT_DEPTH)
    glutInitWindowSize(500, 500)
    glutInitWindowPosition(100, 100)
    mov rcx,argv
    glutCreateWindow([rcx])
    init()
    glutReshapeFunc(&reshape)
    glutDisplayFunc(&display)
    glutKeyboardFunc(&keyboard)
    glutMouseFunc(&mouse)
    glutMainLoop()
    xor eax,eax
    ret

main endp

    end _tstart

And this one a console app pumping messages on keyboard input. This uses the standard startup from tchar.inc so the exit() function passed to glutInit() needs to be added.
Code: [Select]
;
; https://www.opengl.org/archives/resources/code/samples/redbook/fog.asm
; build: asmc64 -pe fog.asm
;
include GL/glut.inc
include math.inc
include stdio.inc
include tchar.inc

.data
fogMode int_t 0

.code

glutExit proc retval:int_t
    exit(retval)
glutExit endp

init proc

    .data
    position GLfloat 0.5, 0.5, 3.0, 0.0
    .code

    glEnable(GL_DEPTH_TEST)

    glLightfv(GL_LIGHT0, GL_POSITION, &position)
    glEnable(GL_LIGHTING)
    glEnable(GL_LIGHT0)

        .new mat[3]:GLfloat

        mov mat[0],0.1745
        mov mat[4],0.01175
        mov mat[8],0.01175

        glMaterialfv(GL_FRONT, GL_AMBIENT, &mat)
        mov mat[0],0.61424
        mov mat[4],0.04136
        mov mat[8],0.04136
        glMaterialfv(GL_FRONT, GL_DIFFUSE, &mat)
        mov mat[0],0.727811
        mov mat[4],0.626959
        mov mat[8],0.626959
        glMaterialfv(GL_FRONT, GL_SPECULAR, &mat)
        glMaterialf(GL_FRONT, GL_SHININESS, 0.6*128.0)

    glEnable(GL_FOG)

        .new fogColor[4]:GLfloat

        mov fogColor[0],0.5
        mov fogColor[4],0.5
        mov fogColor[8],0.5
        mov fogColor[12],1.0

        mov fogMode,GL_EXP
        glFogi(GL_FOG_MODE, fogMode)
        glFogfv(GL_FOG_COLOR, &fogColor)
        glFogf(GL_FOG_DENSITY, 0.35)
        glHint(GL_FOG_HINT, GL_DONT_CARE)
        glFogf(GL_FOG_START, 1.0)
        glFogf(GL_FOG_END, 5.0)

    glClearColor(0.5, 0.5, 0.5, 1.0)
    ret

init endp



renderSphere proc x:GLfloat, y:GLfloat, z:GLfloat

    glPushMatrix()
    glTranslatef(x, y, z)
    glutSolidSphere(0.4, 16, 16)
    glPopMatrix()
    ret

renderSphere endp

display proc

    glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
    renderSphere(-2.0, -0.5, -1.0)
    renderSphere(-1.0, -0.5, -2.0)
    renderSphere(0.0, -0.5, -3.0)
    renderSphere(1.0, -0.5, -4.0)
    renderSphere(2.0, -0.5, -5.0)
    glFlush()
    ret

display endp

reshape proc w:int_t, h:int_t

    glViewport(0, 0, ecx, edx)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    cvtsi2sd xmm0,w
    cvtsi2sd xmm1,h
    .if w <= h
        divsd xmm1,xmm0
        movsd xmm2,xmm1
        movsd xmm3,xmm1
        mulsd xmm2,-2.5
        mulsd xmm3,2.5
        glOrtho (-2.5, 2.5, xmm2, xmm3, -10.0, 10.0);
    .else
        divsd xmm0,xmm1
        movsd xmm1,xmm0
        mulsd xmm1,2.5
        mulsd xmm0,-2.5
        glOrtho(xmm0, xmm1, -2.5, 2.5, -10.0, 10.0);
    .endif
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    ret

reshape endp


keyboard proc key:byte, x:int_t, y:int_t

    .switch ecx
      .case 'f'
      .case 'F'
         .if (fogMode == GL_EXP)
            mov fogMode,GL_EXP2
            printf ("Fog mode is GL_EXP2\n")
         .elseif (fogMode == GL_EXP2)
            mov fogMode,GL_LINEAR
            printf ("Fog mode is GL_LINEAR\n")
         .elseif (fogMode == GL_LINEAR)
            mov fogMode,GL_EXP
            printf("Fog mode is GL_EXP\n")
         .endif
         glFogi(GL_FOG_MODE, fogMode)
         glutPostRedisplay()
         .endc
      .case 27:
         exit(0);
         .endc
   .endsw
   ret

keyboard endp


main proc argc:int_t, argv:array_t

   glutInit(&argc, argv)
   glutInitDisplayMode(GLUT_SINGLE or GLUT_RGB or GLUT_DEPTH)
   glutInitWindowSize(500, 500)
   mov rcx,argv
   glutCreateWindow([rcx])
   init()
   glutReshapeFunc(&reshape)
   glutDisplayFunc(&display)
   glutKeyboardFunc(&keyboard)
   glutMainLoop()
   xor eax,eax
   ret

main endp

    end _tstart

mabdelouahab

  • Member
  • ***
  • Posts: 449
Re: OpenGL Glut32 example
« Reply #6 on: May 18, 2020, 02:39:19 AM »
what about linux??

Assembly:
mulsd xmm2,-2.5;-->error A2070: invalid instruction operands
mulsd xmm3,2.5 ;-->error A2070: invalid instruction operands

link:
undefined reference to `__glutInitWithExit'
undefined reference to `__glutCreateWindowWithExit'

nidud

  • Member
  • *****
  • Posts: 1928
    • https://github.com/nidud/asmc
Re: OpenGL Glut32 example
« Reply #7 on: May 18, 2020, 03:36:09 AM »
The development is done in assembler and the Linux version depends on the C source. This will be updated at some point but the samples here are written for Windows.

The latest Linux version is 2.30 and the current version is 2.31.30.

mabdelouahab

  • Member
  • ***
  • Posts: 449
Re: OpenGL Glut32 example
« Reply #8 on: May 18, 2020, 04:33:54 PM »

nidud

  • Member
  • *****
  • Posts: 1928
    • https://github.com/nidud/asmc
Re: OpenGL Glut32 example
« Reply #9 on: May 19, 2020, 05:46:43 AM »
Excellent :biggrin:

big success apparently  :thumbsup:

I'm soaked in diesel and paint at the moment, but I will add the rest of the samples later on.

nidud

  • Member
  • *****
  • Posts: 1928
    • https://github.com/nidud/asmc
Re: OpenGL Glut32 example
« Reply #10 on: May 19, 2020, 10:53:34 AM »
All the 55 samples are now added.
Source and binaries also added to the ZIP above.

nidud

  • Member
  • *****
  • Posts: 1928
    • https://github.com/nidud/asmc
Re: OpenGL Glut32 example
« Reply #11 on: May 22, 2020, 03:04:10 AM »
The C source is updated so it's possible to build the current version now. A Linux version is also added to the BIN directory. This is not tested but the C version passes the current regression test.

mabdelouahab

  • Member
  • ***
  • Posts: 449
Re: OpenGL Glut32 example
« Reply #12 on: May 22, 2020, 03:51:53 PM »
 :thumbsup:

Mikl__

  • Member
  • ****
  • Posts: 899
Re: OpenGL Glut32 example
« Reply #13 on: May 26, 2020, 04:10:30 AM »
Hi, nidud!
In bezmesh.exe and bezsurf.exe I see empty black screen. So it was originally conceived or is it some kind of failure?

nidud

  • Member
  • *****
  • Posts: 1928
    • https://github.com/nidud/asmc
Re: OpenGL Glut32 example
« Reply #14 on: May 26, 2020, 05:19:48 AM »
Been some fiddling there so it's difficult to say. They all work on my end except from aaindex and alpha3D I think. I zip the two binaries so you can test, maybe it's been updated.