Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ executable("aquarium") {
"src/common/AQUARIUM_ASSERT.h",
"src/common/FPSTimer.cpp",
"src/common/FPSTimer.h",
"src/common/Path.cpp",
"src/common/Path.h",
]

deps = [
Expand Down Expand Up @@ -309,6 +311,8 @@ executable("aquarium-direct-map") {
"src/common/AQUARIUM_ASSERT.h",
"src/common/FPSTimer.cpp",
"src/common/FPSTimer.h",
"src/common/Path.cpp",
"src/common/Path.h",
]

deps = [
Expand Down
4 changes: 0 additions & 4 deletions src/aquarium-direct-map/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@
#include "common/FPSTimer.h"

#if defined(OS_WIN)
const std::string slash = "\\";
#define M_PI 3.141592653589793
#endif
#if defined(OS_MAC) || (defined(OS_LINUX) && !defined(OS_CHROMEOS))
const std::string slash = "/";
#endif

class Program;
class Scene;
Expand Down
45 changes: 4 additions & 41 deletions src/aquarium-direct-map/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <cmath>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

Expand All @@ -30,18 +29,13 @@
#include "Model.h"
#include "Program.h"
#include "common/AQUARIUM_ASSERT.h"
#include "common/Path.h"
#include "include/CmdArgsHelper.h"

#if defined(OS_WIN)
#include <Windows.h>
#include <direct.h>
#endif
#if defined(OS_MAC)
#include <mach-o/dyld.h>
#include <ctime>
#endif
#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
#include <unistd.h>
#if defined(OS_MAC) || (defined(OS_LINUX) && !defined(OS_CHROMEOS))
#include <ctime>
#endif

Expand All @@ -55,7 +49,7 @@ int clientWidth;
int clientHeight;

// Get current path of the binary
std::string mPath;
Path mPath = Path::getExecutablePath().pop().pop().pop();

// The number of fish is passed from cmd args directly
int g_numFish;
Expand Down Expand Up @@ -260,9 +254,7 @@ void initializeGlobalInfo() {

// Load json file from assets. Initialize g_sceneGroups and classify groups.
void LoadPlacement() {
std::ostringstream oss;
oss << mPath << resourceFolder << slash << "PropPlacement.js";
std::string proppath = oss.str();
Path proppath = Path(mPath).push(resourceFolder).push("PropPlacement.js");
std::ifstream PlacementStream(proppath, std::ios::in);
rapidjson::IStreamWrapper isPlacement(PlacementStream);
rapidjson::Document document;
Expand Down Expand Up @@ -332,36 +324,7 @@ void onDestroy() {
}
}

void getCurrentPath() {
// Get path of current build.
#if defined(OS_WIN)
TCHAR temp[200];
GetModuleFileName(NULL, temp, MAX_PATH);
std::wstring ws(temp);
mPath = std::string(ws.begin(), ws.end());
size_t nPos = mPath.find_last_of(slash);
mPath = mPath.substr(0, nPos) + slash + ".." + slash + ".." + slash;
#elif defined(OS_MAC)
char temp[200];
uint32_t size = sizeof(temp);
_NSGetExecutablePath(temp, &size);
mPath = std::string(temp);
int nPos = mPath.find_last_of(slash);
mPath = mPath.substr(0, nPos) + slash + ".." + slash + ".." + slash;
#elif defined(OS_LINUX) && !defined(OS_CHROMEOS)
char temp[200];
readlink("/proc/self/exe", temp, sizeof(temp));
mPath = std::string(temp);
int nPos = mPath.find_last_of(slash);
mPath = mPath.substr(0, nPos) + slash + ".." + slash + ".." + slash;
#else
ASSERT(false);
#endif
}

bool initialize(int argc, char **argv) {
getCurrentPath();

glEnable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Expand Down
6 changes: 3 additions & 3 deletions src/aquarium-direct-map/Program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
#include "build/build_config.h"

#include "common/AQUARIUM_ASSERT.h"
#include "common/Path.h"

Program::Program(const std::string &vId, const std::string &fId)
Program::Program(Path &vId, Path &fId)
: program(0u), attribLocs(), uniforms(), textureUnits() {
createProgramFromTags(vId, fId);
createSetters();
Expand All @@ -36,8 +37,7 @@ Program::~Program() {
}
}

void Program::createProgramFromTags(const std::string &vId,
const std::string &fId) {
void Program::createProgramFromTags(Path &vId, Path &fId) {
std::ifstream VertexShaderStream(vId, std::ios::in);
std::string VertexShaderCode(
(std::istreambuf_iterator<char>(VertexShaderStream)),
Expand Down
6 changes: 4 additions & 2 deletions src/aquarium-direct-map/Program.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
#include "Texture.h"
#include "Uniform.h"

class Path;

class Program {
public:
Program(const std::string &vId, const std::string &fId);
Program(Path &vId, Path &fId);
~Program();
void use();
void setUniform(const std::string &name, float v);
Expand All @@ -39,7 +41,7 @@ class Program {
GLuint getProgramId() { return program; }

private:
void createProgramFromTags(const std::string &vId, const std::string &fId);
void createProgramFromTags(Path &vId, Path &fId);
GLuint LoadProgram(const std::string &vertexShader,
const std::string &fragmentShader);
void createSetters();
Expand Down
46 changes: 20 additions & 26 deletions src/aquarium-direct-map/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <cstdio>
#include <fstream>
#include <iostream>
#include <sstream>

#include "rapidjson/document.h"
#include "rapidjson/filereadstream.h"
Expand All @@ -23,14 +22,15 @@
#include "Model.h"
#include "Program.h"
#include "common/AQUARIUM_ASSERT.h"
#include "common/Path.h"

std::vector<std::string> g_skyBoxUrls = {
"GlobeOuter_EM_positive_x.jpg", "GlobeOuter_EM_negative_x.jpg",
"GlobeOuter_EM_positive_y.jpg", "GlobeOuter_EM_negative_y.jpg",
"GlobeOuter_EM_positive_z.jpg", "GlobeOuter_EM_negative_z.jpg"};
std::vector<Path> g_skyBoxUrls = {
Path("GlobeOuter_EM_positive_x.jpg"), Path("GlobeOuter_EM_negative_x.jpg"),
Path("GlobeOuter_EM_positive_y.jpg"), Path("GlobeOuter_EM_negative_y.jpg"),
Path("GlobeOuter_EM_positive_z.jpg"), Path("GlobeOuter_EM_negative_z.jpg")};

Scene::Scene(const std::string opt_programIds[2])
: url(), models(), textureMap(), arrayMap() {
: url(Path::getVoidPath()), models(), textureMap(), arrayMap() {
programIds[0] = opt_programIds[0];
programIds[1] = opt_programIds[1];
}
Expand Down Expand Up @@ -65,26 +65,18 @@ Scene::~Scene() {
}
}

void Scene::setupSkybox(const std::string &path) {
void Scene::setupSkybox(const Path &path) {
for (auto &v : g_skyBoxUrls) {
std::ostringstream url;
url << path << resourceFolder << slash << v;

v = url.str();
v = Path(path).push(resourceFolder).push(v);
}
}

void Scene::load(const std::string &path, const std::string &name) {
std::ostringstream oss;
oss << path << resourceFolder << slash;
std::string imagePath = oss.str();
oss << name << ".js";
std::string modelPath = oss.str();
oss.str("");
oss << path << shaderFolder << slash << shaderVersion << slash;
std::string programPath = oss.str();

this->url = modelPath;
void Scene::load(const Path &path, const std::string &name) {
Path imagePath = Path(path).push(resourceFolder);
Path modelPath = Path(imagePath).push(name + ".js");
Path programPath = Path(path).push(shaderFolder).push(shaderVersion);

this->url.push(modelPath);
this->loaded = true;

std::ifstream PlacementStream(modelPath, std::ios::in);
Expand All @@ -102,7 +94,7 @@ void Scene::load(const std::string &path, const std::string &name) {
std::string image = itr->value.GetString();

if (g_textureMap.find(image) == g_textureMap.end()) {
g_textureMap[image] = new Texture(imagePath + image, true);
g_textureMap[image] = new Texture(Path(imagePath).push(image), true);
}

textureMap[name] = g_textureMap[image];
Expand Down Expand Up @@ -143,7 +135,8 @@ void Scene::load(const std::string &path, const std::string &name) {
std::string fsId;

if (textureMap.find("diffuse") == textureMap.end()) {
std::cout << "missing diffuse texture for" << url.c_str() << std::endl;
std::cout << "missing diffuse texture for" << std::string(url)
<< std::endl;
}

if (g_textureMap.find("skybox") == g_textureMap.end()) {
Expand All @@ -159,7 +152,7 @@ void Scene::load(const std::string &path, const std::string &name) {
textureMap["skybox"] = g_textureMap["skybox"];
} else if (textureMap.find("reflectionMap") != textureMap.end()) {
if (textureMap.find("normalMap") != textureMap.end()) {
std::cout << "missing normal Map for" << url.c_str() << std::endl;
std::cout << "missing normal Map for" << std::string(url) << std::endl;
}

type = "reflection";
Expand All @@ -181,7 +174,8 @@ void Scene::load(const std::string &path, const std::string &name) {
if (g_programMap.find(vsId + fsId) != g_programMap.end()) {
program = g_programMap[vsId + fsId];
} else {
program = new Program(programPath + vsId, programPath + fsId);
program = new Program(Path(programPath).push(vsId),
Path(programPath).push(fsId));
g_programMap[vsId + fsId] = program;
}

Expand Down
7 changes: 4 additions & 3 deletions src/aquarium-direct-map/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "AttribBuffer.h"
#include "Texture.h"
#include "common/Path.h"

class Model;

Expand All @@ -22,16 +23,16 @@ class Scene {
~Scene();
Scene(const std::string opt_programIds[2]);

void load(const std::string &path, const std::string &name);
void load(const Path &path, const std::string &name);
const std::vector<Model *> &getModels() const { return models; }

bool loaded;

private:
void setupSkybox(const std::string &path);
void setupSkybox(const Path &path);

std::string programIds[2];
std::string url;
Path url;
std::vector<Model *> models;
std::unordered_map<std::string, Texture *> textureMap;
std::unordered_map<std::string, const AttribBuffer *> arrayMap;
Expand Down
16 changes: 9 additions & 7 deletions src/aquarium-direct-map/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,31 @@
#include "Texture.h"

#include <iostream>
#include <string>

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

#include "common/AQUARIUM_ASSERT.h"
#include "common/Path.h"

// initializs texture 2d
Texture::Texture(const std::string &url, bool flip)
Texture::Texture(const Path &url, bool flip)
: urls(),
target(GL_TEXTURE_2D),
texture(0u),
params(),
width(0),
height(0),
flip(flip) {
std::string urlpath = url;
urls.push_back(urlpath);
urls.push_back(url);
glGenTextures(1, &texture);

uploadTextures();
}

// initializs cube map
Texture::Texture(const std::vector<std::string> &urls) : urls(urls) {
Texture::Texture(const std::vector<Path> &urls) : urls(urls) {
ASSERT(urls.size() == 6);
target = GL_TEXTURE_CUBE_MAP;
glGenTextures(1, &texture);
Expand All @@ -58,11 +59,12 @@ bool Texture::isPowerOf2(int value) {
// support 3 channel formats currently. The group is discussing on whether
// webgpu shoud support 3 channel format.
// https://github.com/gpuweb/gpuweb/issues/66#issuecomment-410021505
bool Texture::loadImageBySTB(const std::string &filename, uint8_t **pixels) {
bool Texture::loadImageBySTB(Path &filename, uint8_t **pixels) {
stbi_set_flip_vertically_on_load(flip);
*pixels = stbi_load(filename.c_str(), &width, &height, 0, 4);
*pixels = stbi_load(std::string(filename).c_str(), &width, &height, 0, 4);
if (*pixels == 0) {
std::cout << stderr << "Couldn't open input file" << filename << std::endl;
std::cout << stderr << "Couldn't open input file" << std::string(filename)
<< std::endl;
return false;
}
return true;
Expand Down
11 changes: 6 additions & 5 deletions src/aquarium-direct-map/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,31 @@
#ifndef TEXTURE_H
#define TEXTURE_H

#include <string>
#include <unordered_map>
#include <vector>

#include "glad/glad.h"

#include "common/Path.h"

class Texture {
public:
~Texture();
Texture(const std::string &url, bool flip);
Texture(const std::vector<std::string> &urls);
Texture(const Path &url, bool flip);
Texture(const std::vector<Path> &urls);

GLuint getTexture() const { return texture; }
GLenum getTarget() const { return target; }
void setTexture(GLuint texId) { texture = texId; }
bool loadImageBySTB(const std::string &filename, uint8_t **pixels);
bool loadImageBySTB(Path &filename, uint8_t **pixels);
void DestroyImageData(uint8_t *pixels);

private:
void setParameter(GLenum, GLint);
void uploadTextures();
bool isPowerOf2(int value);

std::vector<std::string> urls;
std::vector<Path> urls;
GLenum target;
GLuint texture;
std::unordered_map<GLenum, GLint> params;
Expand Down
Loading