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

On NeHe all they put is

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?

Obviously some text either loaded from a file or in a const, like

char* pShader = "my_shader_inst1;my_shader_inst2;"
                     "my_shader_inst3;"
                     "my_shader_inst4;"
                     "...;"
                     "my_shader_instN;"

or

char shader[1000];
FILE *pFile = fopen( "shader.txt", "rt" );
fread( shader, 1, 1000, pFile );
fclose( pFile );

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

Originally posted by tfpsly:
[b]Obviously some text either loaded from a file or in a const, like (snip) or

char shader[1000];
FILE *pFile = fopen( "shader.txt", "rt" );
fread( shader, 1, 1000, pFile );
fclose( pFile );

[/b]
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. :slight_smile:

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

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

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.