Writing Samples from Books

I find that I tend to repeat patterns over and over, through years. I believe I do this because I’m improving things, or perhaps it’s because I forget how to do something, and I’m revisiting the topic again.

Here’s a picture:

tricolor

Here’s the code that made it:

 

package.path = package.path..";../?.lua"
local View3D = require("View3D");

function init()
  gl.glClearColor(0,0,0,0);
  gl.glShadeModel(GL_SMOOTH);
end

function triangle()
  gl.glBegin(GL_TRIANGLES);
  gl.glColor3f(1,0,0);
  gl.glVertex2f(5,5);
  gl.glColor3f(0,1,0);
  gl.glVertex2f(25,5);
  gl.glColor3f(0,0,1);
  gl.glVertex2f(5,25);
  gl.glEnd();
end

function display()
  gl.glClear(GL_COLOR_BUFFER_BIT);
  triangle();
  gl.glFlush();
end

function reshape(w, h)
  gl.glViewport(0,0,w,h);
  gl.glMatrixMode(GL_PROJECTION);
  gl.glLoadIdentity();

  if (w <= h) then
    glu.gluOrtho2D(0,30,0,30*h/w);
  else
    glu.gluOrtho2D(0,30*w/h, 0, 30);
  end

  gl.glMatrixMode(GL_MODELVIEW);
end

run(View3D.main);

I got this code from the “OpenGL Programming Guide – Sixth Edition” (the Red Book), page 180. There are a couple of changes. I prefix things with ‘gl’ and ‘glu’. Other than that, the two lines at the top, and the one line at the bottom are all that I need to run this in the TINN environment.

TINN is not the only Lua based environment that allows you to easily script OpenGL. Since Lua is almost synonymous with game programming, I’m sure there are tens of such things. This is my version.

What I like about this is the relative ease with which I can just copy a simple sample out of a book, or magazine, or online source, and try it out. Often times, you need to use a lot more machinery, run a compiler, blah blah blah. Even worse, the sample might have been written to run on a Mac, so there might be a lot of specifics for that.

I can do this because I have added a khronos module to my TINN installation. One of the key pieces is the View3D class, which sets up an automatic window, event loop, opengl context and the like. If you’re familiar with GLFW, it’s a similar capability, only with scripting.

One difference between TINN and these other environments, is that TINN integrates that low level networking stuff as well. So, hooking up a http server/client interface to my OpenGL based ramblings will be fairly straight forward. I should be able to create collaborative applications, for example, without too much fuss, it’s just a built in feature of the environment.

I like having this tool at hand. The combination of a robust runtime environment, combined with scripting, makes it real easy to just pull books off my shelf and try things out. I’m repeating an exercise for I don’t know how many times, but I’m really enjoying the results this time around.

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s