code:
#include <stddef.h>

#include "../inc/gl.h"

#ifndef models_H
#define models_H

extern GLfloat exo_vertices[];

#ifndef EXO_VERTICES_ONLY
extern GLfloat exo_colors[];
extern const GLuint exo_indices[];
extern const GLsizeiptr exo_numind;
#endif

extern const GLsizeiptr exo_numvert;
extern const size_t exo_vertices_size;

#ifndef EXO_VERTICES_ONLY
extern const size_t exo_colors_size;
extern const size_t exo_indices_size;

extern GLfloat ncube_vertices[];
extern const GLuint ncube_indices[];
extern const GLsizeiptr ncube_numind;
extern const GLsizeiptr ncube_numvert;
extern const size_t ncube_vertices_size;
extern const size_t ncube_indices_size;

extern GLfloat inner_colors[];
extern const size_t inner_colors_size;

extern const GLfloat low_vertices[];
extern const GLushort low_indices[];
extern const GLsizeiptr low_numind;
extern const GLsizeiptr low_numvert;
extern const size_t low_vertices_size;
extern const size_t low_indices_size;

extern const GLfloat rock8_vertices[];
extern const GLfloat rock8_normals[];
extern const GLfloat rock8_uvmap[];
extern const GLushort rock8_indices[];
extern const GLsizeiptr rock8_numind;
extern const GLsizeiptr rock8_numvert;
extern const size_t rock8_vertices_size;
extern const size_t rock8_normals_size;
extern const size_t rock8_uvmap_size;
extern const size_t rock8_indices_size;

extern const GLfloat rock7_vertices[];
extern const GLfloat rock7_normals[];
extern const GLfloat rock7_uvmap[];
extern const GLushort rock7_indices[];
extern const GLsizeiptr rock7_numind;
extern const GLsizeiptr rock7_numvert;
extern const size_t rock7_vertices_size;
extern const size_t rock7_normals_size;
extern const size_t rock7_uvmap_size;
extern const size_t rock7_indices_size;

extern const GLfloat rock6_vertices[];
extern const GLfloat rock6_normals[];
extern const GLfloat rock6_uvmap[];
extern const GLushort rock6_indices[];
extern const GLsizeiptr rock6_numind;
extern const GLsizeiptr rock6_numvert;
extern const size_t rock6_vertices_size;
extern const size_t rock6_normals_size;
extern const size_t rock6_uvmap_size;
extern const size_t rock6_indices_size;

extern const GLfloat rock5_vertices[];
extern const GLfloat rock5_normals[];
extern const GLfloat rock5_uvmap[];
extern const GLushort rock5_indices[];
extern const GLsizeiptr rock5_numind;
extern const GLsizeiptr rock5_numvert;
extern const size_t rock5_vertices_size;
extern const size_t rock5_normals_size;
extern const size_t rock5_uvmap_size;
extern const size_t rock5_indices_size;

extern const GLfloat rock4_vertices[];
extern const GLfloat rock4_normals[];
extern const GLfloat rock4_uvmap[];
extern const GLushort rock4_indices[];
extern const GLsizeiptr rock4_numind;
extern const GLsizeiptr rock4_numvert;
extern const size_t rock4_vertices_size;
extern const size_t rock4_normals_size;
extern const size_t rock4_uvmap_size;
extern const size_t rock4_indices_size;

extern const GLfloat rock3_vertices[];
extern const GLfloat rock3_normals[];
extern const GLfloat rock3_uvmap[];
extern const GLushort rock3_indices[];
extern const GLsizeiptr rock3_numind;
extern const GLsizeiptr rock3_numvert;
extern const size_t rock3_vertices_size;
extern const size_t rock3_normals_size;
extern const size_t rock3_uvmap_size;
extern const size_t rock3_indices_size;

extern const GLfloat rock2_vertices[];
extern const GLfloat rock2_normals[];
extern const GLfloat rock2_uvmap[];
extern const GLushort rock2_indices[];
extern const GLsizeiptr rock2_numind;
extern const GLsizeiptr rock2_numvert;
extern const size_t rock2_vertices_size;
extern const size_t rock2_normals_size;
extern const size_t rock2_uvmap_size;
extern const size_t rock2_indices_size;

extern const GLfloat rock1_vertices[];
extern const GLfloat rock1_normals[];
extern const GLfloat rock1_uvmap[];
extern const GLushort rock1_indices[];
extern const GLsizeiptr rock1_numind;
extern const GLsizeiptr rock1_numvert;
extern const size_t rock1_vertices_size;
extern const size_t rock1_normals_size;
extern const size_t rock1_uvmap_size;
extern const size_t rock1_indices_size;

extern GLfloat beam_vertices[];
extern const GLfloat beam_normals[];
extern const GLfloat beam_uvmap[];
extern const GLubyte beam_indices[];
extern const GLsizeiptr beam_numind;
extern const GLsizeiptr beam_numvert;
extern const size_t beam_vertices_size;
extern const size_t beam_normals_size;
extern const size_t beam_uvmap_size;
extern const size_t beam_indices_size;

extern GLfloat ufo_vertices[];
extern const GLfloat ufo_normals[];
extern GLfloat ufo_uvmap[];
extern const GLushort ufo_indices[];
extern const GLsizeiptr ufo_numind;
extern const GLsizeiptr ufo_numvert;
extern const size_t ufo_vertices_size;
extern const size_t ufo_normals_size;
extern const size_t ufo_uvmap_size;
extern const size_t ufo_indices_size;

// extern GLfloat ufo_damaged_vertices[];
// extern const GLfloat ufo_damaged_normals[];
// extern GLfloat ufo_damaged_uvmap[];
// extern const GLushort ufo_damaged_indices[];
// extern const GLsizeiptr ufo_damaged_numind;
// extern const GLsizeiptr ufo_damaged_numvert;
// extern const size_t ufo_damaged_vertices_size;
// extern const size_t ufo_damaged_normals_size;
// extern const size_t ufo_damaged_uvmap_size;
// extern const size_t ufo_damaged_indices_size;

extern GLfloat ufo_lights_vertices[];
extern const GLubyte ufo_lights_indices[];
extern const GLsizeiptr ufo_lights_numvert;
extern const GLsizeiptr ufo_lights_numind;
extern const size_t ufo_lights_vertices_size;
extern const size_t ufo_lights_indices_size;

extern GLfloat ufo_tri_vertices[];
extern const GLubyte ufo_tri_indices[];
extern const GLsizeiptr ufo_tri_numvert;
extern const GLsizeiptr ufo_tri_numind;
extern const size_t ufo_tri_vertices_size;
extern const size_t ufo_tri_indices_size;

extern GLfloat ufo_shield_vertices[];
extern const GLushort ufo_shield_indices[];
extern const GLfloat ufo_shield_colors[];
extern const GLsizeiptr ufo_shield_numvert;
extern const GLsizeiptr ufo_shield_numind;
extern const size_t ufo_shield_vertices_size;
extern const size_t ufo_shield_indices_size;
extern const size_t ufo_shield_colors_size;

#endif
#endif


code:
all: main.cpp
	g++ -std=c++17 -Ofast -march=native -o obj2header main.cpp tiny_obj_loader.h

repo_name: AstroImpact/AstroImpact

license: mit


code:
# Obj to C header converter

Converts the obj 3D model into a C header file including the model vertex data in the following format (not present attributes are omited):

```
struct ModelVertex {
float position[3];
float normal[3];
float uv[2];
};

struct ModelVertex const modelVertices[N] = {...
const unsigned int modelIndices[M] = {...
```

Vertex deduplication is performed so M >= N

## Build

```
wget https://github.com/syoyo/tinyobjloader/raw/master/tiny_obj_loader.h
make
```

## Usage

```
./obj2header input.obj output.h
```

## downloaded from
https://github.com/ichlubna/obj2header

## other useful links
https://github.com/syoyo/tinyobjloader-c


code:
#define TINYOBJLOADER_IMPLEMENTATION
#include "tiny_obj_loader.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

struct Vertex {
	std::vector<float> position;
	std::vector<float> normal;
	std::vector<float> uv;

	bool operator==(const Vertex &a) const
	{
		return a.position == this->position && a.normal == this->normal && a.uv == this->uv;
		//constexpr float EPS = 0.000001;
		//return (fabs(a.position[0] - this->position[0]) < EPS) && (fabs(a.position[1] - this->position[1]) < EPS) && (fabs(a.position[2] - this->position[2]) < EPS);
	}
};

void convert(std::string input, std::string output)
{
	tinyobj::attrib_t attributes;
    	std::vector<tinyobj::shape_t> shapes;
	std::vector<tinyobj::material_t> materials;
	std::string warning, error;
	std::vector<Vertex> vertices;
	std::vector<unsigned int> indices;

	if(!tinyobj::LoadObj(&attributes, &shapes, &materials, &warning, &error, input.c_str()))
		throw std::runtime_error("Obj loader:" + warning + "\n" + error);

	bool isNormal = attributes.normals.size() > 0;
	bool isUv = attributes.texcoords.size() > 0;

	for(const auto& shape : shapes)
		for(const auto& index : shape.mesh.indices)
		{
			Vertex vertex;
			int offset = 3*index.vertex_index;
			vertex.position = {attributes.vertices[offset], attributes.vertices[offset+1], attributes.vertices[offset+2]};
			if(isNormal)
			{	offset = 3*index.normal_index;
				vertex.normal = {attributes.normals[offset], attributes.normals[offset+1], attributes.normals[offset+2]};
			}
			if(isUv)
			{
				offset = 2*index.texcoord_index;
				vertex.uv = {attributes.texcoords[offset], attributes.texcoords[offset+1]};
			}
			//TODO use map or set
			unsigned int uniqueIndex = vertices.size();
			bool found = false;
			for(int i=0; i<vertices.size(); i++)
				if(vertices[i] == vertex)
				{
					uniqueIndex = i;
					found = true;
					break;
				}
			if(!found)
				vertices.push_back(vertex);
			indices.push_back(uniqueIndex);
		}

	std::ofstream file;
	file.open(output);
	file << "struct ModelVertex {" << std::endl;
	file << "float position[3];" << std::endl;
	if(isNormal)
		file << "float normal[3];" << std::endl;
	if(isUv)
		file << "float uv[2];" << std::endl;
	file << "};" << std::endl << std::endl;

	file << "struct ModelVertex const modelVertices[" << vertices.size() << "] = {" << std::endl;
	file.unsetf ( std::ios::floatfield );
	file.precision(5);
	file.setf( std::ios::fixed, std:: ios::floatfield );
	for(const auto& vertex : vertices)
	{
		file << "{ { " << vertex.position[0] << "f, " << vertex.position[1] << "f, " << vertex.position[2] << "f }, ";
		if(isNormal)
			file << "{ " << vertex.normal[0] << "f, " << vertex.normal[1] << "f, " << vertex.normal[2] << "f }, ";
		if(isUv)
			file << "{ " << vertex.uv[0] << "f, " << vertex.uv[1] << "f } ";
		file << " }," << std::endl;
	}
	file << " };" << std::endl << std::endl;

	file << "const unsigned int modelIndices[" << indices.size() << "] = {" << std::endl;
	unsigned int i = 0;
	for(const auto& index : indices)
	{
		file << index << ", ";
		i++;
		if((i % 3) == 0)
			file << std::endl;
	}
	file << "};";

	file.close();
}

int main(int argc, char **argv)
{
	if(argc != 3)
	{
		std::cerr << "Wrong number of arguments, just give me the the name of input and output file. Example: stargate.obj stargate.h" << std::endl;
		return EXIT_FAILURE;
	}

	try
	{
		convert(std::string(argv[1]), std::string(argv[2]));
	}
	catch(const std::exception &e)
	{
		std::cerr << e.what() << std::endl;
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}


code: gcc main.c -lm -Ofast -march=native -o o


code:
/*
    James William Fletcher (github.com/mrbid)
        December 2022
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

#include "ufo.h"

int main(int argc, char** argv)
{
    FILE* f = fopen("out.txt", "w");
    for(size_t i = 0; i < 45346; i++)
    {
        //fprintf(f, "%g,%g,%g,", modelVertices[i].position[0], modelVertices[i].position[1], modelVertices[i].position[2]);
        //fprintf(f, "%g,%g,%g,", modelVertices[i].normal[0], modelVertices[i].normal[1], modelVertices[i].normal[2]);
        fprintf(f, "%g,%g,", modelVertices[i].uv[0], modelVertices[i].uv[1]);
    }
    fclose(f);
}


code:
#define TINYOBJLOADER_IMPLEMENTATION
#include "tiny_obj_loader.h"