Advertisement
4da

TBO teapot example

4da
Nov 14th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define GL_GLEXT_PROTOTYPES 1
  6. #include <GL/gl.h>
  7. #include <GL/glut.h>
  8.  
  9. static const char Vertex_src[] =
  10.     "void main(void)"
  11.     "{"
  12.     "  gl_Position = ftransform();"
  13.     "}";
  14.  
  15. static const char Fragment_src[] =
  16.     "uniform samplerBuffer tex;"
  17.     ""
  18.     "void main(void)"
  19.     "{"
  20.     "  gl_FragColor = texelFetch( tex, 0 );"
  21.     "}";
  22.  
  23.  
  24. void keybd ( unsigned char, int, int )
  25. {
  26.     exit ( 0 ) ;
  27. }
  28.  
  29.  
  30. void reshape(int wid, int ht)
  31. {
  32.     glViewport(0, 0, wid, ht);
  33. }
  34.  
  35. void showGLerror ()
  36. {
  37.     GLenum err ;
  38.  
  39.     while ( (err = glGetError()) != GL_NO_ERROR )
  40.         fprintf ( stderr, "OpenGL Error: %s\n", gluErrorString ( err ) )  ;
  41. }
  42.  
  43. void display ( void )
  44. {
  45.     static float a = 0.0f ;
  46.  
  47.     a += 0.3f ;
  48.  
  49.     glMatrixMode      ( GL_PROJECTION ) ;
  50.     glLoadIdentity    () ;
  51.     glFrustum         ( -1.0f, 1.0f,
  52.                 -1.0f / (640.0f/480.0f), 1.0f / (640.0f/480.0f),
  53.                 3.0f, 10.0f) ;
  54.  
  55.     glMatrixMode      ( GL_MODELVIEW ) ;
  56.     glLoadIdentity    () ;
  57.     glTranslatef      ( 0.0, 0.0, -5.0 ) ;
  58.     glRotatef         ( a, 0.2, 0.7, 0 ) ;
  59.  
  60.     glEnable          ( GL_DEPTH_TEST ) ;
  61.     glEnable          ( GL_CULL_FACE  ) ;
  62.     glCullFace        ( GL_FRONT ) ;
  63.  
  64.     glClearColor      ( 0.0f, 0.0f, 0.0f, 1.0f ) ;
  65.     glClear           ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
  66.  
  67.     glutSolidTeapot   ( 1.0f ) ;
  68.  
  69.     glutSwapBuffers   () ;
  70.     glutPostRedisplay () ;
  71.  
  72.     showGLerror () ;
  73. }
  74.  
  75. void showShaderInfo ( const char *what, GLuint handle )
  76. {
  77.     int len = 0 ;
  78.  
  79.     glGetObjectParameterivARB ( handle, GL_OBJECT_INFO_LOG_LENGTH_ARB, &len ) ;
  80.  
  81.     if ( len > 0 )
  82.     {
  83.         int trueLen ;
  84.         char *s = new char [ len ] ;
  85.  
  86.         glGetInfoLogARB ( handle, len, &trueLen, s ) ;
  87.  
  88.         if ( trueLen > 0 & &s [ 0 ] != '\0' )
  89.             fprintf ( stderr, "%s:\n%s\n", what, s ) ;
  90.  
  91.         delete [] s ;
  92.     }
  93. }
  94.  
  95. GLuint compileShader ( const char *src, GLenum type )
  96. {
  97.     const char *type_str = type == GL_VERTEX_SHADER ? "vertex" : "fragment";
  98.  
  99.     GLuint handle = glCreateShader( type ) ;
  100.  
  101.     glShaderSource ( handle, 1, &src, 0 ) ;
  102.     glCompileShader( handle ) ;
  103.  
  104.     GLint compiled ;
  105.     glGetShaderiv( handle, GL_COMPILE_STATUS, &compiled ) ;
  106.  
  107.     if ( !compiled )
  108.     {
  109.         showShaderInfo ( type_str, handle ) ;
  110.         fprintf ( stderr, "Failed to compile %s shader.\n", type_str );
  111.         exit ( 1 ) ;
  112.     }
  113.  
  114.     return handle ;
  115. }
  116.  
  117. GLuint linkShaders ( GLuint vsHandle, GLuint fsHandle )
  118. {
  119.     GLint  linked ;
  120.     GLuint handle = glCreateProgram() ;
  121.  
  122.     glAttachShader            ( handle, vsHandle ) ;
  123.     glAttachShader            ( handle, fsHandle ) ;
  124.     glLinkProgram             ( handle ) ;
  125.     glGetProgramiv            ( handle, GL_LINK_STATUS, & linked ) ;
  126.  
  127.     if ( !linked )
  128.     {
  129.         showShaderInfo ( "Linking", handle ) ;
  130.         fprintf ( stderr, "Failed to link shader program.\n" ) ;
  131.         exit ( 1 ) ;
  132.     }
  133.  
  134.     return handle ;
  135. }
  136.  
  137. void init()
  138. {
  139.     // The color to put in the buffer object
  140.     const GLfloat color[4] = { 1, 1, 0, 1 };     // Yellow
  141.     //const GLfloat color[4] = { 1, 0, 0, 1 };     // Red
  142.     //const GLfloat color[4] = { 0, 1, 1, 1 };     // Cyan
  143.  
  144.     const size_t  size = sizeof( color );
  145.  
  146.     // Generate and fill buffer object
  147.     GLuint buffer;
  148.  
  149.     glGenBuffers   ( 1, &buffer );
  150.     glBindBuffer   ( GL_TEXTURE_BUFFER, buffer );
  151.     glBufferData   ( GL_TEXTURE_BUFFER, size, NULL, GL_STATIC_DRAW );  // Alloc
  152.     glBufferSubData( GL_TEXTURE_BUFFER, 0, size, color );              // Fill
  153.  
  154.     // Generate texture "wrapper" around buffer object
  155.     GLuint tex;
  156.  
  157.     glGenTextures  ( 1, &tex );
  158.     glActiveTexture( GL_TEXTURE0 );
  159.     glBindTexture  ( GL_TEXTURE_BUFFER, tex );
  160.     glTexBuffer    ( GL_TEXTURE_BUFFER, GL_RGBA32F, buffer );
  161. }
  162.  
  163.  
  164. int main ( int argc, char **argv )
  165. {
  166.     // Init GL context
  167.     glutInit            ( &argc, argv ) ;
  168.     glutInitDisplayMode ( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE ) ;
  169.     glutInitWindowSize  ( 500, 500 ) ;
  170.     glutCreateWindow    ( "Shader Test" ) ;
  171.     glutDisplayFunc     ( display  ) ;
  172.     glutKeyboardFunc    ( keybd    ) ;
  173.     glutReshapeFunc     ( reshape  ) ;
  174.  
  175.     // Create buffer object and its texture buffer object wrapper
  176.     init();
  177.  
  178.     // Load and compile shaders
  179.     printf( "Compiling vertex shader...\n" );
  180.     GLuint vsHandle = compileShader ( Vertex_src, GL_VERTEX_SHADER  );
  181.     printf( "Compiling fragment shader...\n" );
  182.     GLuint fsHandle = compileShader ( Fragment_src, GL_FRAGMENT_SHADER);
  183.  
  184.     // Link shaders
  185.     printf( "Linking...\n" );
  186.     GLuint handle   = linkShaders ( vsHandle, fsHandle ) ;
  187.  
  188.     // Activate shader
  189.     glUseProgram( handle ) ;
  190.  
  191.     // Draw with shader
  192.     glutMainLoop () ;
  193.     return 0 ;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement