PDA

View Full Version : Color not displaying



loveanirban
11-06-2015, 07:20 PM
Hi All,

I am pretty much new to OPENGL. Below is the code i am using to draw triangle. But it is not displaying white triangle colors are not coming.

GLuint ShaderProgam;
void changeViewPort(int w, int h)
{
glViewport(0, 0, w, h);
}
GLuint InitShader(GLenum type, const char *filename)
{
GLuint shader = glCreateShader(type);
GLint compiled;

string str = textFileRead(filename);
GLchar * cstr = new GLchar[str.size() + 1];
const GLchar * cstr2 = cstr; // Weirdness to get a const char
strcpy(cstr, str.c_str());
glShaderSource(shader, 1, &cstr2, NULL);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
GLenum ErrorCheckValue = glGetError();
if (ErrorCheckValue != GL_NO_ERROR)
{
fprintf(
stderr,
"ERROR: Could not create the shaders: %s \n",
gluErrorString(ErrorCheckValue)
);
}
return shader;
}
void init()
{
GLuint
VertexShaderId,
FragmentShaderId,
ProgramId,
VaoId,
VboId,
ColorBufferId;
glewInit();
GLfloat Vertices[] = {
-0.8f, -0.8f, 0.0f, 1.0f,
0.0f, 0.8f, 0.0f, 1.0f,
0.8f, -0.8f, 0.0f, 1.0f
};

GLfloat Colors[] = {
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f
};

VertexShaderId= InitShader(GL_VERTEX_SHADER, "Shader.VertexShader");
FragmentShaderId = InitShader(GL_FRAGMENT_SHADER, "Shader.VertexShader");
ProgramId = glCreateProgram();
glAttachShader(ProgramId, VertexShaderId);
glAttachShader(ProgramId, FragmentShaderId);
glLinkProgram(ProgramId);
glUseProgram(ProgramId);
GLenum ErrorCheckValue = glGetError();

glGenVertexArrays(1, &VaoId);
glBindVertexArray(VaoId);

glGenBuffers(1, &VboId);
glBindBuffer(GL_ARRAY_BUFFER, VboId);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);

glGenBuffers(1, &ColorBufferId);
glBindBuffer(GL_ARRAY_BUFFER, ColorBufferId);
glBufferData(GL_ARRAY_BUFFER, sizeof(Colors), Colors, GL_STATIC_DRAW);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(1);
}




void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT|GLUT_RGBA);

//glColor3f(.9f, 0, 0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
//glDrawArrays(GL_TRIANGLES, 3, 3);
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
glDisable(GL_BLEND);
glDisable(GL_DEPTH_TEST);

glDisableVertexAttribArray(0); glDisableVertexAttribArray(1);

glutSwapBuffers();
//glFlush();

}

static const GLfloat g_vertex_buffer_data[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f,
};


int main(int argc, char* argv[]) {


glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA );
glutInitWindowSize(1980, 1024);

glutCreateWindow("Hello, GL");
init();

glutDisplayFunc(render);

glutMainLoop();

return 0;
}

Here is my vertex and frag shader

#Version 430

layout(location=0) in vec4 vPosition;
layout(location=1) in vec4 vColor;

out vec4 Color;

void main()
{
Color=vColor;
gl_position= vPosition;
}

#Version 430

in vec4 Color;

out vec4 fColor;

void main()
{

fColor=Color;
}

Please let me know what i am missing

GClements
11-06-2015, 10:28 PM
You aren't checking whether the code compiled or linked. You do query the compile status, but don't actually check it, and you don't query the link status. In the event that the shaders fail to compile or link, use glGetShaderInfoLog() and glGetProgramInfoLog() to obtain any error messages.

I wouldn't expect the shaders to compile due to e.g.:


#Version 430


It should be "#version", with a lower-case "v" (preprocessor directives, keywords and predefined types only use lower case).

Also, use [code] tags when posting code.