
VERTEX SHADER TUTORIAL CODE
You may have noticed the layout (location = 0) code in the declaration of the offset attribute. Here, the parameter index is used to reference the attribute and v is a pointer to the new data to put into the attribute. The prototype for glVertexAttrib4fv(), which we use in this example, is void glVertexAttrib4fv(GLuint index, We can tell this stage what to fill the variable with by using one of the many variants of the vertex attribute functions, glVertexAttrib*(). As it is an input to the first shader in the pipeline, it will be filled automatically by the vertex fetch stage. In Listing 3.1, we have added the variable offset as an input to the vertex shader. Add 'offset' to our hard-coded vertex position 'offset' is an input vertex attribute layout (location = 0) in vec4 offset Ĭonst vec4 vertices = vec4( vec4(0.25, -0.25, 0.5, 1.0), Listing 3.1: Declaration of a vertex attribute #version 450 core An example of this is shown in Listing 3.1, where we declare the variable offset as an input attribute. To declare a vertex attribute, you declare a variable in the vertex shader using the in storage qualifier. Vertex attributes are how vertex data is introduced into the OpenGL pipeline. The variable becomes known as a vertex attribute. It is automatically filled in by the fixed-function vertex fetch stage. This marks the variable as an input to the vertex shader, which means that it is essentially an input to the OpenGL graphics pipeline. For now, consider the input to the vertex shader and what happens if you declare a variable with an in storage qualifier. Between stages, in and out can be used to form conduits from shader to shader and pass data between them. At the start of the OpenGL pipeline, we use the in keyword to bring inputs into the vertex shader. You were briefly introduced to the out qualifier in Chapter 2, “Our First OpenGL Program,” when Listing 2.4 used it to output a color from the fragment shader. In GLSL, the mechanism for getting data in and out of shaders is to declare global variables with the in and out storage qualifiers. This automatically provides inputs to the vertex shader. However, before the vertex shader runs, a fixed-function stage known as vertex fetching, or sometimes vertex pulling, is run. The vertex shader is the first programmable stage in the OpenGL pipeline and has the distinction of being the only mandatory stage in the graphics pipeline. This chapter introduces every part of the pipeline, hooks them up to one another, and provides an example shader for each stage. If we want to render anything interesting with OpenGL, we’re going to have to learn a lot more about the pipeline and all of the things you can do with it. However, the application that you constructed simply drew a single triangle at a fixed position. You have already read a whirlwind introduction to the vertex and fragment shader stages. In this chapter, we will walk all the way along the OpenGL pipeline from start to finish, providing insight into each of the stages, which include fixed-function blocks and programmable shader blocks.
VERTEX SHADER TUTORIAL HOW TO
