Is It Possible To Write WebGL in the Chrome Javascript REPL? - google-chrome

Usually when using WebGL one writes most of the graphics code in a function bound to window.onload. For the sake of REPL-style graphics development, is it possible to write that OpenGL code interactively in the javascript console?

Of course it is but WebGL is a very verbose API. You have to upload, compile and link shaders, look up attributes and uniforms, create buffers and textures and upload data into each, then bind all textures, buffers, set attributes, and set uniforms and finally call a one of the draw functions
Doing that all from a REPL would be pretty tedious and error prone.
That said when I'm debugging I often paste something like this into the devtools REPL
gl = document.querySelector("canvas").getContext("webgl");
Which will give me the WebGLRenderingContext for the first canvas in the page (which is usually what I want). I can then for example check if there's an error
gl.getError();
Another common thing I do is check the available extensions
document.createElements("canvas").getContext("webgl").getSupportedExtensions().join("\n");
Otherwise if you're looking for editing WebGL in real time that's usually limited to things like glslsandbox.com or vertexshaderart.com where you're just editing a single shader that's used in a single way and not using the entire WebGL API in a REPL. There's also shdr which gives you a single model and a both a vertex and fragment shaders to work with.
If you really want a REPL you probably need some engine above it in which case it would be a name-of-engine REPL and not a WebGL REPL.

Related

Tk frame: how to deactivate internal handler for expose events?

I want to draw into a Tk 8.6 frame using Cairo C/X11 code. I found that I could prevent the frame from redrawing itself in case of Expose events (e.g. window size changes) by passing -background "" as option to the frame (so basically the frame doesn't know what to redraw). Two questions:
Is there a better way to tell a frame to not redraw itself but to leave it to somebody else?
Can I replace Tk's internal event handlers (for Expose etc.) by my own on the C level? (At the moment I'm doing it on the Tcl level by bind $frame <Expose> "myExposeHandler...".)
Thanks for your help!
(The -container yes option seems to have no effect, btw.)
The -background "" option is the one which disables redraws of frames, and is typically used where you're going to hand off the XId of the frame as the handle to draw on to some other process. (This used to be how people would integrate video players; I don't know how practical that still is.) There's also the -container true option, as you found, but that's intended only for use where you're going to embed a toplevel from another Tk process inside the frame (it enables a special protocol for exchanging geometry manager information, IIRC) and isn't what you want here.
However, if you're already working with your own C code then you might as well make your own widget. Then you'll have complete control over all the event handling and can do anything you want. The usual place for people to start when doing this is with the square widget in the Tk sources (too long to include here). That demonstrates a lot of things about how Tk does event handling, such as postponing of updates until “idle” (i.e., effectively event coalescing) and handling Tk's configuration system. The key things are that you define a widget record that has all the configurable fields and anything else you need to make the widget work in it (see the Square typedef), a list of option specifications that describe what bits are to be configured and what the defaults are (see the optionSpecs static declaration), a constructor command (SquareObjCmd), an instance command (SquareWidgetObjCmd), an event handler (SquareObjEventProc), and a drawing callback (SquareDisplay). There's other bits too, like how to delete things and so on, but I'm sure you'll get the hang of those. Then all you have to do is register the constructor command as normal for Tcl commands; it's the usual for any code that extends Tcl/Tk.
Doing it this way is a bit more work, but it will make a much more robust binding between your real code and Tcl. It's up to you to decide how much complexity you put in the instance command; Tk's own widgets vary from the very simple (the messagebox widget is only slightly more complicated than the square example widget) to the hugely complicated (both the canvas and the text are really complicated pieces of code).

AS3 Starling custom shader crash "Error #3600: No valid program set" when computer is locked or put to sleep

I'm working on a Flash game using Starling, and I have started coming across an issue where my render crashes immediately after I put my PC to sleep/lock it and then turn it back on. The error I'm getting is:
Error #3600: No valid program set
I was not able to find any advice online on how to prevent this/re-initialize everything appropriately. I'm assuming this is part of a wider issue with how Flash/Starling handles the computer being put to sleep/locked.
Things I have tried so far:
Catching the error and re-uploading the shader programs.
Setting Starling.handleLostContext to true
Has anyone come across this issue before? Any help/pointers would be greatly appreciated.
It sounds like your game is losing the GPU context.
On context loss, all GPU data is lost. You have to restore textures, vertex buffers, etc. Starling handles some of it for you when you set handleLostContext to true, but you still have to handle the textures.
If you use the AssetManager, it will handle re-uploading textures for you, which is clearly the easiest way to go. It automatically creates a texture.root.onRestore callback function which Starling calls after it re-creates the context, and it will attempt to re-load your textures from wherever you first loaded them from, whether from disk, an embedded asset, or a URL.
You can also manually define the function yourself, though that is more complicated and as the article mentions, some gotchas are involved.
More information: http://wiki.starling-framework.org/manual/context_loss

StackOverflow Errors caused by flood fill: How to stop the error?

For my level editor in my game, I have a floodfill function. The map has a size of 3600 tiles (60 by 60), and I get a stack overflow error from my floodfill function (as it calls itself).
If I only floodfill a smaller radius, it works fine. How do I stop the error from occuring?
Alternatively, is there a way to tell the flash runtime to clear the stack as there is no need to return back to the function?
You need to make a flood fill function that doesn't rely on recursion because Flash's stack is quite limited. You can find some non-recursive ways on Wikipedia: http://en.wikipedia.org/wiki/Flood_fill#Alternative_implementations
I'm guessing you might be using a simple recursive implementation of flood-fill; that algorithm can create deep stacks. It's possible you could implement one of the more efficient queue based algorithms and save the stack, however I would suggest using the already built-in flood-fill capabilities of the BitmapData object.
BitmapData is provided by the Flash Player and has a pretty fast and stack friendly flood-fill implementation.
I was playing with flood-fill algorithms a while back and ended up using the built-in APIs mainly because of the speed advantage -- as well it's compiled C code vs ActionScript.
Here are the docs:
flash.display.BitmapData floodFill()

how to create applications with Clozure Common Lisp (on Microsoft Windows)

I am a new one to Common Lisp (using Clozure Common Lisp under Microsoft Windows), who is familiar with c and python before. So maybe the questions are stupid here, but be patient to give me some help.
1) What's is the usual way to run a common lisp script?
Now, I wrote a bat file under windows to call ccl exe(wx86cl.exe) and evaluate (progn (load "my_script_full_path") (ccl:quit)) every time when I want to "run" my script. Is this a standard way to "run" a script for common lisp?
Any other suggestion about this?
2) What's the difference between (require 'cxml) and (asdf:operate 'asdf:load-op :cxml)?
They are seems to be the same for my script, which one should I use?
3) ignore it, not a clear question
4) When I want to load some library (such as require 'cxml), it always takes time(3s or even 5s) to load cxml every time when I "run" my script, there is also much log to standard output I show below, it seems like checking something internal. Does it means I have to spent 3-5s to load cxml every time when I want to run a simple test? It seems like a little inefficient and the output is noisy. Any suggestion?
My Script
(require 'cxml) (some-code-using-cxml)
And the output
; Loading system definition from D:/_play_/lispbox-0.7/quicklisp/dists/quicklisp/software/cxml-20101107-git/cxml.asd into #<Package "ASDF0">
;;; Checking for wide character support... yes, using code points.
; Registering #<SYSTEM "cxml-xml">
......
some my script output
---EDIT TO ADD MORE----
5) I must say that I almost forget the way of dumping image to accelerate the loading speed of lisp library. So, what is the normal process for us to develop a (maybe very simple) lisp script?
Base on the answer of what I got now, I guess maybe
a) edit your script
b) test it via a REPL environment, SLIME is a really good choice, and there should be many loop between a <==> b
c) dump the image to distribute it?( I am no sure about this)
6) Furthermore, what is the common way/form for us to release/distribute the final program?
For a lisp library, we just release our source code, and let someone else can "load/require" them.
For a lisp program, we dump a image to distribute it when we confirm that all functions go well.
Am I right?
What form do we use in a real product? Do we always dump all the thing into a image at final to speed up the loading speed?
1) Yes, the normal way to run a whole programme is to use a launcher script. However, windows has much, much better scripting support these days than just the bat interpreter. Windows Scripting Host and PowerShell ship as standard.
1a) During development, it is usual to simply type things in a the REPL (Read-Eval-Print-Loop, i.e. the lisp command line), or to use something like SLIME (for emacs or xemacs) as a development environment. If you don't know what they are, look them up. You may wish to use Cygwin to install xemacs, which will give you access to a range of linux-ish tools.
2) Require is, IIRC, a part of the standard. ASDF is technically not, it is a library that operates to make libraries work more conveniently. ASDF has a bunch of features that you will eventually want if you really get into writing large Lisp programmes.
3) Question unclear, pass.
4) See 1a) - do your tests and modifications in a running instance, thus avoiding the need to load the library more than once (just as you would in Python - you found the python repl, right?). In addition, when your programme is complete, you can probably dump an image which has all of your libraries pre-loaded.
Edit: additional answers:
5) Yes
6) Once you have dumped the image, you will still need to distribute the lisp binary to load the memory image. To make this transparent to the user, you will also have to have a loader script (or binary) to run the lisp binary with the image.
You don't have to start the lisp from scratch and load everything over again each time you want to run a simple test. For more efficient development, interactively evaluate code in the listener (REPL) of a running lisp environment.
For distribution, I use Zachary Beane's Buildapp tool. Very easy to install and use.
Regarding distribution -
I wrote a routine (it's at home and unavailable at the moment) that will write out the current image as a standard executable and quit. It works for both CLISP and SBCL.
I can rummage it up if you like.

Understanding run time code interpretation and execution

I'm creating a game in XNA and was thinking of creating my own scripting language (extremely simple mind you). I know there's better ways to go about this (and that I'm reinventing the wheel), but I want the learning experience more than to be productive and fast.
When confronted with code at run time, from what I understand, the usual approach is to parse into a machine code or byte code or something else that is actually executable and then execute that, right? But, for instance, when Chrome first came out they said their JavaScript engine was fast because it compiles the JavaScript into machine code. This implies other engines weren't compiling into machine code.
I'd prefer not compiling to a lower language, so are there any known modern techniques for parsing and executing code without compiling to low level? Perhaps something like parsing the code into some sort of tree, branching through the tree, and comparing each symbol and calling some function that handles that symbol? (Wild guessing and stabbing in the dark)
I personally wouldn't roll your own parser ( turning the input into tokens ) or lexer ( checking the input tokens for your language grammar ). Take a look at ANTLR for parsing/lexing - it's a great framework and has full source code if you want to dig into the guts of it.
For executing code that you've parsed, I'd look at running a simple virtual machine or even better look at llvm which is an open-source(ish) attempt to standardise the virtual machine byte code format and provide nice features like JITing ( turning your script compiled byte code into assembly ).
I wouldn't discourage you from the more advanced options that you machine such as native machine code execution but bear in mind that this is a very specialist area and gets real complex, real fast!
Earlz pointed out that my reply might seem to imply 'don't bother doing this yourself. Re-reading my post it does sound a bit that way. The reason I mentioned ANTLR and LLVM is they both have heaps of source code and tutorials so I feel this is a good reference source. Take it as a base and play
You can try this framework for building languages (it works well with XNA):
http://www.meta-alternative.net/mbase.html
There are some tutorials:
http://www.meta-alternative.net/calc.pdf
http://www.meta-alternative.net/pfront.pdf
Python is great as a scripting language. I would recommend you make a C# binding for its C API and use that. Embedding Python is easy. Your application can define functions, types/classes and variables inside modules which the Python interpreter can access. The application can also call functions in Python scripts and get a result back. These two features combined gives you a two-way communication scheme.
Basically, you get the Python syntax and semantics for free. What you would need to implement is the API your application exposes to Python. An example could be access to game logic functions and render functions. Python scripts would then define functions which calls these, and the host application would invoke the Python functions (with parameters) to get work done.
EDIT: Seems like IronPython can save you even more work. It's a C# implementation of CPython, and has its own embedding API: http://www.ironpython.net/