0
|
1 // ----------------------------------------------------------------------------
|
|
2 // OpenGL Anti-Grain Geometry (GL-AGG) - Version 0.1
|
|
3 // A high quality OpenGL rendering engine for C
|
|
4 // Copyright (C) 2012 Nicolas P. Rougier. All rights reserved.
|
|
5 // Contact: Nicolas.Rougier@gmail.com
|
|
6 // http://code.google.com/p/gl-agg/
|
|
7 //
|
|
8 // Redistribution and use in source and binary forms, with or without
|
|
9 // modification, are permitted provided that the following conditions are met:
|
|
10 //
|
|
11 // 1. Redistributions of source code must retain the above copyright notice,
|
|
12 // this list of conditions and the following disclaimer.
|
|
13 //
|
|
14 // 2. Redistributions in binary form must reproduce the above copyright
|
|
15 // notice, this list of conditions and the following disclaimer in the
|
|
16 // documentation and/or other materials provided with the distribution.
|
|
17 //
|
|
18 // THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR
|
|
19 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
20 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
21 // EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
22 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
23 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
24 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
25 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
27 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
28 //
|
|
29 // The views and conclusions contained in the software and documentation are
|
|
30 // those of the authors and should not be interpreted as representing official
|
|
31 // policies, either expressed or implied, of Nicolas P. Rougier.
|
|
32 // ----------------------------------------------------------------------------
|
|
33 #include <stdio.h>
|
|
34 #include <stdlib.h>
|
|
35 #include <string.h>
|
|
36 #include "opengl.h"
|
|
37 #include "shader.h"
|
|
38
|
|
39
|
|
40 // ------------------------------------------------------------ shader_read ---
|
|
41 char *
|
|
42 shader_read( const char *filename )
|
|
43 {
|
|
44 FILE * file;
|
|
45 char * buffer;
|
|
46 size_t size;
|
|
47
|
|
48 file = fopen( filename, "rb" );
|
|
49 if( !file )
|
|
50 {
|
|
51 fprintf( stderr, "Unable to open file \"%s\".\n", filename );
|
|
52 return 0;
|
|
53 }
|
|
54 fseek( file, 0, SEEK_END );
|
|
55 size = ftell( file );
|
|
56 fseek(file, 0, SEEK_SET );
|
|
57 buffer = (char *) malloc( (size+1) * sizeof( char *) );
|
|
58 fread( buffer, sizeof(char), size, file );
|
|
59 buffer[size] = 0;
|
|
60 fclose( file );
|
|
61 return buffer;
|
|
62 }
|
|
63
|
|
64
|
|
65
|
|
66 // --------------------------------------------------------- shader_compile ---
|
|
67 GLuint
|
|
68 shader_compile( const char* source,
|
|
69 const GLenum type )
|
|
70 {
|
|
71 GLint compile_status;
|
|
72 GLuint handle = glCreateShader( type );
|
|
73 glShaderSource( handle, 1, &source, 0 );
|
|
74 glCompileShader( handle );
|
|
75
|
|
76 glGetShaderiv( handle, GL_COMPILE_STATUS, &compile_status );
|
|
77 if( compile_status == GL_FALSE )
|
|
78 {
|
|
79 GLchar messages[256];
|
|
80 glGetShaderInfoLog( handle, sizeof(messages), 0, &messages[0] );
|
|
81 fprintf( stderr, "%s\n", messages );
|
|
82 exit( EXIT_FAILURE );
|
|
83 }
|
|
84 return handle;
|
|
85 }
|
|
86
|
|
87
|
|
88 // ------------------------------------------------------------ shader_load ---
|
|
89 GLuint
|
|
90 shader_load( const char * vert_filename,
|
|
91 const char * frag_filename )
|
|
92 {
|
|
93 GLuint handle = glCreateProgram( );
|
|
94 GLint link_status;
|
|
95
|
|
96 if( vert_filename && strlen( vert_filename ) )
|
|
97 {
|
|
98 char *vert_source = shader_read( vert_filename );
|
|
99 GLuint vert_shader = shader_compile( vert_source, GL_VERTEX_SHADER);
|
|
100 glAttachShader( handle, vert_shader);
|
|
101 free( vert_source );
|
|
102 }
|
|
103 if( frag_filename && strlen( frag_filename ) )
|
|
104 {
|
|
105 char *frag_source = shader_read( frag_filename );
|
|
106 GLuint frag_shader = shader_compile( frag_source, GL_FRAGMENT_SHADER);
|
|
107 glAttachShader( handle, frag_shader);
|
|
108 free( frag_source );
|
|
109 }
|
|
110
|
|
111 glLinkProgram( handle );
|
|
112
|
|
113 glGetProgramiv( handle, GL_LINK_STATUS, &link_status );
|
|
114 if (link_status == GL_FALSE)
|
|
115 {
|
|
116 GLchar messages[256];
|
|
117 glGetProgramInfoLog( handle, sizeof(messages), 0, &messages[0] );
|
|
118 fprintf( stderr, "%s\n", messages );
|
|
119 exit(1);
|
|
120 }
|
|
121 return handle;
|
|
122 }
|