This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
AfterLifeLochie's profile picture

ccGL - CCLights graphics implementation

Started by AfterLifeLochie, 26 December 2012 - 12:03 AM
AfterLifeLochie #1
Posted 26 December 2012 - 01:03 AM
ccGL is a project I'm currently working on to implement an OpenGL-like render toolkit and environment into the CCLights Peripheral by ds84182.

ccGL will eventually provide complete 2-dimensional (and perhaps some 3-dimensional + perspective) functionality - including rendering of things in classic OpenGL, such as GL_POINT, GL_LINE, GL_QUAD, and some more groovy things, such as custom polygons and primitives, creating gradients and binding textures - as well as the ability to create render contexts and functioning buffers - with the same (or practically identically similar) syntax as OpenGL - allowing for fast graphics rendering on CCLights devices and CCLight grids on Peripheral Cables. (I may eventually extend this to support monitors as well, however, monitors have a significantly lower bit-depth and aren't square pixels… or even pixels.)

ccGL currently supports all the code in the below demonstration - lines, points, clearing, colors (and partial undemonstrated quad support).
SpoilerSample code. Note this will probably change a little per release.

api("glapi.lua", "GL")


GLMonitor = peripheral.wrap("top")
GL.glClearColor(128, 128, 128)

local monSize = {GLMonitor.getSize()}


local colorTest = {
{  0,   0,   0},
{255,   0,   0},
{0,   255,   0},
{0,	 0, 255},
{255, 255,   0},
{255,   0, 255},
{  0, 255, 255},
{255, 255, 255} }

-- BEGINBLOCK-GL_POINTS
GL.glBegin(GLMonitor, GL.MODES.GL_POINTS)


for b=1,monSize[1] do
local _color = (255/monSize[1]) * b

GL.glColor3i(_color, 0, 0)
GL.glVertex2i(b, 8)
GL.glColor3i(0, _color, 0)
GL.glVertex2i(b, 9)
GL.glColor3i(0, 0, _color)
GL.glVertex2i(b, 10)


GL.glColor3i(_color, 255, 255)
GL.glVertex2i(b, 11)
GL.glColor3i(255, _color, 255)
GL.glVertex2i(b, 12)
GL.glColor3i(255, 255, _color)
GL.glVertex2i(b, 13)
end

for i,c in pairs(colorTest) do
GL.glColor3i(c[1], c[2], c[3])
GL.glVertex2i(i, 14)
end

for i=1,monSize[1] do
local _color = (255/monSize[1]) * i
GL.glColor3i(_color, _color, _color)
GL.glVertex2i(i, 15)
end

-- BEGINBLOCK-GL_LINES
GL.glBegin(GLMonitor, GL.MODES.GL_LINES)

GL.glColor3i(255, 0, 0)
GL.glVertex2i(3, 1)
GL.glVertex2i(1, 1)

GL.glColor3i(255, 255, 0)
GL.glVertex2i(4, 3)
GL.glVertex2i(4, 1)

GL.glColor3i(0, 255, 0)
GL.glVertex2i(2, 4)
GL.glVertex2i(4, 4)

GL.glColor3i(0, 0, 255)
GL.glVertex2i(1, 2)
GL.glVertex2i(1, 4)

-- BEGINBLOCK-GL_PUSH
GL.glEnd()

… produces this result (okay, don't let me show the picture, forums?).
Dlcruz129 #2
Posted 26 December 2012 - 05:12 AM
Very nice! I look forward to this.
Tiin57 #3
Posted 26 December 2012 - 08:16 AM
This looks pretty awesome, Lochie. Now people can stop complaining about OpenGL in Lua. :D/>
lieudusty #4
Posted 26 December 2012 - 04:54 PM
Looks very nice!
SoniEx2 #5
Posted 26 December 2012 - 05:03 PM
I need this…

Now give me a ym2612 emulator…
PixelToast #6
Posted 26 December 2012 - 05:24 PM
I need this…

Now give me a ym2612 emulator…
um, do you mean this:
Spoiler
ds84182 #7
Posted 27 December 2012 - 03:24 AM
This is great! (Even though AfterLifeLochie already told me about it… lol)
Sooner or later, we could make Minecraft on CCLights monitors :D/>
SoniEx2 #8
Posted 27 December 2012 - 03:31 AM
I need this…

Now give me a ym2612 emulator…
um, do you mean this:
Spoiler

Yes, for an in-game Sega Genesis emulator.
Exerro #9
Posted 27 December 2012 - 01:26 PM
omg i wish the advanced pcs were like this or there was a pc that would do the same job included in the mod
AfterLifeLochie #10
Posted 27 December 2012 - 08:24 PM
Tidied and commented a bit of the code so it's reasonable enough for a pre-release. Download everything except README.md from [gone]

Usage:
You have two options for using the ccGL API - through the custom "glloader.lua" file or through the default API loader in ComputerCraft.

Assuming you have downloaded all the files required, you can simply call
dofile("glloader.lua")
once, and ccGL will put itself in a "protected" mode into _G["GL"]. Accessing ccGL in this method is as one would expect, simply,
GL.<method>()
.
Using the glloader.lua script is preferred, as it allows protection (at metatable level, so it's still bypass-able) of the GL object. This means accidental overwriting of functions won't completely crash the renderer - or your program.

Demo:
Run "gltest.lua" from a shell. It is a semi-practical example and demonstration of how ccGL behaves - the console output can be rather on the spammy side, however. Using this demonstration requires a 32x32 CCLights screen to be directly connected above the Computer.

Configuration + Options:
The only option available is to disable the debugging feature - to do this, open glapi.lua in an editor, and change line 18's "debugMode = 1" to "debugmode = 0". This cannot be done externally, as the parameters table is local and the entire GL library is (usually) protected.