Shader Problems in C

Started by
4 comments, last by KumGame07 16 years, 3 months ago
I'm trying to get shaders working in my C OpenGL program. Below is all the code pertaining to the shaders for my program.
[source lang=c]
// Header File For Loading and Executing GLSL Shader Files
#define __ARB_ENABLE true
// #define EXT_INFO
#define MAX_EXTENSION_SPACE 10240
#define MAX_EXTENSION_LENGTH 256
bool multitextureSupported=false;
bool useMultitexture=true;
GLint maxTexelUnits=1;

PFNGLCREATEPROGRAMPROC glCreateProgram = NULL;
PFNGLATTACHSHADERPROC glAttachShader = NULL;
PFNGLLINKPROGRAMPROC glLinkProgram = NULL;
PFNGLUSEPROGRAMPROC glUseProgram = NULL;
PFNGLCREATESHADERPROC glCreateShader = NULL;
PFNGLSHADERSOURCEPROC glShaderSource = NULL;
PFNGLCOMPILESHADERPROC glCompileShader = NULL;

void loadExtensions()
{
	glCreateProgram = (PFNGLCREATEPROGRAMPROC) wglGetProcAddress("glCreateProgram");
	glAttachShader  = (PFNGLATTACHSHADERPROC)  wglGetProcAddress("glAttachShader");
	glLinkProgram   = (PFNGLLINKPROGRAMPROC)   wglGetProcAddress("glLinkProgram");
	glUseProgram    = (PFNGLUSEPROGRAMPROC)    wglGetProcAddress("glUseProgram");
	glCreateShader	= (PFNGLCREATESHADERPROC)  wglGetProcAddress("glCreateShader");
	glShaderSource  = (PFNGLSHADERSOURCEPROC)  wglGetProcAddress("glShaderSource");
	glCompileShader = (PFNGLCOMPILESHADERPROC) wglGetProcAddress("glCompileShader");

	GLuint v = glCreateShader(GL_VERTEX_SHADER);
	GLuint f = glCreateShader(GL_FRAGMENT_SHADER);
	GLuint p = glCreateProgram();
	char buffer[50];
	sprintf(buffer,"Creating Shader Program(ID:%i)",p);
	writeLog(buffer,"",TRUE);

	FILE *file;
	FILE *debug;
	sprintf(buffer,"Creating Vertex Shader(ID:%i)",v);
	if((file=fopen("Data\\Shaders\\vertex.shader","r"))==NULL)
	{
		writeLog(buffer,"",FALSE);
	} else {
		char vShaderSource[500];
		fscanf(file,vShaderSource);
		fclose(file);
		const GLchar * vs = vShaderSource;
		debug=fopen("shaderLog.txt","w");
		fputs(vs,debug);
		fclose(debug);
		glShaderSource(v,1,&vs,NULL);
		writeLog(buffer,"",TRUE);		
	}
	
	sprintf(buffer,"Creating Fragment Shader(ID:%i)",f);
	if((file=fopen("Data\\Shaders\\fragment.shader","r"))==NULL)
	{
		writeLog(buffer,"",FALSE);
	} else {
		char fShaderSource[500];
		fscanf(file,fShaderSource);
		fclose(file);

		const GLchar * fs = fShaderSource;
		glShaderSource(f,1,&fs,NULL);
		writeLog(buffer,"",TRUE);
	}

	glAttachShader(p,v);
	glAttachShader(p,f);

	glLinkProgram(p);

	glUseProgram(p);
}

On my computer the program still runs, but I know it's not using the shaders, because if I introduce an easily visible error, nothing changes. On some computers it just crashes out. Any suggestions? Cheers
Advertisement
It's better to have some package get the function pointers for you.
http://www.opengl.org/wiki/index.php/Getting_started

"OpenGL 2.0 and extensions" section explains what to do.

Quote:On my computer the program still runs, but I know it's not using the shaders, because if I introduce an easily visible error, nothing changes. On some computers it just crashes out.


Put safety in your code. Don't let it crash.
Check to see that at least GL 2.0 is supported.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
You can also use functions like glInfoLogARB (I think it is called) to get error messages from the GLSL compiler/linker, etc.
As V-man suggested you need to put safety codes. I think your shaders are not compiling fine. There are chances that the shader source that you are providing to GL is NULL as your 'fscanf' looks pretty strange to me.

Best Regards,KumGame07
Any chance of a hint as to the correct code to load the text from the file?
Google it. You get plenty of resources for file reading in C.
Anyways let me tell you what I do for reading shaders

if (fileName){		FILE *filePtr = fopen(fileName, "r");		fseek(filePtr, 0, SEEK_END);		fileSize = ftell(filePtr);		// Reset the file pointer to the beginning		fseek(filePtr, 0, SEEK_SET);				// Allocate memory		source = (char *)malloc(sizeof(char) * fileSize + 1);		// Read at once		fread(source, sizeof(char), fileSize, filePtr);		source[fileSize] = '\0';		// have a null terminated string}


EDIT : modified the code
Best Regards,KumGame07

This topic is closed to new replies.

Advertisement