• Welcome to Dark Bit Factory & Gravity.
 

Loading A Text File / Shader

Started by Clyde, April 03, 2012

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

Clyde

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.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

Knurz

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

Clyde

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:

Knurz

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

hellfire

#4
Quote from: Clyde on April 03, 2012
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).
Challenge Trophies Won:

Clyde

#5
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.




   //===========
        // 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.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

hellfire

Quote from: Clyde on April 03, 2012
It makes perfect sense now you've enlightened me!!
Great :)

QuoteI 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:

Clyde

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:

hellfire

Quote from: Clyde 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:

Clyde

#9
When testing the contents with messagebox, it show at the end about seven 'I's with commas on top.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

Jim

#10
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

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
Challenge Trophies Won:

Clyde

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:

Clyde

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:

hellfire

Quote from: Clyde on April 05, 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:

Clyde

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:

hellfire

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:
void main() {
   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}


pixel:

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


Always check the shader compilation status after glCompileShader( shader ):

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:

Clyde

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: