OpenGL: Walk Around Camera

November 1, 2009 Matteo Bertozzi | Filed Under OpenGL | No Comments

Again, another week spent on OpenGL. I don’t know why but I cannot stop to experiment with Meshes, Vertexes, Faces and Textures. It’s too amusing! But Todays, post it’s just an update of GLData, the small gl data library that I use for my experiments.

OpenGL Walk Around

The new features, for this update are: Speed-up in GL throughput, and Camera. I’ve adjusted data structure to use directly glVertexPointer() and related gl*Pointer() functions. The main.c file was adjusted to handle direction keys (Up, Down, Left, Right) to allows you to walk in your 3d world, like any First-Person shooter video game.

You can download the GLData Walk Around Source Code it. (Mesh not Included)

PS: For my Qt followers, and for those which they are waiting for my File-System/Distributed Computational System: I haven’t forget Qt and the lovely memset(); I’m just exploring a bit (give me some week), a world that I’ve completely ignored.

OpenGL: Meshes and PNG Textures

October 25, 2009 Matteo Bertozzi | Filed Under OpenGL | No Comments

Still experimenting with OpenGL, I’ve played just few games in my life Formula 1 and Tomb Raider. The second one, is more interesting to “reproduce” to learn something of OpenGL, meshes and how all of this world works. Following the latest posts, I’ve added the PNG support to the GLData Sample, so you can now load BMP and PNG Textures.

OpenGL Meshes

The Source Code is available here: GL Data Source Code.

OpenGL meets Blender: Meshes and Textures

October 17, 2009 Matteo Bertozzi | Filed Under OpenGL | No Comments

As you can see from the bigger Screenshot below, Today is time to put some characters on the OpenGL stage. And the two guys below are two of the characters of Yo Frankie! (http://www.yofrankie.org/). Following the preview post I’ve extended my GLData Library adding support for Textures and using another source format as Input File, I’ve also made a simple Blender-Export Script that allows you to easily export blender meshes with textures, vertexes and faces.

OpenGL Blender Yo Frankie Chars

If you are, as me, fan of black and white command lines, The result is stunning. And there’re just few lines of code. The code can be downloaded from here: GLData Blender Source Code. It contains the source code, textures, meshes and the Blender Export Script.

OpenGL: Gts Format, Lights and Cameras

October 11, 2009 Matteo Bertozzi | Filed Under OpenGL | No Comments

During my work, I’ve found an interesting library GNU Triangulated Surface Library (http://gts.sourceforge.net/) that does, some nice things, like Constrained Delaunay triangulations. But what I was really searching today are just meshes to use in my OpenGL experiments. And there’re a some GTS samples available on the GTS website.

GlData Gts Sample

The screenshot above represents a GTS sample shape with a simple light effect. Just few lines of code to do it. But what I’m interested in, is create a simple way to load GTS file format, and below you can see the code.

void drawObject (const char *gts_shape_filename) {
    GLDataGts *gts;

    gts = glDataGtsAlloc();
    if (glDataGtsRead(gts, gts_shape_filename)) {
        GLDataSurface *surface;
        GLDataUInt i, count;
        GLDataTriangle *t;

        /* The Surface is an array of Triangles */
        surface = glDataGtsSurface(gts, NULL);
        count = glDataSurfaceCount(surface);
        for (i = 0; i < count; ++i) {
            t = glDataSurfaceGet(surface, i);

            glBegin(GL_LINE_LOOP);
            glVertex3f(t->p1->x, t->p1->y, t->p1->z);
            glVertex3f(t->p2->x, t->p2->y, t->p2->z);
            glVertex3f(t->p3->x, t->p3->y, t->p3->z);
            glEnd();
        }
    }

    glDataGtsRelease(gts);
}


The Source code is available here GLDataGts Source Code, and main.c contains a few comment lines that explain how to compile and run. Check the keyboardHandler() function to learn how to interact with the OpenGL camera and lights.

OpenCL is for Everyone!

October 8, 2009 Matteo Bertozzi | Filed Under OpenCL | No Comments

OpenCLLast night I had a terrible nightmare, where all the apple/mac bloggers says “This app cannot be use used with older macs because it uses OpenCL, so you need the newest NVidia…”

..Fortunately, was just a nightmare. Everyone can use OpenCL, old macs (as my macbook) cannot use GPU, and cannot run faster, but OpenCL code can run on every machine.

You (as developer) need just to keep in mind to get Device in this way. So, if you haven’t the latest NVidia you can still use, as always, CPU.

err = clGetDeviceIDs(NULL, CL_DEVICE_TYPE_GPU, 1, &device_id, NULL);
if (err != CL_SUCCESS)
    err = clGetDeviceIDs(NULL, CL_DEVICE_TYPE_CPU, 1, &device_id, NULL);

[TIP] XCode Header Search Path

September 26, 2009 Matteo Bertozzi | Filed Under Tips, XCode | 1 Comment

For many of you, this sounds like a stupid thing. But for those that just use gcc -I from command line, can be a pain find how to do it.

So, the problem is. How can I specify my Include path in XCode (gcc -I./mypath).

XCode Search Header Path

Tap on your project target, and click “Get Info“, tap on “Build” tab and search “Path” as showed in the figure above. Then click on the “Header Search Paths” options and add your favorite include paths for selected target.

File-System: Delayed Allocation, fsync() solution

September 19, 2009 Matteo Bertozzi | Filed Under Unix C | No Comments

Last week on LWN Valerie Aurora as posted a great article (as always) POSIX v. reality: A position on O_PONIES. http://lwn.net/Articles/351422/.

fsync() is often more expensive than it absolutely needs to be. The easiest way to implement it is to force out every outstanding write to the file system, regardless of whether it is a journaling file system, a COW file system, or a file system with no crash recovery mechanism whatsoever. This is because it is very difficult to map backward from a given file to the dirty file system blocks needing to be written to disk in order to create a consistent file system containing those changes. For example, the block containing the bitmap for newly allocated file data blocks may also have been changed by a later allocation for a different file, which then requires that we also write out the indirect blocks pointing to the data for that second file, which changes another bitmap block… When you solve the problem of tracing specific dependencies of any particular write, you end up with the complexity of soft updates. No surprise then, that most file systems take the brute force approach, with the result that fsync() commonly takes time proportional to all outstanding writes to the file system.

Thinking for a while… Is not hard to implement fsync(), to flush just one file using Delayed Allocation (Allocate on Flush). We’ve all new data in memory, and old data stay on its block. So, modification in place means that you’ve just to flush blocks. Append means that you need to allocate something.
RaleighFS in Memory StructureThe image above, is a little bit old, but it’s the original idea of the RaleighFS in Memory Structure. There’re general information like super-block, bad blocks list and free blocks lis, current cache size and some other things. But today I’m focusing on Block Cache and Write Items Cache.

When you open a file, you load its metadata in memory then when you need file content you load it in the Block Cache. RaleighFS data block contains the File Key, so you can easily find blocks with specified key, but also you can easily find your file blocks using pointers.

So, why is easy to fsync() only the specified file with Delayed Allocation:

  • Modification in place, requires just a scan of the block cache to find what blocks are to flush (and obviously metadata)
  • Append to file, has all new data in memory and there’re no Modification to B*Tree(s) or Free blocks until you flush something.
  • Remove is just a command, and when fsync() is called all the delete operation on B*Tree(s) and list will take places.

But remember, syncing just one file is not a good idea, Trust your File-System’s flush policy!

Grand Central Dispatch: First Look

September 6, 2009 Matteo Bertozzi | Filed Under Apple | No Comments

Mac OS X Grand Central DispatchIn the last years I’ve always used a “parallel task” approach foreach loops that I’ve in the code, not always to speedup but even to clean-up the code. How to do it? Wrapping threads and Thread Pool like in this C# Parallel Forech Code.

Snow Leopard has introduced a new BSD-level infrastructure, with simple and efficient API to do this job. Here a little usage preview.

Block objects are a C-based language feature that you can use in your C, Objective-C, and C++ code. Blocks make it easy to define a self-contained unit of work. Blocks are something like Actions (delegate {}) in C#. Very useful to embed function in loops.

Blocks looks like a “private” function pointer, but you can access to the “parent” vars. (If you’re a Python coder, you’ve exactly the same thing).

/* Blocks in Python...
 * def main():
 *    a = 10
 *    def test(k):
 *        print a, k
 *    test(128)
 */
int main (int argc, const char *argv[]) {
  int a = 12;

  void (^test_block) (int) = ^(int k) {
    printf("A Block: PARENT(%d) ARG(%d)\n", a, k);
  };

  test_block(128);

  return 0;
}

The GCD queue API provides dispatch queues from which threads take tasks to be executed. Because the threads are managed by GCD, Mac OS X can optimize the number of threads based upon available memory, number of currently active CPU cores, and so on. This shifts a great deal of the burden of power and resource management to the operating system itself, freeing your application to focus on the actual work to be accomplished.

#include <dispatch/dispatch.h>
#include <stdlib.h>
#include <stdio.h>

#define ITEM_VMIN       (1)
#define ITEM_VMAX       (200)
#define NR_ITEMS        (100)

static void __fill_item (void *items, size_t n) {
  int *i_items = (int *)items;
  i_items[n] = (ITEM_VMIN + (int)(ITEM_VMAX * ((double)rand() / RAND_MAX)));
}

static void __work_on_item (void *items, size_t n) {
  int *i_items = (int *)items;
  i_items[n] *= 100;  /* Do some Computation on this Item */
}

int main (int argc, const char *argv[]) {
  dispatch_queue_t queue;
  int data[NR_ITEMS];

  /* Get Global Dispatch Queue */
  queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

  /* Initialize data Elements, and run computation on each element */
  dispatch_apply_f(NR_ITEMS, queue, data, __fill_item);
  dispatch_apply_f(NR_ITEMS, queue, data, __work_on_item);

  /* Brief review of the items */
  dispatch_apply(NR_ITEMS, queue, ^(size_t n) {
    printf("Results: Item %lu = %d\n", n, data[n]);
  });

  return 0;
}

Next Page »