Home>Articles>Programming>Graphic Programming

Drawing with OpenGL

This chapter is from the book

顶点规范

Now that you have data in buffers, and you know how to write a basic vertex shader, it’s time to hook the data up to the shader. You’ve already read aboutvertex array objects, which contain information about where data is located and how it is laid out, and functions likeglVertexAttribPointer().It’s time to take a deeper dive into vertex specifications, other variants ofglVertexAttribPointer(), and how to specify data for vertex attributes that aren’t floating point or aren’t enabled.

VertexAttribPointer in Depth

TheglVertexAttribPointer()command was briefly introduced in Chapter 1. The prototype is as follows:

The state set byglVertexAttribPointer()is stored in the currently bound vertex array object (VAO).sizeis the number of elements in the attribute’s vector (1, 2, 3, or 4), or the special token GL_BGRA, which should be specified whenpackedvertex data is used. Thetypeparameter is a token that specifies the type of the data that is contained in the buffer object. Table 3.6 describes the token names that may be specified fortypeand the OpenGL data type that they correspond to:

Table 3.6. Values ofTypefor glVertexAttribPointer()

Token Value

OpenGL Type

GL_BYTE

GLbyte (signed 8-bit bytes)

GL_UNSIGNED_BYTE

GLubyte (unsigned 8-bit bytes)

GL_SHORT

GLshort (signed 16-bit words)

GL_UNSIGNED_SHORT

GLushort (unsigned 16-bit words)

GL_INT

GLint (signed 32-bit integers)

GL_UNSIGNED_INT

GLuint (unsigned 32-bit integers)

GL_FIXED

GLfixed (16.16 signed fixed point)

GL_FLOAT

GLfloat (32-bit IEEE single-precision floating point)

GL_HALF_FLOAT

GLhalf (16-bit S1E5M10 half-precision floating point)

GL_DOUBLE

GLdouble (64-bit IEEE double-precision floating point)

GL_INT_2_10_10_10_REV

GLuint (packed data)

GL_UNSIGNED_INT_2_10_10_10_REV

GLuint (packed data)

Note that while integer types such as GL_SHORT or GL_UNSIGNED_INT can be passed to thetypeargument, this tells OpenGL only what data type is stored in memory in the buffer object. OpenGL will convert this data to floating point in order to load it into floating-point vertex attributes. The way this conversion is performed is controlled by thenormalizeparameter. Whennormalizeis GL_FALSE, integer data is simply typecast into floating-point format before being passed to the vertex shader. This means that if you place the integer value 4 into a buffer and use the GL_INT token for thetypewhennormalizeis GL_FALSE, the value 4.0 will be placed into the shader. Whennormalizeis GL_TRUE, the data is normalized before being passed to the vertex shader. To do this, OpenGL divides each element by a fixed constant that depends on the incoming data type. When the data type is signed, the following formula is used:

109equ01.jpg

Whereas, if the data type is unsigned, the following formula is used:

110equ01.jpg

In both cases,fis the resulting floating-point value,cis the incoming integer component, andbis the number of bits in the data type (i.e., 8 for GL_UNSIGNED_BYTE, 16 for GL_SHORT, and so on). Note that unsigned data types are also scaled and biased before being divided by the type-dependent constant. To return to our example of putting 4 into an integer vertex attribute, we get:

110equ02.jpg

which works out to about 0.000000009313—a pretty small number!

Integer Vertex Attributes

If you are familiar with the way floating-point numbers work, you’ll also realize that precision is lost as numbers become very large, and so the full range of integer values cannot be passed into a vertex shader using floating-point attributes. For this reason, we haveinteger vertex attributes.These are represented in vertex shaders by theint,ivec2,ivec3, orivec4types or their unsigned counterparts—uint,uvec2,uvec3, anduvec4

A second vertex-attribute function is needed in order to pass raw integers into these vertex attributes—one that doesn’t automatically convert everything to floating point. This isglVertexAttribIPointer()—the I stands for integer.

Notice that the parameters toglVertexAttribIPointer()are identical to the parameters toglVertexAttribPointer(), except for the omission of thenormalizeparameter.normalizeis missing because it’s not relevant to integer vertex attributes. Only the integer data type tokens, GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_INT, and GL_UNSIGNED_INT may be used for thetypeparameter.

Double-Precision Vertex Attributes

The third variant ofglVertexAttribPointer()isglVertexAttribLPointer()—here the L stands for “long”. This version of the function is specifically for loading attribute data into64-bit double-precisionfloating-point vertex attributes.

Again, notice the lack of thenormalizeparameter. InglVertexAttribPointer(),normalizewas used only for integer data types that aren’t legal here, and so the parameter is not needed. If GL_DOUBLE is used withglVertexAttribPointer(), the data is automatically down-converted to 32-bit single-precision floating-point representation before being passed to the vertex shader—even if the target vertex attribute was declared using one of the double-precision typesdouble,dvec2,dvec3, ordvec4, or one of the double-precision matrix types such asdmat4.However, withglVertexAttribLPointer(), the full precision of the input data is kept and passed to the vertex shader.

Packed Data Formats for Vertex Attributes

Going back to theglVertexAttribPointer()command, you will notice that the allowed values for thesizeparameter are 1, 2, 3, 4, and the special token GL_BGRA. Also, thetypeparameter may take one of the special values GL_INT_2_10_10_10_REV or GL_UNSIGNED_INT_2_10_10_10_REV, both of which correspond to the GLuint data type. These special tokens are used to representpackeddata that can be consumed by OpenGL. The GL_INT_2_10_10_10_REV and GL_UNSIGNED_INT_2_10_10_10_REV tokens represent four-component data represented as ten bits for each of the first three components and two for the last, packed in reverse order into a single 32-bit quantity (a GLuint). GL_BGRA could just have easily been called GL_ZYXW.5Looking at the data layout within the 32-bit word, you would see the bits divided up as shown inFigure 3.3

Figure 3.3

Figure 3.3.Packing of elements in a BGRA-packed vertex attribute

InFigure 3.3, the elements of the vertex are packed into a single 32-bit integer in the orderw,x,y,z—which when reversed isz,y,x,w, orb,g,r,awhen using color conventions. InFigure 3.4, the coordinates are packed in the orderw,z,y,x, which reversed and written in color conventions isr,g,b,a

Figure 3.4

Figure 3.4.Packing of elements in a RGBA-packed vertex attribute

Vertex data may be specified only in the first of these two formats by using the GL_INT_2_10_10_10_REV or GL_UNSIGNED_INT_2_10_10_10_REV tokens. When one of these tokens is used as thetypeparameter toglVertexAttribPointer(), each vertex consumes one 32-bit word in the vertex array. The word is unpacked into its components and then optionally normalized (depending on the value of thenormalizeparameter before being loaded into the appropriate vertex attribute. This data arrangement is particularly well suited to normals or other types of attributes that can benefit from the additional precision afforded by the 10-bit components but perhaps don’t require the full precision offered by half-float data (which would take 16-bits per component). This allows the conservation of memory space and bandwidth, which helps improve performance.

Static Vertex-Attribute Specification

Remember from Chapter 1 where you were introduced toglEnableVertexAttribArray()andglDisableVertexAttribArray().These functions are used to tell OpenGL which vertex attributes are backed by vertex buffers. Before OpenGL will read any data from your vertex buffers, you must enable the corresponding vertex attribute arrays withglEnableVertexAttribArray().You may wonder what happens if you don’t enable the attribute array for one of your vertex attributes. In that case, thestatic vertex attributeis used. The static vertex attribute for each vertex is the default value that will be used for the attribute when there is no enabled attribute array for it. For example, imagine you had a vertex shader that would read the vertex color from one of the vertex attributes. Now suppose that all of the vertices in a particular mesh or part of that mesh had the same color. It would be a waste of memory and potentially of performance to fill a buffer full of that constant value for all the vertices in the mesh. Instead, you can just disable the vertex attribute array and use the static vertex attribute to specify color for all of the vertices.

The static vertex attribute for each attribute may be specified using one ofglVertexAttrib*()functions. When the vertex attribute is declared as a floating-point quantity in the vertex shader (i.e., it is of typefloat,vec2,vec3,vec4, or one of the floating-point matrix types such asmat4), the followingglVertexAttrib*()commands can be used to set its value.

所有这些函数的隐式转换增刊lied parameters to floating-point before passing them to the vertex shader (unless they’re already floating-point). This conversion is a simple typecast. That is, the values are converted exactly as specified as if they had been specified in a buffer and associated with a vertex attribute by callingglVertexAttribPointer()with thenormalizeparameter set to GL_FALSE. For the integer variants of the functions, versions exist that normalize the parameters to the range [0, 1] or [–1, 1] depending on whether the parameters are signed or unsigned. These are:

Even with these commands, the parameters are still converted to floating-point before being passed to the vertex shader. Thus, they are suitable only for setting the static values of attributes declared with one of the single-precision floating-point data types. If you have vertex attributes that are declared as integers or double-precision floating-point variables, you should use one of the following functions:

Furthermore, if you have vertex attributes that are declared as one of the double-precision floating-point types, you should use one of theLvariants ofglVertexAttrib*(), which are:

Both theglVertexAttribI*()andglVertexAttribL*()variants ofglVertexAttrib*()pass their parameters through to the underlying vertex attribute just as theIversions ofglVertexAttribIPointer()do.

If you use one of theglVertexAttrib*()functions with less components than there are in the underlying vertex attribute (e.g., you useglVertexAttrib*()2f to set the value of a vertex attribute declared as avec4), default values are filled in for the missing components. Forw, 1.0 is used as the default value, and foryandz, 0.0 is used.6If you use a function that takes more components than are present in the vertex attribute in the shader, the additional components are simply discarded.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

调查和问题,我们收集inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simplyemailinformation@informit.com

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through ourContact Us form

Other Collection and Use of Information


Application and System Logs

皮尔森自动收集日志数据来帮助sure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

这个网站使用cookie和类似的技术personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on theAccount page.如果一个用户不再期望我们的服务和德西res to delete his or her account, please contact us atcustomer-service@informit.comand we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive:www.e-skidka.com/u.aspx

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information toNevadaDesignatedRequest@pearson.com

Supplemental Privacy Statement for California Residents


California residents should read ourSupplemental privacy statement for California residentsin conjunction with this Privacy Notice. TheSupplemental privacy statement for California residentsexplains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Pleasecontact usabout this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020