OpenGL/Phix

From Rosetta Code
(Redirected from OpenGL/Phix here)

Older win32-only and OpenGL 1.0 (ie not p2js compatible) versions of the OpenGL task.

Adapted from the included demo\Arwen32dibdemo\shadepol.exw, draws the same thing as the image at the top of this page, but (as per the talk page) does not use openGL, just windows api (BitBlt etc) and some low-level fully open source routines (all included). Note this is windows 32bit only, whereas the pGUI example below it runs on windows/linx, 32/64bit.

without js
include demo\Arwen32dibdemo\a32dpoly.ew
 
a32Dib0 screen_dib = 0
integer dx = 0, dy = 0, dw = 0, dh = 0
 
constant win = create(Window, "Arwen32Dib bitmap shaded triangle demo", 0, 0, Default, Default, 480, 300, 0)
 
function winHandler(integer id, integer msg, atom wParam, object lParam)
sequence rect
    if id or object(lParam) then end if -- suppress warnings
    if msg=WM_PAINT then
        if sequence(screen_dib) then
            clearDib(screen_dib, {0, 0, 0})
            drawShadedPolygonToDib(screen_dib, {{dx, dy}, {dx, dh-dy}, {dw-dx, dh-dy}}, {{255, 0, 0}, {0, 0, 255}, {0, 255, 0}})
            drawDib(win, screen_dib, 0, 0, 0, 0, screen_dib[DibWidth]-1, screen_dib[DibHeight]-1)
        end if
    elsif msg=WM_SIZE then
        rect = getClientRect(win)
        dw = rect[3]
        dh = rect[4]
        dx = floor(dw/4)+1
        dy = floor(dh/4)+1
        if sequence(screen_dib) then killDib(screen_dib) end if
        screen_dib = newDib(dw, dh)
    elsif msg=WM_CHAR 
      and wParam=VK_ESCAPE then
        closeWindow(win)
    end if
    return 0
end function
setHandler(win, routine_id("winHandler"))
 
WinMain(win, SW_NORMAL)
 
if sequence(screen_dib) then killDib(screen_dib) end if

And here is a proper openGL version, translated from Lua, and included in the distro as demo\pGUI\triangle.exw:

Library: Phix/pGUI
without js -- (OpenGL 1.0)
include pGUI.e
include opengl.e

function resize_cb(Ihandle /*ih*/, integer width, integer height)
    glViewport(0, 0, width, height)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(-30.0, 30.0, -30.0, 30.0, -30.0, 30.0)
    glMatrixMode(GL_MODELVIEW)
    return IUP_DEFAULT
end function

function action(Ihandle /*ih*/)

    glClearColor(0.3,0.3,0.3,0.0)
    glClear(GL_COLOR_BUFFER_BIT+GL_DEPTH_BUFFER_BIT)
 
    glShadeModel(GL_SMOOTH)
 
    glLoadIdentity()
    glTranslate(-15.0, -15.0, 0.0)
 
    glBegin(GL_TRIANGLES)
    glColor(1.0, 0.0, 0.0)
    glVertex(0.0, 0.0)
    glColor(0.0, 1.0, 0.0)
    glVertex(30.0, 0.0)
    glColor(0.0, 0.0, 1.0)
    glVertex(0.0, 30.0)
    glEnd()
 
    glFlush()

    return IUP_DEFAULT
end function

Ihandln dialog
Ihandle canvas

function map_cb(Ihandle /*ih*/)
    IupGLMakeCurrent(canvas)
    integer {width, height} = IupGetIntInt(dialog, "RASTERSIZE")
    {} = resize_cb(dialog, width, height)
    return IUP_DEFAULT
end function

IupOpen()
IupGLCanvasOpen()

canvas = IupGLCanvas(Icallback("action"), "RASTERSIZE=640x480")
IupSetCallback(canvas, "RESIZE_CB", Icallback("resize_cb"))

dialog = IupDialog(canvas, "MAP_CB", Icallback("map_cb"), "TITLE=Triangle, SHRINK=YES")

IupShow(dialog)
if platform()!=JS then
    IupMainLoop()
    dialog = IupDestroy(dialog)
    IupClose()
end if