medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

glsl.cc (1664B)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdio.h>
#include <stdlib.h>

#include "glad.h"
#include "glsl.h"

#include "log.h"

// TODO: this is kinda crap because it calls delete
void check_compile_status( GLuint shader ) {
	GLint ok;
	glGetShaderiv( shader, GL_COMPILE_STATUS, &ok );

	if( ok == GL_FALSE ) {
		GLint len;
		glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &len );

		char * buf = ( char * ) malloc( len );
		if( buf == NULL ) {
			FATAL( "malloc" );
		}
		glGetShaderInfoLog( shader, len, NULL, buf );

		fprintf( stderr, "compiling the shader failed:\n%s", buf );
		free( buf );

		glDeleteShader( shader );
	}
}

void check_link_status( GLuint program ) {
	GLint ok;
	glGetProgramiv( program, GL_LINK_STATUS, &ok );

	if( ok == GL_FALSE ) {
		GLint len;
		glGetProgramiv( program, GL_INFO_LOG_LENGTH, &len );

		char * buf = ( char * ) malloc( len );
		if( buf == NULL ) {
			FATAL( "malloc" );
		}
		glGetProgramInfoLog( program, len, NULL, buf );

		fprintf( stderr, "linking the shader failed:\n%s", buf );
		free( buf );

		glDeleteProgram( program );
	}
}

GLuint compile_shader( const char * vert, const char * frag, const char * out ) {
	GLuint vs = glCreateShader( GL_VERTEX_SHADER );
	GLuint fs = glCreateShader( GL_FRAGMENT_SHADER );

	glShaderSource( vs, 1, &vert, NULL );
	glShaderSource( fs, 1, &frag, NULL );

	glCompileShader( vs );
	check_compile_status( vs );
	glCompileShader( fs );
	check_compile_status( fs );

	GLuint prog = glCreateProgram();

	glAttachShader( prog, vs );
	glAttachShader( prog, fs );
	glBindFragDataLocation( prog, 0, out );
	glLinkProgram( prog );

	glDeleteShader( vs );
	glDeleteShader( fs );

	check_link_status( prog );

	return prog;
}