-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshader.c
More file actions
41 lines (34 loc) · 1.31 KB
/
shader.c
File metadata and controls
41 lines (34 loc) · 1.31 KB
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
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <stdio.h>
#include <string.h>
// function to read text file
static char* readTextFile(char* aTextFile)
{
FILE* filePointer = fopen(aTextFile, "rb");
char* content;
long numVal;
fseek(filePointer, 0L, SEEK_END);
numVal = ftell(filePointer);
fseek(filePointer, 0L, SEEK_SET);
content = (char*) malloc((numVal+1) * sizeof(char));
fread(content, 1, numVal, filePointer);
content[numVal] = '\0';
fclose(filePointer);
return content;
}
// function to initialize shaders
int setShader(char* shaderType, char* shaderFile)
{
int shaderId;
char* shader = readTextFile(shaderFile);
// set shaderId based on the shader type
if (strcmp(shaderType, "tessEvaluation") == 0) shaderId = glCreateShader(GL_TESS_EVALUATION_SHADER);
if (strcmp(shaderType, "tessControl" ) == 0) shaderId = glCreateShader(GL_TESS_CONTROL_SHADER);
if (strcmp(shaderType, "geometry" ) == 0) shaderId = glCreateShader(GL_GEOMETRY_SHADER);
if (strcmp(shaderType, "fragment" ) == 0) shaderId = glCreateShader(GL_FRAGMENT_SHADER);
if (strcmp(shaderType, "vertex" ) == 0) shaderId = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(shaderId, 1, (const char**) &shader, NULL);
glCompileShader(shaderId);
return shaderId;
}