OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
meshloader.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2014-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #include "meshloader.h"
22 #include "glutil.h"
23 #include "common.h"
24 #include <sstream>
25 #include <iostream>
26 #include <fstream>
27 #include <vector>
28 using std::stringstream;
29 
30 /*
31 The .bin format is simply a memory dump of the model data
32 as it resides in the GPU. For instance, the teapot mesh has
33 3xPosition, 2xTexel, 3xNormal and has N vertices.
34 This gives N * 8 attributes, which is the first part of
35 the .bin file. The remaining data is the element index data.
36 */
37 bool load_mesh_binary(Mesh &mesh, string path)
38 {
39  std::ifstream file(path);
40  if (!file.is_open())
41  return false;
42  std::stringstream bindata;
43  bindata << file.rdbuf();
44  file.close();
45  int attrib_count; bindata >> attrib_count;
46  float *vertex_data = new float[attrib_count];
47  for (int i = 0; i < attrib_count; i++)
48  bindata >> vertex_data[i];
49  int index_count; bindata >> index_count;
50  uint32 *index_data = new uint32[index_count];
51  for (int i = 0; i < index_count; i++)
52  bindata >> index_data[i];
53 
54  mesh.vertex_buffer = gen_buffer(GL_ARRAY_BUFFER, GL_STATIC_DRAW, attrib_count * sizeof(float), vertex_data);
55  mesh.index_buffer = gen_buffer(GL_ELEMENT_ARRAY_BUFFER, GL_STATIC_DRAW, index_count * sizeof(uint32), index_data);
56  delete[] vertex_data;
57  delete[] index_data;
58  mesh.num_indices = index_count;
59  return true;
60 }
GLuint index_buffer
Definition: primitives.h:29
unsigned int uint32
Definition: common.h:32
bool load_mesh_binary(Mesh &mesh, string path)
Definition: meshloader.cpp:37
int num_indices
Definition: primitives.h:30
GLuint vertex_buffer
Definition: primitives.h:28
GLuint gen_buffer(GLenum target, GLenum usage, GLsizei size, const void *data)
Definition: glutil.cpp:122
static Mesh * mesh[2]
Definition: ocean.cpp:59