Jump to content
  • Advertisement
Sign in to follow this  
AndyEsser

OpenGL Shader Problems in C

This topic is 3901 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

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

Share this post


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

Share this post


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

Share this post


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

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!