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 1915 - (download) (as text) (annotate)
Wed Jun 6 13:33:54 2012 UTC (6 years, 11 months ago) by jhr
File size: 5987 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>
#include "GL/glfw.h"
#ifdef DIDEROT_CODE
#include "vr-lite-cam.h"
#endif
#include "util.h"
#include "load-png.h"

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

static inline void CheckError ()
{
    GLenum errCode;
    if ((errCode = glGetError()) != GL_NO_ERROR) {
	fprintf(stderr, "OpenGL error: %s\n", gluErrorString(errCode));
	exit (1);
    }
}

/***** 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

typedef struct {
    TwBar	*ctls;
    unsigned int running;
} State_t;

// Callback function called by GLUT to render screen
void Draw (Nrrd *positions)
{
  // 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
    float	halfWid = 0.5 * (float)Width;
    float	halfHt = 0.5 * (float)Height;
    glEnable (GL_TEXTURE_2D);
    glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    glBindTexture (GL_TEXTURE_2D, ImageTexture);
    glBegin (GL_QUADS);
	glColor3f (0.1, 0.1, 0.1);
	glTexCoord2f (0.0, 0.0);
	glVertex2f (-halfWid, -halfHt);
	glTexCoord2f (1.0, 0.0);
	glVertex2f (halfWid, -halfHt);
	glTexCoord2f (1.0, 1.0);
	glVertex2f (halfWid, halfHt);
	glTexCoord2f (0.0, 1.0);
	glVertex2f (-halfWid, halfHt);
    glEnd();
    glDisable (GL_TEXTURE_2D);
    CheckError ();

  // Draw current render state

  // Draw tweak bars
    TwDraw();

  // Present frame buffer
    glfwSwapBuffers();
}

// Callback function called by GLFW when window size changes
void GLFWCALL WindowSizeCB (int width, int height)
{
    double	halfWid = 0.5 * (double)width;
    double	halfHt = 0.5 * (double)height;

  // Set OpenGL viewport and camera
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D (-halfWid, halfWid, -halfHt, halfHt);

    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity ();
    gluLookAt (
	0.0, 0.0, 1.0,
	0.0, 0.0, 0.0,
	0.0, 1.0, 0.0);

    // 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 ()
{
    Image2D_t *img = LoadImage ("data/ddro-tx.png", false, GRAY_IMAGE);
    if (img == 0) {
        fprintf (stderr, "unable to load texture file\n");
        exit (1);
    }

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

  // load the texture data
    glBindTexture (GL_TEXTURE_2D, texId);
    int sts = TexImage (img);
    if (sts != GL_NO_ERROR) {
	fprintf(stderr, "error loading texture\n");
	exit (1);
    }

    FreeImage (img);

    ImageTexture = texId;
}

void TW_CALL StartButtonCB (void *data)
{
    State_t *st = (State_t *)data;

    if (st->running == 0) {
	st->running = 1;
	TwSetParam (st->ctls, "StartButton", "label", TW_PARAM_CSTRING, 1, "Stop");
    }
    else {
	st->running = 0;
	TwSetParam (st->ctls, "StartButton", "label", TW_PARAM_CSTRING, 1, "Start");
    }
}

int main (int argc, const char **argv)
{
    State_t	state = { .running = 0 };

  // Initialize GLFW
    if (0 == glfwInit()) {
      // An error occured
        fprintf(stderr, "GLFW initialization failed\n");
        return 1;
    }

    GLFWvidmode mode;
    glfwGetDesktopMode (&mode);
    if (0 == glfwOpenWindow(WIDTH, HEIGHT, mode.RedBits, mode.GreenBits, mode.BlueBits, 0, 0, 0, GLFW_WINDOW) ) {
      // A fatal error occured    
        fprintf(stderr, "Cannot open GLFW window\n");
        glfwTerminate();
        return 1;
    }
    glfwEnable(GLFW_MOUSE_CURSOR);
    glfwEnable(GLFW_KEY_REPEAT);
    glfwSetWindowTitle("Diderot Iso2D Animation");

  // Initialize AntTweakBar
    TwInit (TW_OPENGL, 0);

  // create a tweakbar
    TwBar *tweakBar = TwNewBar("Controls");
    state.ctls = tweakBar;
    unsigned int NumSteps = 0;
    TwAddVarRO (tweakBar, "NumSteps", TW_TYPE_UINT32, &NumSteps, " label='# of steps' ");
    TwAddButton (tweakBar, "StartButton", StartButtonCB, &state, " label='Start' ");

  // Set GLFW event callbacks
    glfwSetWindowSizeCallback (WindowSizeCB);
    glfwSetMouseButtonCallback ((GLFWmousebuttonfun)TwEventMouseButtonGLFW);  // redirect to AntTweakBar
    glfwSetMousePosCallback ((GLFWmouseposfun)TwEventMousePosGLFW);  // redirect to AntTweakBar
    glfwSetMouseWheelCallback ((GLFWmousewheelfun)TwEventMouseWheelGLFW);  // redirect to AntTweakBar
    glfwSetKeyCallback ((GLFWkeyfun)TwEventKeyGLFW);  // redirect to AntTweakBar
    glfwSetCharCallback ((GLFWcharfun)TwEventCharGLFW);  // redirect to AntTweakBar

  // load Diderot image for background texture
    InitTexture ();

  // Main loop (repeated while window is not closed and [ESC] is not pressed)
    while (glfwGetWindowParam(GLFW_OPENED) && !glfwGetKey(GLFW_KEY_ESC)) {
	Draw(0);
    }

#ifdef DIDEROT_CODE
    ISO_World_t *wrld = ISO_Init ();

    ISO_Initially (wrld);

    Nrrd *nData = nrrdNew();
    do {
      // get snapshot of state
	if (ISO_Sapshot_outRGBA (wrld, nData)) {
	  // error
	    fprintf(stderr, "Error getting nrrd data\n");
	    return 1;
	}
      // render the state
	Draw (nData);
      // step the computation
	uint32_t nSteps = ISO_Run (wrld, 1);
    } while (ISO_NumActive() > 0);

  // get and render final state
    if (ISO_OutputGet_outRGBA (wrld, nData)) {
      // error
	fprintf(stderr, "Error getting nrrd data\n");
	return 1;
    }
    Draw (nData);

    ISO_Shutdown (wrld);
#endif

  // Terminate AntTweakBar and GLFW
    TwTerminate();
    glfwTerminate();

    return 0;
}

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