Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: How do you guys load the source into a char*?

  1. #1
    Intern Contributor
    Join Date
    May 2003
    Posts
    50

    How do you guys load the source into a char*?

    On NeHe all they put is
    Code :
    char * my_vertex_shader_source;
    my_vertex_shader_source = GetVertexShaderSource();
     
     
    GLenum my_vertex_shader;
     
    my_vertex_shader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
     
    // Load Shader Sources
    glShaderSourceARB(my_vertex_shader, 1, &my_vertex_shader_source, NULL);
    But I was wondering what exactly char * my_vertex_shader_source needs to have in it, and how to read it... prefreably with ifstream

    also, random question.... with fragment shaders, do vertex normals have to be defined when rendering?

  2. #2
    Member Regular Contributor
    Join Date
    Feb 2001
    Location
    Montréal, QC, Canada
    Posts
    345

    Re: How do you guys load the source into a char*?

    Obviously some text either loaded from a file or in a const, like
    Code :
    char* pShader = "my_shader_inst1;my_shader_inst2;"
                         "my_shader_inst3;"
                         "my_shader_inst4;"
                         "...;"
                         "my_shader_instN;"
    or
    Code :
    char shader[1000];
    FILE *pFile = fopen( "shader.txt", "rt" );
    fread( shader, 1, 1000, pFile );
    fclose( pFile );

  3. #3
    Junior Member Newbie
    Join Date
    Sep 2004
    Location
    Germany
    Posts
    8

    Re: How do you guys load the source into a char*?

    well, simple answer to the random question: if you use the vertex normal yes, if you do not use it, no.

  4. #4
    Junior Member Regular Contributor
    Join Date
    Aug 2001
    Location
    Norrkoping, Sweden
    Posts
    119

    Re: How do you guys load the source into a char*?

    Originally posted by tfpsly:
    Obviously some text either loaded from a file or in a const, like (snip) or
    Code :
    char shader[1000];
    FILE *pFile = fopen( "shader.txt", "rt" );
    fread( shader, 1, 1000, pFile );
    fclose( pFile );
    Aargh! Fixed length buffer!
    No risk of overflow because at most 1000 characters are read, but to stay away from truncating shaders longer than 1000 bytes, find the true the file size, and allocate the string dynamically with the correct length.

    In C, this means using filelength(), which is declared in <fcntl.h>, not in <stdio.h>, and malloc(), declared in <malloc.h>.

    In C++, I wouldn't know. Im a hardware guy.

  5. #5
    Junior Member Regular Contributor
    Join Date
    Aug 2004
    Location
    Palo Alto, CA
    Posts
    111

    Re: How do you guys load the source into a char*?

    Code :
     /* Returns the size in bytes of the shader fileName.
       If an error occurred, it returns -1 */
    int Shader::ShaderSize(char * fName) const
    {
        int  count, shader;
        char name[100];
     
        strcpy(name, fName);
     
        /* Open the file */
        shader = _open(name, _O_RDONLY);
        if (shader == -1)
            return -1;
     
        /* Seek to the end and find its position */
        count = _lseek(shader, 0, SEEK_END);
     
        _close(shader);
     
        return count;
    }
     
     
    bool Shader::ReadShaderSource(char * fName, GLcharARB * & shader) const
    {
        int size;
     
        size = ShaderSize(fName);
     
        if(size == -1)
        {
            cerr << "Cannot determine size of the shader " << fName << endl;
            return false;
        }
     
        shader = new GLcharARB[size];
     
        ifstream  inf;
     
    	inf.open(fName, ios::in);
    	if(!inf.is_open())
    	{
    		cerr << "Could not open shader file " << fName << endl;
    		return false;
    	}
     
    	inf.read(shader, size);
     
    	inf.close();
     
    	shader[size] = '\0';
     
    	return true;
    }
    Also you need to add

    #include <io.h>
    #include <fcntl.h>

    Hoep this helps!

  6. #6
    Advanced Member Frequent Contributor
    Join Date
    Oct 2000
    Location
    Belgium
    Posts
    857

    Re: How do you guys load the source into a char*?

    Uh, guys? This an OpenGL forum. What does reading a text file into a memory buffer have to do with OpenGL? This is the type of thing that usually comes right after "hello world"!

    -- Tom

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •