PDA

View Full Version : gl_Vertex and gl_Position not accessable?



Hylke Donker
06-24-2006, 05:01 AM
Hello,
I'm reading the OpenGL Shading Language, and it had an example of how to transform temperature into color.
I've made my own version, but when I start my program I get the follow errors out the info log functions:

Fragment info
-------------
(0) : error C5052: gl_Vertex is not accessable in this profile
(0) : error C5052: gl_Position is not accessable in this profile
Here are my shaders:
vertex.shad

uniform float MaxTemp;
uniform float MixPercentageColor;
attribute float Incoming_Temperature;
varying vec3 InOut_Color;

void main()
{
float InOut_Temperature = (Incoming_Temperature - 273.0) / (MaxTemp - 273.0);
// Handle the r component
if(InOut_Temperature <= 0.5)
InOut_Color.r = 0.0;
else
InOut_Color.r = (InOut_Temperature-0.5)*2.0;
// Deal with the g component
if(InOut_Temperature <= 0.25)
InOut_Color.g = 0.0;
else if(InOut_Temperature <= 0.5)
InOut_Color.g = (InOut_Temperature-0.25)*4.0;
else
InOut_Color.g = 1.0 - (4.0 * InOut_Temperature);
// And lastly, set the b component
InOut_Color.b = 1.0 - 5.0 * InOut_Temperature;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}fragment.shad

uniform float MaxTemp;
uniform float MixPercentageColor;
varying vec3 InOut_Color;

void main()
{
gl_FragColor = mix(vec4(InOut_Color, 1.0), gl_Color, MixPercentageColor);
}I use the following code to activate the two shaders:

VShader = glCreateShader(GL_VERTEX_SHADER);
FShader = glCreateShader(GL_FRAGMENT_SHADER);
const GLchar *DataF = ReadFile("fragment.shad").c_str();
const GLchar *DataV = ReadFile("vertex.shad").c_str();
glShaderSource(VShader, 1, &amp;DataV, NULL);
glShaderSource(FShader, 1, &amp;DataF, NULL);
glCompileShader(VShader);
OutputShaderLog(VShader);
glCompileShader(FShader);
OutputShaderLog(FShader);
ShaderProgram = glCreateProgram();
glAttachShader(ShaderProgram, VShader);
glAttachShader(ShaderProgram, FShader);
glLinkProgram(ShaderProgram);
OutputProgramLog(ShaderProgram);
glUseProgram(ShaderProgram);

Uni_MaxTemp = glGetUniformLocation(ShaderProgram, "MaxTemp");
Uni_MixPercentageColor = glGetUniformLocation(ShaderProgram, "MixPercentageColor");
Attr_Incoming_Temperature = glGetAttribLocation(ShaderProgram, "Incoming_Temperature");
glUniform1f(Uni_MaxTemp, 500.0);
glUniform1f(Uni_MixPercentageColor, 0.0);Any idea what could be wrong with my shaders? Because to me, the shaders look alright.
thanks in advance,
Hylke

sqrt[-1]
06-24-2006, 06:11 AM
That error message looks wrong:

"Fragment info" ??!!

It seems you are probably loading your vertex shader into a fragment shader.

Edit:
I see your problem, it is a C++ code problem with these lines:

const GLchar *DataF = ReadFile("fragment.shad").c_str();
const GLchar *DataV = ReadFile("vertex.shad").c_str();

They should read:

string DataF = ReadFile("fragment.shad");
string DataV = ReadFile("vertex.shad");

Then when passing to OpenGL do:
DataF.c_str() and
DataV.c_str()

The reason being is that the previous code was taking a internal pointer to a temporary return string object that went out of scope and was deleted. Therefore, you got access to random memory. (eg. the other string data)

Hylke Donker
06-24-2006, 06:57 AM
Because I can't directly do &DataF.c_str() to the function(it complains about non-lvalue in unary `&').
I've changed it into this:

std::string DataFStr = ReadFile("fragment.shad");
std::string DataVStr = ReadFile("vertex.shad");
const GLchar *DataF = DataFStr.c_str();
const GLchar *DataV = DataVStr.c_str();
glShaderSource(VShader, 1, &amp;DataV, NULL);
glShaderSource(FShader, 1, &amp;DataF, NULL);
DataFStr.clear();
DataVStr.clear();And it seems to be working.
Thank you for pointing out the problem.
Hylke