forked from QianMo/GPU-Gems-Book-Source-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfragprog.h
130 lines (109 loc) · 3.52 KB
/
fragprog.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#ifndef _FRAGPROG_H
#define _FRAGPROG_H
const int maxparams = 8;
const int channels = 4;
const int pnamelen = 16;
class FragProg{
public:
FragProg(){}
~FragProg(){}
void Load(const char* progname, char* prog){
glGenProgramsNV( 1, &prog_id );
glLoadProgramNV( GL_FRAGMENT_PROGRAM_NV, prog_id, strlen(prog), (GLubyte*)prog );
GLenum error = glGetError();
if( error != GL_NO_ERROR )
fprintf( stderr, "ERROR\n%s\n", gluErrorString( error ) );
else
printf("Loaded program [%s] (id %i) successfully\n", progname, prog_id);
}
void Load(char* filename){
glGenProgramsNV( 1, &prog_id );
char buf[512];
char prog[16384*16];
char *s = prog;
FILE *fp = fopen(filename, "r");
if (!fp)
{
fprintf(stderr, "Couldn't open file [%s].\n", filename);
exit(1);
}
while (!feof(fp))
{
if (!fgets(buf, 511, fp))
{
break;
}
s += sprintf(s, "%s", buf);
}
fclose(fp);
glLoadProgramNV( GL_FRAGMENT_PROGRAM_NV, prog_id, strlen(prog), (GLubyte*)prog );
GLenum error = glGetError();
if( error != GL_NO_ERROR )
fprintf( stderr, "ERROR\n%s\n", gluErrorString( error ) );
else
printf("Loaded program [%s] (id %i) successfully\n", filename, prog_id);
}
void Bind(){
glEnable( GL_FRAGMENT_PROGRAM_NV );
glBindProgramNV( GL_FRAGMENT_PROGRAM_NV, prog_id );
GLenum error = glGetError();
if( error != GL_NO_ERROR )
fprintf( stderr, "ERROR - Bind()\n%s\n", gluErrorString( error ) );
}
void Release(){
glDisable( GL_FRAGMENT_PROGRAM_NV );
GLenum error = glGetError();
if( error != GL_NO_ERROR )
fprintf( stderr, "ERROR - Release()\n%s\n", gluErrorString( error ) );
}
void SetParameter1f(const char* pname, float x){
glProgramNamedParameter4fNV(prog_id, strlen(pname), (GLubyte*)(pname), x, 0, 0, 0);
}
void SetParameter2f(const char* pname, float x, float y){
glProgramNamedParameter4fNV(prog_id, strlen(pname), (GLubyte*)(pname), x, y, 0, 0);
}
void SetParameter3f(const char* pname, float x, float y, float z){
glProgramNamedParameter4fNV(prog_id, strlen(pname), (GLubyte*)(pname), x, y, z, 0);
}
void SetParameter4f(const char* pname, float x, float y, float z, float w){
glProgramNamedParameter4fNV(prog_id, strlen(pname), (GLubyte*)(pname), x, y, z, w);
}
void Run( int minx, int miny, int maxx, int maxy ){
glViewport(0,0,maxx, maxy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, maxx, 0.0, maxy);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glBegin(GL_QUADS);
glVertex2f((float) 0, (float)0);
glVertex2f((float) maxx, (float) 0);
glVertex2f((float) maxx, (float) maxy);
glVertex2f((float) 0, (float) maxy);
glEnd();
glFlush();
}
void Run1( int minx, int miny, int maxx, int maxy,
int mins, int mint, int maxs, int maxt){
glViewport(0,0,maxx, maxy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, maxx, 0.0, maxy);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glBegin(GL_QUADS);
glTexCoord2f ((float) mins, (float) mint);
glVertex2f ((float) minx, (float) miny);
glTexCoord2f ((float) maxs, (float) mint);
glVertex2f ((float) maxx, (float) miny);
glTexCoord2f ((float) maxs, (float) maxt);
glVertex2f ((float) maxx, (float) maxy);
glTexCoord2f ((float) mins, (float) maxt);
glVertex2f ((float) minx, (float) maxy);
glEnd();
glFlush();
}
//private:
GLuint prog_id;
};
#endif /* _FRAGPROG_H */