SCM Repository
[diderot] / examples / iso2d-demo / main.c |
View of /examples/iso2d-demo/main.c
Parent Directory
|
Revision Log
Revision 1882 -
(download)
(as text)
(annotate)
Sat May 19 11:45:04 2012 UTC (10 years, 1 month ago) by jhr
File size: 6128 byte(s)
Sat May 19 11:45:04 2012 UTC (10 years, 1 month ago) by jhr
File size: 6128 byte(s)
working on 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 "teem/nrrd.h" #include "teem/biff.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 typedef struct { TwBar *ctls; unsigned int running; } State_t; // Callback function called by GLUT to render screen void Draw () { // 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; glBindTexture (GL_TEXTURE_2D, ImageTexture); glBegin (GL_QUADS); 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(); // 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 (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; } 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 as nrrd file 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; } InitTexture (Diderot); // Main loop (repeated while window is not closed and [ESC] is not pressed) while (glfwGetWindowParam(GLFW_OPENED) && !glfwGetKey(GLFW_KEY_ESC)) { Draw(); } #ifdef DIDEROT_CODE 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); #endif // Terminate AntTweakBar and GLFW TwTerminate(); glfwTerminate(); return 0; }
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |