render from json

This commit is contained in:
2026-02-19 22:41:46 -06:00
parent 74c980adb0
commit ce43c56ea6
11 changed files with 537 additions and 171 deletions

View File

@@ -1,89 +1,135 @@
#include "pch.hpp"
#include "utils/Objects/Body.h"
#define STB_TRUETYPE_IMPLIMENTATION
#include <stb/stb_truetype.h>
#include <GL/glew.h>
namespace Archimedes {
Body readOBJ(std::string path, Shader shader) {
class Text {
std::ifstream file(path);
GLuint tex;
if(!file.is_open()) return Body();
std::vector<unsigned char> ttf;
stbtt_bakedchar cdata[96];
std::string s;
std::string path;
std::vector<float> verticies;
std::vector<unsigned int> indicies;
public:
unsigned int i = 0;
unsigned int j = 0;
Text(std::string p) : path(p) {}
~Text() {}
float point;
unsigned int idx;
while(!file.eof()) {
getline(file, s, ' ');
//std::cout << "\nline start: " << s << std::endl;
if(s == "v") {
file >> point;
verticies.push_back(point);
//std::cout << "point1: " << point << std::endl;
file >> point;
verticies.push_back(point);
//std::cout << "point2: " << point << std::endl;
file >> point;
verticies.push_back(point);
//std::cout << "point3: " << point << std::endl;
//std::cout << "index: " << j << std::endl;
void textInit() {
getline(file, s);
//file.ignore();
} else if(s == "f") {
file >> idx;
indicies.push_back(--idx);
file.ignore(5, ' ');
file >> idx;
indicies.push_back(--idx);
file.ignore(5, ' ');
ttf.reserve(1<<20);
unsigned char temp_bitmap[1024*1024];
std::ifstream file(path, std::ios::binary);
file >> idx;
indicies.push_back(--idx);
getline(file, s);
} else {
getline(file, s);
if(!file.is_open()) {
std::cout << "ttf won't open!\n";
}
while(!file.eof()) {
ttf.push_back(file.get());
}
file.close();
stbtt_BakeFontBitmap(ttf.data(),0, 64.0, temp_bitmap,1024,1024, 32,96, cdata);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 1024,1024,0, GL_ALPHA, GL_UNSIGNED_BYTE, temp_bitmap);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
file.close();
Body* genText(float x, float y, std::string text) {
// assume orthographic projection with units = screen pixels, origin at top left
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex);
return Body(
VertexBuffer(verticies),
IndexArray(indicies),
VertexLayout(),
shader
);
}
std::vector<float> v;
std::vector<unsigned int> i;
for(unsigned int j = 0; j < text.length(); j++) {
if (text[j] >= 32) {
stbtt_aligned_quad q;
stbtt_GetBakedQuad(cdata, 512,512, text[j]-32, &x,&y,&q,1);//1=opengl & d3d10+,0=d3d9
v.push_back(q.x0);
v.push_back(q.y0);
v.push_back(0.0f);
v.push_back(q.s0);
v.push_back(q.t0);
i.push_back(j * 4);
v.push_back(q.x1);
v.push_back(q.y0);
v.push_back(0.0f);
v.push_back(q.s1);
v.push_back(q.t0);
i.push_back(j * 4 + 1);
v.push_back(q.x1);
v.push_back(q.y1);
v.push_back(0.0f);
v.push_back(q.s1);
v.push_back(q.t1);
i.push_back(j * 4 + 2);
v.push_back(q.x0);
v.push_back(q.y1);
v.push_back(0.0f);
v.push_back(q.s0);
v.push_back(q.t1);
i.push_back(j * 4 + 3);
}
}
return new Body(
VertexBuffer(v),
IndexArray(i),
VertexLayout(),
Shader()
);
}
};
using json = nlohmann::json;
class JObject {
public:
JObject() {}
JObject() = default;
void load(std::string path) {
std::ifstream file(path);
if (!file.is_open()) goodJSON = false;
document = json::parse(file);
file.close();
goodJSON = true;
templates = document["Templates"];
objects = document["Objects"];
setup = document["Set Up"];
@@ -94,19 +140,141 @@ namespace Archimedes {
bool isValid() const { return goodJSON; }
Body createCircle(json& element, std::string name) {
Body* createCircle(json& element, std::string name) {
std::vector<float> v;
std::vector<unsigned int> i;
float d = element["diameter"];
for(int it = 0; it < resolution; it++) {
v.push_back(glm::cos(2.0f * glm::pi<float>() / resolution));
v.push_back(glm::sin(2.0f * glm::pi<float>() / resolution));
v.push_back(d / 2.0f * glm::cos(it * 2.0f * glm::pi<float>() / resolution));
v.push_back(d / 2.0f * glm::sin(it * 2.0f * glm::pi<float>() / resolution));
v.push_back(0.0f);
i.push_back(it);
}
return Body(
return new Body(
VertexBuffer(v),
IndexArray(i),
VertexLayout(),
shader,
RenderMode::ConnectedLinesLooped
);
}
Body* createSquare(json& element, std::string name) {
std::vector<float> v;
std::vector<unsigned int> i;
float width = element["width"];
v.push_back(width / 2.0f);
v.push_back(width / 2.0f);
v.push_back(0.0f);
i.push_back(0);
v.push_back(-width / 2.0f);
v.push_back(width / 2.0f);
v.push_back(0.0f);
i.push_back(1);
v.push_back(-width / 2.0f);
v.push_back(-width / 2.0f);
v.push_back(0.0f);
i.push_back(2);
v.push_back(width / 2.0f);
v.push_back(-width / 2.0f);
v.push_back(0.0f);
i.push_back(3);
return new Body(
VertexBuffer(v),
IndexArray(i),
VertexLayout(),
shader,
RenderMode::ConnectedLinesLooped
);
}
Body* createRect(json& element, std::string name) {
std::vector<float> v;
std::vector<unsigned int> i;
float width = element["width"];
float height = element["height"];
v.push_back(width / 2.0f);
v.push_back(height / 2.0f);
v.push_back(0.0f);
i.push_back(0);
v.push_back(-width / 2.0f);
v.push_back(height / 2.0f);
v.push_back(0.0f);
i.push_back(1);
v.push_back(-width / 2.0f);
v.push_back(-height / 2.0f);
v.push_back(0.0f);
i.push_back(2);
v.push_back(width / 2.0f);
v.push_back(-height / 2.0f);
v.push_back(0.0f);
i.push_back(3);
return new Body(
VertexBuffer(v),
IndexArray(i),
VertexLayout(),
shader,
RenderMode::ConnectedLinesLooped
);
}
Body* createPoly(json& element, std::string name) {
std::vector<float> v;
std::vector<unsigned int> i;
json coords = element["coords"];
unsigned int j = 0;
for(json::const_iterator it = coords.cbegin(); it != coords.cend(); it++) {
json c = *it;
v.push_back(c["x"]);
v.push_back(c["y"]);
v.push_back(c["z"]);
i.push_back(j++);
}
return new Body(
VertexBuffer(v),
IndexArray(i),
VertexLayout(),
shader,
RenderMode::ConnectedLinesLooped
);
}
Body* createText(json& element, std::string name) {
std::vector<float> v;
std::vector<unsigned int> i;
//do something
return new Body(
VertexBuffer(v),
IndexArray(i),
VertexLayout(),
@@ -114,24 +282,10 @@ namespace Archimedes {
);
}
Body createRect(json& element, std::string name) {
return Body();
}
Body createPoly(json& element, std::string name) {
return Body();
}
Body createText(json& element, std::string name) {
return Body();
}
std::vector<Body> buildObjects() {
std::vector<Body> v;
void buildObjects() {
for (json::const_iterator it = templates.cbegin(); it != templates.cend(); it++)
for (json::const_iterator it = objects.cbegin(); it != objects.cend(); it++)
{
json element = *it;
std::string name = element["name"];
@@ -139,27 +293,79 @@ namespace Archimedes {
if (type == "CIRCLE") {
objectMap[name] = createCircle(element, name);
}
if (type == "SQUARE") {}
if (type == "RECTANGLE") {}
if (type == "POLYGON") {}
if (type == "TEXT") {}
if (type == "SQUARE") {
objectMap[name] = createSquare(element, name);
}
if (type == "RECTANGLE") {
objectMap[name] = createRect(element, name);
}
if (type == "POLYGON") {
objectMap[name] = createPoly(element, name);
}
if (type == "TEXT") {
objectMap[name] = nullptr;
}
}
return v;
}
void objectActions()
{
for (json::const_iterator it = setup.cbegin(); it != setup.cend(); it++)
{
json element = *it;
std::string name = element["name"];
Body* o = objectMap[name];
if (o == nullptr) continue;
if (element.contains("moveto"))
{
json moveTo = element["moveto"];
float x = moveTo["x"];
float y = moveTo["y"];
float z = moveTo["z"];
o->moveTo(glm::vec3(x, y, z));
}
if (element.contains("moveby"))
{
json moveBy = element["moveby"];
float x = moveBy["x"];
float y = moveBy["y"];
float z = moveBy["z"];
o->moveRel(glm::vec3(x, y, z));
}
if (element.contains("rotateto"))
{
json rotateTo = element["rotateto"];
float p = rotateTo["pitch"];
float y = rotateTo["yaw"];
float r = rotateTo["roll"];
o->rotateTo(glm::vec3(p, y, r));
}
if (element.contains("rotateby"))
{
json rotateBy = element["rotateby"];
float p = rotateBy["pitch"];
float y = rotateBy["yaw"];
float r = rotateBy["roll"];
o->rotateRel(glm::vec3(p, y, r));
}
}
}
std::unordered_map<std::string, Body*> objectMap;
Shader shader;
private:
std::unordered_map<std::string, Body> objectMap;
std::unordered_map<std::string, Body> templateMap;
bool goodJSON = true;
bool goodJSON = false;
Shader shader;
unsigned int resolution = 50;
json document, templates, objects, setup, cameras, lights, actions;
};
}