shader compilation failed

I’m trying use code in opengl 8th edition in triangles .cpp when compile code the windows opens and then i get this error
Shader compilation failed: error: 0:1: ‘’ : Incorrect GLSL version: 4
ERROR: 0:7: ‘fcolor’ : undeclared identifier
ERROR: 0:7: ‘assign’ : cannnot convert from ‘const 4-component vector of float’
to ‘float’

////////////////////////////////////////////////////////////
// //
// myTriangles2.cpp //
// //
////////////////////////////////////////////////////////////

#include <iostream>

using namespace std;

#include “vgl.h”

#include “LoadShaders.h”

enum VAO_IDs {Triangles, NumVAOs};
enum Buffer_IDs {ArrayBuffer, NumBuffers};
enum Attrib_IDs {vPosition = 0};

GLuint VAOs[NumVAOs];
GLuint Buffers[NumBuffers];

const GLuint NumVertices = 6;

void init(void)
{
glGenVertexArrays (NumVAOs, VAOs);
glBindVertexArray (VAOs[Triangles]);

GLfloat vertices [NumVertices][2] = {
	{-0.90, -0.90},   //triangle 1
	{ 0.85,  0.90},
	{-0.90,  0.85},
	{ 0.90, -0.85},   //triangle 2
	{ 0.90,  0.90},
	{-0.85,  0.90}
};

glGenBuffers(NumBuffers, Buffers);
glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
ShaderInfo shaders[] = {
	{GL_VERTEX_SHADER, "triangles.vert"},
	{GL_FRAGMENT_SHADER, "triangles.frag"},
	{GL_NONE, NULL}
};

GLuint program = LoadShaders(shaders);

glUseProgram(program);
glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) );
glEnableVertexAttribArray(vPosition);

}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);

glBindVertexArray(VAOs[Triangles]);
glDrawArrays(GL_TRIANGLES, 0, NumVertices);

glFlush();

}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowPosition(523, 512);
glutInitContextVersion(4,0);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow(argv[0]);

glewExperimental = true;
if (glewInit())
{
	cerr &lt;&lt; "Unable to initialize glew.....exiting" &lt;&lt; endl;
	exit(EXIT_FAILURE);
}

init();

glutDisplayFunc(display);

}

shaders

#version 400

layout(location = 0) in vec4 vPosition;

void main()
{
gl_Position = vPosition;

}

#version 400

out vec4 fcolor;

void main()
{
fcolor = vec4(0.0, 0.0, 1.0, 1.0);
}

Try using “#version 4xx core” where xx may be 00, 10, 20, 30 or 40.

tried that i get same error

What graphics card do you have? Either your hardware or the installed drivers do not make OpenGL 4.x available and the shaders as written require it (through the #version 400 tag). I never can remeber the differences between GLSL versions, but I don’t think these particular shaders require anything from OpenGL 4.x, so you could try lowering the version number e.g. to 330 (OpenGL 3.3), 150 (OpenGL 3.2), or 140 (OpenGL 3.1).
If your hardware supports OpenGL 4.x you’ll need updated drivers - also note that on linux the open source drivers are a bit behind the proprietary drivers from AMD/Nvidia in terms of supported features.

PS: when posting source code please use [noparse]


[/noparse] around it to preserve formatting.

I’ve noticed that you did this:


GLfloat vertices [NumVertices][2] = {
{-0.90, -0.90}, //triangle 1
{ 0.85, 0.90},
{-0.90, 0.85},
{ 0.90, -0.85}, //triangle 2
{ 0.90, 0.90},
{-0.85, 0.90}
};

It’s not a good idea to declare OpenGL’s data with array of arrays because if you dereference “vertices” you’ll get an array of pointers not GLfloats. This won’t do the shader fail, but you won’t get any output.

This is not an array of pointers, it’s a regular 2D array of GLfloat values that are stored contiguously in memory. It’s perfectly fine to use this.

On the original question: At first sight it certainly looks like your shader compiler does not support 4.0 shaders. It seems odd, though, because your code also shows that you requested a 4.0 core context. Since that appears to have succeeded, I would have expected that you can compile 4.0 shaders in that context.