Jump to content
  • Advertisement
Sign in to follow this  
CirdanValen

Shader giving purple color

This topic is 2496 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!