Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /examples/iso2d-demo/main.c
ViewVC logotype

View of /examples/iso2d-demo/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1880 - (download) (as text) (annotate)
Fri May 18 21:49:16 2012 UTC (7 years, 1 month ago) by jhr
File size: 4658 byte(s)
  Working on iso2d-demo
/*! \file main.c
 *
 * \author John Reppy
 *
 * Main program for Diderot ISO2d animation.
 */

/*
 * COPYRIGHT (c) 2012 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 */

#include <AntTweakBar.h>
#if defined(__APPLE__)
#   include <GLUT/glut.h>
#else
#   include <GL/glut.h>
#endif
#include "vr-lite-cam.h"

/* pixel dimensions of output */
#define WIDTH   512
#define HEIGHT  512

/***** Globals for viewing, etc. *****/

Nrrd            *Diderot;       // input image nrrd
unsigned int    Width;          // view window width
unsigned int    Height;         // view window height
GLuint          ImageTexture;   // Texture ID of background image

// Callback function called by GLUT to render screen
void Display(void)
{
  // Clear frame buffer
    glClearColor (0, 0, 0, 1);
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glEnable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);

  // Draw background

  // Draw current render state

  // Draw tweak bars
    TwDraw();

  // Present frame buffer
    glutSwapBuffers();
}

// Callback function called by GLUT when window size changes
void Reshape (int width, int height)
{
  // Set OpenGL viewport and camera
    glViewport (0, 0, width, height);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    /* FIXME */
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity ();
    /* FIXME */

  // Send the new window size to AntTweakBar
    TwWindowSize (width, height);

  // remember width and height
    Width = width;
    Height = height;

}

// initialize a texture from a grayscale Nrrd file
void InitTexture (Nrrd *nin)
{
    if ((nin->dim != 2)
    ||  (nin->axis[0].kind != nrrdKindSpace)
    ||  (nin->axis[1].kind != nrrdKindSpace)) {
        fprintf (stderr, "unable to handle nrrd file\n");
        exit (1);
    }

  // determine the pixel format for the texture
    GLenum type;
    switch (nin->type) {
      case nrrdTypeUChar: type = GL_UNSIGNED_BYTE; break;
      case nrrdTypeFloat: type = GL_FLOAT; break;
      default:
        fprintf (stderr, "unable to handle nrdd file type %d\n", nin->type);
        exit (1);
    }

  // generate the TeXID
    GLuint texId;
    glGenTextures (1, &texId);

  // load the texture data
    glBindTexture (GL_TEXTURE_2D, texId);
    glTexImage2D (GL_TEXTURE_2D,  /* or GL_TEXTURE_RECTANGLE ? */
        0,
        GL_RED,
        nin->axis[0]->size, nin->axis[1]->size, 0,
        GL_RED, type,
        nin->data);
/* check for error */

    ImageTexture = texId;
}

int main (int argc, const char **argv)
{
  // Initialize GLUT
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize (WIDTH, HEIGHT);
    glutCreateWindow ("Diderot Iso2D Animation");
    glutCreateMenu (0);

  // Set GLUT callbacks
    glutDisplayFunc (Display);
    glutReshapeFunc (Reshape);
    atexit (TwTerminate);  // Called after glutMainLoop ends

  // Initialize AntTweakBar
    TwInit (TW_OPENGL, 0);

  // Set GLUT event callbacks
  // - Directly redirect GLUT mouse button events to AntTweakBar
    glutMouseFunc ((GLUTmousebuttonfun)TwEventMouseButtonGLUT);
  // - Directly redirect GLUT mouse motion events to AntTweakBar
    glutMotionFunc ((GLUTmousemotionfun)TwEventMouseMotionGLUT);
  // - Directly redirect GLUT mouse "passive" motion events to AntTweakBar (same as MouseMotion)
    glutPassiveMotionFunc ((GLUTmousemotionfun)TwEventMouseMotionGLUT);
  // - Directly redirect GLUT key events to AntTweakBar
    glutKeyboardFunc ((GLUTkeyboardfun)TwEventKeyboardGLUT);
  // - Directly redirect GLUT special key events to AntTweakBar
    glutSpecialFunc ((GLUTspecialfun)TwEventSpecialGLUT);
  // - Send 'glutGetModifers' function pointer to AntTweakBar;
  //   required because the GLUT key event functions do not report key modifiers states.
    TwGLUTModifiersFunc (glutGetModifiers);

  // load Diderot image as nrrd file
    Nrrd *Diderot = nrrdNew();
    if (nrrdLoad(diderot, "data/ddro-80.nrrd", 0) != 0) {
        char *msg = biffGetDone(NRRD);
        fprintf (stderr, "Error loading nrrd file: %s", msg);
	free (msg);
        return 0;
    }

    ISO_World_t *wrld = ISO_Init ();

    ISO_Initially (wrld);

    uint32_t nSteps = ISO_Run (wrld, 0);

  // get results
    Nrrd *nData = nrrdNew();
    if (ISO_OutputGet_outRGBA (wrld, nData)) {
      // error
	fprintf(stderr, "Error getting nrrd data\n");
	return 1;
    }

    if (nrrdSave("out.nrrd", nData, NULL)) {
        char *err = biffGetDone(NRRD);
        fprintf(stderr, "Trouble saving nrrd struct: %s\n", err);
        return 1;
    }

    ISO_Shutdown (wrld);

    return 0;
}

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0