您的位置:首页 > 其它

Vertex Attributes - 官网上的文章

2015-11-30 16:15 453 查看
原文地址: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_MultiTexCoord6Texture 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) .



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.)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: