25

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);
}
Alim Ul Gias
  • 6,351
  • 2
  • 28
  • 39
ColacX
  • 3,928
  • 6
  • 34
  • 36
  • In the most technical sense, you cannot turn off the shader in core GL 3.3, since the ability to render without shaders was removed in [core OpenGL contexts](http://www.opengl.org/wiki/Core_And_Compatibility_in_Contexts). The OpenGL Wiki maintains a [list of shader-based tutorials](http://www.opengl.org/wiki/Getting_started#Tutorials_and_How_To_Guides) (one of which is mine), but none of them have shaders which directly map to what fixed-functionality would do. That's probably the *worst* way to learn shader-based programming anyway. – Nicol Bolas Dec 26 '11 at 02:24

1 Answers1

37

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.

Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
Robert Rouhani
  • 14,512
  • 6
  • 44
  • 59
  • 5
    thanks i also looked in the book "OpenGL 4.0 Shading Language Cookbook" and there was similar color triangle example that clarified everything for me. i was so confused that all the basic/trivial things was deprecated, felt like someone ripped out the carped under me. – ColacX Jan 04 '12 at 00:55