android version
This commit is contained in:
commit
1c9b1b1969
64 files changed
+25059
No files matched your search
@@ -0,0 +1,41 @@
|
||||
cmake_minimum_required(VERSION 3.6)
|
||||
|
||||
project(GAME)
|
||||
|
||||
if(POLICY CMP0079)
|
||||
cmake_policy(SET CMP0079 NEW)
|
||||
endif()
|
||||
|
||||
# Копируем pnglibconf.h.prebuilt
|
||||
execute_process(
|
||||
COMMAND ${CMAKE_COMMAND} -E copy
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/libpng/scripts/pnglibconf.h.prebuilt
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/libpng/pnglibconf.h
|
||||
)
|
||||
|
||||
# Сначала zlib
|
||||
add_subdirectory(zlib)
|
||||
|
||||
# ВАЖНО: Установите опции ДО add_subdirectory(libpng)
|
||||
# Libpng создает ДВЕ цели: png_shared (shared) и png_static (static)
|
||||
# Мы хотим статическую библиотеку для Android
|
||||
set(PNG_STATIC ON CACHE BOOL "Build static library" FORCE)
|
||||
set(PNG_SHARED OFF CACHE BOOL "Don't build shared library" FORCE)
|
||||
set(PNG_TESTS OFF CACHE BOOL "Disable tests" FORCE)
|
||||
set(PNG_TOOLS OFF CACHE BOOL "Disable tools" FORCE)
|
||||
set(PNG_EXECUTABLES OFF CACHE BOOL "Disable executables" FORCE)
|
||||
set(PNG_DEBUG OFF CACHE BOOL "Disable debug" FORCE)
|
||||
set(SKIP_INSTALL_ALL ON CACHE BOOL "Skip installation" FORCE)
|
||||
|
||||
# Для Android отключаем оптимизации
|
||||
set(PNG_HARDWARE_OPTIMIZATIONS OFF CACHE BOOL "Disable hardware optimizations" FORCE)
|
||||
set(PNG_ARM_NEON "off" CACHE STRING "Disable ARM NEON" FORCE)
|
||||
|
||||
# Добавляем libpng
|
||||
add_subdirectory(libpng)
|
||||
|
||||
# Затем SDL
|
||||
add_subdirectory(SDL)
|
||||
|
||||
# И ваш код
|
||||
add_subdirectory(src)
|
||||
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
#include "Renderer.h"
|
||||
#include "TextureManager.h"
|
||||
|
||||
namespace ZL
|
||||
{
|
||||
|
||||
struct MeshGroup
|
||||
{
|
||||
std::vector<std::shared_ptr<Texture>> textures;
|
||||
std::vector<VertexDataStruct> meshes;
|
||||
std::vector<VertexRenderStruct> renderMeshes;
|
||||
};
|
||||
|
||||
struct AnimatedModel
|
||||
{
|
||||
std::vector<MeshGroup> parts;
|
||||
|
||||
void RefreshRenderMeshes()
|
||||
{
|
||||
for (int i = 0; i < parts.size(); i++)
|
||||
{
|
||||
parts[i].renderMeshes.resize(parts[i].meshes.size());
|
||||
|
||||
for (int j = 0; j < parts[i].meshes.size(); j++)
|
||||
{
|
||||
parts[i].renderMeshes[j].AssignFrom(parts[i].meshes[j]);
|
||||
parts[i].renderMeshes[j].RefreshVBO();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,719 @@
|
||||
#include "BoneAnimatedModel.h"
|
||||
#include <regex>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
namespace ZL
|
||||
{
|
||||
|
||||
int getIndexByValue(const std::string& name, const std::vector<std::string>& words)
|
||||
{
|
||||
for (int i = 0; i < words.size(); i++)
|
||||
{
|
||||
if (words[i] == name)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
void BoneSystem::LoadFromFile(const std::string& fileName, const std::string& ZIPFileName)
|
||||
{
|
||||
std::ifstream filestream;
|
||||
std::istringstream zipStream;
|
||||
|
||||
if (!ZIPFileName.empty())
|
||||
{
|
||||
std::vector<char> fileData = readFileFromZIP(fileName, ZIPFileName);
|
||||
std::string fileContents(fileData.begin(), fileData.end());
|
||||
zipStream.str(fileContents);
|
||||
}
|
||||
else
|
||||
{
|
||||
filestream.open(fileName);
|
||||
}
|
||||
|
||||
std::istream& f = (!ZIPFileName.empty()) ? static_cast<std::istream&>(zipStream) : static_cast<std::istream&>(filestream);
|
||||
|
||||
//Skip first 5 lines
|
||||
std::string tempLine;
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
std::getline(f, tempLine);
|
||||
}
|
||||
std::getline(f, tempLine);
|
||||
|
||||
static const std::regex pattern_count(R"(\d+)");
|
||||
static const std::regex pattern_float(R"([-]?\d+\.\d+)");
|
||||
static const std::regex pattern_int(R"([-]?\d+)");
|
||||
static const std::regex pattern_boneChildren(R"(\'([^\']+)\')");
|
||||
static const std::regex pattern_bone_weight(R"(\'([^\']+)\'.*?([-]?\d+\.\d+))");
|
||||
|
||||
std::smatch match;
|
||||
|
||||
int numberBones;
|
||||
|
||||
if (std::regex_search(tempLine, match, pattern_count)) {
|
||||
std::string number_str = match.str();
|
||||
numberBones = std::stoi(number_str);
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("No number found in the input string.");
|
||||
}
|
||||
|
||||
std::vector<Bone> bones;
|
||||
std::vector<std::string> boneNames;
|
||||
std::vector<std::string> boneParentNames;
|
||||
std::unordered_map<std::string, std::vector<std::string>> boneChildren;
|
||||
|
||||
bones.resize(numberBones);
|
||||
boneNames.resize(numberBones);
|
||||
boneParentNames.resize(numberBones);
|
||||
|
||||
for (int i = 0; i < numberBones; i++)
|
||||
{
|
||||
std::getline(f, tempLine);
|
||||
std::string boneName = tempLine.substr(6);
|
||||
|
||||
boneNames[i] = boneName;
|
||||
|
||||
std::getline(f, tempLine);
|
||||
|
||||
std::vector<float> floatValues;
|
||||
|
||||
auto b = tempLine.cbegin();
|
||||
auto e = tempLine.cend();
|
||||
while (std::regex_search(b, e, match, pattern_float)) {
|
||||
floatValues.push_back(std::stof(match.str()));
|
||||
b = match.suffix().first;
|
||||
}
|
||||
|
||||
bones[i].boneStartWorld = Vector3f{ floatValues[0], floatValues[1], floatValues[2] };
|
||||
|
||||
|
||||
std::getline(f, tempLine); //skip tail
|
||||
|
||||
std::getline(f, tempLine); //len
|
||||
|
||||
if (std::regex_search(tempLine, match, pattern_float)) {
|
||||
std::string len_str = match.str();
|
||||
bones[i].boneLength = std::stof(len_str);
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("No number found in the input string.");
|
||||
}
|
||||
//---------- matrix begin
|
||||
|
||||
std::getline(f, tempLine);
|
||||
|
||||
b = tempLine.cbegin();
|
||||
e = tempLine.cend();
|
||||
floatValues.clear();
|
||||
while (std::regex_search(b, e, match, pattern_float)) {
|
||||
floatValues.push_back(std::stof(match.str()));
|
||||
b = match.suffix().first;
|
||||
}
|
||||
|
||||
bones[i].boneMatrixWorld.m[0] = floatValues[0];
|
||||
bones[i].boneMatrixWorld.m[0 + 1 * 3] = floatValues[1];
|
||||
bones[i].boneMatrixWorld.m[0 + 2 * 3] = floatValues[2];
|
||||
|
||||
|
||||
std::getline(f, tempLine);
|
||||
|
||||
b = tempLine.cbegin();
|
||||
e = tempLine.cend();
|
||||
floatValues.clear();
|
||||
while (std::regex_search(b, e, match, pattern_float)) {
|
||||
floatValues.push_back(std::stof(match.str()));
|
||||
b = match.suffix().first;
|
||||
}
|
||||
|
||||
bones[i].boneMatrixWorld.m[1] = floatValues[0];
|
||||
bones[i].boneMatrixWorld.m[1 + 1 * 3] = floatValues[1];
|
||||
bones[i].boneMatrixWorld.m[1 + 2 * 3] = floatValues[2];
|
||||
|
||||
|
||||
std::getline(f, tempLine);
|
||||
|
||||
b = tempLine.cbegin();
|
||||
e = tempLine.cend();
|
||||
floatValues.clear();
|
||||
while (std::regex_search(b, e, match, pattern_float)) {
|
||||
floatValues.push_back(std::stof(match.str()));
|
||||
b = match.suffix().first;
|
||||
}
|
||||
|
||||
bones[i].boneMatrixWorld.m[2] = floatValues[0];
|
||||
bones[i].boneMatrixWorld.m[2 + 1 * 3] = floatValues[1];
|
||||
bones[i].boneMatrixWorld.m[2 + 2 * 3] = floatValues[2];
|
||||
|
||||
//----------- matrix end
|
||||
std::getline(f, tempLine); //parent
|
||||
|
||||
if (tempLine == " Parent: None")
|
||||
{
|
||||
bones[i].parent = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string boneParent = tempLine.substr(10);
|
||||
boneParentNames[i] = boneParent;
|
||||
}
|
||||
|
||||
std::getline(f, tempLine); //children
|
||||
|
||||
b = tempLine.cbegin();
|
||||
e = tempLine.cend();
|
||||
while (std::regex_search(b, e, match, pattern_boneChildren)) {
|
||||
|
||||
boneChildren[boneName].push_back(match.str(1));
|
||||
b = match.suffix().first;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//Now process all the bones:
|
||||
for (int i = 0; i < numberBones; i++)
|
||||
{
|
||||
std::string boneName = boneNames[i];
|
||||
std::string boneParent = boneParentNames[i];
|
||||
if (boneParent == "")
|
||||
{
|
||||
bones[i].parent = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
bones[i].parent = getIndexByValue(boneParent, boneNames);
|
||||
}
|
||||
|
||||
for (int j = 0; j < boneChildren[boneName].size(); j++)
|
||||
{
|
||||
bones[i].children.push_back(getIndexByValue(boneChildren[boneName][j], boneNames));
|
||||
}
|
||||
|
||||
/*if (boneName == "Bone.020")
|
||||
{
|
||||
std::cout << i << std::endl;
|
||||
}*/
|
||||
}
|
||||
|
||||
startBones = bones;
|
||||
currentBones = bones;
|
||||
|
||||
///std::cout << "Hello!" << std::endl;
|
||||
|
||||
std::getline(f, tempLine); //vertice count
|
||||
int numberVertices;
|
||||
|
||||
if (std::regex_search(tempLine, match, pattern_count)) {
|
||||
std::string number_str = match.str();
|
||||
numberVertices = std::stoi(number_str);
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("No number found in the input string.");
|
||||
}
|
||||
|
||||
std::vector<Vector3f> vertices;
|
||||
|
||||
vertices.resize(numberVertices);
|
||||
for (int i = 0; i < numberVertices; i++)
|
||||
{
|
||||
std::getline(f, tempLine);
|
||||
|
||||
std::vector<float> floatValues;
|
||||
|
||||
auto b = tempLine.cbegin();
|
||||
auto e = tempLine.cend();
|
||||
while (std::regex_search(b, e, match, pattern_float)) {
|
||||
floatValues.push_back(std::stof(match.str()));
|
||||
b = match.suffix().first;
|
||||
}
|
||||
|
||||
vertices[i] = Vector3f{floatValues[0], floatValues[1], floatValues[2]};
|
||||
}
|
||||
|
||||
|
||||
//==== process uv and normals begin
|
||||
|
||||
std::cout << "Hello x1" << std::endl;
|
||||
|
||||
std::getline(f, tempLine); //===UV Coordinates:
|
||||
|
||||
std::getline(f, tempLine); //triangle count
|
||||
int numberTriangles;
|
||||
|
||||
if (std::regex_search(tempLine, match, pattern_count)) {
|
||||
std::string number_str = match.str();
|
||||
numberTriangles = std::stoi(number_str);
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("No number found in the input string.");
|
||||
}
|
||||
|
||||
|
||||
// Now process UVs
|
||||
std::vector<std::array<Vector2f, 3>> uvCoords;
|
||||
|
||||
uvCoords.resize(numberTriangles);
|
||||
|
||||
for (int i = 0; i < numberTriangles; i++)
|
||||
{
|
||||
std::getline(f, tempLine); //Face 0
|
||||
|
||||
int uvCount;
|
||||
std::getline(f, tempLine);
|
||||
if (std::regex_search(tempLine, match, pattern_count)) {
|
||||
std::string number_str = match.str();
|
||||
uvCount = std::stoi(number_str);
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("No number found in the input string.");
|
||||
}
|
||||
|
||||
if (uvCount != 3)
|
||||
{
|
||||
throw std::runtime_error("more than 3 uvs");
|
||||
}
|
||||
|
||||
std::vector<float> floatValues;
|
||||
|
||||
for (int j = 0; j < 3; j++)
|
||||
{
|
||||
std::getline(f, tempLine); //UV <Vector (-0.3661, -1.1665)>
|
||||
|
||||
auto b = tempLine.cbegin();
|
||||
auto e = tempLine.cend();
|
||||
floatValues.clear();
|
||||
while (std::regex_search(b, e, match, pattern_float)) {
|
||||
floatValues.push_back(std::stof(match.str()));
|
||||
b = match.suffix().first;
|
||||
}
|
||||
|
||||
if (floatValues.size() != 2)
|
||||
{
|
||||
throw std::runtime_error("more than 2 uvs---");
|
||||
}
|
||||
|
||||
uvCoords[i][j] = Vector2f{ floatValues[0],floatValues[1] };
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
std::cout << "Hello eee" << std::endl;
|
||||
|
||||
std::getline(f, tempLine); //===Normals:
|
||||
|
||||
|
||||
std::vector<Vector3f> normals;
|
||||
|
||||
normals.resize(numberVertices);
|
||||
for (int i = 0; i < numberVertices; i++)
|
||||
{
|
||||
std::getline(f, tempLine);
|
||||
|
||||
std::vector<float> floatValues;
|
||||
|
||||
auto b = tempLine.cbegin();
|
||||
auto e = tempLine.cend();
|
||||
while (std::regex_search(b, e, match, pattern_float)) {
|
||||
floatValues.push_back(std::stof(match.str()));
|
||||
b = match.suffix().first;
|
||||
}
|
||||
|
||||
normals[i] = Vector3f{ floatValues[0], floatValues[1], floatValues[2] };
|
||||
}
|
||||
|
||||
//==== process uv and normals end
|
||||
|
||||
std::getline(f, tempLine); //triangle count.
|
||||
//numberTriangles; //Need to check if new value is the same as was read before
|
||||
|
||||
if (std::regex_search(tempLine, match, pattern_count)) {
|
||||
std::string number_str = match.str();
|
||||
numberTriangles = std::stoi(number_str);
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("No number found in the input string.");
|
||||
}
|
||||
|
||||
std::vector<std::array<int, 3>> triangles;
|
||||
|
||||
triangles.resize(numberTriangles);
|
||||
for (int i = 0; i < numberTriangles; i++)
|
||||
{
|
||||
std::getline(f, tempLine);
|
||||
|
||||
std::vector<int> intValues;
|
||||
|
||||
auto b = tempLine.cbegin();
|
||||
auto e = tempLine.cend();
|
||||
while (std::regex_search(b, e, match, pattern_int)) {
|
||||
intValues.push_back(std::stoi(match.str()));
|
||||
b = match.suffix().first;
|
||||
}
|
||||
|
||||
triangles[i] = { intValues[0], intValues[1], intValues[2] };
|
||||
}
|
||||
|
||||
std::getline(f, tempLine);//=== Vertex Weights ===
|
||||
std::vector<std::array<BoneWeight, MAX_BONE_COUNT>> localVerticesBoneWeight;
|
||||
localVerticesBoneWeight.resize(numberVertices);
|
||||
|
||||
for (int i = 0; i < numberVertices; i++)
|
||||
{
|
||||
std::getline(f, tempLine); //skip Vertex 0:
|
||||
std::getline(f, tempLine); //vertex group count
|
||||
int boneCount;
|
||||
|
||||
if (std::regex_search(tempLine, match, pattern_count)) {
|
||||
std::string number_str = match.str();
|
||||
boneCount = std::stoi(number_str);
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("No number found in the input string.");
|
||||
}
|
||||
|
||||
if (boneCount > MAX_BONE_COUNT)
|
||||
{
|
||||
throw std::runtime_error("more than 5 bones");
|
||||
}
|
||||
|
||||
float sumWeights = 0;
|
||||
|
||||
for (int j = 0; j < boneCount; j++)
|
||||
{
|
||||
std::getline(f, tempLine); //Group: 'Bone', Weight: 0.9929084181785583
|
||||
if (std::regex_search(tempLine, match, pattern_bone_weight)) {
|
||||
// ��������� ����� (��� �������)
|
||||
std::string word = match.str(1);
|
||||
double weight = std::stod(match.str(2));
|
||||
|
||||
int boneNumber = getIndexByValue(word, boneNames);
|
||||
localVerticesBoneWeight[i][j].boneIndex = boneNumber;
|
||||
localVerticesBoneWeight[i][j].weight = weight;
|
||||
sumWeights += weight;
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("No match found in the input string.");
|
||||
}
|
||||
}
|
||||
|
||||
//Normalize weights:
|
||||
for (int j = 0; j < boneCount; j++)
|
||||
{
|
||||
localVerticesBoneWeight[i][j].weight = localVerticesBoneWeight[i][j].weight / sumWeights;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
std::getline(f, tempLine);//=== Animation Keyframes ===
|
||||
std::getline(f, tempLine);//=== Bone Transforms per Keyframe ===
|
||||
|
||||
|
||||
|
||||
std::getline(f, tempLine);
|
||||
int numberKeyFrames;
|
||||
|
||||
if (std::regex_search(tempLine, match, pattern_count)) {
|
||||
std::string number_str = match.str();
|
||||
numberKeyFrames = std::stoi(number_str);
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("No number found in the input string.");
|
||||
}
|
||||
|
||||
animations.resize(1);
|
||||
|
||||
animations[0].keyFrames.resize(numberKeyFrames);
|
||||
|
||||
for (int i = 0; i < numberKeyFrames; i++)
|
||||
{
|
||||
std::getline(f, tempLine);
|
||||
int numberFrame;
|
||||
|
||||
if (std::regex_search(tempLine, match, pattern_count)) {
|
||||
std::string number_str = match.str();
|
||||
numberFrame = std::stoi(number_str);
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("No number found in the input string.");
|
||||
}
|
||||
|
||||
animations[0].keyFrames[i].frame = numberFrame;
|
||||
|
||||
animations[0].keyFrames[i].bones.resize(numberBones);
|
||||
|
||||
for (int j = 0; j < numberBones; j++)
|
||||
{
|
||||
std::getline(f, tempLine);
|
||||
std::string boneName = tempLine.substr(8);
|
||||
int boneNumber = getIndexByValue(boneName, boneNames);
|
||||
animations[0].keyFrames[i].bones[boneNumber] = startBones[boneNumber];
|
||||
|
||||
std::getline(f, tempLine); // Location: <Vector (0.0000, 0.0000, -0.0091)>
|
||||
|
||||
std::vector<float> floatValues;
|
||||
|
||||
auto b = tempLine.cbegin();
|
||||
auto e = tempLine.cend();
|
||||
while (std::regex_search(b, e, match, pattern_float)) {
|
||||
floatValues.push_back(std::stof(match.str()));
|
||||
b = match.suffix().first;
|
||||
}
|
||||
|
||||
animations[0].keyFrames[i].bones[boneNumber].boneStartWorld = Vector3f{ floatValues[0], floatValues[1], floatValues[2] };
|
||||
|
||||
std::getline(f, tempLine); // Rotation
|
||||
std::getline(f, tempLine); // Matrix
|
||||
|
||||
//=============== Matrix begin ==================
|
||||
std::getline(f, tempLine);
|
||||
|
||||
|
||||
b = tempLine.cbegin();
|
||||
e = tempLine.cend();
|
||||
floatValues.clear();
|
||||
while (std::regex_search(b, e, match, pattern_float)) {
|
||||
floatValues.push_back(std::stof(match.str()));
|
||||
b = match.suffix().first;
|
||||
}
|
||||
|
||||
animations[0].keyFrames[i].bones[boneNumber].boneMatrixWorld.m[0] = floatValues[0];
|
||||
animations[0].keyFrames[i].bones[boneNumber].boneMatrixWorld.m[0 + 1 * 4] = floatValues[1];
|
||||
animations[0].keyFrames[i].bones[boneNumber].boneMatrixWorld.m[0 + 2 * 4] = floatValues[2];
|
||||
animations[0].keyFrames[i].bones[boneNumber].boneMatrixWorld.m[0 + 3 * 4] = floatValues[3];
|
||||
|
||||
|
||||
std::getline(f, tempLine);
|
||||
b = tempLine.cbegin();
|
||||
e = tempLine.cend();
|
||||
floatValues.clear();
|
||||
while (std::regex_search(b, e, match, pattern_float)) {
|
||||
floatValues.push_back(std::stof(match.str()));
|
||||
b = match.suffix().first;
|
||||
}
|
||||
|
||||
animations[0].keyFrames[i].bones[boneNumber].boneMatrixWorld.m[1] = floatValues[0];
|
||||
animations[0].keyFrames[i].bones[boneNumber].boneMatrixWorld.m[1 + 1 * 4] = floatValues[1];
|
||||
animations[0].keyFrames[i].bones[boneNumber].boneMatrixWorld.m[1 + 2 * 4] = floatValues[2];
|
||||
animations[0].keyFrames[i].bones[boneNumber].boneMatrixWorld.m[1 + 3 * 4] = floatValues[3];
|
||||
|
||||
std::getline(f, tempLine);
|
||||
b = tempLine.cbegin();
|
||||
e = tempLine.cend();
|
||||
floatValues.clear();
|
||||
while (std::regex_search(b, e, match, pattern_float)) {
|
||||
floatValues.push_back(std::stof(match.str()));
|
||||
b = match.suffix().first;
|
||||
}
|
||||
|
||||
animations[0].keyFrames[i].bones[boneNumber].boneMatrixWorld.m[2] = floatValues[0];
|
||||
animations[0].keyFrames[i].bones[boneNumber].boneMatrixWorld.m[2 + 1 * 4] = floatValues[1];
|
||||
animations[0].keyFrames[i].bones[boneNumber].boneMatrixWorld.m[2 + 2 * 4] = floatValues[2];
|
||||
animations[0].keyFrames[i].bones[boneNumber].boneMatrixWorld.m[2 + 3 * 4] = floatValues[3];
|
||||
|
||||
|
||||
std::getline(f, tempLine);
|
||||
b = tempLine.cbegin();
|
||||
e = tempLine.cend();
|
||||
floatValues.clear();
|
||||
while (std::regex_search(b, e, match, pattern_float)) {
|
||||
floatValues.push_back(std::stof(match.str()));
|
||||
b = match.suffix().first;
|
||||
}
|
||||
|
||||
animations[0].keyFrames[i].bones[boneNumber].boneMatrixWorld.m[3] = floatValues[0];
|
||||
animations[0].keyFrames[i].bones[boneNumber].boneMatrixWorld.m[3 + 1 * 4] = floatValues[1];
|
||||
animations[0].keyFrames[i].bones[boneNumber].boneMatrixWorld.m[3 + 2 * 4] = floatValues[2];
|
||||
animations[0].keyFrames[i].bones[boneNumber].boneMatrixWorld.m[3 + 3 * 4] = floatValues[3];
|
||||
|
||||
//std::getline(f, tempLine);// ignore last matrix line
|
||||
|
||||
//=============== Matrix end ==================
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Now let's process bone weights and vertices
|
||||
|
||||
for (int i = 0; i < numberTriangles; i++)
|
||||
{
|
||||
|
||||
mesh.PositionData.push_back(vertices[triangles[i][0]]);
|
||||
mesh.PositionData.push_back(vertices[triangles[i][1]]);
|
||||
mesh.PositionData.push_back(vertices[triangles[i][2]]);
|
||||
|
||||
verticesBoneWeight.push_back(localVerticesBoneWeight[triangles[i][0]]);
|
||||
verticesBoneWeight.push_back(localVerticesBoneWeight[triangles[i][1]]);
|
||||
verticesBoneWeight.push_back(localVerticesBoneWeight[triangles[i][2]]);
|
||||
|
||||
mesh.TexCoordData.push_back(uvCoords[i][0]);
|
||||
mesh.TexCoordData.push_back(uvCoords[i][1]);
|
||||
mesh.TexCoordData.push_back(uvCoords[i][2]);
|
||||
}
|
||||
|
||||
startMesh = mesh;
|
||||
}
|
||||
|
||||
void BoneSystem::Interpolate(int frame)
|
||||
{
|
||||
int startingFrame = -1;
|
||||
for (int i = 0; i < animations[0].keyFrames.size() - 1; i++)
|
||||
{
|
||||
int oldFrame = animations[0].keyFrames[i].frame;
|
||||
int nextFrame = animations[0].keyFrames[i + 1].frame;
|
||||
if (frame >= oldFrame && frame < nextFrame)
|
||||
{
|
||||
startingFrame = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (startingFrame == -1)
|
||||
{
|
||||
throw std::runtime_error("Exception here");
|
||||
}
|
||||
|
||||
int modifiedFrameNumber = frame - animations[0].keyFrames[startingFrame].frame;
|
||||
|
||||
int diffFrames = animations[0].keyFrames[startingFrame + 1].frame - animations[0].keyFrames[startingFrame].frame;
|
||||
|
||||
float t = (modifiedFrameNumber + 0.f) / diffFrames;
|
||||
|
||||
std::vector<Bone>& oneFrameBones = animations[0].keyFrames[startingFrame].bones;
|
||||
std::vector<Bone>& nextFrameBones = animations[0].keyFrames[startingFrame+1].bones;
|
||||
|
||||
std::vector<Matrix4f> skinningMatrixForEachBone;
|
||||
//std::vector<Matrix3f> skinningMatrixForEachBone;
|
||||
skinningMatrixForEachBone.resize(currentBones.size());
|
||||
|
||||
|
||||
for (int i = 0; i < currentBones.size(); i++)
|
||||
{
|
||||
currentBones[i].boneStartWorld.v[0] = oneFrameBones[i].boneStartWorld.v[0] + t * (nextFrameBones[i].boneStartWorld.v[0] - oneFrameBones[i].boneStartWorld.v[0]);
|
||||
currentBones[i].boneStartWorld.v[1] = oneFrameBones[i].boneStartWorld.v[1] + t * (nextFrameBones[i].boneStartWorld.v[1] - oneFrameBones[i].boneStartWorld.v[1]);
|
||||
currentBones[i].boneStartWorld.v[2] = oneFrameBones[i].boneStartWorld.v[2] + t * (nextFrameBones[i].boneStartWorld.v[2] - oneFrameBones[i].boneStartWorld.v[2]);
|
||||
|
||||
Matrix3f oneFrameBonesMatrix;
|
||||
|
||||
oneFrameBonesMatrix.m[0] = oneFrameBones[i].boneMatrixWorld.m[0];
|
||||
oneFrameBonesMatrix.m[1] = oneFrameBones[i].boneMatrixWorld.m[1];
|
||||
oneFrameBonesMatrix.m[2] = oneFrameBones[i].boneMatrixWorld.m[2];
|
||||
|
||||
oneFrameBonesMatrix.m[3] = oneFrameBones[i].boneMatrixWorld.m[0 + 1*4];
|
||||
oneFrameBonesMatrix.m[4] = oneFrameBones[i].boneMatrixWorld.m[1 + 1*4];
|
||||
oneFrameBonesMatrix.m[5] = oneFrameBones[i].boneMatrixWorld.m[2 + 1*4];
|
||||
|
||||
oneFrameBonesMatrix.m[6] = oneFrameBones[i].boneMatrixWorld.m[0 + 2*4];
|
||||
oneFrameBonesMatrix.m[7] = oneFrameBones[i].boneMatrixWorld.m[1 + 2*4];
|
||||
oneFrameBonesMatrix.m[8] = oneFrameBones[i].boneMatrixWorld.m[2 + 2*4];
|
||||
|
||||
Matrix3f nextFrameBonesMatrix;
|
||||
|
||||
nextFrameBonesMatrix.m[0] = nextFrameBones[i].boneMatrixWorld.m[0];
|
||||
nextFrameBonesMatrix.m[1] = nextFrameBones[i].boneMatrixWorld.m[1];
|
||||
nextFrameBonesMatrix.m[2] = nextFrameBones[i].boneMatrixWorld.m[2];
|
||||
|
||||
nextFrameBonesMatrix.m[3] = nextFrameBones[i].boneMatrixWorld.m[0 + 1 * 4];
|
||||
nextFrameBonesMatrix.m[4] = nextFrameBones[i].boneMatrixWorld.m[1 + 1 * 4];
|
||||
nextFrameBonesMatrix.m[5] = nextFrameBones[i].boneMatrixWorld.m[2 + 1 * 4];
|
||||
|
||||
nextFrameBonesMatrix.m[6] = nextFrameBones[i].boneMatrixWorld.m[0 + 2 * 4];
|
||||
nextFrameBonesMatrix.m[7] = nextFrameBones[i].boneMatrixWorld.m[1 + 2 * 4];
|
||||
nextFrameBonesMatrix.m[8] = nextFrameBones[i].boneMatrixWorld.m[2 + 2 * 4];
|
||||
|
||||
Vector4f q1 = MatrixToQuat(oneFrameBonesMatrix);
|
||||
Vector4f q2 = MatrixToQuat(nextFrameBonesMatrix);
|
||||
Vector4f q1_norm = q1.normalized();
|
||||
Vector4f q2_norm = q2.normalized();
|
||||
|
||||
Vector4f result = slerp(q1_norm, q2_norm, t);
|
||||
|
||||
Matrix3f boneMatrixWorld3 = QuatToMatrix(result);
|
||||
|
||||
currentBones[i].boneMatrixWorld = MakeMatrix4x4(boneMatrixWorld3, currentBones[i].boneStartWorld);
|
||||
|
||||
Matrix4f currentBoneMatrixWorld4 = currentBones[i].boneMatrixWorld;
|
||||
Matrix4f startBoneMatrixWorld4 = animations[0].keyFrames[0].bones[i].boneMatrixWorld;
|
||||
|
||||
Matrix4f inverstedStartBoneMatrixWorld4 = InverseMatrix(startBoneMatrixWorld4);
|
||||
|
||||
skinningMatrixForEachBone[i] = MultMatrixMatrix(currentBoneMatrixWorld4, inverstedStartBoneMatrixWorld4);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
for (int i = 0; i < currentBones.size(); i++)
|
||||
{
|
||||
currentBones[i].boneStartWorld = oneFrameBones[i].boneStartWorld;
|
||||
currentBones[i].boneMatrixWorld = oneFrameBones[i].boneMatrixWorld;
|
||||
//Matrix4f currentBoneMatrixWorld4 = MakeMatrix4x4(currentBones[i].boneMatrixWorld, currentBones[i].boneStartWorld);
|
||||
//Matrix4f startBoneMatrixWorld4 = MakeMatrix4x4(animations[0].keyFrames[0].bones[i].boneMatrixWorld, animations[0].keyFrames[0].bones[i].boneStartWorld);
|
||||
Matrix4f currentBoneMatrixWorld4 = currentBones[i].boneMatrixWorld;
|
||||
Matrix4f startBoneMatrixWorld4 = animations[0].keyFrames[0].bones[i].boneMatrixWorld;
|
||||
Matrix4f inverstedStartBoneMatrixWorld4 = InverseMatrix(startBoneMatrixWorld4);
|
||||
skinningMatrixForEachBone[i] = MultMatrixMatrix(currentBoneMatrixWorld4, inverstedStartBoneMatrixWorld4);
|
||||
|
||||
if (i == 10)
|
||||
{
|
||||
std::cout << i << std::endl;
|
||||
}
|
||||
}*/
|
||||
|
||||
for (int i = 0; i < mesh.PositionData.size(); i++)
|
||||
{
|
||||
Vector4f originalPos = {
|
||||
startMesh.PositionData[i].v[0],
|
||||
startMesh.PositionData[i].v[1],
|
||||
startMesh.PositionData[i].v[2], 1.0};
|
||||
|
||||
Vector4f finalPos = Vector4f{0.f, 0.f, 0.f, 0.f};
|
||||
|
||||
bool vMoved = false;
|
||||
//Vector3f finalPos = Vector3f{ 0.f, 0.f, 0.f };
|
||||
|
||||
for (int j = 0; j < MAX_BONE_COUNT; j++)
|
||||
{
|
||||
if (verticesBoneWeight[i][j].weight != 0)
|
||||
{
|
||||
vMoved = true;
|
||||
//finalPos = finalPos + MultVectorMatrix(originalPos, skinningMatrixForEachBone[verticesBoneWeight[i][j].boneIndex]) * verticesBoneWeight[i][j].weight;
|
||||
finalPos = finalPos + MultMatrixVector(skinningMatrixForEachBone[verticesBoneWeight[i][j].boneIndex], originalPos) * verticesBoneWeight[i][j].weight;
|
||||
}
|
||||
}
|
||||
|
||||
if (abs(finalPos.v[0] - originalPos.v[0]) > 1 || abs(finalPos.v[1] - originalPos.v[1]) > 1 || abs(finalPos.v[2] - originalPos.v[2]) > 1)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
if (!vMoved)
|
||||
{
|
||||
finalPos = originalPos;
|
||||
}
|
||||
|
||||
mesh.PositionData[i].v[0] = finalPos.v[0];
|
||||
mesh.PositionData[i].v[1] = finalPos.v[1];
|
||||
mesh.PositionData[i].v[2] = finalPos.v[2];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
#pragma once
|
||||
#include "Math.h"
|
||||
#include "Renderer.h"
|
||||
#include <unordered_map>
|
||||
|
||||
|
||||
namespace ZL
|
||||
{
|
||||
constexpr int MAX_BONE_COUNT = 6;
|
||||
struct Bone
|
||||
{
|
||||
Vector3f boneStartWorld;
|
||||
float boneLength;
|
||||
Matrix4f boneMatrixWorld;
|
||||
// boneVector = boneLength * (0, 1, 0) � ���� ��������
|
||||
// Then multiply by boneMatrixWorld � �� �������� �������� �����
|
||||
|
||||
int parent;
|
||||
std::vector<int> children;
|
||||
};
|
||||
|
||||
struct BoneWeight
|
||||
{
|
||||
int boneIndex = -1;
|
||||
float weight = 0;
|
||||
};
|
||||
|
||||
struct AnimationKeyFrame
|
||||
{
|
||||
int frame;
|
||||
std::vector<Bone> bones;
|
||||
};
|
||||
|
||||
struct Animation
|
||||
{
|
||||
std::vector<AnimationKeyFrame> keyFrames;
|
||||
};
|
||||
|
||||
struct BoneSystem
|
||||
{
|
||||
VertexDataStruct mesh;
|
||||
VertexDataStruct startMesh;
|
||||
std::vector<std::array<BoneWeight, MAX_BONE_COUNT>> verticesBoneWeight;
|
||||
|
||||
Matrix4f armatureMatrix;
|
||||
|
||||
std::vector<Bone> startBones;
|
||||
std::vector<Bone> currentBones;
|
||||
|
||||
std::vector<Animation> animations;
|
||||
|
||||
void LoadFromFile(const std::string& fileName, const std::string& ZIPFileName = "");
|
||||
|
||||
void Interpolate(int frame);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
@@ -0,0 +1,62 @@
|
||||
cmake_minimum_required(VERSION 3.6)
|
||||
|
||||
project(MY_APP CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
add_library(main SHARED
|
||||
SDL_android_main.c
|
||||
main.cpp
|
||||
|
||||
Utils.h
|
||||
Utils.cpp
|
||||
TextureManager.h
|
||||
TextureManager.cpp
|
||||
TextModel.h
|
||||
TextModel.cpp
|
||||
ShaderManager.h
|
||||
ShaderManager.cpp
|
||||
Renderer.h
|
||||
Renderer.cpp
|
||||
Math.h
|
||||
Math.cpp
|
||||
Environment.h
|
||||
Environment.cpp
|
||||
BoneAnimatedModel.h
|
||||
BoneAnimatedModel.cpp
|
||||
AnimatedModel.h
|
||||
Game.h
|
||||
Game.cpp
|
||||
)
|
||||
|
||||
# Подключаем заголовки
|
||||
target_include_directories(main PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../SDL/include
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../zlib
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../libpng
|
||||
)
|
||||
|
||||
# ВАЖНО: Линкуемся с png_static (статика) или png_shared (динамика)
|
||||
# Так как мы установили PNG_STATIC=ON и PNG_SHARED=OFF,
|
||||
# должна создаться цель png_static
|
||||
target_link_libraries(main
|
||||
png_static # ← ЭТО ПРАВИЛЬНОЕ ИМЯ ЦЕЛИ!
|
||||
z
|
||||
SDL2
|
||||
)
|
||||
|
||||
if(ANDROID)
|
||||
# OpenGL ES 2.0 или 3.0 для Android
|
||||
find_library(OPENGLES2_LIB GLESv2)
|
||||
find_library(OPENGLES1_LIB GLESv1_CM)
|
||||
|
||||
target_link_libraries(main
|
||||
${OPENGLES2_LIB} # OpenGL ES 2.0/3.0
|
||||
${OPENGLES1_LIB} # OpenGL ES 1.x (если нужно)
|
||||
log
|
||||
android
|
||||
OpenSLES
|
||||
dl
|
||||
)
|
||||
endif()
|
||||
@@ -0,0 +1,35 @@
|
||||
#include "Environment.h"
|
||||
|
||||
#include "Utils.h"
|
||||
//#include <GL/gl.h>
|
||||
#include <GLES2/gl2.h>
|
||||
#include <GLES2/gl2ext.h>
|
||||
namespace ZL {
|
||||
|
||||
int Environment::windowHeaderHeight = 0;
|
||||
int Environment::width = 0;
|
||||
int Environment::height = 0;
|
||||
float Environment::zoom = 20.f;
|
||||
|
||||
bool Environment::leftPressed = false;
|
||||
bool Environment::rightPressed = false;
|
||||
bool Environment::upPressed = false;
|
||||
bool Environment::downPressed = false;
|
||||
|
||||
Vector3f Environment::cameraShift = {0, 0, 0};
|
||||
Vector3f Environment::characterPos = {0, 0, 0};
|
||||
|
||||
float Environment::cameraPhi = 0.f;
|
||||
float Environment::cameraAlpha = 0.3*M_PI / 2.0;
|
||||
|
||||
bool Environment::settings_inverseVertical = false;
|
||||
|
||||
SDL_Window* Environment::window = nullptr;
|
||||
|
||||
bool Environment::showMouse = false;
|
||||
|
||||
bool Environment::exitGameLoop = false;
|
||||
|
||||
|
||||
|
||||
} // namespace ZL
|
||||
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
#include "Math.h"
|
||||
#ifdef __linux__
|
||||
#include <SDL2/SDL.h>
|
||||
#endif
|
||||
//#include "OpenGlExtensions.h"
|
||||
#include <GLES2/gl2.h>
|
||||
#include <GLES2/gl2ext.h>
|
||||
namespace ZL {
|
||||
|
||||
class Environment {
|
||||
public:
|
||||
static int windowHeaderHeight;
|
||||
static int width;
|
||||
static int height;
|
||||
static float zoom;
|
||||
|
||||
static bool leftPressed;
|
||||
static bool rightPressed;
|
||||
static bool upPressed;
|
||||
static bool downPressed;
|
||||
|
||||
static Vector3f cameraShift;
|
||||
static Vector3f characterPos;
|
||||
static float cameraPhi;
|
||||
static float cameraAlpha;
|
||||
|
||||
|
||||
|
||||
static bool settings_inverseVertical;
|
||||
|
||||
static SDL_Window* window;
|
||||
|
||||
static bool showMouse;
|
||||
static bool exitGameLoop;
|
||||
|
||||
|
||||
};
|
||||
|
||||
} // namespace ZL
|
||||
@@ -0,0 +1,167 @@
|
||||
#include "Game.h"
|
||||
#include "AnimatedModel.h"
|
||||
#include "BoneAnimatedModel.h"
|
||||
#include "Utils.h"
|
||||
//#include "OpenGlExtensions.h"
|
||||
#include <iostream>
|
||||
#include "TextureManager.h"
|
||||
#include "TextModel.h"
|
||||
#include <GLES2/gl2.h>
|
||||
#include <GLES2/gl2ext.h>
|
||||
|
||||
namespace ZL
|
||||
{
|
||||
const char* CONST_ZIP_FILE = "";
|
||||
|
||||
Game::Game()
|
||||
: window(nullptr)
|
||||
, glContext(nullptr)
|
||||
, newTickCount(0)
|
||||
, lastTickCount(0)
|
||||
{
|
||||
}
|
||||
|
||||
Game::~Game() {
|
||||
if (glContext) {
|
||||
SDL_GL_DeleteContext(glContext);
|
||||
}
|
||||
if (window) {
|
||||
SDL_DestroyWindow(window);
|
||||
}
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
void Game::setup() {
|
||||
glContext = SDL_GL_CreateContext(ZL::Environment::window);
|
||||
|
||||
// ZL::BindOpenGlFunctions();
|
||||
// ZL::CheckGlError();
|
||||
|
||||
// Initialize renderer
|
||||
|
||||
/*#ifdef EMSCRIPTEN
|
||||
renderer.shaderManager.AddShaderFromFiles("default", "./shaders/default.vertex", "./shaders/default_web.fragment", CONST_ZIP_FILE);
|
||||
renderer.shaderManager.AddShaderFromFiles("defaultColor", "./shaders/defaultColor.vertex", "./shaders/defaultColor_web.fragment", CONST_ZIP_FILE);
|
||||
#else
|
||||
renderer.shaderManager.AddShaderFromFiles("default", "./shaders/default.vertex", "./shaders/default_desktop.fragment", CONST_ZIP_FILE);
|
||||
renderer.shaderManager.AddShaderFromFiles("defaultColor", "./shaders/defaultColor.vertex", "./shaders/defaultColor_desktop.fragment", CONST_ZIP_FILE);
|
||||
#endif
|
||||
|
||||
//Load texture
|
||||
spaceshipTexture = std::make_unique<Texture>(CreateTextureDataFromPng("./resources/sship001x.png"));
|
||||
spaceshipBase = LoadFromTextFile02("./resources/spaceship004.txt");
|
||||
spaceshipBase.RotateByMatrix(QuatToMatrix(QuatFromRotateAroundY(M_PI / 2.0)));
|
||||
spaceship.AssignFrom(spaceshipBase);
|
||||
spaceship.RefreshVBO();
|
||||
*/
|
||||
|
||||
renderer.InitOpenGL();
|
||||
|
||||
}
|
||||
|
||||
void Game::drawScene() {
|
||||
static const std::string defaultShaderName = "default";
|
||||
static const std::string vPositionName = "vPosition";
|
||||
static const std::string vTexCoordName = "vTexCoord";
|
||||
static const std::string textureUniformName = "Texture";
|
||||
|
||||
glClearColor(0.0f, 0.5f, 1.0f, 1.0f);
|
||||
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glViewport(0, 0, Environment::width, Environment::height);
|
||||
|
||||
/*CheckGlError();
|
||||
|
||||
renderer.shaderManager.PushShader(defaultShaderName);
|
||||
renderer.RenderUniform1i(textureUniformName, 0);
|
||||
renderer.EnableVertexAttribArray(vPositionName);
|
||||
renderer.EnableVertexAttribArray(vTexCoordName);
|
||||
|
||||
renderer.PushPerspectiveProjectionMatrix(1.0 / 1.5,
|
||||
static_cast<float>(Environment::width) / static_cast<float>(Environment::height),
|
||||
1, 1000);
|
||||
renderer.PushMatrix();
|
||||
|
||||
renderer.LoadIdentity();
|
||||
renderer.TranslateMatrix({ 0,0, -1.0f*Environment::zoom });
|
||||
renderer.RotateMatrix(QuatFromRotateAroundX(M_PI/6.0));
|
||||
//renderer.RotateMatrix(QuatFromRotateAroundX(Environment::cameraAlpha));
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, spaceshipTexture->getTexID());
|
||||
renderer.DrawVertexRenderStruct(spaceship);
|
||||
|
||||
renderer.PopMatrix();
|
||||
renderer.PopProjectionMatrix();
|
||||
renderer.DisableVertexAttribArray(vPositionName);
|
||||
renderer.DisableVertexAttribArray(vTexCoordName);
|
||||
|
||||
renderer.shaderManager.PopShader();*/
|
||||
|
||||
//CheckGlError();
|
||||
}
|
||||
|
||||
void Game::processTickCount() {
|
||||
|
||||
if (lastTickCount == 0) {
|
||||
lastTickCount = SDL_GetTicks64();
|
||||
return;
|
||||
}
|
||||
|
||||
newTickCount = SDL_GetTicks64();
|
||||
if (newTickCount - lastTickCount > CONST_TIMER_INTERVAL) {
|
||||
size_t delta = (newTickCount - lastTickCount > CONST_MAX_TIME_INTERVAL) ?
|
||||
CONST_MAX_TIME_INTERVAL : newTickCount - lastTickCount;
|
||||
|
||||
//gameObjects.updateScene(delta);
|
||||
|
||||
Environment::cameraAlpha = Environment::cameraAlpha + delta * M_PI / 10000.f;
|
||||
|
||||
lastTickCount = newTickCount;
|
||||
}
|
||||
}
|
||||
|
||||
void Game::render() {
|
||||
SDL_GL_MakeCurrent(ZL::Environment::window, glContext);
|
||||
//ZL::CheckGlError();
|
||||
|
||||
glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
drawScene();
|
||||
processTickCount();
|
||||
|
||||
SDL_GL_SwapWindow(ZL::Environment::window);
|
||||
}
|
||||
|
||||
void Game::update() {
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_QUIT) {
|
||||
Environment::exitGameLoop = true;
|
||||
|
||||
}
|
||||
else if (event.type == SDL_MOUSEWHEEL) {
|
||||
static const float zoomstep = 2.0f;
|
||||
if (event.wheel.y > 0) {
|
||||
Environment::zoom -= zoomstep;
|
||||
}
|
||||
else if (event.wheel.y < 0) {
|
||||
Environment::zoom += zoomstep;
|
||||
}
|
||||
if (Environment::zoom < zoomstep) {
|
||||
Environment::zoom = zoomstep;
|
||||
}
|
||||
/*if (Environment::zoom > 4) {
|
||||
Environment::zoom = 4;
|
||||
}*/
|
||||
|
||||
//this->modelMeshRender.data.Scale(0.5);
|
||||
//this->modelMeshRender.RefreshVBO();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
render();
|
||||
}
|
||||
|
||||
} // namespace ZL
|
||||
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
//#include "OpenGlExtensions.h"
|
||||
#include "Renderer.h"
|
||||
#include "Environment.h"
|
||||
#include "TextureManager.h"
|
||||
#include <GLES2/gl2.h>
|
||||
#include <GLES2/gl2ext.h>
|
||||
|
||||
namespace ZL {
|
||||
|
||||
class Game {
|
||||
public:
|
||||
Game();
|
||||
~Game();
|
||||
|
||||
void setup();
|
||||
void update();
|
||||
void render();
|
||||
|
||||
bool shouldExit() const { return Environment::exitGameLoop; }
|
||||
|
||||
private:
|
||||
void processTickCount();
|
||||
void drawScene();
|
||||
|
||||
SDL_Window* window;
|
||||
SDL_GLContext glContext;
|
||||
Renderer renderer;
|
||||
|
||||
size_t newTickCount;
|
||||
size_t lastTickCount;
|
||||
|
||||
static const size_t CONST_TIMER_INTERVAL = 10;
|
||||
static const size_t CONST_MAX_TIME_INTERVAL = 1000;
|
||||
|
||||
std::shared_ptr<Texture> spaceshipTexture;
|
||||
VertexDataStruct spaceshipBase;
|
||||
VertexRenderStruct spaceship;
|
||||
};
|
||||
|
||||
} // namespace ZL
|
||||
@@ -0,0 +1,734 @@
|
||||
#include "Math.h"
|
||||
|
||||
#include <exception>
|
||||
#include <cmath>
|
||||
|
||||
namespace ZL {
|
||||
|
||||
Vector2f operator+(const Vector2f& x, const Vector2f& y)
|
||||
{
|
||||
Vector2f result;
|
||||
|
||||
result.v[0] = x.v[0] + y.v[0];
|
||||
result.v[1] = x.v[1] + y.v[1];
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector2f operator-(const Vector2f& x, const Vector2f& y)
|
||||
{
|
||||
Vector2f result;
|
||||
|
||||
result.v[0] = x.v[0] - y.v[0];
|
||||
result.v[1] = x.v[1] - y.v[1];
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector3f operator+(const Vector3f& x, const Vector3f& y)
|
||||
{
|
||||
Vector3f result;
|
||||
|
||||
result.v[0] = x.v[0] + y.v[0];
|
||||
result.v[1] = x.v[1] + y.v[1];
|
||||
result.v[2] = x.v[2] + y.v[2];
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector3f operator-(const Vector3f& x, const Vector3f& y)
|
||||
{
|
||||
Vector3f result;
|
||||
|
||||
result.v[0] = x.v[0] - y.v[0];
|
||||
result.v[1] = x.v[1] - y.v[1];
|
||||
result.v[2] = x.v[2] - y.v[2];
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector4f operator+(const Vector4f& x, const Vector4f& y)
|
||||
{
|
||||
Vector4f result;
|
||||
|
||||
result.v[0] = x.v[0] + y.v[0];
|
||||
result.v[1] = x.v[1] + y.v[1];
|
||||
result.v[2] = x.v[2] + y.v[2];
|
||||
result.v[3] = x.v[3] + y.v[3];
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector4f operator-(const Vector4f& x, const Vector4f& y)
|
||||
{
|
||||
Vector4f result;
|
||||
|
||||
result.v[0] = x.v[0] - y.v[0];
|
||||
result.v[1] = x.v[1] - y.v[1];
|
||||
result.v[2] = x.v[2] - y.v[2];
|
||||
result.v[3] = x.v[3] - y.v[3];
|
||||
return result;
|
||||
}
|
||||
|
||||
Matrix3f Matrix3f::Identity()
|
||||
{
|
||||
Matrix3f r;
|
||||
|
||||
r.m[0] = 1.f;
|
||||
r.m[1] = 0.f;
|
||||
r.m[2] = 0.f;
|
||||
|
||||
r.m[3] = 0.f;
|
||||
r.m[4] = 1.f;
|
||||
r.m[5] = 0.f;
|
||||
|
||||
r.m[6] = 0.f;
|
||||
r.m[7] = 0.f;
|
||||
r.m[8] = 1.f;
|
||||
|
||||
return r;
|
||||
|
||||
}
|
||||
|
||||
Matrix4f Matrix4f::Identity()
|
||||
{
|
||||
Matrix4f r;
|
||||
|
||||
r.m[0] = 1.f;
|
||||
r.m[1] = 0.f;
|
||||
r.m[2] = 0.f;
|
||||
r.m[3] = 0.f;
|
||||
|
||||
r.m[4] = 0.f;
|
||||
r.m[5] = 1.f;
|
||||
r.m[6] = 0.f;
|
||||
r.m[7] = 0.f;
|
||||
|
||||
r.m[8] = 0.f;
|
||||
r.m[9] = 0.f;
|
||||
r.m[10] = 1.f;
|
||||
r.m[11] = 0.f;
|
||||
|
||||
r.m[12] = 0.f;
|
||||
r.m[13] = 0.f;
|
||||
r.m[14] = 0.f;
|
||||
r.m[15] = 1.f;
|
||||
|
||||
return r;
|
||||
|
||||
}
|
||||
|
||||
Matrix4f operator*(const Matrix4f& m1, const Matrix4f& m2)
|
||||
{
|
||||
Matrix4f r;
|
||||
|
||||
r.m[0] = m1.m[0] * m2.m[0] + m1.m[4] * m2.m[1] + m1.m[8] * m2.m[2] + m1.m[12] * m2.m[3];
|
||||
r.m[1] = m1.m[1] * m2.m[0] + m1.m[5] * m2.m[1] + m1.m[9] * m2.m[2] + m1.m[13] * m2.m[3];
|
||||
r.m[2] = m1.m[2] * m2.m[0] + m1.m[6] * m2.m[1] + m1.m[10] * m2.m[2] + m1.m[14] * m2.m[3];
|
||||
r.m[3] = m1.m[3] * m2.m[0] + m1.m[7] * m2.m[1] + m1.m[11] * m2.m[2] + m1.m[15] * m2.m[3];
|
||||
|
||||
r.m[4] = m1.m[0] * m2.m[4] + m1.m[4] * m2.m[5] + m1.m[8] * m2.m[6] + m1.m[12] * m2.m[7];
|
||||
r.m[5] = m1.m[1] * m2.m[4] + m1.m[5] * m2.m[5] + m1.m[9] * m2.m[6] + m1.m[13] * m2.m[7];
|
||||
r.m[6] = m1.m[2] * m2.m[4] + m1.m[6] * m2.m[5] + m1.m[10] * m2.m[6] + m1.m[14] * m2.m[7];
|
||||
r.m[7] = m1.m[3] * m2.m[4] + m1.m[7] * m2.m[5] + m1.m[11] * m2.m[6] + m1.m[15] * m2.m[7];
|
||||
|
||||
|
||||
r.m[8] = m1.m[0] * m2.m[8] + m1.m[4] * m2.m[9] + m1.m[8] * m2.m[10] + m1.m[12] * m2.m[11];
|
||||
r.m[9] = m1.m[1] * m2.m[8] + m1.m[5] * m2.m[9] + m1.m[9] * m2.m[10] + m1.m[13] * m2.m[11];
|
||||
r.m[10] = m1.m[2] * m2.m[8] + m1.m[6] * m2.m[9] + m1.m[10] * m2.m[10] + m1.m[14] * m2.m[11];
|
||||
r.m[11] = m1.m[3] * m2.m[8] + m1.m[7] * m2.m[9] + m1.m[11] * m2.m[10] + m1.m[15] * m2.m[11];
|
||||
|
||||
r.m[12] = m1.m[0] * m2.m[12] + m1.m[4] * m2.m[13] + m1.m[8] * m2.m[14] + m1.m[12] * m2.m[15];
|
||||
r.m[13] = m1.m[1] * m2.m[12] + m1.m[5] * m2.m[13] + m1.m[9] * m2.m[14] + m1.m[13] * m2.m[15];
|
||||
r.m[14] = m1.m[2] * m2.m[12] + m1.m[6] * m2.m[13] + m1.m[10] * m2.m[14] + m1.m[14] * m2.m[15];
|
||||
r.m[15] = m1.m[3] * m2.m[12] + m1.m[7] * m2.m[13] + m1.m[11] * m2.m[14] + m1.m[15] * m2.m[15];
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
Matrix4f MakeOrthoMatrix(float width, float height, float zNear, float zFar)
|
||||
{
|
||||
float depthRange = zFar - zNear;
|
||||
|
||||
if (depthRange <= 0)
|
||||
{
|
||||
throw std::runtime_error("zFar must be greater than zNear");
|
||||
}
|
||||
|
||||
Matrix4f r;
|
||||
|
||||
r.m[0] = 2.f / width;
|
||||
r.m[1] = 0;
|
||||
r.m[2] = 0;
|
||||
r.m[3] = 0;
|
||||
|
||||
r.m[4] = 0;
|
||||
r.m[5] = 2.f / height;
|
||||
r.m[6] = 0;
|
||||
r.m[7] = 0;
|
||||
|
||||
r.m[8] = 0;
|
||||
r.m[9] = 0;
|
||||
r.m[10] = -1 / depthRange;
|
||||
r.m[11] = 0;
|
||||
|
||||
r.m[12] = -1;
|
||||
r.m[13] = -1;
|
||||
r.m[14] = zNear / depthRange;
|
||||
r.m[15] = 1;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
Matrix4f MakePerspectiveMatrix(float fovY, float aspectRatio, float zNear, float zFar)
|
||||
{
|
||||
float tanHalfFovy = tan(fovY / 2.f);
|
||||
Matrix4f r;
|
||||
|
||||
if (zNear >= zFar || aspectRatio == 0)
|
||||
{
|
||||
throw std::runtime_error("Invalid perspective parameters");
|
||||
}
|
||||
|
||||
r.m[0] = 1.f / (aspectRatio * tanHalfFovy);
|
||||
r.m[1] = 0;
|
||||
r.m[2] = 0;
|
||||
r.m[3] = 0;
|
||||
|
||||
r.m[4] = 0;
|
||||
r.m[5] = 1.f / (tanHalfFovy);
|
||||
r.m[6] = 0;
|
||||
r.m[7] = 0;
|
||||
|
||||
r.m[8] = 0;
|
||||
r.m[9] = 0;
|
||||
r.m[10] = -(zFar + zNear) / (zFar - zNear);
|
||||
r.m[11] = -1;
|
||||
|
||||
r.m[12] = 0;
|
||||
r.m[13] = 0;
|
||||
r.m[14] = -(2.f * zFar * zNear) / (zFar - zNear);
|
||||
r.m[15] = 0;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
Matrix3f QuatToMatrix(const Vector4f& q)
|
||||
{
|
||||
Matrix3f m;
|
||||
|
||||
float wx, wy, wz, xx, yy, yz, xy, xz, zz, s, x2, y2, z2;
|
||||
|
||||
s = 2.0f / (q.v[0] * q.v[0] + q.v[1] * q.v[1] + q.v[2] * q.v[2] + q.v[3] * q.v[3]);
|
||||
|
||||
|
||||
x2 = q.v[0] * s;
|
||||
y2 = q.v[1] * s;
|
||||
z2 = q.v[2] * s;
|
||||
|
||||
wx = q.v[3] * x2; wy = q.v[3] * y2; wz = q.v[3] * z2;
|
||||
xx = q.v[0] * x2; xy = q.v[1] * x2; xz = q.v[2] * x2;
|
||||
yy = q.v[1] * y2; yz = q.v[2] * y2;
|
||||
zz = q.v[2] * z2;
|
||||
|
||||
m.m[0] = 1.0f - (yy + zz);
|
||||
m.m[1] = xy + wz;
|
||||
m.m[2] = xz - wy;
|
||||
|
||||
m.m[3] = xy - wz;
|
||||
m.m[4] = 1.0f - (xx + zz);
|
||||
m.m[5] = yz + wx;
|
||||
|
||||
m.m[6] = xz + wy;
|
||||
m.m[7] = yz - wx;
|
||||
m.m[8] = 1.0f - (xx + yy);
|
||||
|
||||
return m;
|
||||
|
||||
}
|
||||
|
||||
Vector4f MatrixToQuat(const Matrix3f& m)
|
||||
{
|
||||
Vector4f r;
|
||||
float trace = m.m[0] + m.m[4] + m.m[8];
|
||||
|
||||
if (trace > 0)
|
||||
{
|
||||
float s = 0.5f / sqrtf(trace + 1.0f);
|
||||
r.v[3] = 0.25f / s;
|
||||
r.v[0] = (m.m[5] - m.m[7]) * s;
|
||||
r.v[1] = (m.m[6] - m.m[2]) * s;
|
||||
r.v[2] = (m.m[1] - m.m[3]) * s;
|
||||
}
|
||||
else if (m.m[0] > m.m[4] && m.m[0] > m.m[8])
|
||||
{
|
||||
float s = 2.0f * sqrtf(1.0f + m.m[0] - m.m[4] - m.m[8]);
|
||||
r.v[3] = (m.m[5] - m.m[7]) / s;
|
||||
r.v[0] = 0.25f * s;
|
||||
r.v[1] = (m.m[1] + m.m[3]) / s;
|
||||
r.v[2] = (m.m[6] + m.m[2]) / s;
|
||||
}
|
||||
else if (m.m[4] > m.m[8])
|
||||
{
|
||||
float s = 2.0f * sqrtf(1.0f + m.m[4] - m.m[0] - m.m[8]);
|
||||
r.v[3] = (m.m[6] - m.m[2]) / s;
|
||||
r.v[0] = (m.m[1] + m.m[3]) / s;
|
||||
r.v[1] = 0.25f * s;
|
||||
r.v[2] = (m.m[5] + m.m[7]) / s;
|
||||
}
|
||||
else
|
||||
{
|
||||
float s = 2.0f * sqrtf(1.0f + m.m[8] - m.m[0] - m.m[4]);
|
||||
r.v[3] = (m.m[1] - m.m[3]) / s;
|
||||
r.v[0] = (m.m[6] + m.m[2]) / s;
|
||||
r.v[1] = (m.m[5] + m.m[7]) / s;
|
||||
r.v[2] = 0.25f * s;
|
||||
}
|
||||
|
||||
return r.normalized();
|
||||
}
|
||||
|
||||
Vector4f QuatFromRotateAroundX(float angle)
|
||||
{
|
||||
Vector4f result;
|
||||
|
||||
result.v[0] = sinf(angle * 0.5f);
|
||||
result.v[1] = 0.f;
|
||||
result.v[2] = 0.f;
|
||||
result.v[3] = cosf(angle * 0.5f);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector4f QuatFromRotateAroundY(float angle)
|
||||
{
|
||||
Vector4f result;
|
||||
|
||||
result.v[0] = 0.f;
|
||||
result.v[1] = sinf(angle * 0.5f);
|
||||
result.v[2] = 0.f;
|
||||
result.v[3] = cosf(angle * 0.5f);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector4f QuatFromRotateAroundZ(float angle)
|
||||
{
|
||||
Vector4f result;
|
||||
|
||||
result.v[0] = 0.f;
|
||||
result.v[1] = 0.f;
|
||||
result.v[2] = sinf(angle * 0.5f);
|
||||
result.v[3] = cosf(angle * 0.5f);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Matrix3f TransposeMatrix(const Matrix3f& m)
|
||||
{
|
||||
Matrix3f r;
|
||||
r.m[0] = m.m[0];
|
||||
r.m[1] = m.m[3];
|
||||
r.m[2] = m.m[6];
|
||||
r.m[3] = m.m[1];
|
||||
r.m[4] = m.m[4];
|
||||
r.m[5] = m.m[7];
|
||||
r.m[6] = m.m[2];
|
||||
r.m[7] = m.m[5];
|
||||
r.m[8] = m.m[8];
|
||||
return r;
|
||||
}
|
||||
|
||||
Matrix3f InverseMatrix(const Matrix3f& m)
|
||||
{
|
||||
float d;
|
||||
Matrix3f r;
|
||||
|
||||
d = m.m[0] * (m.m[4] * m.m[8] - m.m[5] * m.m[7]);
|
||||
d -= m.m[1] * (m.m[3] * m.m[8] - m.m[6] * m.m[5]);
|
||||
d += m.m[2] * (m.m[3] * m.m[7] - m.m[6] * m.m[4]);
|
||||
|
||||
if (fabs(d) < 0.01f)
|
||||
{
|
||||
throw std::runtime_error("Error: matrix cannot be inversed!");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
r.m[0] = (m.m[4] * m.m[8] - m.m[5] * m.m[7]) / d;
|
||||
r.m[1] = -(m.m[1] * m.m[8] - m.m[2] * m.m[7]) / d;
|
||||
r.m[2] = (m.m[1] * m.m[5] - m.m[2] * m.m[4]) / d;
|
||||
|
||||
r.m[3] = -(m.m[3] * m.m[8] - m.m[5] * m.m[6]) / d;
|
||||
r.m[4] = (m.m[0] * m.m[8] - m.m[2] * m.m[6]) / d;
|
||||
r.m[5] = -(m.m[0] * m.m[5] - m.m[2] * m.m[3]) / d;
|
||||
|
||||
r.m[6] = (m.m[3] * m.m[7] - m.m[6] * m.m[4]) / d;
|
||||
r.m[7] = -(m.m[0] * m.m[7] - m.m[6] * m.m[1]) / d;
|
||||
r.m[8] = (m.m[0] * m.m[4] - m.m[1] * m.m[3]) / d;
|
||||
|
||||
};
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
Matrix4f InverseMatrix(const Matrix4f& mat)
|
||||
{
|
||||
Matrix4f inv;
|
||||
float det;
|
||||
|
||||
inv.m[0] = mat.m[5] * mat.m[10] * mat.m[15] -
|
||||
mat.m[5] * mat.m[11] * mat.m[14] -
|
||||
mat.m[9] * mat.m[6] * mat.m[15] +
|
||||
mat.m[9] * mat.m[7] * mat.m[14] +
|
||||
mat.m[13] * mat.m[6] * mat.m[11] -
|
||||
mat.m[13] * mat.m[7] * mat.m[10];
|
||||
|
||||
inv.m[4] = -mat.m[4] * mat.m[10] * mat.m[15] +
|
||||
mat.m[4] * mat.m[11] * mat.m[14] +
|
||||
mat.m[8] * mat.m[6] * mat.m[15] -
|
||||
mat.m[8] * mat.m[7] * mat.m[14] -
|
||||
mat.m[12] * mat.m[6] * mat.m[11] +
|
||||
mat.m[12] * mat.m[7] * mat.m[10];
|
||||
|
||||
inv.m[8] = mat.m[4] * mat.m[9] * mat.m[15] -
|
||||
mat.m[4] * mat.m[11] * mat.m[13] -
|
||||
mat.m[8] * mat.m[5] * mat.m[15] +
|
||||
mat.m[8] * mat.m[7] * mat.m[13] +
|
||||
mat.m[12] * mat.m[5] * mat.m[11] -
|
||||
mat.m[12] * mat.m[7] * mat.m[9];
|
||||
|
||||
inv.m[12] = -mat.m[4] * mat.m[9] * mat.m[14] +
|
||||
mat.m[4] * mat.m[10] * mat.m[13] +
|
||||
mat.m[8] * mat.m[5] * mat.m[14] -
|
||||
mat.m[8] * mat.m[6] * mat.m[13] -
|
||||
mat.m[12] * mat.m[5] * mat.m[10] +
|
||||
mat.m[12] * mat.m[6] * mat.m[9];
|
||||
|
||||
inv.m[1] = -mat.m[1] * mat.m[10] * mat.m[15] +
|
||||
mat.m[1] * mat.m[11] * mat.m[14] +
|
||||
mat.m[9] * mat.m[2] * mat.m[15] -
|
||||
mat.m[9] * mat.m[3] * mat.m[14] -
|
||||
mat.m[13] * mat.m[2] * mat.m[11] +
|
||||
mat.m[13] * mat.m[3] * mat.m[10];
|
||||
|
||||
inv.m[5] = mat.m[0] * mat.m[10] * mat.m[15] -
|
||||
mat.m[0] * mat.m[11] * mat.m[14] -
|
||||
mat.m[8] * mat.m[2] * mat.m[15] +
|
||||
mat.m[8] * mat.m[3] * mat.m[14] +
|
||||
mat.m[12] * mat.m[2] * mat.m[11] -
|
||||
mat.m[12] * mat.m[3] * mat.m[10];
|
||||
|
||||
inv.m[9] = -mat.m[0] * mat.m[9] * mat.m[15] +
|
||||
mat.m[0] * mat.m[11] * mat.m[13] +
|
||||
mat.m[8] * mat.m[1] * mat.m[15] -
|
||||
mat.m[8] * mat.m[3] * mat.m[13] -
|
||||
mat.m[12] * mat.m[1] * mat.m[11] +
|
||||
mat.m[12] * mat.m[3] * mat.m[9];
|
||||
|
||||
inv.m[13] = mat.m[0] * mat.m[9] * mat.m[14] -
|
||||
mat.m[0] * mat.m[10] * mat.m[13] -
|
||||
mat.m[8] * mat.m[1] * mat.m[14] +
|
||||
mat.m[8] * mat.m[2] * mat.m[13] +
|
||||
mat.m[12] * mat.m[1] * mat.m[10] -
|
||||
mat.m[12] * mat.m[2] * mat.m[9];
|
||||
|
||||
inv.m[2] = mat.m[1] * mat.m[6] * mat.m[15] -
|
||||
mat.m[1] * mat.m[7] * mat.m[14] -
|
||||
mat.m[5] * mat.m[2] * mat.m[15] +
|
||||
mat.m[5] * mat.m[3] * mat.m[14] +
|
||||
mat.m[13] * mat.m[2] * mat.m[7] -
|
||||
mat.m[13] * mat.m[3] * mat.m[6];
|
||||
|
||||
inv.m[6] = -mat.m[0] * mat.m[6] * mat.m[15] +
|
||||
mat.m[0] * mat.m[7] * mat.m[14] +
|
||||
mat.m[4] * mat.m[2] * mat.m[15] -
|
||||
mat.m[4] * mat.m[3] * mat.m[14] -
|
||||
mat.m[12] * mat.m[2] * mat.m[7] +
|
||||
mat.m[12] * mat.m[3] * mat.m[6];
|
||||
|
||||
inv.m[10] = mat.m[0] * mat.m[5] * mat.m[15] -
|
||||
mat.m[0] * mat.m[7] * mat.m[13] -
|
||||
mat.m[4] * mat.m[1] * mat.m[15] +
|
||||
mat.m[4] * mat.m[3] * mat.m[13] +
|
||||
mat.m[12] * mat.m[1] * mat.m[7] -
|
||||
mat.m[12] * mat.m[3] * mat.m[5];
|
||||
|
||||
inv.m[14] = -mat.m[0] * mat.m[5] * mat.m[14] +
|
||||
mat.m[0] * mat.m[6] * mat.m[13] +
|
||||
mat.m[4] * mat.m[1] * mat.m[14] -
|
||||
mat.m[4] * mat.m[2] * mat.m[13] -
|
||||
mat.m[12] * mat.m[1] * mat.m[6] +
|
||||
mat.m[12] * mat.m[2] * mat.m[5];
|
||||
|
||||
inv.m[3] = -mat.m[1] * mat.m[6] * mat.m[11] +
|
||||
mat.m[1] * mat.m[7] * mat.m[10] +
|
||||
mat.m[5] * mat.m[2] * mat.m[11] -
|
||||
mat.m[5] * mat.m[3] * mat.m[10] -
|
||||
mat.m[9] * mat.m[2] * mat.m[7] +
|
||||
mat.m[9] * mat.m[3] * mat.m[6];
|
||||
|
||||
inv.m[7] = mat.m[0] * mat.m[6] * mat.m[11] -
|
||||
mat.m[0] * mat.m[7] * mat.m[10] -
|
||||
mat.m[4] * mat.m[2] * mat.m[11] +
|
||||
mat.m[4] * mat.m[3] * mat.m[10] +
|
||||
mat.m[8] * mat.m[2] * mat.m[7] -
|
||||
mat.m[8] * mat.m[3] * mat.m[6];
|
||||
|
||||
inv.m[11] = -mat.m[0] * mat.m[5] * mat.m[11] +
|
||||
mat.m[0] * mat.m[7] * mat.m[9] +
|
||||
mat.m[4] * mat.m[1] * mat.m[11] -
|
||||
mat.m[4] * mat.m[3] * mat.m[9] -
|
||||
mat.m[8] * mat.m[1] * mat.m[7] +
|
||||
mat.m[8] * mat.m[3] * mat.m[5];
|
||||
|
||||
inv.m[15] = mat.m[0] * mat.m[5] * mat.m[10] -
|
||||
mat.m[0] * mat.m[6] * mat.m[9] -
|
||||
mat.m[4] * mat.m[1] * mat.m[10] +
|
||||
mat.m[4] * mat.m[2] * mat.m[9] +
|
||||
mat.m[8] * mat.m[1] * mat.m[6] -
|
||||
mat.m[8] * mat.m[2] * mat.m[5];
|
||||
|
||||
// ��������� �����������
|
||||
det = mat.m[0] * inv.m[0] + mat.m[1] * inv.m[4] + mat.m[2] * inv.m[8] + mat.m[3] * inv.m[12];
|
||||
|
||||
if (std::fabs(det) < 0.01f)
|
||||
{
|
||||
throw std::runtime_error("Error: matrix cannot be inversed!");
|
||||
}
|
||||
|
||||
// ����� ��� �������� �� �����������
|
||||
det = 1.0f / det;
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
inv.m[i] *= det;
|
||||
}
|
||||
|
||||
return inv;
|
||||
}
|
||||
|
||||
Matrix3f CreateZRotationMatrix(float angle)
|
||||
{
|
||||
Matrix3f result = Matrix3f::Identity();
|
||||
|
||||
result.m[0] = cosf(angle);
|
||||
result.m[1] = -sinf(angle);
|
||||
result.m[3] = sinf(angle);
|
||||
result.m[4] = cosf(angle);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Matrix4f MultMatrixMatrix(const Matrix4f& m1, const Matrix4f& m2)
|
||||
{
|
||||
Matrix4f rx;
|
||||
|
||||
rx.m[0] = m1.m[0] * m2.m[0] + m1.m[4] * m2.m[1] + m1.m[8] * m2.m[2] + m1.m[12] * m2.m[3];
|
||||
rx.m[1] = m1.m[1] * m2.m[0] + m1.m[5] * m2.m[1] + m1.m[9] * m2.m[2] + m1.m[13] * m2.m[3];
|
||||
rx.m[2] = m1.m[2] * m2.m[0] + m1.m[6] * m2.m[1] + m1.m[10] * m2.m[2] + m1.m[14] * m2.m[3];
|
||||
rx.m[3] = m1.m[3] * m2.m[0] + m1.m[7] * m2.m[1] + m1.m[11] * m2.m[2] + m1.m[15] * m2.m[3];
|
||||
|
||||
rx.m[4] = m1.m[0] * m2.m[4] + m1.m[4] * m2.m[5] + m1.m[8] * m2.m[6] + m1.m[12] * m2.m[7];
|
||||
rx.m[5] = m1.m[1] * m2.m[4] + m1.m[5] * m2.m[5] + m1.m[9] * m2.m[6] + m1.m[13] * m2.m[7];
|
||||
rx.m[6] = m1.m[2] * m2.m[4] + m1.m[6] * m2.m[5] + m1.m[10] * m2.m[6] + m1.m[14] * m2.m[7];
|
||||
rx.m[7] = m1.m[3] * m2.m[4] + m1.m[7] * m2.m[5] + m1.m[11] * m2.m[6] + m1.m[15] * m2.m[7];
|
||||
|
||||
|
||||
rx.m[8] = m1.m[0] * m2.m[8] + m1.m[4] * m2.m[9] + m1.m[8] * m2.m[10] + m1.m[12] * m2.m[11];
|
||||
rx.m[9] = m1.m[1] * m2.m[8] + m1.m[5] * m2.m[9] + m1.m[9] * m2.m[10] + m1.m[13] * m2.m[11];
|
||||
rx.m[10] = m1.m[2] * m2.m[8] + m1.m[6] * m2.m[9] + m1.m[10] * m2.m[10] + m1.m[14] * m2.m[11];
|
||||
rx.m[11] = m1.m[3] * m2.m[8] + m1.m[7] * m2.m[9] + m1.m[11] * m2.m[10] + m1.m[15] * m2.m[11];
|
||||
|
||||
rx.m[12] = m1.m[0] * m2.m[12] + m1.m[4] * m2.m[13] + m1.m[8] * m2.m[14] + m1.m[12] * m2.m[15];
|
||||
rx.m[13] = m1.m[1] * m2.m[12] + m1.m[5] * m2.m[13] + m1.m[9] * m2.m[14] + m1.m[13] * m2.m[15];
|
||||
rx.m[14] = m1.m[2] * m2.m[12] + m1.m[6] * m2.m[13] + m1.m[10] * m2.m[14] + m1.m[14] * m2.m[15];
|
||||
rx.m[15] = m1.m[3] * m2.m[12] + m1.m[7] * m2.m[13] + m1.m[11] * m2.m[14] + m1.m[15] * m2.m[15];
|
||||
|
||||
return rx;
|
||||
}
|
||||
|
||||
Matrix3f MultMatrixMatrix(const Matrix3f& m1, const Matrix3f& m2)
|
||||
{
|
||||
Matrix3f r;
|
||||
|
||||
r.m[0] = m1.m[0] * m2.m[0] + m1.m[3] * m2.m[1] + m1.m[6] * m2.m[2];
|
||||
r.m[1] = m1.m[1] * m2.m[0] + m1.m[4] * m2.m[1] + m1.m[7] * m2.m[2];
|
||||
r.m[2] = m1.m[2] * m2.m[0] + m1.m[5] * m2.m[1] + m1.m[8] * m2.m[2];
|
||||
|
||||
r.m[3] = m1.m[0] * m2.m[3] + m1.m[3] * m2.m[4] + m1.m[6] * m2.m[5];
|
||||
r.m[4] = m1.m[1] * m2.m[3] + m1.m[4] * m2.m[4] + m1.m[7] * m2.m[5];
|
||||
r.m[5] = m1.m[2] * m2.m[3] + m1.m[5] * m2.m[4] + m1.m[8] * m2.m[5];
|
||||
|
||||
r.m[6] = m1.m[0] * m2.m[6] + m1.m[3] * m2.m[7] + m1.m[6] * m2.m[8] ;
|
||||
r.m[7] = m1.m[1] * m2.m[6] + m1.m[4] * m2.m[7] + m1.m[7] * m2.m[8];
|
||||
r.m[8] = m1.m[2] * m2.m[6] + m1.m[5] * m2.m[7] + m1.m[8] * m2.m[8];
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
Matrix3f MakeTranslationMatrix(const Vector3f& p)
|
||||
{
|
||||
Matrix3f r = Matrix3f::Identity();
|
||||
|
||||
r.m[12] = p.v[0];
|
||||
r.m[13] = p.v[1];
|
||||
r.m[14] = p.v[2];
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
Matrix3f MakeScaleMatrix(float scale)
|
||||
{
|
||||
Matrix3f r = Matrix3f::Identity();
|
||||
|
||||
r.m[0] = scale;
|
||||
r.m[5] = scale;
|
||||
r.m[10] = scale;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
Matrix3f MakeRotationMatrix(const Vector3f& p)
|
||||
{
|
||||
Matrix3f r = Matrix3f::Identity();
|
||||
|
||||
r.m[12] = p.v[0];
|
||||
r.m[13] = p.v[1];
|
||||
r.m[14] = p.v[2];
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
Vector3f operator*(Vector3f v, float scale)
|
||||
{
|
||||
Vector3f r = v;
|
||||
|
||||
r.v[0] = v.v[0] * scale;
|
||||
r.v[1] = v.v[1] * scale;
|
||||
r.v[2] = v.v[2] * scale;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
Vector4f operator*(Vector4f v, float scale)
|
||||
{
|
||||
Vector4f r = v;
|
||||
|
||||
r.v[0] = v.v[0] * scale;
|
||||
r.v[1] = v.v[1] * scale;
|
||||
r.v[2] = v.v[2] * scale;
|
||||
r.v[3] = v.v[3] * scale;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
Vector3f MultVectorMatrix(Vector3f v, Matrix3f mt)
|
||||
{
|
||||
Vector3f r;
|
||||
|
||||
r.v[0] = v.v[0] * mt.m[0] + v.v[1] * mt.m[1] + v.v[2] * mt.m[2];
|
||||
r.v[1] = v.v[0] * mt.m[3] + v.v[1] * mt.m[4] + v.v[2] * mt.m[5];
|
||||
r.v[2] = v.v[0] * mt.m[6] + v.v[1] * mt.m[7] + v.v[2] * mt.m[8];
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
Vector4f MultVectorMatrix(Vector4f v, Matrix4f mt)
|
||||
{
|
||||
Vector4f r;
|
||||
|
||||
r.v[0] = v.v[0] * mt.m[0] + v.v[1] * mt.m[1] + v.v[2] * mt.m[2] + v.v[3] * mt.m[3];
|
||||
r.v[1] = v.v[0] * mt.m[4] + v.v[1] * mt.m[5] + v.v[2] * mt.m[6] + v.v[3] * mt.m[7];
|
||||
r.v[2] = v.v[0] * mt.m[8] + v.v[1] * mt.m[9] + v.v[2] * mt.m[10] + v.v[3] * mt.m[11];
|
||||
r.v[3] = v.v[0] * mt.m[12] + v.v[1] * mt.m[13] + v.v[2] * mt.m[14] + v.v[3] * mt.m[15];
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
Vector4f MultMatrixVector(Matrix4f mt, Vector4f v)
|
||||
{
|
||||
Vector4f r;
|
||||
|
||||
r.v[0] = v.v[0] * mt.m[0] + v.v[1] * mt.m[4] + v.v[2] * mt.m[8] + v.v[3] * mt.m[12];
|
||||
r.v[1] = v.v[0] * mt.m[1] + v.v[1] * mt.m[5] + v.v[2] * mt.m[9] + v.v[3] * mt.m[13];
|
||||
r.v[2] = v.v[0] * mt.m[2] + v.v[1] * mt.m[6] + v.v[2] * mt.m[10] + v.v[3] * mt.m[14];
|
||||
r.v[3] = v.v[0] * mt.m[3] + v.v[1] * mt.m[7] + v.v[2] * mt.m[11] + v.v[3] * mt.m[15];
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
Vector4f slerp(const Vector4f& q1, const Vector4f& q2, float t)
|
||||
{
|
||||
const float epsilon = 1e-6f;
|
||||
|
||||
// Нормализация входных кватернионов
|
||||
Vector4f q1_norm = q1.normalized();
|
||||
Vector4f q2_norm = q2.normalized();
|
||||
|
||||
float cosTheta = q1_norm.dot(q2_norm);
|
||||
|
||||
// Если q1 и q2 близки к противоположным направлениям, корректируем q2
|
||||
Vector4f q2_adjusted = q2_norm;
|
||||
if (cosTheta < 0.0f) {
|
||||
q2_adjusted.v[0] = -q2_adjusted.v[0];
|
||||
q2_adjusted.v[1] = -q2_adjusted.v[1];
|
||||
q2_adjusted.v[2] = -q2_adjusted.v[2];
|
||||
q2_adjusted.v[3] = -q2_adjusted.v[3];
|
||||
cosTheta = -cosTheta;
|
||||
}
|
||||
|
||||
// Если кватернионы близки, используем линейную интерполяцию
|
||||
if (cosTheta > 1.0f - epsilon) {
|
||||
Vector4f result;
|
||||
|
||||
result.v[0] = q1_norm.v[0] + t * (q2_adjusted.v[0] - q1_norm.v[0]);
|
||||
result.v[1] = q1_norm.v[1] + t * (q2_adjusted.v[1] - q1_norm.v[1]);
|
||||
result.v[2] = q1_norm.v[2] + t * (q2_adjusted.v[2] - q1_norm.v[2]);
|
||||
result.v[3] = q1_norm.v[3] + t * (q2_adjusted.v[3] - q1_norm.v[3]);
|
||||
|
||||
return result.normalized();
|
||||
}
|
||||
|
||||
// Иначе используем сферическую интерполяцию
|
||||
float theta = std::acos(cosTheta);
|
||||
float sinTheta = std::sin(theta);
|
||||
|
||||
float coeff1 = std::sin((1.0f - t) * theta) / sinTheta;
|
||||
float coeff2 = std::sin(t * theta) / sinTheta;
|
||||
|
||||
Vector4f result;
|
||||
|
||||
result.v[0] = coeff1 * q1_norm.v[0] + coeff2 * q2_adjusted.v[0];
|
||||
result.v[1] = coeff1 * q1_norm.v[1] + coeff2 * q2_adjusted.v[1];
|
||||
result.v[2] = coeff1 * q1_norm.v[2] + coeff2 * q2_adjusted.v[2];
|
||||
result.v[3] = coeff1 * q1_norm.v[3] + coeff2 * q2_adjusted.v[3];
|
||||
|
||||
return result.normalized();
|
||||
}
|
||||
|
||||
Matrix4f MakeMatrix4x4(const Matrix3f& m, const Vector3f pos)
|
||||
{
|
||||
Matrix4f r;
|
||||
|
||||
r.m[0] = m.m[0];
|
||||
r.m[1] = m.m[1];
|
||||
r.m[2] = m.m[2];
|
||||
r.m[3] = 0;
|
||||
|
||||
r.m[4] = m.m[3];
|
||||
r.m[5] = m.m[4];
|
||||
r.m[6] = m.m[5];
|
||||
r.m[7] = 0;
|
||||
|
||||
r.m[8] = m.m[6];
|
||||
r.m[9] = m.m[7];
|
||||
r.m[10] = m.m[8];
|
||||
r.m[11] = 0;
|
||||
|
||||
r.m[12] = pos.v[0];
|
||||
r.m[13] = pos.v[1];
|
||||
r.m[14] = pos.v[2];
|
||||
r.m[15] = 1.0;
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
@@ -0,0 +1,110 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <exception>
|
||||
#include <stdexcept>
|
||||
#include <cmath>
|
||||
|
||||
namespace ZL {
|
||||
|
||||
struct Vector4f
|
||||
{
|
||||
std::array<float, 4> v = { 0.f, 0.f, 0.f, 0.f };
|
||||
|
||||
Vector4f normalized() const {
|
||||
double norm = std::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2] + v[3] * v[3]);
|
||||
Vector4f r;
|
||||
|
||||
r.v[0] = v[0] / norm;
|
||||
r.v[1] = v[1] / norm;
|
||||
r.v[2] = v[2] / norm;
|
||||
r.v[3] = v[3] / norm;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
double dot(const Vector4f& other) const {
|
||||
return v[0] * other.v[0] + v[1] * other.v[1] + v[2] * other.v[2] + v[3] * other.v[3];
|
||||
}
|
||||
};
|
||||
|
||||
struct Vector3f
|
||||
{
|
||||
std::array<float, 3> v = { 0.f, 0.f, 0.f };
|
||||
};
|
||||
|
||||
struct Vector2f
|
||||
{
|
||||
std::array<float, 2> v = {0.f, 0.f};
|
||||
};
|
||||
|
||||
Vector2f operator+(const Vector2f& x, const Vector2f& y);
|
||||
|
||||
Vector2f operator-(const Vector2f& x, const Vector2f& y);
|
||||
|
||||
Vector3f operator+(const Vector3f& x, const Vector3f& y);
|
||||
|
||||
Vector3f operator-(const Vector3f& x, const Vector3f& y);
|
||||
Vector4f operator+(const Vector4f& x, const Vector4f& y);
|
||||
|
||||
Vector4f operator-(const Vector4f& x, const Vector4f& y);
|
||||
|
||||
|
||||
struct Matrix3f
|
||||
{
|
||||
std::array<float, 9> m = { 0.f, 0.f, 0.f,
|
||||
0.f, 0.f, 0.f,
|
||||
0.f, 0.f, 0.f, };
|
||||
|
||||
static Matrix3f Identity();
|
||||
};
|
||||
|
||||
struct Matrix4f
|
||||
{
|
||||
std::array<float, 16> m = { 0.f, 0.f, 0.f, 0.f,
|
||||
0.f, 0.f, 0.f, 0.f,
|
||||
0.f, 0.f, 0.f, 0.f,
|
||||
0.f, 0.f, 0.f, 0.f };
|
||||
|
||||
static Matrix4f Identity();
|
||||
|
||||
float& operator()(int row, int col) {
|
||||
//return m[row * 4 + col]; //OpenGL specific
|
||||
return m[col * 4 + row];
|
||||
}
|
||||
|
||||
const float& operator()(int row, int col) const {
|
||||
//return m[row * 4 + col];
|
||||
return m[col * 4 + row];
|
||||
}
|
||||
};
|
||||
|
||||
Matrix4f operator*(const Matrix4f& m1, const Matrix4f& m2);
|
||||
|
||||
Matrix4f MakeOrthoMatrix(float width, float height, float zNear, float zFar);
|
||||
|
||||
Matrix4f MakePerspectiveMatrix(float fovY, float aspectRatio, float zNear, float zFar);
|
||||
|
||||
Matrix3f QuatToMatrix(const Vector4f& q);
|
||||
|
||||
Vector4f MatrixToQuat(const Matrix3f& m);
|
||||
|
||||
Vector4f QuatFromRotateAroundX(float angle);
|
||||
Vector4f QuatFromRotateAroundY(float angle);
|
||||
Vector4f QuatFromRotateAroundZ(float angle);
|
||||
|
||||
Vector3f operator*(Vector3f v, float scale);
|
||||
Vector4f operator*(Vector4f v, float scale);
|
||||
|
||||
Vector3f MultVectorMatrix(Vector3f v, Matrix3f mt);
|
||||
Vector4f MultVectorMatrix(Vector4f v, Matrix4f mt);
|
||||
Vector4f MultMatrixVector(Matrix4f mt, Vector4f v);
|
||||
|
||||
Vector4f slerp(const Vector4f& q1, const Vector4f& q2, float t);
|
||||
Matrix3f InverseMatrix(const Matrix3f& m);
|
||||
Matrix4f InverseMatrix(const Matrix4f& m);
|
||||
Matrix3f MultMatrixMatrix(const Matrix3f& m1, const Matrix3f& m2);
|
||||
Matrix4f MultMatrixMatrix(const Matrix4f& m1, const Matrix4f& m2);
|
||||
Matrix4f MakeMatrix4x4(const Matrix3f& m, const Vector3f pos);
|
||||
|
||||
};
|
||||
@@ -0,0 +1,700 @@
|
||||
#include "Renderer.h"
|
||||
#include <cmath>
|
||||
|
||||
namespace ZL {
|
||||
|
||||
VBOHolder::VBOHolder()
|
||||
{
|
||||
glGenBuffers(1, &Buffer);
|
||||
}
|
||||
|
||||
VBOHolder::~VBOHolder()
|
||||
{
|
||||
glDeleteBuffers(1, &Buffer);
|
||||
}
|
||||
|
||||
GLuint VBOHolder::getBuffer()
|
||||
{
|
||||
return Buffer;
|
||||
}
|
||||
|
||||
VAOHolder::VAOHolder()
|
||||
{
|
||||
#ifndef EMSCRIPTEN
|
||||
#if defined(_WIN32) || defined(__linux__) && !defined(__ANDROID__)
|
||||
glGenVertexArrays(1, &vao);
|
||||
#else
|
||||
vao = 0;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
VAOHolder::~VAOHolder()
|
||||
{
|
||||
#ifndef EMSCRIPTEN
|
||||
#if defined(_WIN32) || defined(__linux__) && !defined(__ANDROID__)
|
||||
glDeleteVertexArrays(1, &vao);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
GLuint VAOHolder::getBuffer()
|
||||
{
|
||||
return vao;
|
||||
}
|
||||
|
||||
|
||||
VertexDataStruct CreateRect2D(Vector2f center, Vector2f halfWidthHeight, float zLevel)
|
||||
{
|
||||
Vector2f posFrom = center - halfWidthHeight;
|
||||
|
||||
Vector2f posTo = center + halfWidthHeight;
|
||||
|
||||
Vector3f pos1 = { posFrom.v[0], posFrom.v[1], zLevel };
|
||||
Vector3f pos2 = { posFrom.v[0], posTo.v[1], zLevel };
|
||||
Vector3f pos3 = { posTo.v[0], posTo.v[1], zLevel };
|
||||
Vector3f pos4 = { posTo.v[0], posFrom.v[1], zLevel };
|
||||
|
||||
|
||||
Vector2f texCoordPos1 = { 0.0f, 0.0f };
|
||||
Vector2f texCoordPos2 = { 0.0f, 1.0f };
|
||||
Vector2f texCoordPos3 = { 1.0f, 1.0f };
|
||||
Vector2f texCoordPos4 = { 1.0f, 0.0f };
|
||||
|
||||
VertexDataStruct result;
|
||||
|
||||
result.PositionData.push_back(pos1);
|
||||
result.PositionData.push_back(pos2);
|
||||
result.PositionData.push_back(pos3);
|
||||
result.PositionData.push_back(pos3);
|
||||
result.PositionData.push_back(pos4);
|
||||
result.PositionData.push_back(pos1);
|
||||
|
||||
result.TexCoordData.push_back(texCoordPos1);
|
||||
result.TexCoordData.push_back(texCoordPos2);
|
||||
result.TexCoordData.push_back(texCoordPos3);
|
||||
result.TexCoordData.push_back(texCoordPos3);
|
||||
result.TexCoordData.push_back(texCoordPos4);
|
||||
result.TexCoordData.push_back(texCoordPos1);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
VertexDataStruct CreateRectHorizontalSections2D(Vector2f center, Vector2f halfWidthHeight, float zLevel, size_t sectionCount)
|
||||
{
|
||||
Vector2f posFrom = center - halfWidthHeight;
|
||||
|
||||
Vector2f posTo = center + halfWidthHeight;
|
||||
|
||||
float sectionWidth = halfWidthHeight.v[0] * 2.f;
|
||||
|
||||
VertexDataStruct result;
|
||||
|
||||
for (size_t i = 0; i < sectionCount; i++)
|
||||
{
|
||||
Vector3f pos1 = { posFrom.v[0]+sectionWidth*i, posFrom.v[1], zLevel };
|
||||
Vector3f pos2 = { posFrom.v[0] + sectionWidth * i, posTo.v[1], zLevel };
|
||||
Vector3f pos3 = { posTo.v[0] + sectionWidth * i, posTo.v[1], zLevel };
|
||||
Vector3f pos4 = { posTo.v[0] + sectionWidth * i, posFrom.v[1], zLevel };
|
||||
|
||||
result.PositionData.push_back(pos1);
|
||||
result.PositionData.push_back(pos2);
|
||||
result.PositionData.push_back(pos3);
|
||||
result.PositionData.push_back(pos3);
|
||||
result.PositionData.push_back(pos4);
|
||||
result.PositionData.push_back(pos1);
|
||||
|
||||
Vector2f texCoordPos1 = { 0.0f, 0.0f };
|
||||
Vector2f texCoordPos2 = { 0.0f, 1.0f };
|
||||
Vector2f texCoordPos3 = { 1.0f, 1.0f };
|
||||
Vector2f texCoordPos4 = { 1.0f, 0.0f };
|
||||
|
||||
result.TexCoordData.push_back(texCoordPos1);
|
||||
result.TexCoordData.push_back(texCoordPos2);
|
||||
result.TexCoordData.push_back(texCoordPos3);
|
||||
result.TexCoordData.push_back(texCoordPos3);
|
||||
result.TexCoordData.push_back(texCoordPos4);
|
||||
result.TexCoordData.push_back(texCoordPos1);
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
VertexDataStruct CreateCube3D(float scale)
|
||||
{
|
||||
|
||||
std::array<std::array<Vector3f, 4>, 6> cubeSides;
|
||||
|
||||
std::array<Vector3f, 6> cubeColors;
|
||||
|
||||
|
||||
cubeSides[0][0] = { -1, -1, -1 };
|
||||
cubeSides[0][1] = { -1, 1, -1 };
|
||||
cubeSides[0][2] = { 1, 1, -1 };
|
||||
cubeSides[0][3] = { 1, -1, -1 };
|
||||
|
||||
cubeSides[1][0] = { -1, -1, 1 };
|
||||
cubeSides[1][1] = { -1, 1, 1 };
|
||||
cubeSides[1][2] = { 1, 1, 1 };
|
||||
cubeSides[1][3] = { 1, -1, 1 };
|
||||
|
||||
//------------
|
||||
|
||||
cubeSides[2][0] = { -1, -1, -1 };
|
||||
cubeSides[2][1] = { -1, -1, 1 };
|
||||
cubeSides[2][2] = { 1, -1, 1 };
|
||||
cubeSides[2][3] = { 1, -1, -1 };
|
||||
|
||||
cubeSides[3][0] = { -1, 1, -1 };
|
||||
cubeSides[3][1] = { -1, 1, 1 };
|
||||
cubeSides[3][2] = { 1, 1, 1 };
|
||||
cubeSides[3][3] = { 1, 1, -1 };
|
||||
|
||||
//------------
|
||||
cubeSides[4][0] = { -1, -1, -1 };
|
||||
cubeSides[4][1] = { -1, -1, 1 };
|
||||
cubeSides[4][2] = { -1, 1, 1 };
|
||||
cubeSides[4][3] = { -1, 1, -1 };
|
||||
|
||||
cubeSides[5][0] = { 1, -1, -1 };
|
||||
cubeSides[5][1] = { 1, -1, 1 };
|
||||
cubeSides[5][2] = { 1, 1, 1 };
|
||||
cubeSides[5][3] = { 1, 1, -1 };
|
||||
|
||||
//-----------
|
||||
|
||||
cubeColors[0] = Vector3f{ 1, 0, 0 };
|
||||
cubeColors[1] = Vector3f{ 0, 1, 0 };
|
||||
cubeColors[2] = Vector3f{ 0, 0, 1 };
|
||||
cubeColors[3] = Vector3f{ 1, 1, 0 };
|
||||
cubeColors[4] = Vector3f{ 0, 1, 1 };
|
||||
cubeColors[5] = Vector3f{ 1, 0, 1 };
|
||||
|
||||
//-----------
|
||||
|
||||
VertexDataStruct result;
|
||||
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
result.PositionData.push_back(cubeSides[i][0] * scale);
|
||||
result.PositionData.push_back(cubeSides[i][1] * scale);
|
||||
result.PositionData.push_back(cubeSides[i][2] * scale);
|
||||
result.PositionData.push_back(cubeSides[i][2] * scale);
|
||||
result.PositionData.push_back(cubeSides[i][3] * scale);
|
||||
result.PositionData.push_back(cubeSides[i][0] * scale);
|
||||
|
||||
result.ColorData.push_back(cubeColors[i]);
|
||||
result.ColorData.push_back(cubeColors[i]);
|
||||
result.ColorData.push_back(cubeColors[i]);
|
||||
result.ColorData.push_back(cubeColors[i]);
|
||||
result.ColorData.push_back(cubeColors[i]);
|
||||
result.ColorData.push_back(cubeColors[i]);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void VertexRenderStruct::RefreshVBO()
|
||||
{
|
||||
//Check if main thread, check if data is not empty...
|
||||
|
||||
#ifndef EMSCRIPTEN
|
||||
#if defined(_WIN32) || defined(__linux__) && !defined(__ANDROID__)
|
||||
if (!vao)
|
||||
{
|
||||
vao = std::make_shared<VAOHolder>();
|
||||
}
|
||||
glBindVertexArray(vao->getBuffer());
|
||||
#endif
|
||||
#endif
|
||||
if (!positionVBO)
|
||||
{
|
||||
positionVBO = std::make_shared<VBOHolder>();
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, positionVBO->getBuffer());
|
||||
|
||||
glBufferData(GL_ARRAY_BUFFER, data.PositionData.size() * 12, &data.PositionData[0], GL_STATIC_DRAW);
|
||||
|
||||
if (data.TexCoordData.size() > 0)
|
||||
{
|
||||
if (!texCoordVBO)
|
||||
{
|
||||
texCoordVBO = std::make_shared<VBOHolder>();
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, texCoordVBO->getBuffer());
|
||||
|
||||
glBufferData(GL_ARRAY_BUFFER, data.TexCoordData.size() * 8, &data.TexCoordData[0], GL_STATIC_DRAW);
|
||||
}
|
||||
|
||||
|
||||
if (data.NormalData.size() > 0)
|
||||
{
|
||||
if (!normalVBO)
|
||||
{
|
||||
normalVBO = std::make_shared<VBOHolder>();
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, normalVBO->getBuffer());
|
||||
|
||||
glBufferData(GL_ARRAY_BUFFER, data.NormalData.size() * 12, &data.NormalData[0], GL_STATIC_DRAW);
|
||||
}
|
||||
|
||||
if (data.TangentData.size() > 0)
|
||||
{
|
||||
if (!tangentVBO)
|
||||
{
|
||||
tangentVBO = std::make_shared<VBOHolder>();
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, tangentVBO->getBuffer());
|
||||
|
||||
glBufferData(GL_ARRAY_BUFFER, data.TangentData.size() * 12, &data.TangentData[0], GL_STATIC_DRAW);
|
||||
}
|
||||
|
||||
if (data.BinormalData.size() > 0)
|
||||
{
|
||||
if (!binormalVBO)
|
||||
{
|
||||
binormalVBO = std::make_shared<VBOHolder>();
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, binormalVBO->getBuffer());
|
||||
|
||||
glBufferData(GL_ARRAY_BUFFER, data.BinormalData.size() * 12, &data.BinormalData[0], GL_STATIC_DRAW);
|
||||
}
|
||||
|
||||
if (data.ColorData.size() > 0)
|
||||
{
|
||||
if (!colorVBO)
|
||||
{
|
||||
colorVBO = std::make_shared<VBOHolder>();
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, colorVBO->getBuffer());
|
||||
|
||||
glBufferData(GL_ARRAY_BUFFER, data.ColorData.size() * 12, &data.ColorData[0], GL_STATIC_DRAW);
|
||||
}
|
||||
}
|
||||
|
||||
void VertexDataStruct::Scale(float scale)
|
||||
{
|
||||
for (int i = 0; i < PositionData.size(); i++)
|
||||
{
|
||||
PositionData[i] = PositionData[i] * scale;
|
||||
}
|
||||
}
|
||||
void VertexDataStruct::Move(Vector3f diff)
|
||||
{
|
||||
for (int i = 0; i < PositionData.size(); i++)
|
||||
{
|
||||
PositionData[i] = PositionData[i] + diff;
|
||||
}
|
||||
}
|
||||
|
||||
void VertexDataStruct::SwapZandY()
|
||||
{
|
||||
for (int i = 0; i < PositionData.size(); i++)
|
||||
{
|
||||
auto value = PositionData[i].v[1];
|
||||
PositionData[i].v[1] = PositionData[i].v[2];
|
||||
PositionData[i].v[2] = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void VertexDataStruct::RotateByMatrix(Matrix3f m)
|
||||
{
|
||||
|
||||
for (int i = 0; i < PositionData.size(); i++)
|
||||
{
|
||||
PositionData[i] = MultVectorMatrix(PositionData[i], m);
|
||||
}
|
||||
|
||||
for (int i = 0; i < NormalData.size(); i++)
|
||||
{
|
||||
NormalData[i] = MultVectorMatrix(NormalData[i], m);
|
||||
}
|
||||
|
||||
for (int i = 0; i < TangentData.size(); i++)
|
||||
{
|
||||
TangentData[i] = MultVectorMatrix(TangentData[i], m);
|
||||
}
|
||||
|
||||
for (int i = 0; i < BinormalData.size(); i++)
|
||||
{
|
||||
BinormalData[i] = MultVectorMatrix(BinormalData[i], m);
|
||||
}
|
||||
}
|
||||
|
||||
void VertexRenderStruct::AssignFrom(const VertexDataStruct& v)
|
||||
{
|
||||
data = v;
|
||||
RefreshVBO();
|
||||
}
|
||||
|
||||
void Renderer::InitOpenGL()
|
||||
{
|
||||
ModelviewMatrixStack.push(Matrix4f::Identity());
|
||||
ProjectionMatrixStack.push(Matrix4f::Identity());
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glEnable(GL_BLEND);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
|
||||
#ifndef EMSCRIPTEN
|
||||
#if defined(_WIN32) || defined(__linux__) && !defined(__ANDROID__)
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
#endif
|
||||
#endif
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glDepthFunc(GL_LEQUAL);
|
||||
|
||||
//CheckGlError();
|
||||
}
|
||||
|
||||
void Renderer::PushProjectionMatrix(float width, float height, float zNear, float zFar)
|
||||
{
|
||||
Matrix4f m = MakeOrthoMatrix(width, height, zNear, zFar);
|
||||
ProjectionMatrixStack.push(m);
|
||||
SetMatrix();
|
||||
|
||||
if (ProjectionMatrixStack.size() > CONST_MATRIX_STACK_SIZE)
|
||||
{
|
||||
throw std::runtime_error("Projection matrix stack overflow!!!!");
|
||||
}
|
||||
}
|
||||
|
||||
void Renderer::PushPerspectiveProjectionMatrix(float fovY, float aspectRatio, float zNear, float zFar)
|
||||
{
|
||||
Matrix4f m = MakePerspectiveMatrix(fovY, aspectRatio, zNear, zFar);
|
||||
ProjectionMatrixStack.push(m);
|
||||
SetMatrix();
|
||||
|
||||
if (ProjectionMatrixStack.size() > CONST_MATRIX_STACK_SIZE)
|
||||
{
|
||||
throw std::runtime_error("Projection matrix stack overflow!!!!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Renderer::PopProjectionMatrix()
|
||||
{
|
||||
if (ProjectionMatrixStack.size() == 0)
|
||||
{
|
||||
throw std::runtime_error("Projection matrix stack underflow!!!!");
|
||||
}
|
||||
ProjectionMatrixStack.pop();
|
||||
SetMatrix();
|
||||
}
|
||||
|
||||
Matrix4f Renderer::GetProjectionModelViewMatrix()
|
||||
{
|
||||
return ProjectionModelViewMatrix;
|
||||
}
|
||||
|
||||
void Renderer::SetMatrix()
|
||||
{
|
||||
if (ProjectionMatrixStack.size() <= 0)
|
||||
{
|
||||
throw std::runtime_error("Projection matrix stack out!");
|
||||
}
|
||||
|
||||
if (ModelviewMatrixStack.size() <= 0)
|
||||
{
|
||||
throw std::runtime_error("Modelview matrix stack out!");
|
||||
}
|
||||
|
||||
ProjectionModelViewMatrix = ProjectionMatrixStack.top() * ModelviewMatrixStack.top();
|
||||
|
||||
static const std::string ProjectionModelViewMatrixName = "ProjectionModelViewMatrix";
|
||||
|
||||
//static const std::string ProjectionMatrixName = "ProjectionMatrix";
|
||||
|
||||
RenderUniformMatrix4fv(ProjectionModelViewMatrixName, false, &ProjectionModelViewMatrix.m[0]);
|
||||
|
||||
//RenderUniformMatrix4fv(ProjectionMatrixName, false, &ProjectionMatrixStack.top().m[0]);
|
||||
|
||||
}
|
||||
|
||||
void Renderer::PushMatrix()
|
||||
{
|
||||
if (ModelviewMatrixStack.size() == 0)
|
||||
{
|
||||
throw std::runtime_error("Modelview matrix stack underflow!!!!");
|
||||
}
|
||||
|
||||
ModelviewMatrixStack.push(ModelviewMatrixStack.top());
|
||||
|
||||
if (ModelviewMatrixStack.size() > CONST_MATRIX_STACK_SIZE)
|
||||
{
|
||||
throw std::runtime_error("Modelview matrix stack overflow!!!!");
|
||||
}
|
||||
}
|
||||
|
||||
void Renderer::LoadIdentity()
|
||||
{
|
||||
if (ModelviewMatrixStack.size() == 0)
|
||||
{
|
||||
throw std::runtime_error("Modelview matrix stack underflow!!!!");
|
||||
}
|
||||
|
||||
ModelviewMatrixStack.pop();
|
||||
ModelviewMatrixStack.push(Matrix4f::Identity());
|
||||
|
||||
SetMatrix();
|
||||
}
|
||||
|
||||
void Renderer::TranslateMatrix(const Vector3f& p)
|
||||
{
|
||||
|
||||
Matrix4f m = Matrix4f::Identity();
|
||||
m.m[12] = p.v[0];
|
||||
m.m[13] = p.v[1];
|
||||
m.m[14] = p.v[2];
|
||||
|
||||
m = ModelviewMatrixStack.top() * m;
|
||||
|
||||
if (ModelviewMatrixStack.size() == 0)
|
||||
{
|
||||
throw std::runtime_error("Modelview matrix stack underflow!!!!");
|
||||
}
|
||||
|
||||
ModelviewMatrixStack.pop();
|
||||
ModelviewMatrixStack.push(m);
|
||||
|
||||
SetMatrix();
|
||||
}
|
||||
|
||||
void Renderer::ScaleMatrix(float scale)
|
||||
{
|
||||
Matrix4f m = Matrix4f::Identity();
|
||||
m.m[0] = scale;
|
||||
m.m[5] = scale;
|
||||
m.m[10] = scale;
|
||||
|
||||
m = ModelviewMatrixStack.top() * m;
|
||||
|
||||
if (ModelviewMatrixStack.size() == 0)
|
||||
{
|
||||
throw std::runtime_error("Modelview matrix stack underflow!!!!");
|
||||
}
|
||||
|
||||
ModelviewMatrixStack.pop();
|
||||
ModelviewMatrixStack.push(m);
|
||||
|
||||
SetMatrix();
|
||||
}
|
||||
|
||||
void Renderer::ScaleMatrix(const Vector3f& scale)
|
||||
{
|
||||
Matrix4f m = Matrix4f::Identity();
|
||||
m.m[0] = scale.v[0];
|
||||
m.m[5] = scale.v[1];
|
||||
m.m[10] = scale.v[2];
|
||||
|
||||
m = ModelviewMatrixStack.top() * m;
|
||||
|
||||
if (ModelviewMatrixStack.size() == 0)
|
||||
{
|
||||
throw std::runtime_error("Modelview matrix stack underflow!!!!");
|
||||
}
|
||||
|
||||
ModelviewMatrixStack.pop();
|
||||
ModelviewMatrixStack.push(m);
|
||||
|
||||
SetMatrix();
|
||||
}
|
||||
|
||||
void Renderer::RotateMatrix(const Vector4f& q)
|
||||
{
|
||||
|
||||
Matrix3f m3 = QuatToMatrix(q);
|
||||
Matrix4f m = Matrix4f::Identity();
|
||||
m.m[0] = m3.m[0];
|
||||
m.m[1] = m3.m[1];
|
||||
m.m[2] = m3.m[2];
|
||||
|
||||
m.m[4] = m3.m[3];
|
||||
m.m[5] = m3.m[4];
|
||||
m.m[6] = m3.m[5];
|
||||
|
||||
m.m[8] = m3.m[6];
|
||||
m.m[9] = m3.m[7];
|
||||
m.m[10] = m3.m[8];
|
||||
|
||||
m = ModelviewMatrixStack.top() * m;
|
||||
|
||||
if (ModelviewMatrixStack.size() == 0)
|
||||
{
|
||||
throw std::runtime_error("Modelview matrix stack underflow!!!!");
|
||||
}
|
||||
|
||||
ModelviewMatrixStack.pop();
|
||||
ModelviewMatrixStack.push(m);
|
||||
|
||||
|
||||
SetMatrix();
|
||||
}
|
||||
|
||||
void Renderer::PushSpecialMatrix(const Matrix4f& m)
|
||||
{
|
||||
if (ModelviewMatrixStack.size() > 64)
|
||||
{
|
||||
throw std::runtime_error("Modelview matrix stack overflow!!!!");
|
||||
}
|
||||
ModelviewMatrixStack.push(m);
|
||||
SetMatrix();
|
||||
}
|
||||
|
||||
|
||||
void Renderer::PopMatrix()
|
||||
{
|
||||
if (ModelviewMatrixStack.size() == 0)
|
||||
{
|
||||
throw std::runtime_error("Modelview matrix stack underflow!!!!");
|
||||
}
|
||||
ModelviewMatrixStack.pop();
|
||||
|
||||
SetMatrix();
|
||||
}
|
||||
|
||||
|
||||
void Renderer::EnableVertexAttribArray(const std::string& attribName)
|
||||
{
|
||||
|
||||
auto shader = shaderManager.GetCurrentShader();
|
||||
if (shader->attribList.find(attribName) != shader->attribList.end())
|
||||
glEnableVertexAttribArray(shader->attribList[attribName]);
|
||||
}
|
||||
|
||||
void Renderer::DisableVertexAttribArray(const std::string& attribName)
|
||||
{
|
||||
auto shader = shaderManager.GetCurrentShader();
|
||||
if (shader->attribList.find(attribName) != shader->attribList.end())
|
||||
glDisableVertexAttribArray(shader->attribList[attribName]);
|
||||
}
|
||||
|
||||
|
||||
void Renderer::RenderUniformMatrix4fv(const std::string& uniformName, bool transpose, const float* value)
|
||||
{
|
||||
auto shader = shaderManager.GetCurrentShader();
|
||||
|
||||
auto uniform = shader->uniformList.find(uniformName);
|
||||
|
||||
if (uniform != shader->uniformList.end())
|
||||
{
|
||||
glUniformMatrix4fv(uniform->second, 1, transpose, value);
|
||||
}
|
||||
}
|
||||
|
||||
void Renderer::RenderUniform3fv(const std::string& uniformName, const float* value)
|
||||
{
|
||||
auto shader = shaderManager.GetCurrentShader();
|
||||
|
||||
auto uniform = shader->uniformList.find(uniformName);
|
||||
|
||||
if (uniform != shader->uniformList.end())
|
||||
{
|
||||
glUniform3fv(uniform->second, 1, value);
|
||||
}
|
||||
}
|
||||
|
||||
void Renderer::RenderUniform1i(const std::string& uniformName, const int value)
|
||||
{
|
||||
auto shader = shaderManager.GetCurrentShader();
|
||||
|
||||
auto uniform = shader->uniformList.find(uniformName);
|
||||
|
||||
if (uniform != shader->uniformList.end())
|
||||
{
|
||||
glUniform1i(uniform->second, value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Renderer::VertexAttribPointer2fv(const std::string& attribName, int stride, const char* pointer)
|
||||
{
|
||||
auto shader = shaderManager.GetCurrentShader();
|
||||
if (shader->attribList.find(attribName) != shader->attribList.end())
|
||||
glVertexAttribPointer(shader->attribList[attribName], 2, GL_FLOAT, GL_FALSE, stride, pointer);
|
||||
|
||||
}
|
||||
|
||||
void Renderer::VertexAttribPointer3fv(const std::string& attribName, int stride, const char* pointer)
|
||||
{
|
||||
|
||||
auto shader = shaderManager.GetCurrentShader();
|
||||
if (shader->attribList.find(attribName) != shader->attribList.end())
|
||||
glVertexAttribPointer(shader->attribList[attribName], 3, GL_FLOAT, GL_FALSE, stride, pointer);
|
||||
}
|
||||
|
||||
void Renderer::DrawVertexRenderStruct(const VertexRenderStruct& VertexRenderStruct)
|
||||
{
|
||||
static const std::string vNormal("vNormal");
|
||||
static const std::string vTangent("vTangent");
|
||||
static const std::string vBinormal("vBinormal");
|
||||
static const std::string vColor("vColor");
|
||||
static const std::string vTexCoord("vTexCoord");
|
||||
static const std::string vPosition("vPosition");
|
||||
|
||||
//glBindVertexArray(VertexRenderStruct.vao->getBuffer());
|
||||
|
||||
//Check if main thread, check if data is not empty...
|
||||
if (VertexRenderStruct.data.NormalData.size() > 0)
|
||||
{
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VertexRenderStruct.normalVBO->getBuffer());
|
||||
VertexAttribPointer3fv(vNormal, 0, NULL);
|
||||
}
|
||||
if (VertexRenderStruct.data.TangentData.size() > 0)
|
||||
{
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VertexRenderStruct.tangentVBO->getBuffer());
|
||||
VertexAttribPointer3fv(vTangent, 0, NULL);
|
||||
}
|
||||
if (VertexRenderStruct.data.BinormalData.size() > 0)
|
||||
{
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VertexRenderStruct.binormalVBO->getBuffer());
|
||||
VertexAttribPointer3fv(vBinormal, 0, NULL);
|
||||
}
|
||||
if (VertexRenderStruct.data.ColorData.size() > 0)
|
||||
{
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VertexRenderStruct.colorVBO->getBuffer());
|
||||
VertexAttribPointer3fv(vColor, 0, NULL);
|
||||
}
|
||||
if (VertexRenderStruct.data.TexCoordData.size() > 0)
|
||||
{
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VertexRenderStruct.texCoordVBO->getBuffer());
|
||||
VertexAttribPointer2fv(vTexCoord, 0, NULL);
|
||||
}
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VertexRenderStruct.positionVBO->getBuffer());
|
||||
VertexAttribPointer3fv(vPosition, 0, NULL);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(VertexRenderStruct.data.PositionData.size()));
|
||||
|
||||
}
|
||||
|
||||
void worldToScreenCoordinates(Vector3f objectPos,
|
||||
Matrix4f projectionModelView,
|
||||
int screenWidth, int screenHeight,
|
||||
int& screenX, int& screenY) {
|
||||
|
||||
Vector4f inx = { objectPos.v[0], objectPos.v[1], objectPos.v[2], 1.0f };
|
||||
Vector4f clipCoords = MultMatrixVector(projectionModelView, inx);
|
||||
|
||||
float ndcX = clipCoords.v[0] / clipCoords.v[3];
|
||||
float ndcY = clipCoords.v[1] / clipCoords.v[3];
|
||||
|
||||
screenX = (int)((ndcX + 1.0f) * 0.5f * screenWidth);
|
||||
screenY = (int)((1.0f + ndcY) * 0.5f * screenHeight);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
#pragma once
|
||||
|
||||
//#include "OpenGlExtensions.h"
|
||||
#include "Math.h"
|
||||
#include <exception>
|
||||
#include <stdexcept>
|
||||
#include "ShaderManager.h"
|
||||
#include <GLES2/gl2.h>
|
||||
#include <GLES2/gl2ext.h>
|
||||
|
||||
namespace ZL {
|
||||
|
||||
constexpr size_t CONST_MATRIX_STACK_SIZE = 64;
|
||||
|
||||
class VBOHolder {
|
||||
GLuint Buffer;
|
||||
|
||||
public:
|
||||
VBOHolder();
|
||||
|
||||
VBOHolder(const VBOHolder& v) = delete;
|
||||
|
||||
VBOHolder& operator=(const VBOHolder& v) = delete;
|
||||
|
||||
~VBOHolder();
|
||||
|
||||
GLuint getBuffer();
|
||||
};
|
||||
|
||||
class VAOHolder {
|
||||
GLuint vao;
|
||||
|
||||
public:
|
||||
VAOHolder();
|
||||
|
||||
VAOHolder(const VAOHolder& v) = delete;
|
||||
|
||||
VAOHolder& operator=(const VAOHolder& v) = delete;
|
||||
|
||||
~VAOHolder();
|
||||
|
||||
GLuint getBuffer();
|
||||
};
|
||||
|
||||
struct VertexDataStruct
|
||||
{
|
||||
std::vector<Vector3f> PositionData;
|
||||
std::vector<Vector2f> TexCoordData;
|
||||
std::vector<Vector3f> NormalData;
|
||||
std::vector<Vector3f> TangentData;
|
||||
std::vector<Vector3f> BinormalData;
|
||||
std::vector<Vector3f> ColorData;
|
||||
|
||||
void RotateByMatrix(Matrix3f m);
|
||||
|
||||
void Scale(float scale);
|
||||
void Move(Vector3f diff);
|
||||
void SwapZandY();
|
||||
};
|
||||
|
||||
struct VertexRenderStruct
|
||||
{
|
||||
VertexDataStruct data;
|
||||
|
||||
std::shared_ptr<VAOHolder> vao;
|
||||
std::shared_ptr<VBOHolder> positionVBO;
|
||||
std::shared_ptr<VBOHolder> texCoordVBO;
|
||||
std::shared_ptr<VBOHolder> normalVBO;
|
||||
std::shared_ptr<VBOHolder> tangentVBO;
|
||||
std::shared_ptr<VBOHolder> binormalVBO;
|
||||
std::shared_ptr<VBOHolder> colorVBO;
|
||||
void RefreshVBO();
|
||||
|
||||
void AssignFrom(const VertexDataStruct& v);
|
||||
};
|
||||
|
||||
VertexDataStruct CreateRect2D(Vector2f center, Vector2f halfWidthHeight, float zLevel);
|
||||
VertexDataStruct CreateRectHorizontalSections2D(Vector2f center, Vector2f halfWidthHeight, float zLevel, size_t sectionCount);
|
||||
VertexDataStruct CreateCube3D(float scale);
|
||||
|
||||
|
||||
class Renderer
|
||||
{
|
||||
protected:
|
||||
std::stack<Matrix4f> ProjectionMatrixStack;
|
||||
std::stack<Matrix4f> ModelviewMatrixStack;
|
||||
|
||||
Matrix4f ProjectionModelViewMatrix;
|
||||
|
||||
public:
|
||||
|
||||
ShaderManager shaderManager;
|
||||
|
||||
void InitOpenGL();
|
||||
|
||||
void PushProjectionMatrix(float width, float height, float zNear = 0.f, float zFar = 1.f);
|
||||
void PushPerspectiveProjectionMatrix(float fovY, float aspectRatio, float zNear, float zFar);
|
||||
void PopProjectionMatrix();
|
||||
|
||||
void PushMatrix();
|
||||
void LoadIdentity();
|
||||
void TranslateMatrix(const Vector3f& p);
|
||||
void ScaleMatrix(float scale);
|
||||
void ScaleMatrix(const Vector3f& scale);
|
||||
void RotateMatrix(const Vector4f& q);
|
||||
void PushSpecialMatrix(const Matrix4f& m);
|
||||
void PopMatrix();
|
||||
|
||||
|
||||
Matrix4f GetProjectionModelViewMatrix();
|
||||
|
||||
void SetMatrix();
|
||||
|
||||
|
||||
|
||||
void EnableVertexAttribArray(const std::string& attribName);
|
||||
|
||||
void DisableVertexAttribArray(const std::string& attribName);
|
||||
|
||||
|
||||
void RenderUniformMatrix4fv(const std::string& uniformName, bool transpose, const float* value);
|
||||
void RenderUniform1i(const std::string& uniformName, const int value);
|
||||
void RenderUniform3fv(const std::string& uniformName, const float* value);
|
||||
|
||||
|
||||
void VertexAttribPointer2fv(const std::string& attribName, int stride, const char* pointer);
|
||||
|
||||
void VertexAttribPointer3fv(const std::string& attribName, int stride, const char* pointer);
|
||||
|
||||
void DrawVertexRenderStruct(const VertexRenderStruct& VertexRenderStruct);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
void worldToScreenCoordinates(Vector3f objectPos,
|
||||
Matrix4f projectionModelView,
|
||||
int screenWidth, int screenHeight,
|
||||
int& screenX, int& screenY);
|
||||
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
#include <jni.h>
|
||||
#include <android/log.h>
|
||||
|
||||
extern int SDL_main(int argc, char *argv[]);
|
||||
|
||||
void Java_org_libsdl_app_SDLActivity_nativeInit(JNIEnv* env, jclass cls, jobject obj) {
|
||||
/* Run the application code! */
|
||||
SDL_main(0, NULL);
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
#include "ShaderManager.h"
|
||||
#include <iostream>
|
||||
|
||||
|
||||
namespace ZL {
|
||||
|
||||
ShaderResource::ShaderResource(const std::string& vertexCode, const std::string& fragmentCode)
|
||||
{
|
||||
|
||||
const int CONST_INFOLOG_LENGTH = 256;
|
||||
|
||||
char infoLog[CONST_INFOLOG_LENGTH];
|
||||
int infoLogLength;
|
||||
|
||||
int vertexShaderCompiled;
|
||||
int fragmentShaderCompiled;
|
||||
int programLinked;
|
||||
|
||||
GLuint vertexShader;
|
||||
GLuint fragmentShader;
|
||||
|
||||
int vertexCodeLength = static_cast<int>(strlen(vertexCode.c_str()));
|
||||
int fragmentCodeLength = static_cast<int>(strlen(fragmentCode.c_str()));
|
||||
|
||||
const char* vc = &vertexCode[0];
|
||||
const char* fc = &fragmentCode[0];
|
||||
|
||||
vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||
glShaderSource(vertexShader, 1, &(vc), &vertexCodeLength);
|
||||
|
||||
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||
glShaderSource(fragmentShader, 1, &(fc), &fragmentCodeLength);
|
||||
|
||||
glCompileShader(vertexShader);
|
||||
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &vertexShaderCompiled);
|
||||
glGetShaderInfoLog(vertexShader, CONST_INFOLOG_LENGTH, &infoLogLength, infoLog);
|
||||
|
||||
glCompileShader(fragmentShader);
|
||||
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &fragmentShaderCompiled);
|
||||
glGetShaderInfoLog(fragmentShader, CONST_INFOLOG_LENGTH, &infoLogLength, infoLog);
|
||||
|
||||
if (!vertexShaderCompiled)
|
||||
{
|
||||
throw std::runtime_error("Failed to compile vertex shader code!");
|
||||
}
|
||||
|
||||
if (!fragmentShaderCompiled)
|
||||
{
|
||||
throw std::runtime_error("Failed to compile fragment shader code!");
|
||||
}
|
||||
|
||||
shaderProgram = glCreateProgram();
|
||||
|
||||
glAttachShader(shaderProgram, vertexShader);
|
||||
glAttachShader(shaderProgram, fragmentShader);
|
||||
|
||||
glLinkProgram(shaderProgram);
|
||||
|
||||
glDeleteShader(vertexShader);
|
||||
glDeleteShader(fragmentShader);
|
||||
|
||||
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &programLinked);
|
||||
glGetProgramInfoLog(shaderProgram, CONST_INFOLOG_LENGTH, &infoLogLength, infoLog);
|
||||
|
||||
if (!programLinked)
|
||||
{
|
||||
shaderProgram = 0;
|
||||
throw std::runtime_error("Failed to link shader program!");
|
||||
}
|
||||
|
||||
|
||||
int dummySize; //Dummy
|
||||
int dummyLen; //Dummy
|
||||
GLenum dummyEnum;
|
||||
|
||||
|
||||
//================= Parsing all uniforms ================
|
||||
|
||||
int activeUniforms;
|
||||
|
||||
const int CONST_UNIFORM_NAME_LENGTH = 256;
|
||||
char uniformName[CONST_UNIFORM_NAME_LENGTH];
|
||||
|
||||
glGetProgramiv(shaderProgram, GL_ACTIVE_UNIFORMS, &activeUniforms);
|
||||
|
||||
for (int i = 0; i < activeUniforms; i++)
|
||||
{
|
||||
glGetActiveUniform(shaderProgram, i, CONST_UNIFORM_NAME_LENGTH, &dummyLen, &dummySize, &dummyEnum, uniformName);
|
||||
|
||||
uniformList[uniformName] = glGetUniformLocation(shaderProgram, uniformName);
|
||||
}
|
||||
|
||||
//================= Parsing all attributes ================
|
||||
int activeAttribs;
|
||||
|
||||
const int CONST_ATTRIB_NAME_LENGTH = 256;
|
||||
char attribName[CONST_ATTRIB_NAME_LENGTH];
|
||||
|
||||
glGetProgramiv(shaderProgram, GL_ACTIVE_ATTRIBUTES, &activeAttribs);
|
||||
|
||||
for (int i = 0; i < activeAttribs; i++)
|
||||
{
|
||||
glGetActiveAttrib(shaderProgram, i, CONST_ATTRIB_NAME_LENGTH, &dummyLen, &dummySize, &dummyEnum, attribName);
|
||||
attribList[attribName] = glGetAttribLocation(shaderProgram, attribName);
|
||||
}
|
||||
}
|
||||
|
||||
ShaderResource::~ShaderResource()
|
||||
{
|
||||
if (shaderProgram != 0)
|
||||
{
|
||||
glDeleteProgram(shaderProgram);
|
||||
|
||||
shaderProgram = 0;
|
||||
}
|
||||
}
|
||||
|
||||
GLuint ShaderResource::getShaderProgram()
|
||||
{
|
||||
return shaderProgram;
|
||||
}
|
||||
|
||||
|
||||
void ShaderManager::AddShaderFromFiles(const std::string& shaderName, const std::string& vertexShaderFileName, const std::string& fragmentShaderFileName, const std::string& ZIPFileName)
|
||||
{
|
||||
|
||||
std::string vertexShader;
|
||||
std::string fragmentShader;
|
||||
|
||||
if (!ZIPFileName.empty()){
|
||||
|
||||
std::vector<char> vertexShaderData;
|
||||
std::vector<char> fragmentShaderData;
|
||||
|
||||
vertexShaderData = readFileFromZIP(vertexShaderFileName, ZIPFileName);
|
||||
fragmentShaderData = readFileFromZIP(fragmentShaderFileName, ZIPFileName);
|
||||
|
||||
vertexShader = std::string(vertexShaderData.begin(), vertexShaderData.end());
|
||||
fragmentShader = std::string(fragmentShaderData.begin(), fragmentShaderData.end());
|
||||
|
||||
}else{
|
||||
vertexShader = readTextFile(vertexShaderFileName);
|
||||
fragmentShader = readTextFile(fragmentShaderFileName);
|
||||
}
|
||||
///std::cout << "Shader: "<< vertexShader << std::endl;
|
||||
shaderResourceMap[shaderName] = std::make_shared<ShaderResource>(vertexShader, fragmentShader);
|
||||
}
|
||||
|
||||
void ShaderManager::PushShader(const std::string& shaderName)
|
||||
{
|
||||
if (shaderStack.size() >= CONST_MAX_SHADER_STACK_SIZE)
|
||||
{
|
||||
throw std::runtime_error("Shader stack overflow!");
|
||||
}
|
||||
|
||||
if (shaderResourceMap.find(shaderName) == shaderResourceMap.end())
|
||||
{
|
||||
throw std::runtime_error("Shader does not exist!");
|
||||
}
|
||||
|
||||
shaderStack.push(shaderName);
|
||||
|
||||
glUseProgram(shaderResourceMap[shaderName]->getShaderProgram());
|
||||
}
|
||||
|
||||
|
||||
void ShaderManager::PopShader()
|
||||
{
|
||||
if (shaderStack.size() == 0)
|
||||
{
|
||||
throw std::runtime_error("Shader stack underflow!");
|
||||
}
|
||||
|
||||
shaderStack.pop();
|
||||
|
||||
if (shaderStack.size() == 0)
|
||||
{
|
||||
glUseProgram(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
glUseProgram(shaderResourceMap[shaderStack.top()]->getShaderProgram());
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<ShaderResource> ShaderManager::GetCurrentShader()
|
||||
{
|
||||
if (shaderStack.size() == 0)
|
||||
{
|
||||
throw std::runtime_error("Shader stack underflow!");
|
||||
}
|
||||
|
||||
|
||||
return shaderResourceMap[shaderStack.top()];
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
ShaderSetter::ShaderSetter(ShaderManager& inShaderManager, const std::string& shaderName)
|
||||
: shaderManager(shaderManager)
|
||||
{
|
||||
shaderManager.PushShader(shaderName);
|
||||
}
|
||||
|
||||
ShaderSetter::~ShaderSetter()
|
||||
{
|
||||
shaderManager.PopShader();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
#pragma once
|
||||
//#include "OpenGlExtensions.h"
|
||||
#include "Utils.h"
|
||||
#include <GLES2/gl2.h>
|
||||
#include <GLES2/gl2ext.h>
|
||||
|
||||
namespace ZL {
|
||||
|
||||
|
||||
constexpr size_t CONST_MAX_SHADER_STACK_SIZE = 16;
|
||||
|
||||
class ShaderResource
|
||||
{
|
||||
protected:
|
||||
GLuint shaderProgram;
|
||||
|
||||
std::unordered_map<std::string, GLuint> uniformList;
|
||||
|
||||
//std::unordered_map<std::string, std::pair<bool, size_t>> UniformList;
|
||||
std::map<std::string, GLuint> attribList;
|
||||
|
||||
|
||||
public:
|
||||
|
||||
GLuint getShaderProgram();
|
||||
|
||||
ShaderResource(const std::string& vertexCode, const std::string& fragmentCode);
|
||||
~ShaderResource();
|
||||
|
||||
public:
|
||||
friend class ShaderManager;
|
||||
friend class Renderer;
|
||||
};
|
||||
|
||||
class ShaderManager {
|
||||
protected:
|
||||
std::unordered_map<std::string, std::shared_ptr<ShaderResource>> shaderResourceMap;
|
||||
|
||||
std::stack<std::string> shaderStack;
|
||||
|
||||
public:
|
||||
void AddShaderFromFiles(const std::string& shaderName, const std::string& vertexShaderFileName, const std::string& fragmentShaderFileName, const std::string& ZIPFileName = "");
|
||||
|
||||
void PushShader(const std::string& shaderName);
|
||||
void PopShader();
|
||||
|
||||
std::shared_ptr<ShaderResource> GetCurrentShader();
|
||||
};
|
||||
|
||||
|
||||
class ShaderSetter
|
||||
{
|
||||
protected:
|
||||
|
||||
ShaderManager& shaderManager;
|
||||
|
||||
public:
|
||||
ShaderSetter(ShaderManager& inShaderManager, const std::string& shaderName);
|
||||
|
||||
~ShaderSetter();
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,390 @@
|
||||
#include "TextModel.h"
|
||||
#include <regex>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
namespace ZL
|
||||
{
|
||||
|
||||
|
||||
VertexDataStruct LoadFromTextFile(const std::string& fileName, const std::string& ZIPFileName)
|
||||
{
|
||||
VertexDataStruct result;
|
||||
std::ifstream filestream;
|
||||
std::istringstream zipStream;
|
||||
|
||||
if (!ZIPFileName.empty())
|
||||
{
|
||||
std::vector<char> fileData = readFileFromZIP(fileName, ZIPFileName);
|
||||
std::string fileContents(fileData.begin(), fileData.end());
|
||||
zipStream.str(fileContents);
|
||||
}
|
||||
else
|
||||
{
|
||||
filestream.open(fileName);
|
||||
}
|
||||
|
||||
// Создаем ссылку f на нужный поток – после этого код ниже остается без изменений
|
||||
std::istream& f = (!ZIPFileName.empty()) ? static_cast<std::istream&>(zipStream) : static_cast<std::istream&>(filestream);
|
||||
|
||||
|
||||
//Skip first 5 lines
|
||||
std::string tempLine;
|
||||
|
||||
std::getline(f, tempLine);
|
||||
|
||||
static const std::regex pattern_count(R"(\d+)");
|
||||
static const std::regex pattern_float(R"([-]?\d+\.\d+)");
|
||||
static const std::regex pattern_int(R"([-]?\d+)");
|
||||
|
||||
|
||||
std::smatch match;
|
||||
|
||||
int numberVertices;
|
||||
|
||||
if (std::regex_search(tempLine, match, pattern_count)) {
|
||||
std::string number_str = match.str();
|
||||
numberVertices = std::stoi(number_str);
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("No number found in the input string.");
|
||||
}
|
||||
|
||||
std::vector<Vector3f> vertices;
|
||||
|
||||
vertices.resize(numberVertices);
|
||||
for (int i = 0; i < numberVertices; i++)
|
||||
{
|
||||
std::getline(f, tempLine);
|
||||
|
||||
std::vector<float> floatValues;
|
||||
|
||||
auto b = tempLine.cbegin();
|
||||
auto e = tempLine.cend();
|
||||
while (std::regex_search(b, e, match, pattern_float)) {
|
||||
floatValues.push_back(std::stof(match.str()));
|
||||
b = match.suffix().first;
|
||||
}
|
||||
|
||||
vertices[i] = Vector3f{ floatValues[0], floatValues[1], floatValues[2] };
|
||||
}
|
||||
|
||||
std::cout << "UV Coordinates" << std::endl;
|
||||
|
||||
std::getline(f, tempLine); //===UV Coordinates:
|
||||
|
||||
std::getline(f, tempLine); //triangle count
|
||||
int numberTriangles;
|
||||
|
||||
if (std::regex_search(tempLine, match, pattern_count)) {
|
||||
std::string number_str = match.str();
|
||||
numberTriangles = std::stoi(number_str);
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("No number found in the input string.");
|
||||
}
|
||||
|
||||
|
||||
// Now process UVs
|
||||
std::vector<std::array<Vector2f, 3>> uvCoords;
|
||||
|
||||
uvCoords.resize(numberTriangles);
|
||||
|
||||
for (int i = 0; i < numberTriangles; i++)
|
||||
{
|
||||
std::getline(f, tempLine); //Face 0
|
||||
|
||||
int uvCount;
|
||||
std::getline(f, tempLine);
|
||||
if (std::regex_search(tempLine, match, pattern_count)) {
|
||||
std::string number_str = match.str();
|
||||
uvCount = std::stoi(number_str);
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("No number found in the input string.");
|
||||
}
|
||||
|
||||
if (uvCount != 3)
|
||||
{
|
||||
throw std::runtime_error("more than 3 uvs");
|
||||
}
|
||||
|
||||
std::vector<float> floatValues;
|
||||
|
||||
for (int j = 0; j < 3; j++)
|
||||
{
|
||||
std::getline(f, tempLine); //UV <Vector (-0.3661, -1.1665)>
|
||||
|
||||
auto b = tempLine.cbegin();
|
||||
auto e = tempLine.cend();
|
||||
floatValues.clear();
|
||||
while (std::regex_search(b, e, match, pattern_float)) {
|
||||
floatValues.push_back(std::stof(match.str()));
|
||||
b = match.suffix().first;
|
||||
}
|
||||
|
||||
if (floatValues.size() != 2)
|
||||
{
|
||||
throw std::runtime_error("more than 2 uvs---");
|
||||
}
|
||||
|
||||
uvCoords[i][j] = Vector2f{ floatValues[0],floatValues[1] };
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "Normals go" << std::endl;
|
||||
|
||||
std::getline(f, tempLine); //===Normals:
|
||||
|
||||
|
||||
std::vector<Vector3f> normals;
|
||||
|
||||
normals.resize(numberVertices);
|
||||
for (int i = 0; i < numberVertices; i++)
|
||||
{
|
||||
std::getline(f, tempLine);
|
||||
|
||||
std::vector<float> floatValues;
|
||||
|
||||
auto b = tempLine.cbegin();
|
||||
auto e = tempLine.cend();
|
||||
while (std::regex_search(b, e, match, pattern_float)) {
|
||||
floatValues.push_back(std::stof(match.str()));
|
||||
b = match.suffix().first;
|
||||
}
|
||||
|
||||
normals[i] = Vector3f{ floatValues[0], floatValues[1], floatValues[2] };
|
||||
}
|
||||
|
||||
std::cout << "Triangles go:" << std::endl;
|
||||
|
||||
std::getline(f, tempLine); //===Triangles: 3974
|
||||
|
||||
|
||||
std::vector<std::array<int, 3>> triangles;
|
||||
|
||||
triangles.resize(numberTriangles);
|
||||
for (int i = 0; i < numberTriangles; i++)
|
||||
{
|
||||
std::getline(f, tempLine);
|
||||
|
||||
std::vector<int> intValues;
|
||||
|
||||
auto b = tempLine.cbegin();
|
||||
auto e = tempLine.cend();
|
||||
while (std::regex_search(b, e, match, pattern_int)) {
|
||||
intValues.push_back(std::stoi(match.str()));
|
||||
b = match.suffix().first;
|
||||
}
|
||||
|
||||
triangles[i] = { intValues[0], intValues[1], intValues[2] };
|
||||
}
|
||||
|
||||
|
||||
std::cout << "Process vertices" << std::endl;
|
||||
|
||||
|
||||
|
||||
// Now let's process vertices
|
||||
|
||||
for (int i = 0; i < numberTriangles; i++)
|
||||
{
|
||||
|
||||
result.PositionData.push_back(vertices[triangles[i][0]]);
|
||||
result.PositionData.push_back(vertices[triangles[i][1]]);
|
||||
result.PositionData.push_back(vertices[triangles[i][2]]);
|
||||
|
||||
/*
|
||||
result.NormalData.push_back(normals[triangles[i][0]]);
|
||||
result.NormalData.push_back(normals[triangles[i][1]]);
|
||||
result.NormalData.push_back(normals[triangles[i][2]]);
|
||||
*/
|
||||
result.TexCoordData.push_back(uvCoords[i][0]);
|
||||
result.TexCoordData.push_back(uvCoords[i][1]);
|
||||
result.TexCoordData.push_back(uvCoords[i][2]);
|
||||
|
||||
}
|
||||
|
||||
//Swap from Blender format to OpenGL format
|
||||
for (int i = 0; i < result.PositionData.size(); i++)
|
||||
{
|
||||
Vector3f tempVec = result.PositionData[i];
|
||||
result.PositionData[i].v[0] = tempVec.v[1];
|
||||
result.PositionData[i].v[1] = tempVec.v[2];
|
||||
result.PositionData[i].v[2] = tempVec.v[0];
|
||||
|
||||
/*
|
||||
tempVec = result.NormalData[i];
|
||||
result.NormalData[i].v[0] = tempVec.v[1];
|
||||
result.NormalData[i].v[1] = tempVec.v[2];
|
||||
result.NormalData[i].v[2] = tempVec.v[0];*/
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
VertexDataStruct LoadFromTextFile02(const std::string& fileName, const std::string& ZIPFileName)
|
||||
{
|
||||
VertexDataStruct result;
|
||||
std::ifstream filestream;
|
||||
std::istringstream zipStream;
|
||||
|
||||
// --- 1. Открытие потока (без изменений) ---
|
||||
if (!ZIPFileName.empty())
|
||||
{
|
||||
std::vector<char> fileData = readFileFromZIP(fileName, ZIPFileName);
|
||||
std::string fileContents(fileData.begin(), fileData.end());
|
||||
zipStream.str(fileContents);
|
||||
}
|
||||
else
|
||||
{
|
||||
filestream.open(fileName);
|
||||
if (!filestream.is_open()) {
|
||||
throw std::runtime_error("Failed to open file: " + fileName);
|
||||
}
|
||||
}
|
||||
|
||||
std::istream& f = (!ZIPFileName.empty()) ? static_cast<std::istream&>(zipStream) : static_cast<std::istream&>(filestream);
|
||||
|
||||
std::string tempLine;
|
||||
std::smatch match;
|
||||
|
||||
// Обновленные регулярки
|
||||
// pattern_float стал чуть надежнее для чисел вида "0" или "-1" без точки, если вдруг Python округлит до int
|
||||
static const std::regex pattern_count(R"(\d+)");
|
||||
static const std::regex pattern_float(R"([-]?\d+(\.\d+)?)");
|
||||
static const std::regex pattern_int(R"([-]?\d+)");
|
||||
|
||||
// --- 2. Парсинг Вершин (Pos + Norm + UV) ---
|
||||
|
||||
// Ищем заголовок ===Vertices
|
||||
while (std::getline(f, tempLine)) {
|
||||
if (tempLine.find("===Vertices") != std::string::npos) break;
|
||||
}
|
||||
|
||||
int numberVertices = 0;
|
||||
if (std::regex_search(tempLine, match, pattern_count)) {
|
||||
numberVertices = std::stoi(match.str());
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("Vertices header not found or invalid.");
|
||||
}
|
||||
|
||||
// Временные буферы для хранения "уникальных" вершин перед разверткой по индексам
|
||||
std::vector<Vector3f> tempPositions(numberVertices);
|
||||
std::vector<Vector3f> tempNormals(numberVertices);
|
||||
std::vector<Vector2f> tempUVs(numberVertices);
|
||||
|
||||
for (int i = 0; i < numberVertices; i++)
|
||||
{
|
||||
std::getline(f, tempLine);
|
||||
// Строка вида: V 0: Pos(x, y, z) Norm(x, y, z) UV(u, v)
|
||||
|
||||
std::vector<float> floatValues;
|
||||
floatValues.reserve(8); // Ожидаем ровно 8 чисел (3 pos + 3 norm + 2 uv)
|
||||
|
||||
auto b = tempLine.cbegin();
|
||||
auto e = tempLine.cend();
|
||||
while (std::regex_search(b, e, match, pattern_float)) {
|
||||
floatValues.push_back(std::stof(match.str()));
|
||||
b = match.suffix().first;
|
||||
}
|
||||
|
||||
// Проверка целостности строки (ID вершины regex может поймать первым, но нас интересуют данные)
|
||||
// Обычно ID идет первым (0), потом 3+3+2 float. Итого 9 чисел, если считать ID.
|
||||
// Ваш Python пишет "V 0:", regex поймает 0. Потом 8 флоатов.
|
||||
|
||||
// Если regex ловит ID вершины как float (что вероятно), нам нужно смещение.
|
||||
// ID - floatValues[0]
|
||||
// Pos - [1], [2], [3]
|
||||
// Norm - [4], [5], [6]
|
||||
// UV - [7], [8]
|
||||
|
||||
if (floatValues.size() < 9) {
|
||||
throw std::runtime_error("Malformed vertex line at index " + std::to_string(i));
|
||||
}
|
||||
|
||||
tempPositions[i] = Vector3f{ floatValues[1], floatValues[2], floatValues[3] };
|
||||
tempNormals[i] = Vector3f{ floatValues[4], floatValues[5], floatValues[6] };
|
||||
tempUVs[i] = Vector2f{ floatValues[7], floatValues[8] };
|
||||
}
|
||||
|
||||
// --- 3. Парсинг Треугольников (Индексов) ---
|
||||
|
||||
// Пропускаем пустые строки до заголовка треугольников
|
||||
while (std::getline(f, tempLine)) {
|
||||
if (tempLine.find("===Triangles") != std::string::npos) break;
|
||||
}
|
||||
|
||||
int numberTriangles = 0;
|
||||
if (std::regex_search(tempLine, match, pattern_count)) {
|
||||
numberTriangles = std::stoi(match.str());
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("Triangles header not found.");
|
||||
}
|
||||
|
||||
// Резервируем память в result, чтобы избежать лишних аллокаций
|
||||
result.PositionData.reserve(numberTriangles * 3);
|
||||
result.NormalData.reserve(numberTriangles * 3);
|
||||
result.TexCoordData.reserve(numberTriangles * 3);
|
||||
|
||||
for (int i = 0; i < numberTriangles; i++)
|
||||
{
|
||||
std::getline(f, tempLine);
|
||||
// Строка вида: Tri: 0 1 2
|
||||
|
||||
std::vector<int> indices;
|
||||
indices.reserve(3);
|
||||
|
||||
auto b = tempLine.cbegin();
|
||||
auto e = tempLine.cend();
|
||||
while (std::regex_search(b, e, match, pattern_int)) {
|
||||
indices.push_back(std::stoi(match.str()));
|
||||
b = match.suffix().first;
|
||||
}
|
||||
|
||||
if (indices.size() != 3) {
|
||||
throw std::runtime_error("Malformed triangle line at index " + std::to_string(i));
|
||||
}
|
||||
|
||||
// --- 4. Заполнение VertexDataStruct (Flattening) ---
|
||||
// Берем данные из временных буферов по индексам и кладем в итоговый массив
|
||||
|
||||
for (int k = 0; k < 3; k++) {
|
||||
int idx = indices[k];
|
||||
result.PositionData.push_back(tempPositions[idx]);
|
||||
result.NormalData.push_back(tempNormals[idx]);
|
||||
result.TexCoordData.push_back(tempUVs[idx]);
|
||||
}
|
||||
}
|
||||
|
||||
// --- 5. Конвертация координат (Blender -> OpenGL/Engine) ---
|
||||
// Сохраняем вашу логику смены осей: X->Z, Y->X, Z->Y
|
||||
|
||||
for (size_t i = 0; i < result.PositionData.size(); i++)
|
||||
{
|
||||
Vector3f originalPos = result.PositionData[i];
|
||||
result.PositionData[i].v[0] = originalPos.v[1]; // New X = Old Y
|
||||
result.PositionData[i].v[1] = originalPos.v[2]; // New Y = Old Z
|
||||
result.PositionData[i].v[2] = originalPos.v[0]; // New Z = Old X
|
||||
|
||||
Vector3f originalNorm = result.NormalData[i];
|
||||
result.NormalData[i].v[0] = originalNorm.v[1];
|
||||
result.NormalData[i].v[1] = originalNorm.v[2];
|
||||
result.NormalData[i].v[2] = originalNorm.v[0];
|
||||
}
|
||||
|
||||
std::cout << "Model loaded: " << numberVertices << " verts, " << numberTriangles << " tris." << std::endl;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "Math.h"
|
||||
#include "Renderer.h"
|
||||
#include <unordered_map>
|
||||
|
||||
|
||||
namespace ZL
|
||||
{
|
||||
VertexDataStruct LoadFromTextFile(const std::string& fileName, const std::string& ZIPFileName = "");
|
||||
VertexDataStruct LoadFromTextFile02(const std::string& fileName, const std::string& ZIPFileName = "");
|
||||
}
|
||||
@@ -0,0 +1,285 @@
|
||||
#include "TextureManager.h"
|
||||
#ifdef PNG_ENABLED
|
||||
#include "png.h"
|
||||
#endif
|
||||
|
||||
namespace ZL
|
||||
{
|
||||
|
||||
Texture::Texture(const TextureDataStruct& texData)
|
||||
{
|
||||
|
||||
width = texData.width;
|
||||
height = texData.height;
|
||||
|
||||
glGenTextures(1, &texID);
|
||||
|
||||
if (texID == 0)
|
||||
{
|
||||
throw std::runtime_error("glGenTextures did not work");
|
||||
}
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, texID);
|
||||
|
||||
//CheckGlError();
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
|
||||
//CheckGlError();
|
||||
|
||||
//This should be only for Windows
|
||||
//glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
|
||||
|
||||
//CheckGlError();
|
||||
|
||||
if (texData.bitSize == TextureDataStruct::BS_24BIT)
|
||||
{
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, static_cast<GLsizei>(texData.width), static_cast<GLsizei>(texData.height), 0, GL_RGB, GL_UNSIGNED_BYTE, &texData.data[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, static_cast<GLsizei>(texData.width), static_cast<GLsizei>(texData.height), 0, GL_RGBA, GL_UNSIGNED_BYTE, &texData.data[0]);
|
||||
}
|
||||
|
||||
//CheckGlError();
|
||||
|
||||
}
|
||||
|
||||
Texture::~Texture()
|
||||
{
|
||||
glDeleteTextures(1, &texID);
|
||||
texID = 0;
|
||||
}
|
||||
|
||||
GLuint Texture::getTexID()
|
||||
{
|
||||
return texID;
|
||||
}
|
||||
|
||||
size_t Texture::getWidth()
|
||||
{
|
||||
return width;
|
||||
}
|
||||
|
||||
size_t Texture::getHeight()
|
||||
{
|
||||
return height;
|
||||
}
|
||||
|
||||
|
||||
|
||||
TextureDataStruct CreateTextureDataFromBmp24(const std::string& fullFileName, const std::string& ZIPFileName)
|
||||
{
|
||||
|
||||
TextureDataStruct texData;
|
||||
std::vector<char> fileArr;
|
||||
|
||||
fileArr = !ZIPFileName.empty() ? readFileFromZIP(fullFileName, ZIPFileName) : readFile(fullFileName);
|
||||
|
||||
size_t fileSize = fileArr.size();
|
||||
|
||||
if (fileSize < 22)
|
||||
{
|
||||
throw std::runtime_error("File is too short or not correct!");
|
||||
}
|
||||
|
||||
//This refers to BITMAPV5HEADER
|
||||
texData.width = *reinterpret_cast<uint32_t*>(&fileArr[18]);
|
||||
texData.height = *reinterpret_cast<uint32_t*>(&fileArr[22]);
|
||||
|
||||
texData.bitSize = TextureDataStruct::BS_24BIT;
|
||||
|
||||
size_t dataSize = texData.width * texData.height * 3;
|
||||
|
||||
texData.data.resize(dataSize);
|
||||
|
||||
size_t pos = *reinterpret_cast<uint32_t*>(&fileArr[10]);
|
||||
size_t x = 0;
|
||||
|
||||
for (size_t i = 0; i < texData.width; i++)
|
||||
for (size_t j = 0; j < texData.height; j++)
|
||||
{
|
||||
|
||||
if (pos + 3 > fileSize)
|
||||
{
|
||||
throw std::runtime_error("File is too short!");
|
||||
}
|
||||
|
||||
|
||||
x = (i * texData.height + j) + (i * texData.height + j) + (i * texData.height + j);
|
||||
|
||||
texData.data[x + 2] = fileArr[pos++];
|
||||
texData.data[x + 1] = fileArr[pos++];
|
||||
texData.data[x + 0] = fileArr[pos++];
|
||||
|
||||
}
|
||||
|
||||
return texData;
|
||||
}
|
||||
|
||||
TextureDataStruct CreateTextureDataFromBmp32(const std::string& fullFileName, const std::string& ZIPFileName)
|
||||
{
|
||||
|
||||
TextureDataStruct texData;
|
||||
std::vector<char> fileArr;
|
||||
|
||||
fileArr = !ZIPFileName.empty() ? readFileFromZIP(fullFileName, ZIPFileName) : readFile(fullFileName);
|
||||
|
||||
size_t fileSize = fileArr.size();
|
||||
|
||||
if (fileSize < 22)
|
||||
{
|
||||
throw std::runtime_error("File is too short or not correct!");
|
||||
}
|
||||
|
||||
//This refers to BITMAPV5HEADER
|
||||
texData.width = *reinterpret_cast<uint32_t*>(&fileArr[18]);
|
||||
texData.height = *reinterpret_cast<uint32_t*>(&fileArr[22]);
|
||||
|
||||
texData.bitSize = TextureDataStruct::BS_32BIT;
|
||||
|
||||
size_t dataSize = texData.width * texData.height * 4;
|
||||
|
||||
texData.data.resize(dataSize);
|
||||
|
||||
size_t pos = *reinterpret_cast<uint32_t*>(&fileArr[10]);
|
||||
size_t x = 0;
|
||||
|
||||
for (size_t i = 0; i < texData.width; i++)
|
||||
for (size_t j = 0; j < texData.height; j++)
|
||||
{
|
||||
|
||||
if (pos + 4 > fileSize)
|
||||
{
|
||||
throw std::runtime_error("File is too short!");
|
||||
}
|
||||
|
||||
|
||||
x = (i * texData.height + j) + (i * texData.height + j) + (i * texData.height + j) + (i * texData.height + j);
|
||||
|
||||
texData.data[x + 2] = fileArr[pos++];
|
||||
texData.data[x + 1] = fileArr[pos++];
|
||||
texData.data[x + 0] = fileArr[pos++];
|
||||
texData.data[x + 3] = fileArr[pos++];
|
||||
|
||||
}
|
||||
|
||||
return texData;
|
||||
}
|
||||
|
||||
#ifdef PNG_ENABLED
|
||||
|
||||
TextureDataStruct CreateTextureDataFromPng(const std::string& fullFileName)
|
||||
{
|
||||
TextureDataStruct texData;
|
||||
|
||||
FILE* file = fopen(fullFileName.c_str(), "rb");
|
||||
if (!file) {
|
||||
fclose(file);
|
||||
throw std::runtime_error("Could not open file " + fullFileName);
|
||||
}
|
||||
|
||||
png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
|
||||
if (!png) {
|
||||
fclose(file);
|
||||
throw std::runtime_error("Could not create PNG read structure");
|
||||
}
|
||||
|
||||
png_infop info = png_create_info_struct(png);
|
||||
if (!info) {
|
||||
fclose(file);
|
||||
png_destroy_read_struct(&png, nullptr, nullptr);
|
||||
throw std::runtime_error("Could not create PNG info structure");
|
||||
}
|
||||
|
||||
if (setjmp(png_jmpbuf(png))) {
|
||||
png_destroy_read_struct(&png, &info, nullptr);
|
||||
fclose(file);
|
||||
throw std::runtime_error("Error during PNG read");
|
||||
}
|
||||
|
||||
png_init_io(png, file);
|
||||
png_read_info(png, info);
|
||||
|
||||
texData.width = png_get_image_width(png, info);
|
||||
texData.height = png_get_image_height(png, info);
|
||||
png_byte color_type = png_get_color_type(png, info);
|
||||
png_byte bit_depth = png_get_bit_depth(png, info);
|
||||
|
||||
if (bit_depth == 16)
|
||||
png_set_strip_16(png);
|
||||
|
||||
if (color_type == PNG_COLOR_TYPE_PALETTE)
|
||||
png_set_palette_to_rgb(png);
|
||||
|
||||
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
|
||||
png_set_expand_gray_1_2_4_to_8(png);
|
||||
|
||||
if (png_get_valid(png, info, PNG_INFO_tRNS))
|
||||
png_set_tRNS_to_alpha(png);
|
||||
|
||||
if (color_type == PNG_COLOR_TYPE_RGB ||
|
||||
color_type == PNG_COLOR_TYPE_GRAY ||
|
||||
color_type == PNG_COLOR_TYPE_PALETTE)
|
||||
png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
|
||||
|
||||
if (color_type == PNG_COLOR_TYPE_GRAY ||
|
||||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
|
||||
png_set_gray_to_rgb(png);
|
||||
|
||||
png_read_update_info(png, info);
|
||||
|
||||
|
||||
png_bytep* row_pointers = (png_bytep*)malloc(sizeof(png_bytep) * texData.height);
|
||||
for (int y = 0; y < texData.height; y++) {
|
||||
row_pointers[y] = (png_byte*)malloc(png_get_rowbytes(png, info));
|
||||
}
|
||||
|
||||
png_read_image(png, row_pointers);
|
||||
|
||||
fclose(file);
|
||||
|
||||
bool has_alpha = (color_type & PNG_COLOR_MASK_ALPHA) || (png_get_valid(png, info, PNG_INFO_tRNS));
|
||||
|
||||
size_t dataSize;
|
||||
|
||||
if (has_alpha)
|
||||
{
|
||||
texData.bitSize = TextureDataStruct::BS_32BIT;
|
||||
}
|
||||
else
|
||||
{
|
||||
texData.bitSize = TextureDataStruct::BS_24BIT;
|
||||
}
|
||||
|
||||
int channels = has_alpha ? 4 : 3;
|
||||
|
||||
dataSize = texData.width * texData.height * channels;
|
||||
texData.data.resize(dataSize);
|
||||
|
||||
|
||||
//for (int y = 0; y < texData.height; y++) { //Go in reverse because of UV coord start point
|
||||
for (int y = texData.height-1; y >= 0; y--) {
|
||||
//png_bytep row = row_pointers[y];//Go in reverse because of UV coord start point
|
||||
png_bytep row = row_pointers[texData.height - 1 - y];
|
||||
for (int x = 0; x < texData.width; x++) {
|
||||
png_bytep px = &(row[x * 4]);
|
||||
texData.data[(y * texData.width + x) * channels + 0] = px[0]; // R
|
||||
texData.data[(y * texData.width + x) * channels + 1] = px[1]; // G
|
||||
texData.data[(y * texData.width + x) * channels + 2] = px[2]; // B
|
||||
if (has_alpha) {
|
||||
texData.data[(y * texData.width + x) * channels + 3] = px[3]; // A
|
||||
}
|
||||
}
|
||||
//free(row_pointers[y]);//Go in reverse because of UV coord start point
|
||||
free(row_pointers[texData.height - 1 - y]);//Go in reverse because of UV coord start point
|
||||
}
|
||||
free(row_pointers);
|
||||
|
||||
png_destroy_read_struct(&png, &info, nullptr);
|
||||
|
||||
return texData;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
|
||||
//#include "OpenGlExtensions.h"
|
||||
#include "Utils.h"
|
||||
#include <GLES2/gl2.h>
|
||||
#include <GLES2/gl2ext.h>
|
||||
|
||||
namespace ZL
|
||||
{
|
||||
|
||||
struct TextureDataStruct
|
||||
{
|
||||
size_t width;
|
||||
size_t height;
|
||||
std::vector<char> data;
|
||||
enum BitSize {
|
||||
BS_24BIT,
|
||||
BS_32BIT
|
||||
};
|
||||
|
||||
BitSize bitSize;
|
||||
};
|
||||
|
||||
class Texture
|
||||
{
|
||||
size_t width = 0;
|
||||
size_t height = 0;
|
||||
GLuint texID = 0;
|
||||
|
||||
public:
|
||||
|
||||
Texture(const TextureDataStruct& texData);
|
||||
|
||||
~Texture();
|
||||
|
||||
GLuint getTexID();
|
||||
|
||||
size_t getWidth();
|
||||
size_t getHeight();
|
||||
|
||||
|
||||
};
|
||||
|
||||
TextureDataStruct CreateTextureDataFromBmp24(const std::string& fullFileName, const std::string& ZIPFileName="");
|
||||
TextureDataStruct CreateTextureDataFromBmp32(const std::string& fullFileName, const std::string& ZIPFileName="");
|
||||
#ifdef PNG_ENABLED
|
||||
TextureDataStruct CreateTextureDataFromPng(const std::string& fullFileName);
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
#include "Utils.h"
|
||||
#include <cstring>
|
||||
#include <iterator>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
//#include <zip.h>
|
||||
|
||||
namespace ZL
|
||||
{
|
||||
|
||||
std::string readTextFile(const std::string& filename)
|
||||
{
|
||||
std::ifstream f(filename);
|
||||
|
||||
std::string str((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
std::vector<char> readFile(const std::string& filename)
|
||||
{
|
||||
std::ifstream file(filename, std::ios::binary);
|
||||
|
||||
file.unsetf(std::ios::skipws);
|
||||
|
||||
std::streampos fileSize;
|
||||
|
||||
file.seekg(0, std::ios::end);
|
||||
fileSize = file.tellg();
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
std::vector<char> vec;
|
||||
vec.reserve(fileSize);
|
||||
|
||||
vec.insert(vec.begin(),
|
||||
std::istream_iterator<char>(file),
|
||||
std::istream_iterator<char>());
|
||||
|
||||
return vec;
|
||||
}
|
||||
|
||||
std::vector<char> readFileFromZIP(const std::string& filename, const std::string& zipfilename) {
|
||||
/*const std::string zipPath = zipfilename;
|
||||
int zipErr;
|
||||
zip_t* archive = zip_open(zipPath.c_str(), ZIP_RDONLY, &zipErr);
|
||||
if (!archive) {
|
||||
throw std::runtime_error("Ошибка открытия ZIP: " + zipPath);
|
||||
}
|
||||
|
||||
std::string cleanFilename = filename;
|
||||
if (cleanFilename.rfind("./", 0) == 0) {
|
||||
cleanFilename = cleanFilename.substr(2);
|
||||
}
|
||||
|
||||
std::cout << "Ищем в ZIP: " << cleanFilename << std::endl;
|
||||
|
||||
zip_file_t* zipFile = zip_fopen(archive, cleanFilename.c_str(), 0);
|
||||
if (!zipFile) {
|
||||
zip_close(archive);
|
||||
throw std::runtime_error("Файл не найден в ZIP: " + cleanFilename);
|
||||
}
|
||||
|
||||
zip_stat_t fileStat;
|
||||
if (zip_stat(archive, cleanFilename.c_str(), 0, &fileStat) != 0) {
|
||||
zip_fclose(zipFile);
|
||||
zip_close(archive);
|
||||
throw std::runtime_error("Ошибка чтения ZIP-статистики.");
|
||||
}
|
||||
|
||||
std::vector<char> fileData;
|
||||
fileData.resize(fileStat.size);
|
||||
|
||||
zip_fread(zipFile, fileData.data(), fileData.size());
|
||||
|
||||
zip_fclose(zipFile);
|
||||
zip_close(archive);
|
||||
|
||||
return fileData;*/
|
||||
return {};
|
||||
}
|
||||
|
||||
bool findString(const char* in, char* list)
|
||||
{
|
||||
size_t thisLength = strlen(in);
|
||||
while (*list != 0)
|
||||
{
|
||||
size_t length = strcspn(list, " ");
|
||||
|
||||
if (thisLength == length)
|
||||
if (!strncmp(in, list, length))
|
||||
return true;
|
||||
|
||||
list += length;
|
||||
list += 1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <stack>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
namespace ZL
|
||||
{
|
||||
std::string readTextFile(const std::string& filename);
|
||||
|
||||
std::vector<char> readFile(const std::string& filename);
|
||||
|
||||
std::vector<char> readFileFromZIP(const std::string& filename, const std::string& zipfilename);
|
||||
|
||||
bool findString(const char* in, char* list);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
#include "Game.h"
|
||||
#include "Environment.h"
|
||||
|
||||
ZL::Game game;
|
||||
|
||||
#ifdef __ANDROID__
|
||||
|
||||
extern "C" int SDL_main(int argc, char* argv[]) {
|
||||
#else
|
||||
int main(int argc, char* argv[]) {
|
||||
#endif
|
||||
|
||||
#ifdef __ANDROID__
|
||||
SDL_DisplayMode displayMode;
|
||||
SDL_GetCurrentDisplayMode(0, &displayMode);
|
||||
ZL::Environment::width = displayMode.w;
|
||||
ZL::Environment::height = displayMode.h;
|
||||
#else
|
||||
constexpr int CONST_WIDTH = 1280;
|
||||
constexpr int CONST_HEIGHT = 720;
|
||||
ZL::Environment::width = CONST_WIDTH;
|
||||
ZL::Environment::height = CONST_HEIGHT;
|
||||
#endif
|
||||
|
||||
#ifdef EMSCRIPTEN
|
||||
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
|
||||
std::cerr << "SDL_Init failed: " << SDL_GetError() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
|
||||
|
||||
SDL_Window* win = SDL_CreateWindow("Space Ship Game",
|
||||
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
||||
ZL::Environment::width, ZL::Environment::height,
|
||||
SDL_WINDOW_OPENGL);
|
||||
|
||||
if (!win) {
|
||||
std::cerr << "SDL_CreateWindow failed: " << SDL_GetError() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_GLContext glContext = SDL_GL_CreateContext(win);
|
||||
if (!glContext) {
|
||||
std::cerr << "SDL_GL_CreateContext failed: " << SDL_GetError() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_GL_MakeCurrent(win, glContext);
|
||||
ZL::Environment::window = win;
|
||||
|
||||
#else
|
||||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0) {
|
||||
SDL_Log("SDL init failed: %s", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
|
||||
#ifdef __ANDROID__
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
|
||||
|
||||
ZL::Environment::window = SDL_CreateWindow(
|
||||
"Space Ship Game",
|
||||
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
||||
ZL::Environment::width, ZL::Environment::height,
|
||||
SDL_WINDOW_FULLSCREEN | SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN
|
||||
);
|
||||
#else
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||
|
||||
ZL::Environment::window = SDL_CreateWindow(
|
||||
"Space Ship Game",
|
||||
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
||||
ZL::Environment::width, ZL::Environment::height,
|
||||
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN
|
||||
);
|
||||
#endif
|
||||
|
||||
SDL_GLContext ctx = SDL_GL_CreateContext(ZL::Environment::window);
|
||||
SDL_GL_MakeCurrent(ZL::Environment::window, ctx);
|
||||
#endif
|
||||
|
||||
game.setup();
|
||||
|
||||
#ifdef EMSCRIPTEN
|
||||
emscripten_set_main_loop(MainLoop, 0, 1);
|
||||
#else
|
||||
bool running = true;
|
||||
while (running && !game.shouldExit()) {
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_QUIT) {
|
||||
running = false;
|
||||
}
|
||||
#ifdef __ANDROID__
|
||||
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_AC_BACK) {
|
||||
running = false;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
game.update();
|
||||
|
||||
#ifdef __ANDROID__
|
||||
SDL_Delay(16);
|
||||
#else
|
||||
SDL_Delay(2);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user