[solved] GLSL malfunction

Started by
4 comments, last by tre 14 years, 7 months ago
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]
Advertisement
glUseProgram?
I missed that. Put it in but still a no go.
I added it to the bottom of my "initShader" function.
Well the only other thing that I can see is that you're missing a SwapBuffers call (if using Windows), and that you're not setting up a view port or any view matricies (meaning that by default, the origin (0, 0) is at the center of the window, and (-1, -1) represents the upper left, (1, 1) represents the lower right). Given that you're trying to draw something 10 units into the screen, it's possible that it could be clipped from view because you haven't specified the depth field.
The code above is not my entire project.
My project contains one set of .cpp and .h for the initialization of OpenGL and window creation, in which I make a SwapBuffers() call (SwapBuffers(window.hDC)). I understand that it's not really clear from my original post.
Sorry about that.

I also set up a viewport and matrices.

The problem isn't that nothing is seen in the window. The problem is that the vertex and fragment shaders do nothing, even though they should. The vertex shader should push all vertices to 0.0 on the z-axis.

Nothing happens except that the teapots spins around in space :)
After much, much, much frustration the problem is now solved.

It was a problem with the text files.
I don't know why. I had programmed them in Shader Designer 1.6 and everything works there but not in my code.
I scrapped the Shader Designer project, started a new one and poof, everything is working like a charm.

Don't know why.
If anyone's got a guess, please lay it on me :)

Thanks,
Marcus

This topic is closed to new replies.

Advertisement