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 3213, Tue Jul 14 17:16:25 2015 UTC revision 3214, Wed Jul 15 04:29:46 2015 UTC
# Line 50  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 (const char *msg)  void ReportError (GLenum err, const char *file, int line, const char *msg)
54    {
55        const char *error;
56    
57        switch (err) {
58          case GL_INVALID_ENUM: error = "invalid enum"; break;
59          case GL_INVALID_VALUE: error = "invalid value"; break;
60          case GL_INVALID_OPERATION: error = "invalid operation"; break;
61          case GL_INVALID_FRAMEBUFFER_OPERATION: error = "invalid framebuffer operation"; break;
62          case GL_OUT_OF_MEMORY: error = "out of memory"; break;
63          default: error = "unknown"; break;
64        }
65    
66        fprintf (stderr, "[%s:%d] %s; %s\n", file, line, error, msg);
67    
68        exit (1);
69    }
70    
71    static inline void CheckError (const char *file, int line, const char *msg)
72  {  {
73      GLenum errCode;      GLenum errCode;
74      if ((errCode = glGetError()) != GL_NO_ERROR) {      if ((errCode = glGetError()) != GL_NO_ERROR) {
75          fprintf(stderr, "[GL ERROR %04x] %s\n", errCode, msg);          ReportError (errCode, file, line, msg);
         exit (1);  
76      }      }
77  }  }
78    
79    #define CHECK(cmd)                                              \
80            do {                                                    \
81                cmd;                                                \
82                CheckError (__FILE__, __LINE__, "executing " #cmd); \
83            } while (0)
84    
85  /***** Globals for viewing, etc. *****/  /***** Globals for viewing, etc. *****/
86    
87  typedef struct {  typedef struct {
# Line 93  Line 116 
116  {  {
117      if (! txt->init) {      if (! txt->init) {
118        // generate the TeXID        // generate the TeXID
119          glGenTextures (1, &(txt->id));          CHECK( glGenTextures (1, &(txt->id)) );
120    
121        // load the texture data        // load the texture data
122          glBindTexture (GL_TEXTURE_2D, txt->id);          CHECK( glBindTexture (GL_TEXTURE_2D, txt->id) );
123          glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, txt->wid, txt->ht, 0, GL_RGBA, GL_FLOAT, img->data);          CHECK( glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, txt->wid, txt->ht, 0, GL_RGBA, GL_FLOAT, img->data) );
124          CheckError("texture initialization");          CHECK( glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) );
125          glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);          CHECK( glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) );
         glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);  
         CheckError("texture parameter initialization");  
126      }      }
127      else {      else {
128        // reload the texture data        // reload the texture data
129          glBindTexture (GL_TEXTURE_2D, txt->id);          CHECK( glBindTexture (GL_TEXTURE_2D, txt->id) );
130          glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, txt->wid, txt->ht, GL_RGBA, GL_FLOAT, img->data);          CHECK( glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, txt->wid, txt->ht, GL_RGBA, GL_FLOAT, img->data) );
         CheckError("texture binding");  
131      }      }
132  }  }
133    
# Line 119  Line 139 
139  uniform mat4 modelView;\n\  uniform mat4 modelView;\n\
140  uniform mat4 projection;\n\  uniform mat4 projection;\n\
141  layout (location = 0) in vec2 coord;\n\  layout (location = 0) in vec2 coord;\n\
142  out smooth vec2 texCoord;\n\  smooth out vec2 texCoord;\n\
143  \n\  \n\
144  void main ()\n\  void main ()\n\
145  {\n\  {\n\
# Line 132  Line 152 
152  static const char *FragShaderSrc = "\  static const char *FragShaderSrc = "\
153  #version 410\n\  #version 410\n\
154  uniform sampler2D image;\n\  uniform sampler2D image;\n\
155  in smooth vec2  texCoord;\n\  smooth in vec2  texCoord;\n\
156  layout (location = 0) out vec4 fragColor;\n\  layout (location = 0) out vec4 fragColor;\n\
157  void main ()\n\  void main ()\n\
158  {\n\  {\n\
# Line 140  Line 160 
160  }\n\  }\n\
161      ";      ";
162    
163    static void CheckCompileStatus (const char *shader, GLuint id)
164    {
165        GLint sts;
166    
167        glGetShaderiv (id, GL_COMPILE_STATUS, &sts);
168        if (sts != GL_TRUE) {
169          /* the compile failed, so report an error */
170            glGetShaderiv (id, GL_INFO_LOG_LENGTH, &sts);
171            if (sts != 0) {
172                GLchar *log = (GLchar *) malloc(sts);
173                glGetShaderInfoLog (id, sts, 0, log);
174                fprintf (stderr, "Error compiling %s shader:\n%s\n", shader, log);
175                free (log);
176            }
177            else {
178                fprintf (stderr, "Error compiling %s shader:\n  no log\n", shader);
179            }
180            exit (1);
181        }
182    }
183    
184  // Initialize the rendering context  // Initialize the rendering context
185  //  //
186  static void InitGraphics ()  static void InitGraphics ()
# Line 156  Line 197 
197      GLuint vsh = glCreateShader (GL_VERTEX_SHADER);      GLuint vsh = glCreateShader (GL_VERTEX_SHADER);
198      glShaderSource (vsh, 1, &VertexShaderSrc, 0);      glShaderSource (vsh, 1, &VertexShaderSrc, 0);
199      glCompileShader (vsh);      glCompileShader (vsh);
200        CheckCompileStatus ("vertex", vsh);
201    
202      GLuint fsh = glCreateShader (GL_FRAGMENT_SHADER);      GLuint fsh = glCreateShader (GL_FRAGMENT_SHADER);
203      glShaderSource (fsh, 1, &FragShaderSrc, 0);      glShaderSource (fsh, 1, &FragShaderSrc, 0);
204      glCompileShader (fsh);      glCompileShader (fsh);
205        CheckCompileStatus ("fragment", fsh);
206    
207      GLuint shaderProg = glCreateProgram ();      GLuint shaderProg = glCreateProgram ();
208      glAttachShader (shaderProg, vsh);      CHECK( glAttachShader (shaderProg, vsh) );
209      glAttachShader (shaderProg, fsh);      CHECK( glAttachShader (shaderProg, fsh) );
210      glLinkProgram (shaderProg);      CHECK( glLinkProgram (shaderProg) );
211    
212      glUseProgram (shaderProg);      CHECK( glUseProgram (shaderProg) );
213    
214    // initialize the VBO    // initialize the VBO
215      GLuint vaoId, buffers[2];      GLuint vaoId, buffers[2];
216      glGenVertexArrays (1, &vaoId);      CHECK( glGenVertexArrays (1, &vaoId) );
217      glGenBuffers (2, buffers);      CHECK( glGenBuffers (2, buffers) );
218      glBindVertexArray (vaoId);      CHECK( glBindVertexArray (vaoId) );
219    
220      glBindBuffer (GL_ARRAY_BUFFER, buffers[0]);      CHECK( glBindBuffer (GL_ARRAY_BUFFER, buffers[0]) );
221      glBufferData (GL_ARRAY_BUFFER, sizeof(coords), coords, GL_STATIC_DRAW);      CHECK( glBufferData (GL_ARRAY_BUFFER, sizeof(coords), coords, GL_STATIC_DRAW) );
222      glVertexAttribPointer (COORD_ATTR, 2, GL_FLOAT, GL_FALSE, 0, 0);      CHECK( glVertexAttribPointer (COORD_ATTR, 2, GL_FLOAT, GL_FALSE, 0, 0) );
223      glEnableVertexAttribArray (COORD_ATTR);      CHECK( glEnableVertexAttribArray (COORD_ATTR) );
224    
225      glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, buffers[1]);      CHECK( glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, buffers[1]) );
226      glBufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);      CHECK( glBufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW) );
227    
228      QuadMesh.vaoId = vaoId;      QuadMesh.vaoId = vaoId;
229      QuadMesh.coordId = buffers[0];      QuadMesh.coordId = buffers[0];
# Line 219  Line 262 
262    // Draw image    // Draw image
263      glActiveTexture (GL_TEXTURE0);      glActiveTexture (GL_TEXTURE0);
264      glBindTexture (GL_TEXTURE_2D, txtId);      glBindTexture (GL_TEXTURE_2D, txtId);
265    
266      glBindVertexArray (QuadMesh.vaoId);      glBindVertexArray (QuadMesh.vaoId);
267      glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, QuadMesh.ebufId);      glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, QuadMesh.ebufId);
268      glDrawElements (GL_TRIANGLE_FAN, 4, GL_UNSIGNED_SHORT, 0);      glDrawElements (GL_TRIANGLE_FAN, 4, GL_UNSIGNED_SHORT, 0);
269    
     CheckError ("drawing");  
270  }  }
271    
272  // Callback function called by GLFW when window size changes  // Callback function called by GLFW when window size changes
# Line 345  Line 388 
388      VR_InVarSet_valOpacMid (wrld, OpacMid);      VR_InVarSet_valOpacMid (wrld, OpacMid);
389      VR_InVarSetByName_hand (wrld, "data/vfrhand-nohip.nhdr");      VR_InVarSetByName_hand (wrld, "data/vfrhand-nohip.nhdr");
390    
   // create initial strands  
     VR_Initially (wrld);  
   
391    // nrrd for getting computational state    // nrrd for getting computational state
392      Nrrd *nRGB = nrrdNew();      Nrrd *nRGB = nrrdNew();
393    

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

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