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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3212, Tue Jul 14 17:16:24 2015 UTC revision 3213, Tue Jul 14 17:16:25 2015 UTC
# Line 10  Line 10 
10   * All rights reserved.   * All rights reserved.
11   */   */
12    
13  #include "GL/glfw.h"  #define GL_GLEXT_PROTOTYPES
14    #define GLFW_INCLUDE_GLCOREARB
15    #include <GLFW/glfw3.h>
16    #if defined(__APPLE_CC__)
17      #include <OpenGL/glext.h>
18    #else
19    #  include <GL/glext.h>
20    #endif
21    
22  #include "util.h"  #include "util.h"
23  #include "vr.h"  #include "vr.h"
24  #include <unistd.h>  #include <unistd.h>
# Line 42  Line 50 
50  #define WIDTH   (2*FULL_WID)  #define WIDTH   (2*FULL_WID)
51  #define HEIGHT  (2*FULL_HT)  #define HEIGHT  (2*FULL_HT)
52    
53  static inline void CheckError ()  static inline void CheckError (const char *msg)
54  {  {
55      GLenum errCode;      GLenum errCode;
56      if ((errCode = glGetError()) != GL_NO_ERROR) {      if ((errCode = glGetError()) != GL_NO_ERROR) {
57          fprintf(stderr, "OpenGL error: %s\n", gluErrorString(errCode));          fprintf(stderr, "[GL ERROR %04x] %s\n", errCode, msg);
58          exit (1);          exit (1);
59      }      }
60  }  }
# Line 69  Line 77 
77  TexInfo_t       FullTexture = {         // Texture ID of full image  TexInfo_t       FullTexture = {         // Texture ID of full image
78          false, FULL_WID, FULL_HT, 0          false, FULL_WID, FULL_HT, 0
79      };      };
80    struct {
81        GLuint      vaoId;
82        GLuint      coordId;
83        GLuint      ebufId;
84    }               QuadMesh;
85  bool            NeedsRedraw;  bool            NeedsRedraw;
86  bool            NeedsRecompute;  bool            NeedsRecompute;
87  float           OpacMid = 550.0;  float           OpacMid = 550.0;
# Line 85  Line 98 
98        // load the texture data        // load the texture data
99          glBindTexture (GL_TEXTURE_2D, txt->id);          glBindTexture (GL_TEXTURE_2D, txt->id);
100          glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, txt->wid, txt->ht, 0, GL_RGBA, GL_FLOAT, img->data);          glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, txt->wid, txt->ht, 0, GL_RGBA, GL_FLOAT, img->data);
101          CheckError();          CheckError("texture initialization");
102          glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);          glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
103          glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);          glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
104          CheckError();          CheckError("texture parameter initialization");
105      }      }
106      else {      else {
107        // reload the texture data        // reload the texture data
108          glBindTexture (GL_TEXTURE_2D, txt->id);          glBindTexture (GL_TEXTURE_2D, txt->id);
109          glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, txt->wid, txt->ht, GL_RGBA, GL_FLOAT, img->data);          glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, txt->wid, txt->ht, GL_RGBA, GL_FLOAT, img->data);
110          CheckError();          CheckError("texture binding");
111      }      }
112  }  }
113    
114    #define COORD_ATTR 0
115    
116    // vertex shader
117    static const char *VertexShaderSrc = "\
118    #version 410\n\
119    uniform mat4 modelView;\n\
120    uniform mat4 projection;\n\
121    layout (location = 0) in vec2 coord;\n\
122    out smooth vec2 texCoord;\n\
123    \n\
124    void main ()\n\
125    {\n\
126      gl_Position = projection * modelView * vec4(coord, 0, 1);\n\
127      texCoord = coord;\n\
128    }\n\
129    ";
130    
131    // fragment shader
132    static const char *FragShaderSrc = "\
133    #version 410\n\
134    uniform sampler2D image;\n\
135    in smooth vec2  texCoord;\n\
136    layout (location = 0) out vec4 fragColor;\n\
137    void main ()\n\
138    {\n\
139        fragColor = vec4(texture(image, texCoord).rgb, 1);\n\
140    }\n\
141        ";
142    
143    // Initialize the rendering context
144    //
145    static void InitGraphics ()
146    {
147        float coords[4 * 2] = {
148                0.0f, 0.0f,
149                1.0f, 0.0f,
150                1.0f, 1.0f,
151                0.0f, 1.0f,
152            };
153        unsigned short indices[4] = {0, 1, 2, 3};
154    
155      // initialize the shader program
156        GLuint vsh = glCreateShader (GL_VERTEX_SHADER);
157        glShaderSource (vsh, 1, &VertexShaderSrc, 0);
158        glCompileShader (vsh);
159    
160        GLuint fsh = glCreateShader (GL_FRAGMENT_SHADER);
161        glShaderSource (fsh, 1, &FragShaderSrc, 0);
162        glCompileShader (fsh);
163    
164        GLuint shaderProg = glCreateProgram ();
165        glAttachShader (shaderProg, vsh);
166        glAttachShader (shaderProg, fsh);
167        glLinkProgram (shaderProg);
168    
169        glUseProgram (shaderProg);
170    
171      // initialize the VBO
172        GLuint vaoId, buffers[2];
173        glGenVertexArrays (1, &vaoId);
174        glGenBuffers (2, buffers);
175        glBindVertexArray (vaoId);
176    
177        glBindBuffer (GL_ARRAY_BUFFER, buffers[0]);
178        glBufferData (GL_ARRAY_BUFFER, sizeof(coords), coords, GL_STATIC_DRAW);
179        glVertexAttribPointer (COORD_ATTR, 2, GL_FLOAT, GL_FALSE, 0, 0);
180        glEnableVertexAttribArray (COORD_ATTR);
181    
182        glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
183        glBufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
184    
185        QuadMesh.vaoId = vaoId;
186        QuadMesh.coordId = buffers[0];
187        QuadMesh.ebufId = buffers[1];
188    
189      // initialize the transformation matrices
190        float projMat[16] = {  // gluOrtho2D (0.0, 1.0, 0.0, 1.0);
191                 2,  0,  0,  0,
192                 0,  2,  0,  0,
193                 0,  0, -2,  0,
194                -1, -1,  0,  1,
195            };
196        float modelViewMat[16] = { // gluLookAt (0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
197                 1,  0,  0,  0,
198                 0,  1,  0,  0,
199                 0,  0, -1,  0,
200                 0,  0,  0,  1,
201            };
202        glUniformMatrix4fv (glGetUniformLocation (shaderProg, "projection"), 1, GL_FALSE, projMat);
203        glUniformMatrix4fv (glGetUniformLocation (shaderProg, "modelView"), 1, GL_FALSE, modelViewMat);
204    
205      // initialize texture binding
206        glUniform1i (glGetUniformLocation (shaderProg, "image"), 0);  // texture unit 0
207    }
208    
209  // Callback function called by GLUT to render screen  // Callback function called by GLUT to render screen
210  void Draw (GLuint txtId)  void Draw (GLuint txtId)
211  {  {
# Line 109  Line 217 
217      glDisable(GL_CULL_FACE);      glDisable(GL_CULL_FACE);
218    
219    // Draw image    // Draw image
     glEnable (GL_TEXTURE_2D);  
220      glActiveTexture (GL_TEXTURE0);      glActiveTexture (GL_TEXTURE0);
221      glBindTexture (GL_TEXTURE_2D, txtId);      glBindTexture (GL_TEXTURE_2D, txtId);
222      glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);      glBindVertexArray (QuadMesh.vaoId);
223      glBegin (GL_QUADS);      glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, QuadMesh.ebufId);
224          glTexCoord2f (0.0, 0.0);      glDrawElements (GL_TRIANGLE_FAN, 4, GL_UNSIGNED_SHORT, 0);
         glVertex2f (0.0, 0.0);  
         glTexCoord2f (1.0, 0.0);  
         glVertex2f (1.0, 0.0);  
         glTexCoord2f (1.0, 1.0);  
         glVertex2f (1.0, 1.0);  
         glTexCoord2f (0.0, 1.0);  
         glVertex2f (0.0, 1.0);  
     glEnd();  
     glDisable (GL_TEXTURE_2D);  
     CheckError ();  
225    
226    // Present frame buffer      CheckError ("drawing");
     glfwSwapBuffers();  
227  }  }
228    
229  // Callback function called by GLFW when window size changes  // Callback function called by GLFW when window size changes
230  void GLFWCALL WindowSizeCB (int width, int height)  void WindowSizeCB (GLFWwindow *win, int width, int height)
231  {  {
232    // Set OpenGL viewport and camera    // Set OpenGL viewport and camera
233      glViewport(0, 0, width, height);      glViewport(0, 0, width, height);
234    
     glMatrixMode (GL_PROJECTION);  
     glLoadIdentity ();  
     gluOrtho2D (0.0, 1.0, 0.0, 1.0);  
   
     glMatrixMode (GL_MODELVIEW);  
     glLoadIdentity ();  
     gluLookAt (  
         0.0, 0.0, 1.0,  
         0.0, 0.0, 0.0,  
         0.0, 1.0, 0.0);  
   
235    // remember width and height    // remember width and height
236      Width = width;      Width = width;
237      Height = height;      Height = height;
# Line 154  Line 239 
239      NeedsRedraw = true;      NeedsRedraw = true;
240  }  }
241    
242  void GLFWCALL KeyCB (int key, int action)  void KeyCB (GLFWwindow *win, int key, int scancode, int action, int mods)
243  {  {
244      switch (key) {      switch (key) {
245        case '+':        case GLFW_KEY_ESCAPE:
246          if ((action == GLFW_PRESS) && (Dir >= 0))          glfwSetWindowShouldClose (win, GL_TRUE);
247            break;
248          case GLFW_KEY_EQUAL: // with shift is '+'
249            if ((mods == GLFW_MOD_SHIFT) && (action == GLFW_PRESS) && (Dir >= 0)) {
250                Dir++;
251            }
252            break;
253          case GLFW_KEY_KP_ADD:
254            if ((mods == 0) && (action == GLFW_PRESS) && (Dir >= 0))
255              Dir++;              Dir++;
256          break;          break;
257        case '-':        case GLFW_KEY_MINUS:
258          if ((action == GLFW_PRESS) && (Dir <= 0))        case GLFW_KEY_KP_SUBTRACT:
259            if ((mods == 0) && (action == GLFW_PRESS) && (Dir <= 0))
260              Dir--;              Dir--;
261          break;          break;
262        default:        default:
# Line 201  Line 295 
295      }      }
296  }  }
297    
298    static void Error (int err, const char *msg)
299    {
300        fprintf(stderr, "[GLFW ERROR  %d] %s\n", err, msg);
301    }
302    
303  int main (int argc, const char **argv)  int main (int argc, const char **argv)
304  {  {
305        glfwSetErrorCallback (Error);
306    
307    // Initialize GLFW    // Initialize GLFW
308      if (0 == glfwInit()) {      if (0 == glfwInit()) {
309        // An error occured        // An error occured
# Line 210  Line 311 
311          return 1;          return 1;
312      }      }
313    
314      GLFWvidmode mode;      glfwWindowHint (GLFW_RESIZABLE, GL_TRUE);
315      glfwGetDesktopMode (&mode);      glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 4);
316      if (0 == glfwOpenWindow(WIDTH, HEIGHT, mode.RedBits, mode.GreenBits, mode.BlueBits, 0, 0, 0, GLFW_WINDOW) ) {      glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 1);
317        glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
318        glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
319        GLFWwindow *win = glfwCreateWindow(WIDTH, HEIGHT, "Diderot VR Demo", NULL, NULL);
320        if (win== 0) {
321        // A fatal error occured        // A fatal error occured
322          fprintf(stderr, "Cannot open GLFW window\n");          fprintf(stderr, "Cannot open GLFW window\n");
323          glfwTerminate();          glfwTerminate();
324          return 1;          return 1;
325      }      }
326      glfwEnable(GLFW_KEY_REPEAT);      glfwMakeContextCurrent (win);
327      glfwSetWindowTitle("Diderot VR Demo");  
328        InitGraphics ();
329    
330    // Set GLFW event callbacks    // Set GLFW event callbacks
331      glfwSetWindowSizeCallback (WindowSizeCB);      glfwSetWindowSizeCallback (win, WindowSizeCB);
332      glfwSetCharCallback (KeyCB);      glfwSetKeyCallback (win, KeyCB);
333    
334    // initialize the Diderot program    // initialize the Diderot program
335      VR_World_t *wrld = VR_Init ();      VR_World_t *wrld = VR_New ();
336        if (wrld == 0) {
337            fprintf(stderr, "Cannot create world\n");
338            glfwTerminate();
339            return 1;
340        }
341      World = wrld;      World = wrld;
342        VR_Init (wrld);
343    
344    // initialize inputs    // initialize inputs
345      VR_InVarSet_valOpacMid(wrld, OpacMid);      VR_InVarSet_valOpacMid(wrld, OpacMid);
346      VR_InVarSetByName_hand (wrld, "data/vfrhand-nohip.nhdr");      VR_InVarSetByName_hand (wrld, "data/vfrhand-nohip.nhdr");
347    
348      // create initial strands
349        VR_Initially (wrld);
350    
351    // nrrd for getting computational state    // nrrd for getting computational state
352      Nrrd *nRGB = nrrdNew();      Nrrd *nRGB = nrrdNew();
353    
354    // Main loop (repeated while window is not closed and [ESC] is not pressed)    // Main loop (repeated while window is not closed and [ESC] is not pressed)
355      NeedsRecompute = true;      NeedsRecompute = true;
356      GLuint txtId;      GLuint txtId;
357      while (glfwGetWindowParam(GLFW_OPENED) && !glfwGetKey(GLFW_KEY_ESC)) {      while (! glfwWindowShouldClose(win)) {
358          if (Dir < 0) {          if (Dir < 0) {
359              Dir++;              Dir++;
360              float mid = OpacMid - OPAC_DELTA;              float mid = OpacMid - OPAC_DELTA;
# Line 274  Line 389 
389          }          }
390          if (NeedsRedraw) {          if (NeedsRedraw) {
391              Draw (txtId);              Draw (txtId);
392              // Present frame buffer
393                glfwSwapBuffers(win);
394              NeedsRedraw = false;              NeedsRedraw = false;
395          }          }
396          if (NeedsRecompute)          if (NeedsRecompute)

Legend:
Removed from v.3212  
changed lines
  Added in v.3213

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