CG 学习 (1)——CG概览

时间:2021-04-05 23:29:52

     去年因为去Moto上海研发中心研究人机交互的部门去面试, 临时学了一个星期的CG 编程,后来就束之高阁了,现在重新拾起来,在这里给自己学习历程留下一个回忆,若能给初学者一些借鉴,不胜荣幸。

      学一门编程语言,首先需了解其发展历史,才能更全面把握它,了解其适用范围、优势。CG Shading Language  规范由M$和NVidia两强联合开发,NVidia的实现叫CG,MS的实现叫HLSL。HLSL是MS DirectX3D(简写D3D)的一部分,而CG则是跨平台的,即可用于D3D,又可用于OpenGL(OGL)。OGL下也有一个Shading Languge,叫GLSL,据说(这里只是道听途说,没证明过,没有贬低的意思)GLSL在NV的卡上和ATI的卡上效果可能会不一致,不是太稳定,而CG的效果比较稳定。GLSL的优势是跟OGL结合紧密,跟OGL一样也是跨平台的。学CG的好处是CG shader可以不经修改的用于D3D和OGL,到时转到D3D下也方便一些。其实CG、HLSL及GLSL都是类C 的 Shading Language,好多地方大同小异。

       下面就了解一下CG的形成过程,为以后找工作笔试和面试考虑,就摘抄一些《 The CG Tutorial 》里面的一些句子,并根据自己理解,提炼总结一些东西,用英文写。很多大公司笔试都要求英文作答,就练练笔了。

        Cg stands for "C for Graphic". The Cg language enables you to control the shape, appearance, and motion of objects drawn using GPU. It also enables you to modify the traditional graphics pipeline. A conventional graphics hardware pipelines processes a multitude of vertices, geometric primitives, and fragments in a pipelined form. Vertex transformation is the first processing stage in the graphics hardware pipeline, in this stage GPU performs a sequence of math operations on each vertex, including transforming the vertex position to a screen position used by the rasterizer, generating texture coordinates for texturing and lighting the vertex. The second stage is "primitive assembly and rasterization", in this stage GPU assembles vertices into geometric primitives. This results in a sequence of triangles, lines, or points. These primivives may require view frustum clipping, as well as application-specified clipping. The surviving polygons will be rasterized. The rasterizer may also discard polygons based on whether they face forward or backword. The rasterization will light a set of pixels and generate a set of fragments. A fragment has an associated pixel position, a depth value, and a set of interpolated parameters, such as a color, one or more texture coordinate.

       GPU designs have evolved, and the vertex and fragment processors within the GPU have transitioned from being configurable to being programmable. The data-flow model for vetex processing begins by loading each vetex's attributes into vertex processor. When the vertex processor terminates, the output result registers contain the newly transformed vertex. After trianle setup and rasterization, the interpolated values for each register are passed to the fragment processor. The fragment processor has additional capability that it supports texturing operations.

      CG inherits from three sources. First, Cg bases its syntax and semantics on the general-purpose C Language. Second, Cg incorporates many concepts from offline shading languages such as the RenderMan Shading Language, as well as prior hardware shading languages developed by academia. Third, Cg bases its graphics functionality on the OGL and D3D programming interfaces for real-time 3D.

     下篇将会学习CG的基本语法、语义,然后做一个顶点光照实例,框架用 dot net Managed C++ 建立,并把CG 渲染部分封装成一个CGShader 及 CGShaderManager类,以方便按面向对象的方式运用CG。