Jump to content

  • Log In with Google      Sign In   
  • Create Account


Shader giving purple color


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 CirdanValen   Members   -  Reputation: 177

Like
0Likes
Like

Posted 08 February 2012 - 01:11 PM

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);
}


Sponsor:

#2 Yours3!f   Members   -  Reputation: 1299

Like
0Likes
Like

Posted 08 February 2012 - 02:31 PM

are you sure that you are using rgba mode?

#3 CirdanValen   Members   -  Reputation: 177

Like
0Likes
Like

Posted 09 February 2012 - 12:32 AM

I don't know, I didn't set it to anything else?

#4 Hodgman   Moderators   -  Reputation: 29400

Like
0Likes
Like

Posted 09 February 2012 - 01:05 AM

Do you have any kind of blending mode enabled?

#5 xynapse   Members   -  Reputation: 151

Like
0Likes
Like

Posted 09 February 2012 - 02:55 AM

Framebuffer OK?
perfection.is.the.key

#6 V-man   Members   -  Reputation: 805

Like
0Likes
Like

Posted 09 February 2012 - 10:12 AM

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
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

#7 CirdanValen   Members   -  Reputation: 177

Like
0Likes
Like

Posted 09 February 2012 - 12:09 PM

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

#8 CirdanValen   Members   -  Reputation: 177

Like
0Likes
Like

Posted 10 February 2012 - 11:57 AM

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. Posted Image

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();


#9 Yours3!f   Members   -  Reputation: 1299

Like
0Likes
Like

Posted 10 February 2012 - 12:20 PM

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. Posted Image

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.

#10 CirdanValen   Members   -  Reputation: 177

Like
0Likes
Like

Posted 12 February 2012 - 02:07 AM

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);

}



#11 V-man   Members   -  Reputation: 805

Like
0Likes
Like

Posted 12 February 2012 - 09:45 AM

You are only enabling vertices with glEnableClientState(GL_VERTEX_ARRAY); but you are not enabling texcoords.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

#12 CirdanValen   Members   -  Reputation: 177

Like
0Likes
Like

Posted 12 February 2012 - 07:47 PM

Aha! Such a basic fix Posted Image . I am stoked I was able to get this working. Really appreciate your help!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS