Motionstreak. cpp parsing of cocos2dx (-)

Source: Internet
Author: User

It was originally because of work projects that we needed to make a special effect similar to "Sword light and sword shadows", similar to the effect of the blue stick and the swordsmanship. Because it is dynamic, the art plot cannot achieve this effect, so I want to use OpenGL to implement it.

Although I have read a lot of OpenGL books, I have learned everything from English and Chinese. There are also a lot of OpenGL tutorials on the Internet, which can be understood during reading. But when I write my own scripts, I feel like I have no way to start. I just asked my thoughts in the group, the answer is very simple, that is, a triangle belt. Later I found that cocos2dx already has a ready-made special effect, namely motionstreak. I was delighted to read the source code and record my ideas to share with you. The level is limited. please correct me for the error, make progress together.

There are three basic elements in opengles: Point Line and triangle. Points are mainly used in particle systems. The most common elements are triangles. The exquisite 3D model we see is composed of many triangles, the number of triangles determines the precision of the model, because we have more than enough time to use triangles in our needs, not to mention 2D special effects.

The general idea is as follows: our special effect is a triangle belt, and this special effect is followed by the blade or people (we will follow the special effect following the goal below, it is the people here or the cutting edge, called the special effect carrier). In fact, they are all dynamic changes, which requires us to dynamically update the triangle band and constantly add fixed points to and remove fixed points to the triangle band. There are two ways to remove a vertex. One is that each vertex has a lifecycle and will be removed after the lifecycle. The second is to specify the upper limit of the number of vertices. If the number of vertices is exceeded, the vertex is removed. Cocos2dx controls vertex removal by setting a survival time for each vertex, which will be mentioned in the code. So how to add vertices to the triangle band? Because this special effect always follows the carrier, we first think that we should use the carrier position to generate the triangle vertex and add it to the triangle band, in cocos2dx, the carrier is placed in point * _ pointvertexes, and vertices in the generated triangle band are placed in vertex2f * _ vertices.

Note that we cannot directly load the position of the carrier into the triangle band, but generate two vertices based on the position. This is the key to this dynamic effect.

How can we generate two vertices in the triangle band based on the points at the position of a carrier? Generally, it is to take the position of the previous vector, form a vector V1 with the position of the current vector, and then calculate the vertical vector V2 of this vector, because our special effect is wide, when this special effect is created, it will be passed in the parameter. Based on the input width stroke, two vertices on the V2 vector are obtained, put them into the triangle band (we can think of these two vertices as a group for us to understand, and when setting texture coordinates, the V components of these two vertices are the same, the U component is 0, and the U component is 1. You can imagine ). The following code contains comments. In this way, this dynamic triangle band is generated.

This special effect can be customized, so you need to set texture coordinates, as mentioned above. The general idea is to re-set the V component based on the number of vertices in the triangle band after each update. The U component is relatively simple. If it is not 1, it is 0. (This U is equivalent to X, V is equivalent to Y, and the range is from 0 to 1)

You can use OpenGL to assign texture coordinates and texture textures.

The code below is as follows.

The two most important functions are:

Void motionstreak: Update (float delta)

Void motionstreak: ondraw (const kmmat4 & transform, bool transformupdated)

 

We should start with these two methods. Obviously, the update method dynamically updates the triangle band, while the ondraw method draws the current triangle band.

The next article focuses on code.

 

  

 

  

  

Motionstreak. cpp parsing of cocos2dx (-)

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.