Author Topic: Loading A Text File / Shader  (Read 3440 times)

0 Members and 1 Guest are viewing this topic.

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Loading A Text File / Shader
« on: April 03, 2012 »
Dear Demo Dudes,


I've done a bit more in the cpp world with visual studio 2010. And am concentrating on loading text files with fstream.

As soon as I use the command glCreateShader( type ); it will throw up that the loading and size code is bugged, but I don't know why that is, as if it's left commented out it goes through. Perhaps, there's another lib I need to include in the project properties.

Cheers in advanced,
Clyde.
« Last Edit: April 03, 2012 by Clyde »
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Knurz

  • Atari ST
  • ***
  • Posts: 100
  • Karma: 24
    • View Profile
Re: Loading A Text File / Shader
« Reply #1 on: April 03, 2012 »
Hi Clyde!

- Have you checked that the Text is loaded with no error codes and have you checked the content of your loaded File ? (MessageBox or something).

- Have you "bound" the function usign wglGetProcAdress ?

cu,
Knurz



Remember what the dormouse said: Feed your head

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Loading A Text File / Shader
« Reply #2 on: April 03, 2012 »
I have tried it out with MessageBox( NULL, ( LPCSTR ) file_data, "contents", MB_OK | MB_ICONEXCLAMATION );
and it's in some alien language.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Knurz

  • Atari ST
  • ***
  • Posts: 100
  • Karma: 24
    • View Profile
Re: Loading A Text File / Shader
« Reply #3 on: April 03, 2012 »
Are you catching any exceptions that may happen during the loading ?

It seems a little bit strange from my point of view that your program throws an error at this point of execution. Because glCreateShader(ARB) only reserves some memory and returns a Handle to this memory.

You say you are using glCreateShader ( TYPE ) - if you are not using .NET you should have to initialize this function by calling wglGetProcAddress("glCreateProgramObjectARB"); -  ( http://msdn.microsoft.com/en-us/library/dd374386(v=vs.85).aspx ).

cu.
Remember what the dormouse said: Feed your head

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1289
  • Karma: 466
    • View Profile
    • my stuff
Re: Loading A Text File / Shader
« Reply #4 on: April 03, 2012 »
Code: [Select]
ifstream file;
file.open( filename );
file_size=(int) file.tellg();
file_data= new  GLchar[ file_size ];
file.read( file_data,  file_size );
file.close();
tellg() returns the current position in the file - which is 0 because you just opened it.
With this information you're allocating zero space for text and read zero bytes of data.
So it's no surprise that your memory doesn't contain much readable text.
Why didn't you use the debugger?

To get the correct file size you need to jump to the end of the file (using seekg), read the current position (using tellg) and jump back to the beginning of the file (using seekg again).
« Last Edit: April 03, 2012 by hellfire »
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Loading A Text File / Shader
« Reply #5 on: April 03, 2012 »
Thanks Both!!

And surprisingly I used the debugger, that's how I knew about the zero size.

Didn't think too much of what tellg was infact telling me, so used to basic lingos doing alot of the work.

It makes perfect sense now you've enlightened me!!

goto the end to find the size. the beginning is 0.



Code: [Select]
   //===========
        // open text file.
        //===========
   file.open( filename );


   //=======================================================================
   // get correct size / length of file by looking for the end of the file.
   //=======================================================================   
   file.seekg( 0, ios::end );               
   
   //=================================================
   // tell size the current position ( ie ios::end ).
   //=================================================
   file_size=(int) file.tellg();         
   
   //===================================================
   // create the data buffer to match the size of file.
   //===================================================
   file_data=new GLchar[ file_size+1 ];
   
   //========================================================
   // start reading back at the beginning of file. 0 to size
   //========================================================
   file.seekg( 0, ios::beg );               
   
   //=======================================================
   // copy the contents of file over into file data buffer.
   //=======================================================
   file.read   ( &file_data[0],
               file_size );               
   //=============================
   // and finally close the file.
   //=============================
   file.close();
   
   //=========================================
   // last data entry an Arnold Schwazeneger.
   //=========================================
   file_data[ file_size ]=0;

   //=====================================
   // test out the contents of file_data.
   //=====================================
   MessageBox( NULL, ( LPCSTR ) file_data, "contents", MB_OK | MB_ICONEXCLAMATION );
}

I now only get a few extra bits of garbage. Capital I's with commas ontop.


Hope im right in making the last entry a null terminator.

Thanks So Much!!
-Clyde.
« Last Edit: April 03, 2012 by Clyde »
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1289
  • Karma: 466
    • View Profile
    • my stuff
Re: Loading A Text File / Shader
« Reply #6 on: April 03, 2012 »
It makes perfect sense now you've enlightened me!!
Great :)

Quote
I now only get a few extra bits of garbage. Capital I's with commas ontop.
Hope im right in making the last entry a null terminator.
It makes perfectly sense to null-terminate all strings.
But you won't need to do that to compile your shader-source as glShaderSource() expects the size of the string as a separate parameter.
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Loading A Text File / Shader
« Reply #7 on: April 03, 2012 »
Cool And Cheers!!

It still does crash out though.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1289
  • Karma: 466
    • View Profile
    • my stuff
Re: Loading A Text File / Shader
« Reply #8 on: April 03, 2012 »
Cool And Cheers!!

It still does crash out though.
Sure, first you have to create a window, a gl-context and call glewInit.
Otherwise the function pointer glCreateShader is uninitialized.
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Loading A Text File / Shader
« Reply #9 on: April 03, 2012 »
When testing the contents with messagebox, it show at the end about seven 'I's with commas on top.
« Last Edit: April 04, 2012 by Clyde »
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Loading A Text File / Shader
« Reply #10 on: April 04, 2012 »
That's likely to happen.

Strings are things with a 0 at the end.  Your file is just a binary blob.  Passing it to MessageBox makes MessageBox think it's a string when it isn't.  It doesn't know when to stop so is displaying whatever random crap is in RAM after your memory allocation.  This is bad.

Usefully, you've added 1 to the file length you loaded, so you can always do
Code: [Select]
file_data[file_size]=0;
after loading it and that should turn your binary blob into something MessageBox can eat.  And it will still work with the glShaderSource().
Though as Hellfire points out, you can't do anything much until you've created the window and a GL context.  Before you do that most of the GL functions will return an error.

ΜΜ
Jim
« Last Edit: April 04, 2012 by Jim »
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Loading A Text File / Shader
« Reply #11 on: April 04, 2012 »
Yep you found them eyebrow I's !! :)

There's a clue in your reply, it's a 'binary file'.

Needed to add: file.open( filename, ios::binary ); Othewize it wouldnt compile.

I now have a decent enough framework to experiment on. Thanks to all for pointing me in the right direction.
Speaking of null terminators, I'll be back! :)

Cheers!!
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Loading A Text File / Shader
« Reply #12 on: April 05, 2012 »
I'm asking for a book on gl shaders for my birthday but that wont be until late August. Hopefully there's still a good one available for older versions, if not I'm kinda stuffed until I win the Lottery.

In the meantime does anyone know where I can get some examples that are compatible with v2.2?

Cheers and don't eat too many eggs of the chocolate variety! :D
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1289
  • Karma: 466
    • View Profile
    • my stuff
Re: Loading A Text File / Shader
« Reply #13 on: April 06, 2012 »
I'm asking for a book on gl shaders for my birthday but that wont be until late August.
In the meantime does anyone know where I can get some examples that are compatible with v2.2?
The orange book: pdf
Basic GLSL examples: lighthouse3d.com

There's not much difference between shader-model 2.0/3.0 code, you just have to adhere some limitations, mainly:
- avoid conditional branching (if-else)
- the pixel shader can only have 96 instructions (which is usually difficult to tell)
- the vertex shader can't access textures
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Loading A Text File / Shader
« Reply #14 on: April 06, 2012 »
Great thanks for the help mate!!

When using el shader program and the display of the model is in glorious black. Is that because of incompatibility between the shader code and the version of glsl on the gfx card?

Or are there some other commands to use besides the basics, for example: glColor3f ( or is this a material command if using a texture ? ), glVertex glNormal3f glTexcoords2f etc. To apply the shaders code to the model info?

Thanks Heaps,
Clyde.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1289
  • Karma: 466
    • View Profile
    • my stuff
Re: Loading A Text File / Shader
« Reply #15 on: April 15, 2012 »
Oh, I somehow lost track of this thread.

You should start with a trivial pair of shaders which just transform the vertex position and fill the pixels with a constant color.

vertex:
Code: [Select]
void main() {
   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

pixel:
Code: [Select]
void main() {
  gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}

Always check the shader compilation status after glCompileShader( shader ):
Code: [Select]
GLint compileStatus;
glGetObjectParameteriv(shader, GL_OBJECT_COMPILE_STATUS_ARB, &compileStatus);
if (compileStatus!=GL_TRUE)
{
  GLint      infoLogLength;
  glGetObjectParameteriv(shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &infoLogLength);
  if (infoLogLength)
  {
    char *infoLog = new char[ infoLogLength ];
    glGetInfoLog(shader, infoLogLength, NULL, (GLcharARB*)infoLog);
    printf("shader compile error: %s \n", infoLog);
    delete[] infoLog;
  }
}
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Loading A Text File / Shader
« Reply #16 on: April 18, 2012 »
I'm making progress thanks for the top help Dudes!
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won: