Jump to content
  • Advertisement
Sign in to follow this  
PGSCreativeDirector

Text Loading problem prehaps?

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

can someone tell me if this is how you read a text file - this is for glsl coding so that is why the extension is called .frag not .txt :)

//Text Loader
FILE *ColorKeyFilePointer = fopen("ColorKeyShader.frag", "r");
fseek(ColorKeyFilePointer, 0, SEEK_END);
int numBytes = ftell (ColorKeyFilePointer);


GLchar *str = new GLchar [numBytes+1];


rewind(ColorKeyFilePointer);
fread(str, 1, numBytes, ColorKeyFilePointer);
fclose(ColorKeyFilePointer);
str[numBytes] ='\0';

Share this post


Link to post
Share on other sites
Advertisement
I can't see something wrong with it (someone else might) but if the code is not performance critical, a standard class for strings would be safer and easier to use when things get more complicated. :)

Share this post


Link to post
Share on other sites

I can't see something wrong with it (someone else might) but if the code is not performance critical, a standard class for strings would be safer and easier to use when things get more complicated. :)


must be my shader stuff hmm ill have to keep looking ill figure it out eventaully as always :P

EDIT:

actually can you tell me if this is wrong in someway maybe im missing something?

FILE *ColorKeyFilePointer = fopen("ColorKeyShader.frag", "r");
fseek(ColorKeyFilePointer, 0, SEEK_END);
int numBytes = ftell (ColorKeyFilePointer);


GLchar *str = new GLchar [numBytes+1];


rewind(ColorKeyFilePointer);
fread(str, 1, numBytes, ColorKeyFilePointer);
fclose(ColorKeyFilePointer);
str[numBytes] ='\0';
//////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////
//Shader
GLuint Shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(Shader, 1,(const GLchar**)&str,NULL);
glCompileShader(Shader);




////////////////////////////////////////////////////////


////////////////////////////////////////////////////////
//Creating The Program

GLuint program = glCreateProgram();
glAttachShader(program, Shader);

////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////
//Linking The Program

glLinkProgram(program);
//////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////
//Using The Program

glUseProgram(program);
////////////////////////////////////////////////////////



heres the shader code also




#version 120

void main()
{
gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
}

Share this post


Link to post
Share on other sites

Why are you adding the '\0'?


very good question haha ill change that where the fuck did that come from must have not been awake hehe

EDIT:

dam i just cant get my head around this problem it just won't show the shader its rather weird


str[numBytes] = 0;

Share this post


Link to post
Share on other sites

[quote name='Kobo' timestamp='1319132511' post='4874761']
Why are you adding the '\0'?


very good question haha ill change that where the fuck did that come from must have not been awake hehe
[/quote]

The null character is used to terminate the string and removing it may cause access violation when giving the string to a method that don't know the size of the array.

Share this post


Link to post
Share on other sites

[quote name='PGSCreativeDirector' timestamp='1319132555' post='4874762']
[quote name='Kobo' timestamp='1319132511' post='4874761']
Why are you adding the '\0'?


very good question haha ill change that where the fuck did that come from must have not been awake hehe
[/quote]

The null character is used to terminate the string and removing it may cause access violation when giving the string to a method that don't know the size of the array.
[/quote]

Thanks for that just changed it now the only issue the fragments are not Green

Share this post


Link to post
Share on other sites

Your shader outputs green, not red. And thanks for editing the language.


no matter what i try i can't get my shader to compile

im using glee in my project as a .c file iv also included header file using code::Blocks

heres my shader code

#version 120
void main(void)
{
gl_FragColor = vec4(1.0,0.0,0.0,1.0);
}




and heres my actual text loading,etc code



/////////////////////////////////////////////////////////
//Text Loader
FILE *ColorKeyFilePointer = fopen("ColorKeyShader.frag", "r");
fseek(ColorKeyFilePointer, 0, SEEK_END);
int numBytes = ftell (ColorKeyFilePointer);

GLchar *str = new GLchar [numBytes+1];


rewind(ColorKeyFilePointer);
fread(str, 1, numBytes, ColorKeyFilePointer);
fclose(ColorKeyFilePointer);
str[numBytes] = NULL ;//'\o'
//////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////
//Shader

GLuint Shader;
GLuint ShaderType = GL_FRAGMENT_SHADER;
Shader = glCreateShader(ShaderType);
glShaderSource(Shader, 1, (const GLchar**) &str, NULL);
glCompileShader(Shader);
//Error Check Shader
int status;
int logLength;
GLchar* log;
GLint location;

glGetShaderiv(Shader, GL_COMPILE_STATUS, &status);
if(status == FALSE)
{
glGetShaderiv(Shader, GL_INFO_LOG_LENGTH, &logLength);
fprintf(stdout, "FragmentShader Compilation Failed!\n");
log = new GLchar [logLength];
glGetShaderInfoLog( Shader, logLength, NULL, log);
fprintf( stdout, "\n%s\n", log);
delete [] log;
exit(1);
}

////////////////////////////////////////////////////////


////////////////////////////////////////////////////////
//Creating The Program

GLuint program = glCreateProgram();

glAttachShader(program, Shader);

////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////
//Linking The Program
glLinkProgram(program);

//////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////
//Using The Program

glUseProgram(program);
////////////////////////////////////////////////////////

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!