ERROR: 0:1: '' : Version number not supported by GL2 - c++

I'm trying to make shader, but it gives me this error:
ERROR: 0:1: ' ' : Version number not supported by GL2
ERROR: 0:3: 'layout' : syntax error parse error
Please help!
This is my vertex shader code:
#version 330
layout(location = 0) in vec4 position;
void main()
{
gl_Position = position;
}
And this is my fragment shader code:
#version 330
layout(location = 0) out vec4 color;
void main()
{
color = vec4(1.0, 0.0, 0.0, 1.0);
}
I tried making the version lower, but it doesn't work.

Since your graphics driver and card only support OpenGL 2.0, you cannot use a GLSL shader with version 3.30. You need to use GLSL 1.10 which corresponds to OpenGL 2.0.
See OpenGL Shading Language 1.10 Specification and OpenGL specification - Khronos OpenGL registry
An appropriate shader would be:
Vertex shader
#version 110
attribute vec4 position;
void main()
{
gl_Position = position;
}
Fragment shader
#version 110
void main()
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}

Related

unable to link geometry shader properly

I am trying to create a simple passthrough "Geometry Shader" but for some reason its not linking. Debug info says its compiling fine.I don't know what I am doing wrong in this one? It is not the drivers or OS as I have linked and used geometry shader before, and they worked fine.
Vertex Shader code:
#version 450
in vec3 position;
uniform mat4 MVP;
out vec4 color;
void main()
{
gl_Position = MVP * vec4(position, 1.0);
color = vec4(0.5, 0.5, 0.0, 1.0);
}
Fragment Shader code:
#version 450
in vec4 fColor;
out vec4 fcolor;
void main() {
fcolor = fColor;
}
Geometry Shader Code:
#version 450
layout(lines) in;
layout(triangle_strip, max_vertices = 4) out;
in vec4 color;
out vec4 fColor;
void main()
{
fColor = color;
for(int i = 0; i <= gl_in.length(); i++)
{
gl_Position = gl_in[i].gl_Position;
EmitVertex();
}
EndPrimitive();
}
Debug output:
In Compile Shader from file: ../Curve.vert compile result:: 1 Shader
compile and attach success: In Compile Shader from file:
../Curve.geom compile result:: 1 Shader compile and attach success:
In Compile Shader from file: ../Curve.frag compile result:: 1 Shader
compile and attach success: Linking.. , Program handle found: 1 GL
Renderer : GeForce GTX 1660 Ti/PCIe/SSE2 GL Vendor :
NVIDIA Corporation GL Version : 4.6.0 NVIDIA 461.40 GL
Version No. : 4.6 GLSL Version : 4.60 NVIDIA
--------------- Debug message (131216): Program/shader state info: GLSL program 1 failed to link Source: API Type: Other Severity: low
Curve shader program not linked Curve shader program not validated Use
unsuccessful, returning? m_linked status: false Program handle: 1
Curve shader program handle: 1
The Geometry Shader transforms Primitives and is executed for each primitive. Therefore, the input of the Geometry Shader is an array the size of the vertex number of the primitives. e.g.:
in vec4 color[];
You have to emit a vertex for each vertex of the output primitive.

getting 'location' : too large for fragment output error while using glslang

Iam using glslang SDK in my code to compile shader programs online, however i get the following errors for my fragment shader:
GLSL Parsing Failed for: Text.frag ERROR: 0:6: 'location' : too large
for fragment output ERROR: 1 compilation errors. No code generated.
GLSL Linking Failed for: Text.frag ERROR: 0:6: 'location' : too large
for fragment output ERROR: 1 compilation errors. No code generated.
Assertion failed: node->getOp(), file
C:\projects\glslang\SPIRV\GlslangToSpv.cpp, line 2412
my vertex shader:
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout(location = 0) out vec3 fragColor;
vec2 positions[3] = vec2[](
vec2(0.0, -0.5),
vec2(0.5, 0.5),
vec2(-0.5, 0.5)
);
vec3 colors[3] = vec3[](
vec3(1.0, 0.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(0.0, 0.0, 1.0)
);
void main() {
gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
fragColor = colors[gl_VertexIndex];
}
My fragment shader
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout(location = 0) in vec3 fragColor;
layout(location = 0) out vec4 outColor;
void main() {
outColor = vec4(fragColor, 1.0);
}
Iam using glslang SDK in my code to compile the shader programs online, apparently in the glslang code you have to tweak some limitation parameters manually for glslang to accept shader variables that are normally not accepted, this issue was fixed by setting maxDrawBuffers to true as the following
resources = DefaultTBuiltInResource;
resources.maxDrawBuffers = true;
I am not sure what exactly this does and why it is not set natively, but perhaps someone can explain this further.

QGLShaderProgram OpenGL shaders

I'm getting the following error when I try link my fragment shader,
QGLShader::compile(Fragment): 0(4) : error C0000: syntax error, unexpected '.', expecting "::" at token "."
I'm just trying to implement a simple fragment shader that sets colour to be green.
The code for my vertex shader (which is working) file name shader.vert
#version 430
in layout(location=0) vec2 position;
void main()
{
gl_Position = vec4(position, 0.0, 1.0);
}
The code for my fragment shader shader.frag
#version 430
out vec4 finalColour;
void main()
{
finalColour = vec4(0.0, 1.0, 0.0, 1.0);
}
The code that links the QGLShaderProgram mProgram
//Add Shaders
if (!mProgram.addShaderFromSourceFile(QGLShader::Vertex, "shader.vert")) {
error_msg("Vertex shader load failed");
}
if (!mProgram.addShaderFromSourceCode(QGLShader::Fragment, "testShader.frag")) {
error_msg("Fragment shader load failed");
}
if (!mProgram.link()) {
error_msg("Cannot link shaders");
}
mProgram.bind()
The second parameter of addShaderFromSourceCode(, code)
you must provide the content of file not the name of file itself
here you can put this code in a function and use it to load the file
Read whole ASCII file into C++ std::string

JOGL, opengl 4.3, passing color vector from geometry shader, to fragment shader

I am using a book called Opengl SuperBible 6th edition. I am using JOGL 2.5.1 API. Everytihing was working fine, until I created geometry shader (exercise on page 37). When I try to pass color vector from geometry shader to fragment shader, the Java virtual machine crashes. I have also implemented GLSL error check at shader creation and it shows nothing. I really want to know what is the catch here. here is my code from vertex shader to fragment shader. Funny thing is the program crashes only when i send color from geometry shader to fragment shader. If i set color manualy in fragment shader the program works.
VERTEX SHADER:
#version 430 core
layout (location = 0) in vec4 offset;
layout (location = 1) in vec4 color;
out vec4 vs_color;
void main(void){
const vec4 vertices[3] = vec4[3](vec4( 0.25, -0.25, 0.5, 1.0),
vec4(-0.25, -0.25, 0.5, 1.0),
vec4( 0.25, 0.25, 0.5, 1.0));
gl_Position = vertices[gl_VertexID] + offset;
vs_color = color;
}
TESSELATION CONTROL SHADER:
#version 430 core
layout (vertices = 3) out;
in vec4 vs_color[];
out vec4 tsc_color[];
void main(void){
if (gl_InvocationID == 0){
gl_TessLevelInner[0] = 5.0;
gl_TessLevelOuter[0] = 5.0;
gl_TessLevelOuter[1] = 5.0;
gl_TessLevelOuter[2] = 5.0;
}
gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;
tsc_color[0] = vs_color[0];
}
TESSELATION EVALUATION SHADER:
#version 430 core
layout (triangles, equal_spacing, cw) in;
in vec4 tsc_color[];
out vec4 tse_color[];
void main(void){
gl_Position = (gl_TessCoord.x * gl_in[0].gl_Position +
gl_TessCoord.y * gl_in[1].gl_Position +
gl_TessCoord.z * gl_in[2].gl_Position);
tse_color[0] = tsc_color[0];
}
GEOMETRY SHADER:
#version 430 core
layout (triangles) in;
layout (points, max_vertices = 3) out;
in vec4 tse_color[];
out vec4 geo_color;
void main(void){
int i;
for (i = 0; i < gl_in.length(); i++)
{
gl_Position = gl_in[i].gl_Position;
EmitVertex();
}
geo_color = tse_color[0];
}
FRAGMENT SHADER:
#version 430 core
in vec4 geo_color;
out vec4 color;
void main(void){
//color = vec4(0.0f, 0.8f, 1.0f, 1.0f); for manual setting
color = geo_color;
}
THE ERROR THAT OCCURES:
glLinkProgram(<int> 0x6)GLDebugEvent[ id 0x7d1
type Error
severity High: dangerous undefined behavior
source GLSL or extension compiler
msg glLinkProgram failed to link a GLSL program with the following program info log: 'Vertex shader(s) failed to link, tessellation control shader(s) failed to link, tessellation evaluation shader(s) failed to link, geometry shader(s) failed to link, fragment shader(s) failed to link.
Vertex link error: INVALID_OPERATION.
ERROR: error(#275) Symbol "tse_color" is defined with 2 different types between two stages
tessellation control link error: INVALID_OPERATION.
ERROR: error(#275) Symbol "tse_color" is defined with 2 different types between two stages
tessellation evaluation link error: INVALID_OPERATION.
ERROR: error(#275) Symbol "tse_color" is defined with 2 different types between two stages
geometry link error: INVALID_OPERATION.
ERROR: error(#275) Symbol "tse_color" is defined with 2 different types between two stages
fragment link error: INVALID_OPERATION.
ERROR: error(#275) Symbol "tse_color" is defined with 2 different types between two stages
'
when 1401907851402
source 4.4 (Core profile, arb, debug, ES2 compat, ES3 compat, FBO, hardware) - 4.4.12874 Core Profile/Debug Context 14.100.0.0 - hash 0x14e08f00]
Tessellation Evaluation Shaders only output 1 vertex. I do not know why you have declared out vec4 tse_color[];, but that is a mistake.
// out vec4 tse_color[]; // WRONG
out vec4 tse_color; // * Fixed
[...]
// tse_color[0] = tsc_color[0]; // WRONG
tse_color = tsc_color[0]; // * Fixed
Finally, you are only outputting color to a single vertex for every invocation of your control shader, when you should be outputting multiple. To pass your per-vertex color through the control shader, you need the following:
tsc_color [gl_InvocationID] = vs_color [gl_InvocationID];
Now, here is where things get strange... you are only referencing the first color in your patch for each evaluated vertex. I think what you actually want is an input/output variable qualified with patch storage. That will allow you to create exactly 1 instance of the variable shared across all invocations, sort of like a provoking vertex only for tessellation.

How does the default GLSL shaders look like? for version 330

What do the default vertex, fragment and geometry GLSL shaders look like for version #330?
I'll be using #version 330 GLSL Version 3.30 NVIDIA via Cg compiler, because that is what my graphics card supports.
With default shaders, I mean shaders that do the same exact thing as the graphics card would do when the shader program is turned off.
I can't find a good example for #version 330. Been googling all day. Not sure if the term default shader is called something else like trivial or basic and if that is why I can't find it.
Any recommendations for a book with version 330 or link to an easy beginner tutorial with version 330 would be great as well.
example of a trivial vertex shader in #version 110, does the default vertex transformation
#version 110
void main()
{
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
}
example of a trivial fragment shader in #version 110, turns color into red
#version 110
void main()
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
There are no "default" shaders with OpenGL. It looks like what you want a very simple example of a shader that transforms vertices to clip space and gives them a color, so here you go:
Vertex shader:
#version 330
layout(location = 0)in vec4 vert;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;
void main()
{
gl_Position = projection * view * model * vert;
}
Fragment shader:
#version 330
out vec4 fragColor;
void main()
{
fragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
The core OpenGL 3.3 profile drops support for a lot of old fixed-function things like the matrix stack. You are expected to handle your own matrices and send them to your shaders. There is no ftransform, and gl_Position is pretty much the only valid gl_* variable.
While glBindAttribLocation is not deprecated, the preferred method of defining the location of vertex attributes is through "layout(location = x)" in GLSL.
In the vertex shader, "attribute" is now "in" and "varying" is now "out". In the fragment shader, "varying" is now "in" and "gl_FragColor" is defined by an "out" variable. I believe that gl_FragColor is still valid, but now it's possible to use an out variable to define the color.
This tutorial is very good and teaches core OpenGL and GLSL 3.30, I would recommend you use it to help you learn more about GLSL. Also remember that the GLSL Reference Pages is your friend.