Hello.
I'm trying to learn GLSL, in theory it's going pretty well, but in practice... not so good.
The problem is that the shaders doesn't seem to "take"/bind.
I am trying to just run some tests on two teapots. I've got a reader (readShader) and I've got an init function (initShader) and a deinit function (deinitShader). I call the "initShader" function in my "main" function before drawing anything, and I call the "deinitShader" just before the glFlush call.
The shader I'm trying to get to work is a simple one where I'm just zeroing the z-axis in a vertex shader.
The code:
char* vs_src;
char* fs_src;
int vs, fs;
int sp;
char* readShader(char* fn){
FILE* fp;
char* DATA = NULL;
int flen = 0;
fp = fopen(fn, "rt");
fseek(fp, 0, SEEK_END);
flen = ftell(fp);
rewind(fp);
DATA = (char*)malloc(sizeof(char) * (flen + 1));
flen = fread(DATA, sizeof(char), flen, fp);
DATA[flen] = '\0';
fclose(fp);
return DATA;
}
void initShader(void){
vs = glCreateShader(GL_VERTEX_SHADER);
fs = glCreateShader(GL_FRAGMENT_SHADER);
vs_src = readShader("shaders/test.vs");
fs_src = readShader("shaders/test.fs");
const char* VS = vs_src;
const char* FS = fs_src;
glShaderSource(vs, 1, &VS, NULL);
glShaderSource(fs, 1, &FS, NULL);
free(vs_src);
free(fs_src);
glCompileShader(vs);
glCompileShader(fs);
sp = glCreateProgram();
glAttachShader(sp, vs);
glAttachShader(sp, fs);
glLinkProgram(sp);
}
void deinitShader(void){
glDetachShader(sp, vs);
glDetachShader(sp, fs);
glDeleteShader(sp);
}
And here is my entire "main" function
void draw(void){
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
initShader();
glPushMatrix();
glTranslatef(2.0f, 0.0f, -10.0f);
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glutSolidTeapot(1);
glPopMatrix();
glPushMatrix();
glTranslatef(-2.0f, 0.0f, -10.0f);
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glutSolidTeapot(1);
glPopMatrix();
deinitShader();
glFlush();
}
I have tried moving the initShader and deinitShader calls inside the push and pop calls. I tried removing the push and pop calls. I tried renaming the .vs and .fs files. Tried saving them in a different format (ANSI/UNICODE).
Nothing works.
Here are the shaders
// VERTEX SHADER
void main(void){
vec4 v = vec4(gl_Vertex);
v.z = 0.0;
glPosition = gl_ModelViewProjectionMatrix * v;
}
// FRAGMENT SHADER
void main(void){
gl_FragColor = gl_Color;
}
If I'm forgetting something, please let me know.
Thank you.
[Edited by - tre on September 13, 2009 5:32:26 AM]