• Advertisement
Sign in to follow this  

Shader giving purple color

This topic is 2171 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on getting shaders working for the first time, and run into a problem. Using the following two shaders, my rectangle renders a purplish color.

void main(void)
{
gl_Position = ftransform();
}


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


Since the color is not red, and changing the alpha value has no effect...I assume the problem is with how I'm compiling the shaders?


ShaderProgram* ResourceLoader::getShaderProgram(const std::string &vertexShader, const std::string &fragmentShader) {
const char *vertexSource = NULL;
const char *fragmentSource = NULL;
GLuint fragmentProgram;
GLuint vertexProgram;

for(std::list<std::string>::iterator itr = mPaths.begin(); itr != mPaths.end(); itr++) {
if(fileExists((*itr)+vertexShader)) {
// Load vertexShader
vertexSource = loadShaderSource((*itr)+vertexShader);

// Compile shader
vertexProgram = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexProgram, 1, &vertexSource, NULL);
glCompileShader(vertexProgram);

}

if(fileExists((*itr)+fragmentShader)) {
// load fragmentShader
fragmentSource = loadShaderSource((*itr)+fragmentShader);

// Compile shader
fragmentProgram = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentProgram, 1, &fragmentSource, NULL);
glCompileShader(fragmentProgram);
}

// if both shaders were loaded, return the ShaderProgram
if(vertexSource && fragmentSource) {
// Compile the shader program
GLuint program = glCreateProgram();

glAttachShader(program, vertexProgram);
glAttachShader(program, fragmentProgram);

glLinkProgram(program);
ShaderProgram* result = new ShaderProgram(program);

// Release the buffers allocated in loadShaderSource
delete[] vertexSource;
delete[] fragmentSource;

// Return the ShaderProgram
return result;
}
}

// If the shader is incomplete (missing one of the two shader programs)
// make sure we don't leave allocated buffer behind
if(vertexSource)
delete[] vertexSource;

if(fragmentSource)
delete[] fragmentSource;
}


void GraphicsDevice::useShader(ShaderProgram* p) {
glUseProgram(p->getId());
}

void GraphicsDevice::clearShader() {
glUseProgram(0);
}

Share this post


Link to post
Share on other sites
Advertisement
It is always a good idea to check for errors. Even if your shader is simple.

Also, always specify a GLSL version
http://www.opengl.org/wiki/GLSL_:_recommendations

Share this post


Link to post
Share on other sites

Do you have any kind of blending mode enabled?


Nope


Framebuffer OK?


Yea, my FBO is returning gl_framebuffer_complete


It is always a good idea to check for errors. Even if your shader is simple.

Also, always specify a GLSL version
http://www.opengl.or...recommendations


Okay, I'll put in some checks and see what happens when I get home

Share this post


Link to post
Share on other sites
Hidden
Alright, so I added error checking for the shaders and I get this output:

rtex shader was successfully compiled to run on hardware.
ð­º
ð­º
ð­Fragment shader failed to compile with the following errors:
ERROR: 0:1: error(#132) Syntax error: '<' parse error
ERROR: error(#273) 1 compilation errors. No code generated
­º
𭺫«««hader(s) were not successfully compiled before glLinkProgram() was called. Link failed.
ð­º
ð­º
ð­


So for whatever reason, my file reading code is adding a bunch of random crap to the shader and is causing it to fail. I have no idea why it's doing this. angry.png

std::ifstream file;
std::stringstream buffer;

// First load the vertex shader
file.open(filename.c_str());

// Read into buffer
buffer << file.rdbuf();

file.close();

return buffer.str();


When I view "buffer" in the debugger, I see my shader code with a bunch of random crap appended to the end.

Share this post


Link to post
EDIT: Okay, I figured it out. It was a problem converting std::string to const char * for opengl that was causing problems.

Alrighty, I added shader logging and got this output:

Vertex: Vertex shader was successfully compiled to run on hardware.
Fragment: Fragment shader failed to compile with the following errors:
ERROR: 0:1: error(#132) Syntax error: '<' parse error
ERROR: error(#273) 1 compilation errors. No code generated
­Fragment shader(s) were not successfully compiled before glLinkProgram() was called. Link failed.


So for whatever reason, my file reading code is adding random crap to the end of the files which is causing it to fail. angry.png

std::ifstream file;
std::stringstream buffer;

// First load the vertex shader
file.open(filename.c_str());

// Read into buffer
buffer << file.rdbuf();

file.close();

return buffer.str();

Share this post


Link to post
Share on other sites

EDIT: Okay, I figured it out. It was a problem converting std::string to const char * for opengl that was causing problems.

Alrighty, I added shader logging and got this output:

Vertex: Vertex shader was successfully compiled to run on hardware.
Fragment: Fragment shader failed to compile with the following errors:
ERROR: 0:1: error(#132) Syntax error: '<' parse error
ERROR: error(#273) 1 compilation errors. No code generated
­Fragment shader(s) were not successfully compiled before glLinkProgram() was called. Link failed.


So for whatever reason, my file reading code is adding random crap to the end of the files which is causing it to fail. angry.png

std::ifstream file;
std::stringstream buffer;

// First load the vertex shader
file.open(filename.c_str());

// Read into buffer
buffer << file.rdbuf();

file.close();

return buffer.str();



always output the feedback from the APIs. It's there to help you solve your problems.

Share this post


Link to post
Share on other sites
More problems, and no errors in the infologs.


Really banging my head against the desk with this problem. Been stabing around, tirelessly searching google, trying every combination possible...and I just can't get this to work.

I am rendering sprites to a Framebuffer. I know this is working perfectly fine because I can render out the framebuffer and see exactly what I drew to it. I want to pass this fbo texture to a shader, so I can eventually do light calculations. For what ever reason, it just will not work for me at all. I am following what various tutorials and sample code has told me, but it just will not work. >:(. My shader is compiling and linking without errors. I can set the gl_fragcolor to a color in the shader with great success. It's using the texture that is giving me problems. Right now, when I set gl_fragcolor to the texture, I get a dark gray color.

Vertex shader:
#version 120

varying vec4 texCoord;

void main()
{
texCoord = gl_MultiTexCoord0;
gl_Position = ftransform();

}


Fragment shader
#version 120

uniform sampler2D diffuseMap;
uniform sampler2D lightingMap;

varying vec4 texCoord;

void main()
{
vec4 color = texture2D(diffuseMap, gl_TexCoord[0].st);
vec4 light = texture2D(lightingMap, texCoord.st);

gl_FragColor = color;
}


How I'm using the shader.
graphics->useShader(mLightingCombinedShader);
mLightingCombinedShader->setUniformTexture("diffuseMap", mDiffuse->getTextureId());
graphics->drawPrimitive(mScreenRect, 0, 0);
graphics->clearShader();


And the relavant code:



void ShaderProgram::setUniformTexture(const std::string& name, GLuint t) {
GLint var = getUniformLocation(name);
glActiveTexture(GL_TEXTURE0 + ShaderProgram::mTextureUnits);
glBindTexture(GL_TEXTURE_2D, t);
glUniform1i(var, ShaderProgram::mTextureUnits);
}

void GraphicsDevice::useShader(ShaderProgram* p) {
glUseProgram(p->getId());
}

void GraphicsDevice::clearShader() {
glUseProgram(0);
}

void GraphicsDevice::drawPrimitive(Primitive *p, float x, float y) {
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, p->getVbo());

glPushMatrix();

glTranslatef(x, y, 0.f);

glVertexPointer(2, GL_FLOAT, sizeof(Vertex), (void*)offsetof(Vertex, x));
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), (void*)offsetof(Vertex, tx));

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glPopMatrix();

glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableClientState(GL_VERTEX_ARRAY);

}

Share this post


Link to post
Share on other sites
You are only enabling vertices with glEnableClientState(GL_VERTEX_ARRAY); but you are not enabling texcoords.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement