-
Notifications
You must be signed in to change notification settings - Fork 9
/
s_shader.h
88 lines (83 loc) · 2.52 KB
/
s_shader.h
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
78
79
80
81
82
83
84
85
86
87
88
/***********************************************************
* OpenGL shader load helper *
* no warranty implied | use at your own risk *
* author: Andreas Mantler (ands) | last change: 13.04.2018 *
* *
* License: *
* This software is in the public domain. *
* Where that dedication is not recognized, *
* you are granted a perpetual, irrevocable license to copy *
* and modify this file however you want. *
***********************************************************/
static GLuint s_loadShader(GLenum type, const char *source)
{
GLuint shader = glCreateShader(type);
if (shader == 0)
{
fprintf(stderr, "Could not create shader!\n");
return 0;
}
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
GLint compiled;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled)
{
fprintf(stderr, "Could not compile shader!\n");
GLint infoLen = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen)
{
char* infoLog = (char*)malloc(infoLen);
glGetShaderInfoLog(shader, infoLen, NULL, infoLog);
fprintf(stderr, "%s\n", infoLog);
free(infoLog);
}
glDeleteShader(shader);
return 0;
}
return shader;
}
static GLuint s_loadProgram(const char *vp, const char *fp, const char **attributes, int attributeCount)
{
GLuint vertexShader = s_loadShader(GL_VERTEX_SHADER, vp);
if (!vertexShader)
return 0;
GLuint fragmentShader = s_loadShader(GL_FRAGMENT_SHADER, fp);
if (!fragmentShader)
{
glDeleteShader(vertexShader);
return 0;
}
GLuint program = glCreateProgram();
if (program == 0)
{
fprintf(stderr, "Could not create program!\n");
return 0;
}
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
for (int i = 0; i < attributeCount; i++)
glBindAttribLocation(program, i, attributes[i]);
glLinkProgram(program);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
GLint linked;
glGetProgramiv(program, GL_LINK_STATUS, &linked);
if (!linked)
{
fprintf(stderr, "Could not link program!\n");
GLint infoLen = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen)
{
char* infoLog = (char*)malloc(sizeof(char) * infoLen);
glGetProgramInfoLog(program, infoLen, NULL, infoLog);
fprintf(stderr, "%s\n", infoLog);
free(infoLog);
}
glDeleteProgram(program);
return 0;
}
return program;
}