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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2068 - (download) (as text) (annotate)
Fri Nov 2 13:44:53 2012 UTC (6 years, 10 months ago) by jhr
File size: 4719 byte(s)
  Working on VR demo
/*! \file main.c
 *
 * \author John Reppy
 *
 * Main program for Diderot VR viewer.
 */

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

#include "GL/glfw.h"
#include "util.h"
#include "vr.h"
#include <unistd.h>

/* minimum and maximum values from */
#define MIN_VALUE	0
#define MAX_VALUE	2632

/* half the width of the opacity range */
#define OPAC_HALF_WID	150

#define MIN_OPAC_MID	((float)(MIN_VALUE+OPAC_HALF_WID))
#define MAX_OPAC_MID	((float)(MAX_VALUE-OPAC_HALF_WID))

#define OPAC_DELTA	100

/* pixel dimensions of output */
#define WIDTH   480
#define HEIGHT  345

/* world bounds */
#define WRLD_MIN_X      0.0
#define WRLD_MIN_Y      0.0
#define WRLD_MAX_X      1.0
#define WRLD_MAX_Y      1.0
#define WRLD_CENTER_X   0.5
#define WRLD_CENTER_Y   0.5

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

VR_World_t	*World;
unsigned int    Width;          // view window width
unsigned int    Height;         // view window height
bool		NeedsRedraw;
bool		NeedsRecompute;
float		OpacMid = 550.0;


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

    glDisable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);

  // Draw image
    glDrawPixels (Width, Height, GL_RGBA, GL_FLOAT, img->data);
    CheckError ();

  // Present frame buffer
    glfwSwapBuffers();
}

// Callback function called by GLFW when window size changes
void GLFWCALL WindowSizeCB (int width, int height)
{
  // Set OpenGL viewport and camera
    glViewport(0, 0, width, height);

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

}

void GLFWCALL KeyCB (int key, int action)
{
    if (action == GLFW_PRESS) {
	switch (key) {
	  case '+': {
	        float mid = OpacMid + OPAC_DELTA;
		if (mid <= MAX_OPAC_MID) {
		    OpacMid = mid;
		    VR_InVarSet_valOpacMid(World, mid);
		    NeedsRecompute = true;
		}
	    } break;
	  case '-': {
	        float mid = OpacMid - OPAC_DELTA;
		if (mid >= MIN_OPAC_MID) {
		    OpacMid = mid;
		    VR_InVarSet_valOpacMid(World, mid);
		    NeedsRecompute = true;
		}
	    } break;
	  default:
	    break;
	}
    }
}

void GetData (VR_World_t *wrld, Nrrd *nRGB)
{
    if (VR_OutputGet_outRGBA (wrld, nRGB)) {
      // error
	fprintf(stderr, "Error getting nrrd data\n");
	exit(1);
    }
}

int main (int argc, const char **argv)
{
  // 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_KEY_REPEAT);
    glfwSetWindowTitle("Diderot VR Demo");

  // Set GLFW event callbacks
    glfwSetWindowSizeCallback (WindowSizeCB);
    glfwSetCharCallback (KeyCB);

  // initialize the Diderot program
    VR_World_t *wrld = VR_Init ();
    World = wrld;

  // initialize inputs
    VR_InVarSet_valOpacMid(wrld, OpacMid);
    Nrrd *nin = nrrdNew();
  /* read in the nrrd from file */
    if (nrrdLoad(nin, "data/vfrhand-nohip.nhdr", NULL) != 0) {
	char *msg = biffGetDone(NRRD);
	fprintf (stderr, "Error loading data: %s", msg);
        exit (1);
    }
    VR_InVarSet_hand (wrld, nin);
//    nrrdNix (nin);

  // nrrd for getting computational state
    Nrrd *nRGB = nrrdNew();

  // Main loop (repeated while window is not closed and [ESC] is not pressed)
    NeedsRecompute = true;
    while (glfwGetWindowParam(GLFW_OPENED) && !glfwGetKey(GLFW_KEY_ESC)) {
	if (NeedsRecompute) {
printf("computing ... "); fflush(stdout);
	  // recompute
	    if (VR_Initially (wrld)) {
	      // error
		fprintf(stderr, "Error initializing world: %s", VR_GetErrors(wrld));
		exit(1);
	    }
	    int nSteps = VR_Run (wrld, 0);
	    printf(" %d steps\n", nSteps);
	  // get output image
	    if (VR_OutputGet_outRGBA (wrld, nRGB)) {
	      // error
		fprintf(stderr, "Error getting nrrd data: %s", VR_GetErrors(wrld));
		exit(1);
	    }
	    NeedsRecompute = false;
	    NeedsRedraw = true;
	}
	if (NeedsRedraw) {
	    Draw (nRGB);
	    NeedsRedraw = false;
	}
	glfwWaitEvents();
    }

  // shutdown the world
    VR_Shutdown (wrld);

  // Terminate GLFW
    glfwTerminate();

    return 0;
}

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