Advertisement
Guest User

Untitled

a guest
Feb 21st, 2011
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. #pragma once
  2. //Disable string safety warning
  3. #pragma warning(disable:4996)
  4.  
  5. //STD
  6. #include <stdio.h>
  7. #include <iostream>
  8. #include <assert.h>
  9.  
  10. //GL
  11. #include <GLee5_4/GLee.h>
  12. #include <gl/gl.h>
  13. #include <gl/glu.h>
  14. #include <gl/glut.h>
  15.  
  16. //SDL
  17. #include <SDL.h>
  18. #include <SDL_opengl.h>
  19.  
  20. static const char* const sourceVert =
  21.     "void main() {\n"
  22.     "   gl_Position = gl_Vertex + vec4(0.5);\n"
  23.     "}";
  24.  
  25. static void checkShader(const GLuint id, const GLenum type, const char str[])   {
  26.     static char msg[240];
  27.     int size,aux;
  28.     glGetObjectParameterivARB(id, type, &size);
  29.     if(!size) return;
  30.     glGetObjectParameterivARB(id, GL_OBJECT_INFO_LOG_LENGTH_ARB, &size);
  31.     assert(size < sizeof(msg));
  32.     glGetInfoLogARB(id, size, &aux, msg);
  33.     msg[size] = 0;
  34.     printf("%s: \"%s\"\n", str, msg);
  35. }
  36.  
  37. int main(int argc, char **argv) {
  38.     printf("Beginning to test EXT_TRANSFORM_FEEDBACK\n");
  39.  
  40.     SDL_Init(SDL_INIT_EVERYTHING);
  41.     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
  42.     SDL_SetVideoMode(400,300,32,SDL_HWSURFACE|SDL_OPENGL);
  43.  
  44.     GLuint shid, bid, tmp;
  45.     GLint loc;
  46.  
  47.     printf("Initialization complete (current error: %d)\n\n",glGetError());
  48.  
  49.     printf("Loading shader\n");
  50.     shid = glCreateProgramObjectARB();
  51.  
  52.     tmp = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
  53.     glShaderSourceARB(tmp, 1, (const GLcharARB**)&sourceVert, NULL);
  54.     glCompileShaderARB(tmp);
  55.     checkShader(tmp, GL_OBJECT_COMPILE_STATUS_ARB, "Compile vertex");
  56.     glAttachObjectARB(shid,tmp);
  57.  
  58.     glLinkProgramARB(shid);
  59.     checkShader(shid, GL_OBJECT_LINK_STATUS_ARB, "Link result");
  60.     printf("Shader loaded (current error: %d)\n\n", glGetError());
  61.  
  62.     printf("Trying to get location of \"gl_Position\"\n");
  63.     glUseProgramObjectARB(shid);
  64.     loc = glGetVaryingLocationNV(shid,"gl_Position");
  65.     printf("Got location %d (current error: %d)\n\n",loc,glGetError());
  66.  
  67.     //const char *vars[] = {"out_pos"};
  68.     //glTransformFeedbackVaryingsEXT(shid, 1, vars, GL_SEPARATE_ATTRIBS_EXT); //for GLew?
  69.     const GLint locations[] = {loc};
  70.     glTransformFeedbackVaryingsEXT(shid, 1, locations, GL_SEPARATE_ATTRIBS_EXT); //for GLee?
  71.  
  72.     printf("Preparing buffer\n");
  73.     glGenBuffersARB(1, &bid);
  74.     glBindBufferARB(GL_ARRAY_BUFFER, bid);
  75.     const float init[] = {0.1f,0.2f,0.3f,0.4f};
  76.     glBufferDataARB(GL_ARRAY_BUFFER, 4*sizeof(float)*1, NULL, GL_STREAM_DRAW);
  77.     printf("Buffer prepared (current error: %d)\n",glGetError());
  78.  
  79.     glBindBufferBaseEXT(GL_TRANSFORM_FEEDBACK_BUFFER_EXT, 0, bid);
  80.     printf("Feedback assigned (current error: %d)\n\n",glGetError());
  81.  
  82.     printf("Beginning drawing\n");
  83.     glUseProgramObjectARB(shid);
  84.     glBeginTransformFeedbackEXT(GL_POINTS);
  85.     glEnable(GL_RASTERIZER_DISCARD_EXT);
  86.  
  87.     glBegin(GL_POINTS);
  88.     printf("In data: ");
  89.     for(int i=0; i<4; ++i) {
  90.         printf("%.1f ", init[i]);
  91.     }
  92.     printf("\n");
  93.     glVertex4fv(init);
  94.     glEnd();
  95.  
  96.     glDisable(GL_RASTERIZER_DISCARD_EXT);
  97.     glEndTransformFeedbackEXT();
  98.     printf("Drawing completed (current error: %d)\n\n", glGetError());
  99.  
  100.     printf("Reading (hopefully transformed) data\n");
  101.     float *const data = (float *)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
  102.     printf("Out data: ");
  103.     for(int i=0; i<4; ++i) {
  104.         printf("%.1f ", data[i]);
  105.     }
  106.     printf("\n");
  107.     glUnmapBuffer(GL_ARRAY_BUFFER);
  108.     printf("Readback complete (current error: %d)",glGetError());
  109.  
  110.     getchar();
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement