Skip to main content
gamedev.net
🔒 Locked

Text Loading problem prehaps?

Started by PGSCreativeDirector 32 replies 4.2k views
Original Post
PGSCreativeDirector
PGSCreativeDirector
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';
Dawoodoz
Dawoodoz
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.
PGSCreativeDirector
PGSCreativeDirector

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

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);
}
Kobo
Kobo
Why are you adding the '\0'?
PGSCreativeDirector
PGSCreativeDirector

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;
Dawoodoz
Dawoodoz
I have only used OpenGL in school because I prefer DirectX.
Dawoodoz
Dawoodoz

[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.
PGSCreativeDirector
PGSCreativeDirector

[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
PGSCreativeDirector
PGSCreativeDirector

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);
////////////////////////////////////////////////////////
Maarten X
Maarten X
Is the text file you're trying to load in Unicode format by any chance? Try to save it as ASCII, maybe that fixes the problem.
PGSCreativeDirector
PGSCreativeDirector
ahh see it doesn't show nothing and i moved to glew and now i got a problem with compiling my actual application the compiler says terminated with status 3(0 minutes, 0seconds) have no idea why i put my post here My link to see if someone knew a fix but no response in the last few hours and literally about to burst in the next few minutes since i have been trying to get shaders to just work for about 2 days with no luck i spent most of my time trying to figure it out but now im just stuck stuck in a holee but im not gunna let it get too me not until i get it fixed


EDIT:

i went back to glee and trying what you said and it didn't compile the shader
FLeBlanc
FLeBlanc
The OpenGL forums was probably not the correct place to ask that, as it's likely not GL related. I've seen other people having this problem before, usually due to bugs/access-violations, something borked in the build environment, etc... Having libraries compiled with different versions of MinGW can also cause this type of issue. You could try going through your build environment a step at a time, making sure everything works, from Hello World on up to GL applications. Once you've assured yourself that your build environment works, then you can start tackling the more complex stuff, like loading strings from files and ensuring that you are doing that correctly. Then you can phase in shader compiling, once you are assured the build environment works.
PGSCreativeDirector
PGSCreativeDirector
i have been doing that for well now its been like 18hours of constant checking and tweaking on and off glee and glew not kidding***

glee compiles but doesn't compile the shader
^^ this seems logical
extension functions don't compile

debug has no effect

i also took alook at my shader code and locations of the file and put them in 3 different places to represent all the many possiblites that the fopen could be looking for it near the exe,near the debug folder and on the cpp file area




glew doesn't compile and terminates the program with a status of 3
^^
this is not logical
and the functions don't work because it hasn't compiled
shaders don't compile because of that


moving linker flags don't work

adding glew .a file to libs of the compiler link doesn't work


adding the include and lib folders to code::blocks doesn't work

adding glew as a c file in my project doesn't work it dont compile with my application even with the header

debugging glew shows that its related to sdl main plus the function glcreateshader which is weird but who knows its part of the extensions
FLeBlanc
FLeBlanc
Wait, you are able to debug the build with Glew, and it says it's related to SDL_main? So the Glew build does compile, but it gives you the error on execution. Is that correct?

Are you sure that you are calling glewInit() before trying to use any extension pointers? Are you certain the glCreateShader pointer is valid? Are you checking to be sure the extension is supported by your hardware? Call glewIsSupported() with the required extension strings to find out if the driver supports what you need. If it is not supported, or if you didn't call glewInit(), then calling the pointers for extensions would be a Bad Thing.
PGSCreativeDirector
PGSCreativeDirector
This is what i get http://gyazo.com/ed3...5dd3dab90fd78ec



this is my shader compilation stuff



GLuint Shader;

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

if(Shader)
{
fprintf(stderr, "doesnt work!d");
}
else
{
fprintf(stderr, "Does Work!d");
}




this is the actual glsl shader code

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



this is the text loading stuff

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'


and of course
glewInit();


my graphics card supports up to OpenGL 3.3 version so it must support glsl 3.3 got ATI 1GB Graphics card i can play any modern opengl game for example Amensia of the dark descent or OverGrowth
FLeBlanc
FLeBlanc
Yeah, it looks like you're calling a null pointer there. The tip off is the line 0x00000000 in ?. Some pointer you are calling is initialized to 0.
PGSCreativeDirector
PGSCreativeDirector
this is sooooo annoying lol


and somewhere is initialized to 0? is causing this problem??

if i take away glew and anything shader related it compiles fine

heres the hole cpp file for the shader loading ,etc what is wrong with this that you can see if you can be specific please do because it won't compile and debug is the only information i have to that this will help towards
#include "glew.h"
#define GLEW_STATIC


#include "SDL/SDL.h"
#include "SDL/SDL_image.h"

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
using namespace std;



void ColorKey(void)
{



/////////////////////////////////////////////////////////
//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

if(Shader)
{
fprintf(stderr, "doesnt work!d");
}
else
{
fprintf(stderr, "Does Work!d");
}


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

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

GLuint program;
program = glCreateProgram();

glAttachShader(program, Shader);

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


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

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

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

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



}
FLeBlanc
FLeBlanc
Are you checking the return value of glewInit()? From what I see, the only real possibilities in the listed function for de-referencing a null pointer would be the GL calls not being set by Glew, so perhaps the output of glewInit() and glewGetErrorString() would be helpful.

I've encountered issues before where someone had a decent graphics card, but were still running out-of-the-box Microsoft OpenGL drivers, which are OpenGL version 1.1 and will result in a failure on Glew's part to initialize.

Topic Locked

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

Sign in to reply to this topic.