• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
CirdanValen

Shader giving purple color

11 posts in this topic

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.

[code]void main(void)
{
gl_Position = ftransform();
}


void main(void)
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}[/code]

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?

[code]
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;
}
[/code]

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

void GraphicsDevice::clearShader() {
glUseProgram(0);
}[/code]
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
[quote name='Hodgman' timestamp='1328771111' post='4911207']
Do you have any kind of blending mode enabled?
[/quote]

Nope

[quote name='xynapse' timestamp='1328777753' post='4911223']
Framebuffer OK?
[/quote]

Yea, my FBO is returning gl_framebuffer_complete

[quote name='V-man' timestamp='1328803957' post='4911345']
It is always a good idea to check for errors. Even if your shader is simple.

Also, always specify a GLSL version
[url="http://www.opengl.org/wiki/GLSL_:_recommendations"]http://www.opengl.or...recommendations[/url]
[/quote]

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

Share this post


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

[code]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.
ð­º
ð­º
ð­[/code]

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. [img]http://public.gamedev.net//public/style_emoticons/default/angry.png[/img]

[code]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();[/code]

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

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:

[code]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. [/code]

So for whatever reason, my file reading code is adding random crap to the end of the files which is causing it to fail. [img]http://public.gamedev.net//public/style_emoticons/default/angry.png[/img]

[code]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();[/code]
0

Share this post


Link to post
Share on other sites
[quote name='CirdanValen' timestamp='1328896662' post='4911735']
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:

[code]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. [/code]

So for whatever reason, my file reading code is adding random crap to the end of the files which is causing it to fail. [img]http://public.gamedev.net//public/style_emoticons/default/angry.png[/img]

[code]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();[/code]
[/quote]

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

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:
[code]#version 120

varying vec4 texCoord;

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

}[/code]

Fragment shader
[code]#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;
}[/code]

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

And the relavant code:

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

}

[/code]
0

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.
0

Share this post


Link to post
Share on other sites
Aha! Such a basic fix [img]http://public.gamedev.net//public/style_emoticons/default/sleep.png[/img] . I am stoked I was able to get this working. Really appreciate your help!
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0