Vertex Attributes - 官网上的文章

原文地址:https://www.opengl.org/sdk/docs/tutorials/ClockworkCoders/attributes.php

这是一篇openGL官方网站上的文章,比较清晰的讲述了Vertex Attributes的内容。

Vertex Attributes

Introduction

Vertex attributes are used to communicate from "outside" to the vertex shader. Unlike uniform variables, values are provided per vertex (and not globally for all vertices). There are built-in vertex attributes like the normal or the position, or you can specify your own vertex attribute like a tangent or another custom value. Attributes can't be defined in the fragment shader.

Attributes can be defined in the vertex shader using the "attribute" qualifier:

attribute float myAttrib;
Vertex Shader Source Code

 

Built in Vertex Attributes

In many cases the built in vertex attributes are sufficent to use. In the C++ program you can use the regular OpenGL function to set vertex attribute values, for example glVertex3f for the position.

gl_VertexPosition (vec4)
gl_NormalNormal (vec4)
gl_ColorPrimary color of vertex (vec4)
gl_MultiTexCoord0Texture coordinate of texture unit 0 (vec4)
gl_MultiTexCoord1Texture coordinate of texture unit 1 (vec4)
gl_MultiTexCoord2Texture coordinate of texture unit 2 (vec4)
gl_MultiTexCoord3Texture coordinate of texture unit 3 (vec4)
gl_MultiTexCoord4Texture coordinate of texture unit 4 (vec4)
gl_MultiTexCoord5Texture coordinate of texture unit 5 (vec4)
gl_MultiTexCoord6

Texture coordinate of texture unit 6 (vec4)

gl_MultiTexCoord7Texture coordinate of texture unit 7 (vec4)
gl_FogCoordFog Coord (float)

Example: Built-in Vertex Attributes

This example uses the built in attributes gl_Vertex and gl_Color. The color value is used to translate the object (i.e. misuse of color) .

tutorial example

glBegin(GL_TRIANGLES)
   glVertex3f(0.0f, 0.0f, 0.0f);
   glColor3f(0.1,0.0,0.0);
   glVertex3f(1.0f, 0.0f, 0.0f);
   glColor3f(0.0,0.1,0.0);
   glVertex3f(1.0f, 1.0f, 0.0f);
   glColor3f(0.1,0.1,0.0);

glEnd();    
C++ Source Code

void main(void)
{
   vec4 a = gl_Vertex + gl_Color;
   gl_Position = gl_ModelViewProjectionMatrix * a;
}
Vertex Shader Source Code

void main (void)  
{     
   gl_FragColor = vec4(0.0,0.0,1.0,1.0);  
}
Fragment Shader Source Code

Custom Vertex Attributes

A custom, user-defined attribute can also be defined. The OpenGL function glBindAttribLocation associates the name of the variable with an index.

For example, glBindAttribLocation(ProgramObject, 10, "myAttrib") would bind the attribute "myAttrib" to index 10.

The maximum number of attribute locations is limited by the graphics hardware. You can retrieve the maximum supported number of vertex attributes with glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &n).

Setting attribute values can be done using glVertexAttrib function.

Unfortunately there are certain limitations when using this on NVidia Hardware. According to NVidia:
"GLSL attempts to eliminate aliasing of vertex attributes but this is integral to NVIDIA’s hardware approach and necessary for maintaining compatibility with existing OpenGL applications that NVIDIA customers rely on. NVIDIA’s GLSL implementation therefore does not allow built-in vertex attributes to collide with a generic vertex attributes that is assigned to a particular vertex attribute index with glBindAttribLocation. For example, you should not use gl_Normal (a built-in vertex attribute) and also use glBindAttribLocation to bind a generic vertex attribute named "whatever" to vertex attribute index 2 because gl_Normal aliases to index 2."

In other words, NVidia hardware indices are reserved for built-in attributes:

gl_Vertex0
gl_Normal2
gl_Color3
gl_SecondaryColor4
gl_FogCoord5
gl_MultiTexCoord08
gl_MultiTexCoord19
gl_MultiTexCoord210
gl_MultiTexCoord311
gl_MultiTexCoord412
gl_MultiTexCoord513
gl_MultiTexCoord614
gl_MultiTexCoord715

Example Project

Download: GLSL_Attributes.zip (Visual Studio 8 Project)
(If you create a project/makefile for a different platform/compiler, please send it to: christen(at)clockworkcoders.com and I will put it here.)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值