Reconstruction of cocos2d-x model loading, object-oriented in my eyes

Source: Internet
Author: User

Reconstruction of cocos2d-x model loading, object-oriented in my eyes

These two days in writing cocos2d-x load ogre mesh model thing, completed half, still poor animation file did not come in. This article does not teach you how to load model files, because there is nothing to teach about. It is nothing more than loading files and parsing the desired vertex, key frame, and other data, finally, assign a value to MeshData in the coco2d-x. Here I want to express some ideas about code format, code design, and code refactoring. I will analyze them one by one based on some related or irrelevant issues.


I. Why multiple model formats are supported? Cocos2d-x itself defines a new format c3d, c3t such good?

First, I think that a mature game engine does not need to support multiple model formats, at least from rendering and internal code, because that makes no sense. There will always be omissions when there is less support. If there is more support, it will become very bloated. I remember seeing an open-source library for parsing model format. The static library has more than one hundred MB. This is completely meaningless for a game engine.

But there is a problem here, that is, the 3D part of the cocos2d-x is not mature, not competitive. Therefore, from the perspective of engine development, it is very necessary to support several common formats, which makes people feel simple and easy to use. For example, in fbx format, supporting this means that many unity models can be used directly. For example, in mesh format, supporting this means that the models of torch light and tianlong Babu can be directly displayed. This is not to say that we encourage the theft of resources for commercial games, but this support makes it easier for new users and the masses to accept the 3D capabilities of this engine. As an immature engine, this compromise means maturity.

Finally, I have to say that the 3D part of the cocos2d-x, at least the model loading part, is designed very ugly, messy, and not flexible to write. I have always been curious, do developers who do open-source game engines or commercial game engines inside the company like to build cars behind closed doors? We can easily find a reference solution. I'm also curious, Why can a company spend year 35 years developing a game engine and give people a sense of semi-finished products? If I did, encapsulate OGRE directly and spend a month discussing the interfaces and features to be provided. The basics are all there. If a rendering engine cannot be developed in a year, then I will not be a programmer. It is true that even some technical personnel will think that their design is beautiful, and others write the same things as shit. However, if there is no successful product to support it, some complaints will not be answered.

I also like to talk about it myself, including this article, but I admit that I am a cool man. Do the scalpers with millions of annual salary do the same?

To sum up, this part is said, in fact, cocos2d-x does not need to define a new model format, directly support the popular three five formats is more conducive to the promotion, in this way, you don't have to worry about and maintain the corresponding tool chain. In addition, I did not see any technical advantages in its 3D section, so I should put down the Section to take the civilian route.


Ii. potential and skills in project development

This is what I saw in my online novels. The so-called trend is the military trend, the national trend, the so-called tactics, and wonders. Many stars have classic tactics, and these "tricks" have indeed guided victory. However, the reason why these classic tactics are so popular is that they are rare and have never been killed. However, the truly great commander will always let himself stand on the dominant side rather than the weak side. The failure on the "trend" requires more "techniques" to make up for the success, and as long as one of the "techniques" fails, it will all lose.

From the introduction to Game Development, my point of view is that the direction should be correct and the goal should be clear; otherwise, the other cool people will not be able to turn the storm.

Many cool people have no technical skills, but they cannot clearly understand the direction. When you encounter problems, you always want to use your own technology to solve the problem, or always think that your solution is the best. This is why many cool people like to build their own wheels, because they can indeed find numerous disadvantages on other wheels. They created a perfect wheel, became an "expert", and gained fame, but did not sell their cars. In my opinion, this is a wrong direction. Of course, how can we say that their achievements are beyond the control of ordinary people in our generation.


Iii. object-oriented and Design Patterns

This is the basis for learning c ++ and the most frequently asked questions during interviews. This is not to say that I want to write a thesis "Object-Oriented-from theory to practice", but to talk about my views on beautiful code.

When I was in college, I asked a cool student, "I often read beautiful code. What kind of code is beautiful ?". He did not answer the question. This problem should be caused by more code and more code to be written. Different people have different aesthetics. Some people like to reference various models to make their designs extremely flexible. Some people like to show off various technologies to make the Code look very high; some people like to show how solid their foundations are, and the written code is very compliant with the textbook terms.

(To be continued)


4. Reconstruction

The following example shows how to refactor the title. It looks very simple, but this is the skill we need in actual development. What we need is not to compare the 108 design patterns into our project, but to see "Bad taste of code" and then correct them.

For example, the current cocos2d-x model is loaded in a Bundle3D class, the load according to the file extension and the file header call the corresponding function, such as loadMeshData loadMeshDataJson and so on. Sprite3D files also use the extension to determine whether to use Bundle3D for reading or ObjLoader for reading.

In fact, a small change can make the code flexible.

Design a Bundle3DLoader, and then implement a Loader based on the model to be supported, such as Bundle3DMeshLoader and Bundle3DFbxLoader. A Factory manages Loader. Use Factory to obtain the corresponding loader and call functions such as loadMesh and loadMaterial to load the actual model.

Example:

class Bundle3DLoader{public:virtual bool isSupportFile(const std::string filePath) = 0;virtual void reset();virtual bool load(const std::string& path);virtual bool loadMeshData(const std::string& id, MeshData* meshdata);virtual bool loadSkinData(const std::string& id, SkinData* skindata);virtual bool loadMaterialData(const std::string& id, MaterialData* materialdata);virtual bool loadAnimationData(const std::string& id, Animation3DData* animationdata);virtual bool loadMeshDatas(MeshDatas& meshdatas);virtual bool loadNodes(NodeDatas& nodedatas);virtual bool loadMaterials(MaterialDatas& materialdatas);static std::string getFileExt(const std::string filePath);};

In this way, you can directly implement a corresponding Loader by adding a new model format, and implement it outside the engine, and then add and manage through the Factory.

There is no advanced mode, but it is indeed changing the readability of the Code. The implementation of loading a model corresponding to a file is much more readable than Bundle3D with 3000 lines. This is object-oriented. This is refactoring. It is not mysterious. It is very simple and serves engineering. It is not a good object to write more than a dozen classes and implement an object-oriented relationship.


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.