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 2063 - (download) (as text) (annotate)
Wed Oct 31 18:36:09 2012 UTC (6 years, 7 months ago) by jhr
File size: 4199 byte(s)
  woking 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))

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

#define STEP_SIZE       1
#define SLEEP_MS        250

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
bool		NeedsRedraw;
bool		NeedsRecompute;



// 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)
{
printf("window size = %d x %d\n", width, height);
  // Set OpenGL viewport and camera
    glViewport(0, 0, width, height);

    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    double pad = 0.05*(WRLD_MAX_X - WRLD_MIN_X);  // add 10% to world dimensions
    gluOrtho2D (
        WRLD_MIN_X - pad, WRLD_MAX_X + pad,
        WRLD_MAX_Y + pad, WRLD_MIN_Y - pad);

    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity ();
    gluLookAt (
//	WRLD_CENTER_X, WRLD_CENTER_Y, 1.0,
        0.0, 0.0, 1.0,
//	WRLD_CENTER_X, WRLD_CENTER_Y, 0.0,
        0.0, 0.0, 0.0,
	0.0, 1.0, 0.0);

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

}

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);
/* FIXME: set up key callback */

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

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

  // get and render initial state
    GetData (wrld, nRGB);
    Draw (nRGB);

  // 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) {
	  // recompute
	    VR_Initially (wrld);
	    VR_Run (wrld, 0);
	  // get output image
	    if (VR_OutputGet_outRGBA (wrld, nRGB)) {
	      // error
		fprintf(stderr, "Error getting nrrd data\n");
		exit(1);
	    }
	    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