Jump to content
  • Advertisement
Sign in to follow this  
DigiMan Shart

OpenGL Setting an object's color with GLSL

This topic is 3550 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

Right now I'm working with shader's, GLSL, for my program in OpenGL, and I'm trying to set an object's color based off of a color passed into the shader from my OpenGL program. The program implementation somewhat works. The problem is that when I render multiple objects on the screen they all appear to be the same color, despite the fact that I pass in different colors for all the objects. I was wondering if anyone could give me advice on my problem or how to set an object's color based off of a shader. Thanks. Source Code: void cGraphicsManager::InitShaders(GLuint &programId) { static const GLchar* vertexShader = "varying vec3 lightDir,normal;" "void main()" "{" "lightDir = normalize(vec3(gl_LightSource[0].position));" "normal = gl_NormalMatrix * gl_Normal;" "gl_Position = ftransform();" "}"; static const GLchar* pixelShader = "uniform vec4 myColor[5];" "varying vec3 lightDir,normal;" "void main()" "{" "float intensity;" // normalizing the lights position to be on the safe side "vec3 n = normalize(normal);" "intensity = dot(lightDir,n);" "if (intensity > 0.9)" "gl_FragColor = vec4(myColor[0].r, myColor[0].g, myColor[0].b, myColor[0].a);" "else if (intensity > 0.7)" "gl_FragColor = vec4(myColor[1].r, myColor[1].g, myColor[1].b, myColor[1].a);" "else if (intensity > 0.4)" "gl_FragColor = vec4(myColor[2].r, myColor[2].g, myColor[2].b, myColor[2].a);" "else if (intensity > 0.1)" "gl_FragColor = vec4(myColor[3].r, myColor[3].g, myColor[3].b, myColor[3].a);" "else gl_FragColor = vec4(myColor[4].r, myColor[4].g, myColor[4].b, myColor[4].a);" "}"; //Array of our Shader IDs, 0 is Vertex Shader , 1 is Pixel Shader static GLuint shaderId[2]; //GLuint programId; GLint status; char logBuffer[1024]; GLsizei logLength; // ===================== // create vertex shader // ===================== shaderId[0] = glCreateShader(GL_VERTEX_SHADER); glShaderSource(shaderId[0], 1, &vertexShader, 0); glCompileShader(shaderId[0]); glGetShaderiv(shaderId[0], GL_COMPILE_STATUS, &status); if (!status) { glGetShaderInfoLog(shaderId[0], 1024, &logLength, logBuffer); printf("%d | %s\n", logLength, logBuffer); printf("Shader compile error 0!!\n"); glDeleteShader(shaderId[0]); shaderId[0] = 0; return; } // ==================== // create pixel shader // ==================== shaderId[1] = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource (shaderId[1], 1, &pixelShader, 0); glCompileShader (shaderId[1]); glGetShaderiv (shaderId[1], GL_COMPILE_STATUS, &status); if (!status) { glGetShaderInfoLog(shaderId[1], 1024, &logLength, logBuffer); printf("%d | %s\n", logLength, logBuffer); printf("Shader compile error 1!!\n"); glDeleteShader(shaderId[0]); glDeleteShader(shaderId[1]); shaderId[0] = 0; shaderId[1] = 0; return; } // =============== // create program // =============== programId = glCreateProgram(); glAttachShader (programId, shaderId[0]); glAttachShader (programId, shaderId[1]); glLinkProgram (programId); glGetProgramiv (programId, GL_LINK_STATUS, &status); if (!status) { glGetProgramInfoLog(programId, 1024, &logLength, logBuffer); printf("%d | %s\n", logLength, logBuffer); printf("Program link error!!\n"); glDeleteProgram(programId); glDeleteShader(shaderId[0]); glDeleteShader(shaderId[1]); programId = 0; shaderId[0] = 0; shaderId[1] = 0; return; } } /** Renders the scene using shaders. */ void cGraphicsManager::RenderShaders(cObject* obj , GLuint programId) { //If the shader program was created if (programId) { GLuint myColorLocation[5]; glUseProgram(programId); //Find location of variable myColor, which is an array, within shader myColorLocation[0] = glGetUniformLocation(programId, "myColor[0]"); myColorLocation[1] = glGetUniformLocation(programId, "myColor[1]"); myColorLocation[2] = glGetUniformLocation(programId, "myColor[2]"); myColorLocation[3] = glGetUniformLocation(programId, "myColor[3]"); myColorLocation[4] = glGetUniformLocation(programId, "myColor[4]"); if(RenderColor == 1)//Color is Red { //Pass in these parameters to "myColor" glUniform4f(myColorLocation[0], (float)obj->currColor_[0],(float)obj->currColor_[1],(float)obj->currColor_[2],(float)obj->currColor_[3] ); glUniform4f(myColorLocation[1], 0.8f,0.0f,0.0f,1.0f); glUniform4f(myColorLocation[2], 0.6f,0.0f,0.0f,1.0f); glUniform4f(myColorLocation[3], 0.4f,0.0f,0.0f,1.0f ); glUniform4f(myColorLocation[4], 0.2f,0.0f,0.0f,1.0f ); } else if(RenderColor == 2)//Color is Cyan { //Pass in these parameters to "myColor" glUniform4f(myColorLocation[0], (float)obj->currColor_[0],(float)obj->currColor_[1],(float)obj->currColor_[2],(float)obj->currColor_[3] ); glUniform4f(myColorLocation[1], 0.0f,0.8f,0.8f,1.0f); glUniform4f(myColorLocation[2], 0.0f,0.6f,0.6f,1.0f); glUniform4f(myColorLocation[3], 0.0f,0.4f,0.4f,1.0f ); glUniform4f(myColorLocation[4], 0.0f,0.2f,0.2f,1.0f ); } else//Color is white { //Pass in these parameters to "myColor" glUniform4f(myColorLocation[0], (float)obj->currColor_[0],(float)obj->currColor_[1],(float)obj->currColor_[2],(float)obj->currColor_[3] ); glUniform4f(myColorLocation[1], 0.8f,0.8f,0.8f,1.0f); glUniform4f(myColorLocation[2], 0.6f,0.6f,0.6f,1.0f); glUniform4f(myColorLocation[3], 0.4f,0.4f,0.4f,1.0f ); glUniform4f(myColorLocation[4], 0.2f,0.2f,0.2f,1.0f ); } } }

Share this post


Link to post
Share on other sites
Advertisement
Instead of writing
gl_FragColor = vec4(myColor[0].r, myColor[0].g, myColor[0].b, myColor[0].a);
you can simply write
gl_FragColor = myColor[0];

Also, I don't know why it is not working for you. Try to update your divers.

If that doesn't work, use a texture that contains the colors your need.


intensity = clamp(dot(lightDir,n), 0.0, 1.0);

gl_FragColor = texture2D(Texture, vec2(intensity, 0.0));

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!