Skip to main content
GameDev.net gamedev.net
🔒 Locked

Shader Problems in C

Started by AndyEsser Jan 16, 2008 at 8:30 AM 4 replies 3.9k views
Original Post
AndyEsser
AndyEsser
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
V-man
V-man
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.
smitty1276
smitty1276
You can also use functions like glInfoLogARB (I think it is called) to get error messages from the GLSL compiler/linker, etc.
KumGame07
KumGame07
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
AndyEsser
AndyEsser
Any chance of a hint as to the correct code to load the text from the file?
KumGame07
KumGame07
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.