Welcome to mirror list, hosted at ThFree Co, Russian Federation.

model.h « Graphics « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 10e403440ff434b36703dea241e1f9ba03f1477a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//-----------------------------------------------------------------------------
//           Name: model.h
//      Developer: Wolfire Games LLC
//         Author: David Rosen
//    Description: This class loads and displays models
//        License: Read below
//-----------------------------------------------------------------------------
//
//
//   Copyright 2022 Wolfire Games LLC
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//
//-----------------------------------------------------------------------------
#pragma once

#include "Math/vec2.h"
#include "Math/mat3.h"

#include "Graphics/vbocontainer.h"
#include "Internal/filesystem.h"
#include "Asset/Asset/image_sampler.h"

#include <opengl.h>

#include <vector>
#include <stdio.h>
#include <string>

class Mesh;
class DrawBatch;

//-----------------------------------------------------------------------------
// Class Definition
//-----------------------------------------------------------------------------

enum ModelFlags {
    _MDL_CENTER = (1 << 0),
    _MDL_SIMPLE = (1 << 1),
    _MDL_FLIP_FACES = (1 << 2),
    _MDL_USE_TANGENT = (1 << 3)
};

const unsigned short _model_cache_version = 41;
const unsigned short _morph_cache_version = 3;

class Model;
class vec4;

class Model {
   public:
    std::vector<GLfloat> vertices;
    std::vector<GLfloat> normals;
    std::vector<GLfloat> tangents;
    std::vector<GLfloat> bitangents;
    std::vector<vec3> face_normals;
    std::vector<GLfloat> tex_coords;
    std::vector<GLfloat> tex_coords2;
    std::vector<GLfloat> aux;
    std::vector<GLuint> faces;

    std::vector<vec4> bone_weights;
    std::vector<vec4> bone_ids;
    std::vector<std::vector<vec4> > transform_vec;
    std::vector<vec2> tex_transform_vec;
    std::vector<vec3> morph_transform_vec;

    std::set<ImageSamplerRef> image_samplers;

    vec3 min_coords, max_coords, center_coords;

    bool vbo_loaded;
    bool vbo_enabled;
    unsigned short checksum;

    VBOContainer VBO_vertices;
    VBOContainer VBO_normals;
    VBOContainer VBO_tangents;
    VBOContainer VBO_bitangents;
    VBOContainer VBO_tex_coords;
    VBOContainer VBO_tex_coords2;
    VBOContainer VBO_aux;
    VBOContainer VBO_faces;

    std::vector<int> optimize_vert_reorder;
    std::vector<int> precollapse_vert_reorder;
    int precollapse_num_vertices;

    bool use_tangent;
    vec3 old_center;

    vec3 bounding_sphere_origin;
    float bounding_sphere_radius;

    float texel_density;
    float average_triangle_edge_length;

    std::string path;  // Source file. Primarily for debugging purposes.
    ModID modsource_;

    int lineCheckNoBackface(const vec3 &p1, const vec3 &p2, vec3 *p, vec3 *normal = 0) const;
    int lineCheck(const vec3 &p1, const vec3 &p2, vec3 *p, vec3 *normal = 0, bool backface = true) const;
    void LoadObj(const std::string &name, char flags = _MDL_CENTER, const std::string &alt_name = "", const PathFlags searchPaths = kAnyPath);
    void RemoveDoubledTriangles();
    void Draw();
    void DrawAltTexCoords();
    void DrawToTextureCoords();
    void SmoothNormals();
    void SmoothTangents();
    void calcFaceNormals();
    void calcBoundingBox();
    void calcBoundingSphere();
    void CenterModel();
    void calcNormals();
    void calcTangents();
    void calcNormals(bool smooth);
    void createVBO();
    void ResizeVertices(int size);
    void ResizeFaces(int size);
    void WriteToFile(FILE *file);
    void ReadFromFile(FILE *file);
    void CalcTexelDensity();
    void CalcAverageTriangleEdge();
    void Dispose();
    bool LoadAuxFromImage(const std::string &image_path);

    Model();
    Model(const Model &copy);
    Model &operator=(const Model &copy);

    virtual ~Model();

    void StartPreset();
    void EndPreset();
    void DrawPreset();
    void CopyFacesFromModel(const Model &source_model, const std::vector<int> &faces);
    void LoadObjMorph(const std::string &name, const std::string &base_name);
    void CopyVertCollapse(const Model &source_model);
    void RemoveDuplicatedVerts();
    void OptimizeTriangleOrder();
    void SortTrianglesBackToFront(const vec3 &camera);
    void PrintACMR();
    void OptimizeVertexOrder();
    void SaveObj(std::string name);
    void RemoveDegenerateTriangles();

    const static int ERROR_MORE_THAN_ONE_OBJECT;
    int SimpleLoadTriangleCutObj(const std::string &name_to_load);
    const char *GetLoadErrorString(int err);
};
void CopyTexCoords2(Model &a, const Model &b);

class Shaders;
class Graphics;
void DrawModelVerts(Model &model, const char *vert_attrib_str, Shaders *shaders, Graphics *graphics, int shader);