-
Notifications
You must be signed in to change notification settings - Fork 1
/
shader.c
78 lines (63 loc) · 1.95 KB
/
shader.c
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
/*
* This proprietary software may be used only as
* authorised by a licensing agreement from ARM Limited
* (C) COPYRIGHT 2009 - 2011 ARM Limited
* ALL RIGHTS RESERVED
* The entire notice above must be reproduced on all authorised
* copies and copies may only be made to the extent permitted
* by a licensing agreement from ARM Limited.
*/
/*
* shader.c
* Functions for loading and process shaders.
*/
#include "shader.h"
#include "cube.h"
/*
* Loads the shader source into memory.
*
* sFilename: String holding filename to load
*/
char* load_shader(char *sFilename) {
char *pResult = NULL;
FILE *pFile = NULL;
long iLen = 0;
pFile = fopen(sFilename, "r");
if(pFile == NULL) {
fprintf(stderr, "Error: Cannot read file '%s'\n", sFilename);
exit(-1);
}
fseek(pFile, 0, SEEK_END); /* Seek end of file */
iLen = ftell(pFile);
fseek(pFile, 0, SEEK_SET); /* Seek start of file again */
pResult = calloc(iLen+1, sizeof(char));
fread(pResult, sizeof(char), iLen, pFile);
pResult[iLen] = '\0';
fclose(pFile);
return pResult;
}
/*
* Create shader, load in source, compile, dump debug as necessary.
*
* pShader: Pointer to return created shader ID.
* sFilename: Passed-in filename from which to load shader source.
* iShaderType: Passed to GL, e.g. GL_VERTEX_SHADER.
*/
void process_shader(GLuint *pShader, char *sFilename, GLint iShaderType) {
GLint iStatus;
const char *aStrings[1] = { NULL };
/* Create shader and load into GL. */
*pShader = GL_CHECK(glCreateShader(iShaderType));
aStrings[0] = load_shader(sFilename);
GL_CHECK(glShaderSource(*pShader, 1, aStrings, NULL));
/* Clean up shader source. */
free((void *)aStrings[0]);
aStrings[0] = NULL;
/* Try compiling the shader. */
GL_CHECK(glCompileShader(*pShader));
GL_CHECK(glGetShaderiv(*pShader, GL_COMPILE_STATUS, &iStatus));
// Dump debug info (source and log) if compilation failed.
if(iStatus != GL_TRUE) {
exit(-1);
}
}