Simon iphone-OpenGL ES tutorial-04

Source: Internet
Author: User

 

Simon iPhone-OpenGL ES tutorial-04 OpenGL ES 04-color and texture

As my last tutorial said, I'm tired of drawing white objects on the screen, so that we can add some colors, just like the project automatically generated by the apple module. Next I will introduce you to pay attention to these concepts, because these concepts will work backwards (very fast) when we start texture rendering)

In OpenGL ES, a single color block can be designed for the entire object, or color transfer can be used for multi-color and rendering, so that the spectrum can transition from one color to the next. Rendering a single color on our objects is not very complicated.

Like everything in OpenGL, changing the color in OpenGL is a "state", that is, after (changing the State), the painting operation will use this color. Even if we call our "Reset", we are running only the actual vertices on glloadidentity () (this is because glloadidentity ). So we only need to add a line of code to change our two objects to any color. No color is worse than white. Now I will change it to blue.

Start xcode and go to drawview. Add the following after the first glloadidentity:

Glloadidentity ();
Glcolor4f (0.0, 0.0, 0.8, 1.0 );

Glcolor4f () tells OpenGL to start painting (filling) with the blue color ). The parameters are as follows:
Glcolor4f (glfloat red,
Glfloat green,
GLfloat blue,
GLfloat alpha );

In OpenGL ES, you must use four parameters to define the color (RGBA). Here, the RGB color cannot be used. Do not forget that alpha is the transparency value. 1.0 indicates the entity, and 0.0 indicates full transparency.
The red, green, and blue parameters are the floating point values of the color. 0.0 indicates no intensity, and 1.0 indicates full intensity.

Click compile and run. The following figure is displayed:


It is much better than the white one. Let's see how the color rotating object in the Apple template is implemented.

Multiple colors
Changing an object to multiple colors does not require much work. We need to define an array like the vertex array we have used and tell OpenGL to obtain the color from this array. Each color in the array corresponds to a vertex in the vertex array.

It makes me realize more clearly that the rectangle is rendered by color. The following code defines a color array that corresponds to a rectangle array.

 const GLfloat squareVertices[] = {        -1.0, 1.0, 0.0,               // Top left        -1.0, -1.0, 0.0,              // Bottom left        1.0, -1.0, 0.0,               // Bottom right        1.0, 1.0, 0.0                 // Top right    };     const GLfloat squareColours[] = {        1.0, 0.0, 0.0, 1.0,// Red - top left - colour for squareVertices[0]        0.0, 1.0, 0.0, 1.0,   // Green - bottom left - squareVertices[1]        0.0, 0.0, 1.0, 1.0,   // Blue - bottom right - squareVerticies[2]        0.5, 0.5, 0.5, 1.0    // Grey - top right- squareVerticies[3]    }; 

I hope this means that each value of the color array corresponds to a value in the vertex of the rectangle. Before running, we need to add some code for rectangular rendering.

     glLoadIdentity();    glTranslatef(1.5, 0.0, -6.0);    glRotatef(rota, 0.0, 0.0, -1.0);    glVertexPointer(3, GL_FLOAT, 0, squareVertices);    glEnableClientState(GL_VERTEX_ARRAY);    glColorPointer(4, GL_FLOAT, 0, squareColours);      // NEW    glEnableClientState(GL_COLOR_ARRAY);                // NEW    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);    glDisableClientState(GL_COLOR_ARRAY);               // NEW 

Here there are three new lines of code, Let's explain one line:
GlColorPointer (4, GL_FLOAT, 0, squareColours );

This is similar to creating a rectangular vertex array. The four parameters are:
1. Size-number of colors in the array
2. Data Format-here we use gl_float, because we use floating point numbers in the vertex array. You can also use an integer between 0 and.
3. stride-again, this shows the number of hops between two OpenGL values.
4. arry points-this is where data is stored.

Pay attention to the data format. gl_float indicates the parameter format of OpenGL (an enumeration type) and glfloat indicates the data type used in OpenGL.

OK. This function tells OpenGL where the data is and what the format is. However, just as the coordinate vertex array needs to tell OpenGL to use coordinates for objects, we need to give OpenGL a necessary State to tell OpenGL to use our color when rendering objects.

This is:
Glableclientstate (gl_color_array );

This will add this state to the OpenGL engine. Instead of using gl_vertex_array, we only need to tell the OpenGL color array to use gl_color_array.

Next, we can draw a rectangle normally. After drawing a rectangle, we need to close the color array. If we don't do this, we will use color rendering like a rectangle when we draw a triangle at the next time, so we call:
Gldisableclientstate (gl_color_array );

In this case, the current color array status of OpenGL needs to be disabled. If this is not done, a blue triangle will be drawn when drawview is called for the first time, and a color array will be used to draw a triangle when drawview is called for the second time. However, currently only triangleverticies [] are integrated arrays of triangles with three vertices, and only the first three colors are used.

After modification, run the following:

If you like, close the previous rotation function and check the color array corresponding to each vertex.

Coloring
How to notify the rectangle to draw from one color to the next? OpenGL uses coloring. Two coloring models can be used in OpenGL: gl_flat & gl_smooth. Gl_smooth is the default rendering.

To display their differences, insert the following line before the glloadidentity () function of the rectangle:
Glshademodel (gl_flat );

This glshademodel () function changes the state of the plane coloring mode in OpenGL from the smooth coloring mode. Similarly, OpenGL changes its State and keeps it in this state until you tell it to be modified, so you can put this line of code anywhere in the setupview function as long as you like.

Click "Build and go" and change the colored rectangle to the following:

Let me explain what happened here.

Triangle rendering is normal. As a plane color, coloring does not affect triangle painting. Now you can see that OpenGL uses two triangles to form a rectangle. Because of the plane coloring mode, only the last skin color of OpenGL is used to fill every triangle, that is, squarecolours [2] (blue) and squarecolours [3] (Gray ). If you are not sure which vertex is the last vertex of the two triangles in the rectangle, you can refer to the original tutorial.

To sum up, gl_smooth is a smooth coloring, which means that when you start to fill a rectangle, openGL uses the default color in our squarecolours [] array to define each vertex in the squarevertices [] array. Use the points between the area of each pixel in interpolation to smoothly change the four points between colors. In other words, this will be a colored rectangle.

Gl_flat is used to fill the entire element with the color of the last vertex of the object. The rectangle is composed of two triangles, so we can see two colored triangles.

Conclusion
Well, I hope this will be helpful to you. In real life, you may just want to use GL_SMOOTH for coloring, unless you do one of the nostalgic 3D games from C64 days. GL_SMOOTH is preset, so you do not have to enable it.

In addition, please note that the color classification you used above can also be used for texture ing, so I will go back to this tutorial and review the two.

Texture ing is just around the corner. I want to show you how to create a 3D object in future teaching courses. This will be flat color, but it does not matter, because we will start to map textures in the following tutorial.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.