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