现在的位置: 首页 > 综合 > 正文

Cg Tutorial – Chapter1-1 (1)

2017年10月25日 ⁄ 综合 ⁄ 共 8976字 ⁄ 字号 评论关闭

http://blog.sina.com.cn/s/blog_63507a56010112az.html


根据NVidia官方文档和人民邮电出版社的翻译整理。
学习用~
有写错的地方请及时指正~

上面都是没什么用的东西~

=========================================华丽的分割线======================================

简介

Chapter
1. Introduction

         本章由以下四个部分组成:

         1 什么是Cg 将介绍Cg 编程语言

         2 顶点、片段和图形流水线”讲述了现代图形硬件的数据流和解释Cg是如何和这样的数据流配合的。

         3 Cg 的开发历史 提供了Cg开发的一些背景知识。

4 Cg环境”解释了应用程序是如何通过Cg运行环境使用Cg程序的,和现存的一些3D应用程序编程接口(APIs)。

This chapter has the following four sections:

· "What
Is Cg?"
 introduces the Cg programming language.

· "Vertices,
Fragments, and the Graphics Pipeline"
 describes the data flow of modern graphics hardware and explains how Cg fits into this data
flow.

· "Cg's
Historical Development"
 provides some background on how Cg was developed.

· "The
Cg Environment"
 explains how applications go about using C g programs through the Cg runtime and existing 3D application programming
interfaces (APIs).

 

1.1   什么是Cg

1.1
What Is Cg?

本书将教你如何使用一种叫Cg 的编程语言。Cg语言使得通过控制可编程图形硬件绘制形状,外形和动作成为了可能。它把将这些可编程控制的属性和当今高速度、高性能的图形处理器很好的结合在一起。在此之前,从来没有计算机图形从业者(无论是设计师还是工程师)在他们生成自己的实时图像的时候有如此多的控制能力。

This book teaches you how to use a programming language called C g. The C g language makes it possible for you to
control the shape, appearance, and motion of objects drawn using programmable graphics hardware. It marries programmatic control of these attributes with the incredible speed and capabilities of today's graphics processors. Never before have computer graphics
practitioners, whether artists or programmers, had so much control over the real-time images they generate.

 

Cg为开发人员提供了一整套使用便捷的编程平台,使得在多种平台上迅速生成特殊效果、制作实时的、如同电影级别的体验成为可能。通过一个新的抽象层,Cg语言使开发者不需要再直接使用图形硬件汇编语言。因此非常适合OpenGL,
DirectX, Windows, Linux, Macintosh OS X
 和像 Xbox 这样的游戏平台。Cg 是在和微软公司密切合作的基础上开发的,因此CgOpenGL
API
  MicrosoftHLSL完全兼容。

Cg provides developers with a complete programming platform that is easy to use and enables the fast

creation of special effects and real-time cinematic-quality experiences on multiple platforms. By providing a new
level of abstraction, C g removes the need for developers to program directly to the graphics hardware assembly language, and thereby more easily target OpenGL, DirectX, Windows, Linux, Macintosh OS X, and console platforms such as the Xbox. C g was developed
in close collaboration with Microsoft Corporation and is compatible with both the OpenGL API and Microsoft's High-Level Shading Language (HLSL) for DirectX 9.0.

 

Cg代表“C
for Graphics
”。 C语言是一种开发与20世纪70年代、非常流行的通用编程语言。由于它的流行简洁的设计,C语言为之后的多种编程语言提供了基础。例如,C++Java语法结构都是基于C的。Cg语言本身也是基于C语言的。如果你熟悉C语言或者是从C语言派生出来的其他语言,Cg将会非常容易掌握。

Cg stands for "C for graphics." The C programming language is a popular, general-purpose language

invented in the 1970s. Because of its popularity and clean design, C provided the basis for several subsequent programming
languages. For example, C ++ and Java base their syntax and structure largely on C. The C g language bases itself on C as well. If you are familiar with C or one of the many languages derived from C, then C g will be easy to learn.

 

另一方面,如果你不熟悉C语言,或者没接触过一般的编程语言,但是你非常喜欢计算机图形学并想要学习一些新东西。那么无论如何都要阅读本书。Cg程序都十分简短且容易理解。

On the other hand, if you are not familiar with C or even programming languages in general but you enjoy computer
graphics and want to learn something new, read on anyway. C g programs tend to be short and understandable.

 

本章的大部分所讲述背景知识对于你理解和有效的使用Cg语言将非常有价值。另一方面,你将发现通过实践学习Cg是十分容易的。如果你想马上进入Cg教程,那么可以随时跳到第2章节开始学习。

Much of this chapter is background that provides valuable context for understanding C g and using it effectively.
On the other hand, you may find C g is easier to learn by doing. Feel free to skip to Chapter 2 at any time if you feel more comfortable just diving into the tutorial.

 

1.1.1 为可编程图形硬件设计的语言

1.1.1
A Language for Programming Graphics Hardware

Cg不同与CC++Java,因为他非常特别。没有人会用Cg写电子指标软件或者文字处理程序。相反,Cg的目标是为使用图形硬件渲染的物体的形状、外观和运动提供可编程控制的能力。从广义上讲,这种类型的语言被称为光照渲染语言。但是Cg可以做光照意外的很多事情。例如,Cg程序可以实现物理模拟、混合和其他非光照任务。

Cg is different from C , C ++, and Java because it is very specialized. No one will ever write a spreadsheet
or word processor in C g. Instead, C g targets the ability to programmatically control the shape, appearance, and motion of objects rendered using graphics hardware. Broadly, this type of language is called a
 shading
language
. However, C g can do more than just shading. For example, C g programs can perform physical simulation, compositing, and other
nonshading tasks.

 

你可以把Cg看做是一个非常详细的,通过使用可编程图形硬件来渲染物体的解决方案。例如,你可以编写一个Cg程序来使得物体的外表看起来凸凹不平,或者让一个虚拟人物活动起来。在之后的1.3节中,你将学习许多光照渲染语言的历史和Cg是如何融入的该历史中的。

Think of a C g program as a detailed recipe for how to render an object by using programmable graphics hardware.
For example, you can write a C g program to make a surface appear bumpy or to animate a virtual character. Later, in Section 1.3, you will learn more about the history of shading languages and where C g fits into this history.

 

1.1.2 Cg语言的数据流模型

1.1.2
Cg's Data-Flow Model

除了专门为图形设计以外,Cg与其他光照语言是和传统编程语言是截然不同的,,因为这些语言是基于数据流模型的。在这样一个模型里,计算的是为了响应一系列,流经处理序列的数据而发生的。

In addition to being specialized for graphics, C g and other shading languages are different from conventional programming
languages because they are based on a data-flow computational model. In such a model, computation occurs in response to data that flows through a sequence of processing steps.

 

在渲染一副图片时,Cg程序运行在处理顶点(Vertices)和片段(Fragments)上(如果你还不理解片段是什么,那么现在暂时把它当作像素)。你可以把Cg程序想象成一个黑箱,顶点和片段从一边流入,经过某些变换以后,从另一边流出,。但是这个箱子并不是一个真正的黑箱,因为你可以通过编写Cg程序来决定它到底起什么作用。

Cg programs operate on vertices and fragments (think "pixels" for now if you do not know what a fragment is) that
are processed when rendering an image. Think of a C g program as a black box into which vertices or fragments flow on one side, are somehow transformed, and then flow out on the other side. However, the box is not really a black box because you get to determine,
by means of the C g programs you write, exactly what happens inside.

 

在渲染三维场景时,每当处理一个顶点或者光栅器产生一个片段,你对应的顶点或者片段Cg程序就会被执行。第1.3节会进一步解释Cg的数据流模型。

Every time a vertex is processed or the rasterizer generates a fragment while rendering a 3D scene, your corresponding
vertex or fragment C g program executes. Section 1.3 explains C g's data-flow model further.

 

大部分最新的PC和所有最近的游戏平台都包括一个GPU,专门用来处理图形任务,例如变换和光栅化三维模型。你的Cg程序实际上是在你的计算机中的GPU上执行的。

Most recent personal computers—and all recent game consoles—contain a graphics processing unit (GPU) that is dedicated
to graphics tasks such as transforming and rasterizing 3D models. Your C g programs actually execute within the GPU of your computer.

 

1.1.3 GPU的特殊性和CPU的通用性

1.1.3
GPU Specialization and CPU Generalization

无论是PC还是或者游戏平台是否拥有一个GPU 它都必须有一个CPU来运行操作系统和应用程序。CPU是以多用途为目的设计的,其所执行的程序(例如,文字处理器和统计软件包)是用多用途语言编写的,例如C++或者Java

Whether or not a personal computer or game console has a GPU, there must be a C PU that runs the operating system
and application programs. C PUs are, by design, general purpose. CPUs execute applications (for example, word processors and accounting packages) written in general-purpose languages, such as C++ or Java.

 

因为GPU是为专门目的设计,因此它的图形处理任务的速度要比通用的CPU快得多,例如渲染3D场景。新的GPU可以在1s内处理好几千万的顶点和光栅化几亿甚至几十亿片段。而且未来的GPU会更快。这绝对比CPU处理类似数量的顶点和片段的速度快许多。但是GPU不能像CPU那样运行任意的、多种多样的程序。

Because of the GPU's specialized design, it is much faster at graphics tasks, such as rendering 3D scenes, than
a general-purpose CPU would be. New GPUs process tens of millions of vertices per second and rasterizer hundreds of millions or even billions of fragments per second. Future GPUs will be even speedier. This is overwhelmingly faster than the rate at which a
C PU could process a similar number of vertices and fragments. However, the GPU cannot execute the same arbitrary, general-purpose programs that a CPU can.

 

GPU专用性和高性能的特质是Cg为什么能存在的原因。多用途的编程语言对处理顶点和片段这项专门的任务来说太自由了。相反,Cg语言是专用于这项任务的。Cg也同样提供了一个和GPU的执行模型相符合的抽象执行模型。你将在1.2小节学习GPU独特的执行模型。

The specialized, high-performance nature of the GPU is why Cg exists. General-purpose programming languages are
too open-ended for the specialized task of processing vertices and fragments. In contrast, the Cg language is fully dedicated to this task. C g also provides an abstract execution model that matches the GPU's execution model. You will learn about the unique
execution model of GPUs in Section 1.2.

 

1.1.4 Cg性能的基本原理

1.1.4
The Performance Rationale for Cg

为了保持一个看起来连续的交互,3D应用程序需要维持每秒超过15帧的图像刷新率。通常,我们认为不低于60/s的刷新率为实时的(real-time),在这样的帧率下与应用程序的交互看起来就像是即时发生的。计算机的显示器也许有100万后者更多的像素需要重绘。对于一个3D场景而言, GPU对显示器上的每个像素会处理每个像素好几次。原因可能是场景中的物体一般是相互重叠的,或者要提高每个像素的表现。这意味着实时3D应用程序每秒需要更新上亿个像素。伴随这些需要处理的像素的是,由顶点汇成的三维模型,这些顶点只有在被正确的变换后,才能被组成多边形、线段和点,然后被光栅化成像素。这就需要每秒变换上千万的顶点。

To sustain the illusion of interactivity, a 3D application needs to maintain an animation rate of 15 or more images
per second. Generally, we consider 60 or more frames per second to be "real time," the rate at which interaction with applications appears to occur instantaneously. The computer's display may have a million or more pixels that require redrawing. For 3D scenes,
the GPU typically processes every pixel on the screen many times to account for how objects occlude each other, or to improve the appearance of each pixel. This means that real-time 3D applications can require hundreds of millions of pixel updates per second.
Along with the required pixel processing, 3D models are composed of vertices that must be transformed properly before they are assembled into polygons, lines, and points that will be rasterized into pixels. This can require transforming tens of millions of
vertices per second.

 

而且,在这种图形处理之外还需要CPU做相当大的工作来为每个新的图像更新显示。事实上,我们同时需要CPUGPU专门面向图形的功能。以一个可交互的刷新率和3D应用程序和游戏所要求的质量标准来渲染场景同事需要这两种处理器。这意味着一个开发人员可以用C++写一个3D应用或游戏,然后用Cg来充分利用GPU的额外的图形处理能力。

Moreover, this graphical processing happens in addition to the considerable amount of effort required of the CPU
to update the animation for each new image. The reality is that we need both the CPU and the GPU's specialized graphics-oriented capabilities. Both are required to render scenes at the interactive rates and quality standards that users of 3D applications and
games demand. This means a developer can write a 3D application or game in C++ and then use Cg to make the most of the GPU's additional graphics horsepower.

 

抱歉!评论已关闭.