View Full Version : How do you guys load the source into a char*?
c_olin
10-05-2004, 07:01 PM
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?
tfpsly
10-06-2004, 02:07 AM
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 );
Carjay
10-07-2004, 04:48 AM
well, simple answer to the random question: if you use the vertex normal yes, if you do not use it, no.
:)
StefanG
10-11-2004, 01:30 PM
Originally posted by tfpsly:
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 );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. :)
dimensionX
10-11-2004, 04:52 PM
/* 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!
Tom Nuydens
10-11-2004, 11:12 PM
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
Powered by vBulletin® Version 4.2.0 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.