Jump to content
  • Advertisement
Sign in to follow this  

[solved] GLSL malfunction

This topic is 3384 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);

	DATA = (char*)malloc(sizeof(char) * (flen + 1));
	flen = fread(DATA, sizeof(char), flen, fp);
	DATA[flen] = '\0';


	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);



	sp = glCreateProgram();

	glAttachShader(sp, vs);
	glAttachShader(sp, fs);


void deinitShader(void){
	glDetachShader(sp, vs);
	glDetachShader(sp, fs);


And here is my entire "main" function
void draw(void){


		glTranslatef(2.0f, 0.0f, -10.0f);
		glRotatef(angle, 0.0f, 1.0f, 0.0f);


		glTranslatef(-2.0f, 0.0f, -10.0f);
		glRotatef(angle, 0.0f, 1.0f, 0.0f);




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

void main(void){
	vec4 v = vec4(gl_Vertex);
	v.z = 0.0;
	glPosition = gl_ModelViewProjectionMatrix * v;

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]

Share this post

Link to post
Share on other sites
I missed that. Put it in but still a no go.
I added it to the bottom of my "initShader" function.

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
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 :)

Share this post

Link to post
Share on other sites
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 :)


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!