Getting ready

We'll start with an empty OpenGL program, and the following shaders:

The vertex shader (basic.vert.glsl):

#version 460 
 
layout (location=0) in vec3 VertexPosition; 
layout (location=1) in vec3 VertexColor; 
 
out vec3 Color; 
 
void main() 
{ 
  Color = VertexColor; 
 
  gl_Position = vec4(VertexPosition,1.0); 
}

Attributes are the input variables to a vertex shader. In the previous code, there are two input attributes: VertexPosition and VertexColor. They are specified using the in GLSL keyword. Don't worry about the layout prefix, as we'll discuss that later. Our main OpenGL program needs to supply the data for these two attributes for each vertex. We will do so by mapping our polygon data to these variables.

It also has one output variable, ...

Get OpenGL 4 Shading Language Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.