-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshaders.c
More file actions
85 lines (71 loc) · 2.22 KB
/
shaders.c
File metadata and controls
85 lines (71 loc) · 2.22 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
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
#define GL_GLEXT_PROTOTYPES
#define GLX_GLEXT_PROTOTYPES
/* #include <GL/glx.h> */
#if defined(__APPLE__)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "globals.h"
#include "shaders.h"
#ifndef DEBUG
#define DEBUG
#endif
void load_shader(char *file_name) {
const char *vshader =
"attribute vec4 gl_Vertex;"\
"void main() {"\
" gl_TexCoord[0] = gl_MultiTexCoord0;"\
" gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;"\
"}";
const char *fshader =
"uniform sampler2D tex;"\
"void main () {"\
" vec4 color = texture2D(tex, gl_TexCoord[0].st);"\
" gl_FragColor = color;"\
"}";
int vslen = strlen(vshader);
int fslen = strlen(fshader);
// vertex shader
GLuint vsid = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vsid, 1, &vshader, &vslen);
glCompileShader(vsid);
// fragment shader
GLuint fsid = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fsid, 1, &fshader, &fslen);
glCompileShader(fsid);
// create shader program and link
shader_prog_id = glCreateProgram();
glAttachShader(shader_prog_id, vsid);
glAttachShader(shader_prog_id, fsid);
glLinkProgram(shader_prog_id);
glUseProgram(shader_prog_id);
/* setup variable to be used in shader
GLint loc = glGetUniformLocation(shader_prog_id, "foo");
int foo = 5;
glUniform1i(loc, foo);
// */
return;
}
void compute_tex_coords() {
int tc_size = 2*cmap_size*cmap_size;
tex_coords = (GLfloat*)calloc(tc_size, sizeof(GLfloat));
check_for_null((void*)tex_coords, __FILE__, __LINE__);
int x, y;
for (y = 0; y < cmap_size; y++) {
for (x = 0; x < cmap_size; x++) {
int index = 2*(y*cmap_size + x);
#ifdef DEBUG_OLD
printf("xy(%d, %d) index = %d size = %d\n", x, y, index, tc_size);
#endif
check_array_bounds(index, tc_size, __FILE__, __LINE__);
tex_coords[index]= (float)x / cmap_size;
check_array_bounds(index + 1, tc_size, __FILE__, __LINE__);
tex_coords[index + 1] = (float)y / cmap_size;
}
}
return;
}