OpenGL Superbible 7 03 Following the Pipeline

来源:互联网 发布:淘宝3c认证编号怎么弄 编辑:程序博客网 时间:2024/06/05 13:25

01 学习目标

(1) What each of the stages in the OpenGL pipeline does.

(2) How to connect your shaders to the fixed-function pipeline stages.

(3) How to create a program that uses very stage of graphics pipeline.

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.

This chapter introduces every part of the pipeline, hooks them up to one another, and provides an example shader for each stage.


02 Passing Data to the Vertex Shader

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.

However, before the vertex shader runs, a fixed-function stage known as vertex fetching, or sometimes vertex pulling, is run.

In GLSL, the mechanism fir getting data in and out of shaders is to declare global variables with the in and out storage qualifiers.

At the start of the OpenGL pipeline, we use the in keyword to bring inputs into the vertex shader.


03 实现三角形的椭圆形运动

Vertex attributes are how vertex data is introduced into the OpenGL pipeline. To declare a vertex attribute, you declare a variable in the vertex shader using the in storage qualifier.

static const char * vs_source[] =        {            "#version 450 core                                                 \n"            "                                                                  \n"            "// 'offset' is an input vertex attribute           \n"            "layout (location = 0) in vec4 offset;              \n"            "void main(void)                                                   \n"            "{                                                                 \n"            "    const vec4 vertices[] = vec4[](vec4( 0.25, -0.25, 0.5, 1.0),  \n"            "                                   vec4(-0.25, -0.25, 0.5, 1.0),  \n"            "                                   vec4( 0.25,  0.25, 0.5, 1.0)); \n"            "                                                                  \n"            "    gl_Position = vertices[gl_VertexID] + offset;                 \n"            "}                                                                 \n"        };

We can tell this stage what to fill the variable with by using one of the many variants of the vertex attribute functions, glVertexAttrib*(). The prototype for glVertexAttrib4fv(), which we use in this example, is

void glVertexAttrib4fv(GLuint index, const GLfloat *v);

    virtual void render(double currentTime)    {        static const GLfloat green[] = { 0.0f, 0.25f, 0.0f, 1.0f };        glClearBufferfv(GL_COLOR, 0, green);        glUseProgram(program);        GLfloat attrib[] = {(float) sin(currentTime) * 0.5f, (float) cos(currentTime) * 0.6f, 0.0f, 0.0f };        // Update the value of input attribute 0        glVertexAttrib4fv(0, attrib);        glDrawArrays(GL_TRIANGLES, 0, 3);    }
 

04 数据的内部传输

Anything you write to an output variable in one shader is sent to a similar named variable declared with the in keyword in the subsequent stage.

interface blocks

in out 使用


05 碎片化 Tessellation

Tessellation is the process of breaking a high-order primitive (which is known as a patch in OpenGL) into many smaller, simpler primitives such triangles for rendering. OpenGL includes a fixed-function, configurable tessellation engine that is able to break up quadrilaterals, triangles, and lines into a potentially large number of smaller points, lines, or triangles that can be  directly consumed by the normal rasterization hardware further down the pipeline. Logically, the tessellation phase sits directly after the vertex shading stage in the OpenGL pipeline and is made up of three part: the tessellation control shader, the fixed-function tessellation engine, and the tessellation evaluation shader.


06


0 0
原创粉丝点击