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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3013 - (download) (as text) (annotate)
Mon Mar 9 13:06:51 2015 UTC (4 years, 3 months ago) by lamonts
File size: 5857 byte(s)
Made slight changes to iso2d.diderot, main.c
 #define GL_GLEXT_PROTOTYPES
#define GLFW_INCLUDE_GLCOREARB
#include <GLFW/glfw3.h>
#if defined(__APPLE_CC__)
#include <OpenGL/glext.h>
#else
#  include <GL/glext.h>
#endif
#include "utils/utils.h"
#include "utils/linmath.h"
#include "view.h"
#include "utils/circle.h"
#include "iso2d.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h> 
#include "teem/nrrd.h"

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


View * view = NULL;  

typedef enum {
  RED = 0, 
  BLUE = 1 
}Colors; 

typedef struct {
  ISO_World_t * wrld; 
  Nrrd * snapData;
  Circle * particles;

}DiderotProg_t; 

DiderotProg_t dProg; 

static void getData (ISO_World_t *wrld, Nrrd *nData); 
void display (GLFWwindow *win); 

/* Set the viewport to the current size of the framebufffer.
 * We use the framebuffer size instead of the window size, because they
 * are different on Apple's retina displays.
 */
static void setViewPort (GLFWwindow *window)
{
    int fbWid, fbHt;
    glfwGetFramebufferSize (window, &fbWid, &fbHt);
    glViewport(0, 0 , fbWid, fbHt);
}

/** Initializes the diderot program */ 
static void diderot_init(GLFWwindow *window)
{
    dProg.wrld = ISO_Init();
    dProg.snapData = nrrdNew();
    if(ISO_Initially (dProg.wrld)) {
       fprintf(stderr,"Error initializing the world\n");
       exit(EXIT_FAILURE); 
    }

    unsigned int nParticles =  ISO_NumStrands (dProg.wrld);
    float r = 0.0035; 
   // ISO_InVarGet_rr(dProg.wrld, &r);  
    vec4 color = {1.0f, 1.0f,0.0f,1.0f};
    dProg.particles = circle_create(nParticles,r,color); 

    getData (dProg.wrld, dProg.snapData);
    display(window); 
} 
static void getData (ISO_World_t *wrld, Nrrd *nData)
{
  // get snapshot of state
    if (ISO_Snapshot_pos(wrld, nData)) {
      // error
        fprintf(stderr, "Error getting nrrd data\n");
        exit(1);
    }
    if(dProg.particles != NULL) {
    float *particles = (float *)(nData->data);
    unsigned int nParticles = ISO_NumStrands (wrld);
    mat4x4 trans; 
    mat4x4 final; 
    for(int i = 0; i < nParticles; i++) 
    {
        int idx = i * 2; 
        float x = particles[idx];
        float y = particles[idx + 1] ;
        mat4x4_identity(final);
        mat4x4_identity(trans); 
        mat4x4_identity(dProg.particles->models[i]);         
        mat4x4_translate(trans, x, y,  1.0);
        mat4x4_mul(dProg.particles->models[i],trans, final);
    }
   }
}
/***** Callback functions *****/
static void error (int err, const char *msg)
{
    fprintf(stderr,"[GLFW ERROR(%d): %s\n", err,msg); 
}

void display (GLFWwindow *win)
{
    //RENDER
    view_render(dProg.particles,view); 
    glfwSwapBuffers (win);

} /* end of Display */
void step(GLFWwindow *win) {
  static bool first = true;  
    if(ISO_NumActive(dProg.wrld) > 0){
        // step the computation
        ISO_Run(dProg.wrld, STEP_SIZE);
        // get and render the state
        getData (dProg.wrld, dProg.snapData); 
    }else {
        if(first){
          printf("Finished Running\n"); 
          first = false; 
        }
    }
}

void reshape (GLFWwindow *win, int wid, int ht)
{

  /* set the viewport */
    setViewPort(win);

  /* recompute the projection matrix */
   // view->initProjMatrix ();

} /* end of Reshape */
static void key_callback(GLFWwindow* win, int key, int scancode, int action, int mods)
{
    if (key == GLFW_KEY_RIGHT && action == GLFW_PRESS)
    {
        step(win);
        display(win);  
    }
}

void key (GLFWwindow *win, unsigned int key)
{
    switch(key){
      case GLFW_KEY_RIGHT: 
      {
        printf("<1>\n"); 
        step(win);
        display(win);  
      }
        //view->showBlendedCube = !view->showBlendedCube;
      break; 
    }

} /* end of Key */

/*! \brief the main function for the program
 *  \param argc the count of arguments in \a argv
 *  \param argv the array of command-line arguments.
 */
int main(int argc, const char **argv)
{
    glfwSetErrorCallback (error);

    glfwInit ();

    view = view_createView(WIDTH,HEIGHT); 

    dProg.particles = NULL; 

  // Check the GLFW version
    {
      int major;
      glfwGetVersion (&major, NULL, NULL);
      if (major < 3) {
        fprintf(stderr,"the is2d demo requires GLFW 3.0 or later\n\n");
        exit (EXIT_FAILURE);
      }
    }

    glfwWindowHint (GLFW_RESIZABLE, GL_TRUE);
    glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 1);
    glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

    GLFWwindow *window = glfwCreateWindow(view->width, view->height, "iso2d Using Spatial Communication", NULL, NULL);
    if (window == NULL)
      exit (EXIT_FAILURE);

    glfwMakeContextCurrent (window);

    setViewPort(window);

  // Check the OpenGL version
    {
      GLint major, minor;
      glGetIntegerv (GL_MAJOR_VERSION, &major);
      glGetIntegerv (GL_MINOR_VERSION, &minor);
      if ((major < 3) || ((major == 3) && (minor < 2))) {
        fprintf(stderr,"the iso2d demo requires OpenGL 3.2 or later; found %d.%d\n", major,minor); 
        exit (EXIT_FAILURE);
      }
    }

    // NOTE: the shaders must be initialized after a GL context is in place.
    view_init(view,"shaders/shader.vert","shaders/shader.frag"); 


  // set up callbacks
    //glfwSetWindowRefreshCallback (window, display);
   // glfwSetWindowSizeCallback (window, Reshape);
   // glfwSetWindowIconifyCallback (window, Visible);
  //  glfwSetCharCallback (window, key);
    glfwSetKeyCallback(window, key_callback);

    //Initialize Diderot program
    diderot_init(window); 

    while (! glfwWindowShouldClose(window)) {
      step(window);
      display (window);
      glfwPollEvents ();
    //  glfwWaitEvents(); 
    }

    glfwTerminate ();

    return EXIT_SUCCESS;
    
} /* end of main */

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