Added much dodgy code. Fixed bug in Android wherein if AAsset directory did not exist, a close directory call was still made (thereby exploding everything). Added bad mojo for getting user paths for OS X and Linux (USER/Library/Application Support/RtB on OS X and HOME/.RtB/ on Linux). GLSL shaders now reside in their own directories for different versions (e.g., 'data/shaders/100/default_vs.glsl'). Core now supplies getTexture and getMesh functions - these do not yet account for additions to the Asset Caches or similar, as if they successfully load once, they'll forever reference that same object. Added lots of bad stuff to the GUI, including indecision. I want GUI elements to render to a special context so that you can easily have scrolled content (or, perhaps in the future, an embedded browser for embedding HTML content). At the moment it is unused, but a RenderView is created for each GuiElement created. Ideally this RenderView FBO would only be updated whenever a change occurs to the GuiElement, so as to not consume too much. Gui rendering does exist now, although it's prone to change. RenderViews can now be created with a predefined FBO - this sets the STATIC_FBO flag which indicates not to create nor destroy any framebuffers. Additionally, Core now provides a RenderView for the default window fbo, v_fbo. Something is also distinctly screwed up with the RenderCamera/matrix/etc. - I believe an incorrect transformation is happening at some point, as the face culling seems to be reversed.
parent
12b2eed290
commit
27624848bd
|
@ -11,6 +11,7 @@ Our basic Asset class. This class exists as a container for data, but does not d
|
|||
class Asset {
|
||||
friend class AssetManager;
|
||||
friend class AssetCache;
|
||||
friend class Core; // the core sees all
|
||||
#ifdef __ANDROID__
|
||||
friend class ApkAssetCache;
|
||||
#endif
|
||||
|
|
|
@ -343,8 +343,8 @@ Asset* ApkAssetCache::getAsset(const char *filename) {
|
|||
asset->data_length = AAsset_getLength(aasset);
|
||||
asset->flags &= ~Asset::IS_NULL;
|
||||
assets->set(filename, asset);
|
||||
AAsset_close(aasset);
|
||||
}
|
||||
AAsset_close(aasset);
|
||||
}
|
||||
return asset;
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ Beyond the File Cache, there is the Live Cache. This cache is the current list o
|
|||
#include <vector>
|
||||
|
||||
class AssetManager {
|
||||
friend class Core; // so Core can access null_asset
|
||||
public:
|
||||
AssetManager();
|
||||
~AssetManager();
|
||||
|
@ -39,8 +40,9 @@ class AssetManager {
|
|||
// int saveCache(const char *cachefile);
|
||||
//
|
||||
// int loadCache(const char *cachefile)
|
||||
private:
|
||||
protected:
|
||||
Asset null_asset; // Special null asset to return from loadAsset if it could not be found
|
||||
private:
|
||||
std::vector<AssetCache*> caches; // Caches
|
||||
AssetCache live_cache; // Cache of loaded assets
|
||||
};
|
||||
|
|
123
src/Core.cpp
123
src/Core.cpp
|
@ -10,6 +10,19 @@ Core is somewhat like a service locator, but with larger engine capabilities - i
|
|||
#include "State.hpp"
|
||||
#include "MenuState.hpp"
|
||||
#include "TestState.hpp"
|
||||
// includes for getting "user" directory
|
||||
#if defined(__APPLE__) && !defined(__IPHONEOS__)
|
||||
#include <CoreServices/CoreServices.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#elif _WIN32
|
||||
#else
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
Core core;
|
||||
|
||||
|
@ -17,6 +30,7 @@ Core::Core() {
|
|||
v_window = NULL;
|
||||
v_context = 0;
|
||||
v_fbo = 0;
|
||||
window_rview = NULL;
|
||||
scene = NULL;
|
||||
asset_manager = NULL;
|
||||
}
|
||||
|
@ -94,9 +108,18 @@ int Core::initSystem() {
|
|||
glEnable(GL_DEPTH_TEST);
|
||||
//
|
||||
glDepthFunc(GL_LESS);
|
||||
// enable blending
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
// enable back-face culling
|
||||
//glEnable(GL_CULL_FACE);
|
||||
LOG(LOG_INFO) << "OpenGL Renderer: " << glGetString(GL_RENDERER) << "\nOpenGL Version: " << glGetString(GL_VERSION) << "\nGLSL Version: " << glGetString(GL_SHADING_LANGUAGE_VERSION);
|
||||
#ifdef HAVE_OPENGLES
|
||||
shader_version = "100";
|
||||
#else
|
||||
shader_version = "120";
|
||||
#endif
|
||||
|
||||
asset_manager = new AssetManager();
|
||||
// TODO: load APP cache as first cache (using ApkAssetCache in the case of Android)
|
||||
#ifdef __ANDROID__
|
||||
|
@ -111,16 +134,67 @@ int Core::initSystem() {
|
|||
asset_manager->addCache(new AssetCache("data"));
|
||||
// prefer the sandbox's Documents folder
|
||||
asset_manager->addCache(new AssetCache("../Documents"));
|
||||
#else
|
||||
#elif __APPLE__
|
||||
asset_manager->addCache(new AssetCache("data"));
|
||||
FSRef ref;
|
||||
OSType folderType = kApplicationSupportFolderType;
|
||||
char path[PATH_MAX];
|
||||
FSFindFolder(kUserDomain, folderType, kCreateFolder, &ref);
|
||||
FSRefMakePath(&ref, (UInt8*)&path, PATH_MAX);
|
||||
char user_dir[PATH_MAX];
|
||||
snprintf(user_dir, PATH_MAX, "%s/RtB", path);
|
||||
struct stat dir_check = {0};
|
||||
if (stat(user_dir, &dir_check) == -1) {
|
||||
mkdir(user_dir, 0700);
|
||||
}
|
||||
char user_data_dir[PATH_MAX];
|
||||
snprintf(user_data_dir, PATH_MAX, "%s/data", user_dir);
|
||||
if (stat(user_data_dir, &dir_check) == -1) {
|
||||
mkdir(user_data_dir, 0700);
|
||||
}
|
||||
asset_manager->addCache(new AssetCache(user_data_dir));
|
||||
#elif _WIN32
|
||||
asset_manager->addCache(new AssetCache("data"));
|
||||
#else // assume lunix
|
||||
char *path = getenv("HOME");
|
||||
if (path == NULL) path = getpwuid(getuid())->pw_dir;
|
||||
char user_dir[PATH_MAX];
|
||||
snprintf(user_dir, PATH_MAX, "%s/RtB", path);
|
||||
struct stat dir_check = {0};
|
||||
if (stat(user_dir, &dir_check) == -1) {
|
||||
mkdir(user_dir, 0700);
|
||||
}
|
||||
char user_data_dir[PATH_MAX];
|
||||
snprintf(user_data_dir, PATH_MAX, "%s/data", user_dir);
|
||||
if (stat(user_data_dir, &dir_check) == -1) {
|
||||
mkdir(user_data_dir, 0700);
|
||||
}
|
||||
asset_manager->addCache(new AssetCache("data"));
|
||||
asset_manager->addCache(new AssetCache(user_data_dir));
|
||||
#endif
|
||||
// TODO: load USER cache as second
|
||||
// TODO: add others via user pref?
|
||||
// Create our main RenderView (which simply wraps around v_fbo, v_width, and v_height)
|
||||
window_rview = new RenderView(v_fbo, v_width, v_height);
|
||||
// Create our resource management stuff
|
||||
Asset *asset = asset_manager->loadFile("textures/null.png");
|
||||
null_texture = new Texture("textures/null.png", asset->getData(), asset->getDataLength());
|
||||
null_texture->buildTexture();
|
||||
texture_table = new HashTable<Texture*>(256);
|
||||
texture_table->setnull(null_texture);
|
||||
asset->unloadData();
|
||||
asset = asset_manager->loadFile("models/null.obj");
|
||||
null_mesh = new Mesh(asset->getData(), asset->getDataLength());
|
||||
null_mesh->buildMesh();
|
||||
mesh_table = new HashTable<Mesh*>(64);
|
||||
mesh_table->setnull(null_mesh);
|
||||
asset->unloadData();
|
||||
|
||||
// FIXME: temporary location for adding cameras/etc. for testing
|
||||
// Create our basic RenderScene
|
||||
scene = new RenderScene();
|
||||
// Set up our Gui Scene (this is weird, but I didn't want Gui to own it for some reason)
|
||||
gui = new Gui();
|
||||
|
||||
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
|
||||
tick_rate = 10;
|
||||
|
@ -141,6 +215,9 @@ int Core::closeSystem() {
|
|||
// TODO: delete meshes loaded (assets) as well
|
||||
if (asset_manager != NULL) delete asset_manager;
|
||||
|
||||
// delete window RenderView
|
||||
delete window_rview;
|
||||
|
||||
SDL_GL_DeleteContext(v_context);
|
||||
SDL_DestroyWindow(v_window);
|
||||
SDL_Quit();
|
||||
|
@ -163,6 +240,7 @@ void Core::doProcess() {
|
|||
v_width = event.window.data1;
|
||||
v_height = event.window.data2;
|
||||
LOG(LOG_INFO) << "Resized to " << v_width << "x" << v_height;
|
||||
// update our RenderCamera's views
|
||||
RenderCamera *r_camera;
|
||||
RenderView *r_view;
|
||||
std::vector<RenderCamera*>::iterator camera_it, camera_end;
|
||||
|
@ -172,6 +250,10 @@ void Core::doProcess() {
|
|||
r_camera->updateProjection(v_width, v_height);
|
||||
r_view->setView(v_width, v_height);
|
||||
}
|
||||
// update GUI's size
|
||||
gui->setView(v_width, v_height);
|
||||
// update window RenderView
|
||||
window_rview->setView(v_width, v_height);
|
||||
}
|
||||
} else {
|
||||
// send the event to the GUI
|
||||
|
@ -221,6 +303,10 @@ void Core::doRender() {
|
|||
// close 'em
|
||||
glDisableVertexAttribArray(program_vp);
|
||||
}
|
||||
// Render the GUI overtop
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
gui->doRender();
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
// And update!
|
||||
SDL_GL_SwapWindow(v_window);
|
||||
}
|
||||
|
@ -237,9 +323,42 @@ int Core::getWidth() {
|
|||
int Core::getHeight() {
|
||||
return v_height;
|
||||
}
|
||||
RenderView* Core::getWindowView() {
|
||||
return window_rview;
|
||||
}
|
||||
AssetManager* Core::getAssetManager() {
|
||||
return asset_manager;
|
||||
}
|
||||
Gui* Core::getGui() {
|
||||
return gui;
|
||||
}
|
||||
Texture* Core::getTexture(const char *texture_name) {
|
||||
Texture *texture = texture_table->get(texture_name);
|
||||
if (texture == null_texture) {
|
||||
// Either the texture isn't loaded or it doesn't exist - we assume the former first
|
||||
Asset *asset_texture = asset_manager->loadFile(texture_name);
|
||||
if (!(asset_texture->flags & Asset::IS_NULL)) {
|
||||
// Asset was not null, that means we probably have a good texture!
|
||||
texture = new Texture(texture_name, asset_texture->getData(), asset_texture->getDataLength());
|
||||
asset_texture->unloadData();
|
||||
texture->buildTexture();
|
||||
texture_table->set(texture_name, texture);
|
||||
}
|
||||
}
|
||||
return texture;
|
||||
}
|
||||
Mesh* Core::getMesh(const char *mesh_name) {
|
||||
Mesh *mesh = mesh_table->get(mesh_name);
|
||||
if (mesh == null_mesh) {
|
||||
// Either the mesh isn't loaded or it doesn't exist - we assume the former first
|
||||
Asset *asset_mesh = asset_manager->loadFile(mesh_name);
|
||||
if (!(asset_mesh->flags & Asset::IS_NULL)) {
|
||||
// Asset was not null, that means we probably have a good mesh!
|
||||
mesh = new Mesh(asset_mesh->getData(), asset_mesh->getDataLength());
|
||||
asset_mesh->unloadData();
|
||||
mesh->buildMesh();
|
||||
mesh_table->set(mesh_name, mesh);
|
||||
}
|
||||
}
|
||||
return mesh;
|
||||
}
|
||||
|
|
15
src/Core.hpp
15
src/Core.hpp
|
@ -10,6 +10,8 @@ Core is somewhat like a service locator, but with larger engine capabilities - i
|
|||
#include "AssetManager.hpp"
|
||||
#include "Gui.hpp"
|
||||
#include "State.hpp"
|
||||
#include "HashTable.hpp"
|
||||
#include "Texture.hpp"
|
||||
#include <vector>
|
||||
class Core {
|
||||
public:
|
||||
|
@ -22,20 +24,33 @@ class Core {
|
|||
enum Flags { IS_RUNNING = (1 << 1) };
|
||||
//
|
||||
SDL_Window* getWindow();
|
||||
RenderView *getWindowView();
|
||||
RenderScene *getScene();
|
||||
RenderScene *getGuiScene();
|
||||
AssetManager *getAssetManager();
|
||||
Gui* getGui();
|
||||
// Audio* getAudio();
|
||||
// Net* getNet();
|
||||
Texture *getTexture(const char *texture_name);
|
||||
HashTable<Texture*> *texture_table;
|
||||
Texture *null_texture;
|
||||
//
|
||||
Mesh *getMesh(const char *mesh_name);
|
||||
HashTable<Mesh*> *mesh_table;
|
||||
Mesh *null_mesh;
|
||||
//Mesh *null_mesh;
|
||||
//
|
||||
int pushState(State *state);
|
||||
int popState();
|
||||
int getWidth();
|
||||
int getHeight();
|
||||
unsigned int flags; // Core state flags
|
||||
std::string shader_version; // shader version
|
||||
private:
|
||||
SDL_Window *v_window; // Our window
|
||||
SDL_GLContext v_context; // OpenGL context
|
||||
GLint v_fbo; // window framebuffer object
|
||||
RenderView *window_rview; // The window's renderview (v_fbo wrapper)
|
||||
int v_width; // width of our display
|
||||
int v_height; // height of our display
|
||||
int v_flags; // video flags
|
||||
|
|
112
src/Gui.cpp
112
src/Gui.cpp
|
@ -6,10 +6,67 @@ Gui.cpp/Gui.hpp provide the class responsible for creating, destroying, and hand
|
|||
#include "Gui.hpp"
|
||||
#include "Core.hpp"
|
||||
#include "Log.hpp"
|
||||
#include "RenderSet.hpp"
|
||||
#include "RenderObject.hpp"
|
||||
#include "Mesh.hpp"
|
||||
#include "Texture.hpp"
|
||||
|
||||
Gui::Gui() {
|
||||
w = core.getWidth();
|
||||
h = core.getHeight();
|
||||
scene = new RenderScene();
|
||||
camera = new RenderCamera();
|
||||
camera->setRenderMode(1); // Use orthographic mode
|
||||
camera->setPitch(270.0f); // look down
|
||||
camera->setPosition(0, 0, 0);
|
||||
camera->setNear(-1.0f);
|
||||
camera->setFar(1.0f);
|
||||
camera->setRenderFlags(RenderCamera::CLEAR_DEPTH); // only clear the depth before rendering
|
||||
camera->setRenderView(core.getWindowView()); // set the camera's render view to point to the main render view
|
||||
camera->setSize(h*2); // set our vertical size to the height of the window - this means 1 unit will correspond to 1 pixel
|
||||
camera->updateProjection(w, h); // ensure our projection is good
|
||||
camera->doRefresh(); // aaand update
|
||||
scene->addCamera(camera);
|
||||
//
|
||||
asset_manager = core.getAssetManager();
|
||||
// compile our GUI program
|
||||
std::string shader_file;
|
||||
Asset* shader_asset;
|
||||
Program* program = new Program();
|
||||
|
||||
shader_file = "shaders/" + core.shader_version + "/gui_fs.glsl";
|
||||
shader_asset = asset_manager->loadFile(shader_file.c_str());
|
||||
if (program->addShader(shader_asset->getData(), shader_asset->getDataLength(), GL_FRAGMENT_SHADER) != 0) {
|
||||
//return 1;
|
||||
}
|
||||
shader_asset->unloadData();
|
||||
shader_file = "shaders/" + core.shader_version + "/gui_vs.glsl";
|
||||
shader_asset = asset_manager->loadFile(shader_file.c_str());
|
||||
if (program->addShader(shader_asset->getData(), shader_asset->getDataLength(), GL_VERTEX_SHADER) != 0) {
|
||||
// return 1;
|
||||
}
|
||||
shader_asset->unloadData();
|
||||
if (program->doCompile() != 0) {
|
||||
// return 1;
|
||||
}
|
||||
set_basic = new RenderSet(); // create a new basic render set
|
||||
set_basic->setProgram(program); // set the set's program to gui_*
|
||||
scene->addSet(set_basic); // add set to the scene
|
||||
// load and build our basic element mesh
|
||||
Asset *asset = asset_manager->loadFile("models/plane.obj");
|
||||
element_mesh = new Mesh(asset->getData(), asset->getDataLength());
|
||||
element_mesh->buildMesh();
|
||||
asset->unloadData();
|
||||
}
|
||||
Gui::~Gui() {
|
||||
delete camera;
|
||||
delete scene;
|
||||
GuiElement *element = NULL;
|
||||
std::vector<GuiElement*>::iterator element_it, element_end;
|
||||
for (element_it = elements.begin(), element_end = elements.end(); element_it != element_end; ++element_it) {
|
||||
element = *element_it;
|
||||
delete element;
|
||||
}
|
||||
}
|
||||
/* onEvent
|
||||
This function is called by Core::doProcess and receives:
|
||||
|
@ -52,6 +109,19 @@ int Gui::onEvent(SDL_Event event) {
|
|||
return 0;
|
||||
}
|
||||
int Gui::addElement(GuiElement *element) {
|
||||
//
|
||||
element->object = new RenderObject();
|
||||
Asset *asset = asset_manager->loadFile("ui/ui_token.png");
|
||||
Texture *texture = new Texture("ui/ui_token.png", asset->getData(), asset->getDataLength());
|
||||
texture->buildTexture();
|
||||
element->object->setMesh(element_mesh);
|
||||
element->object->setTexture(texture);
|
||||
set_basic->addObject(element->object);
|
||||
// FIXME: calling this here for now
|
||||
element->setGeometry(element->x, element->y, element->w, element->h);
|
||||
|
||||
element->view = new RenderView(element->w, element->h);
|
||||
|
||||
// Get the top-most (parent) element and only add that.
|
||||
GuiElement *top_element = element->parent;
|
||||
while (top_element != NULL) {
|
||||
|
@ -62,3 +132,45 @@ int Gui::addElement(GuiElement *element) {
|
|||
LOG(LOG_INFO) << FUNC_NAME << " added element " << element->text;
|
||||
return elements.size()-1;
|
||||
}
|
||||
int Gui::setView(int width, int height) {
|
||||
w = width;
|
||||
h = height;
|
||||
camera->setSize(h*2); // update vertical size
|
||||
camera->updateProjection(w, h); // update projection
|
||||
camera->doRefresh();
|
||||
// resize and position elements
|
||||
updateElements();
|
||||
return 0;
|
||||
}
|
||||
void Gui::updateElements() {
|
||||
std::vector<GuiElement*>::iterator element_it, element_end;
|
||||
GuiElement *p_element = NULL;
|
||||
GuiElement *c_element = NULL;
|
||||
for (element_it = elements.begin(), element_end = elements.end(); element_it != element_end; ++element_it) {
|
||||
p_element = *element_it;
|
||||
float ex = p_element->x*2;
|
||||
float ey = p_element->y*2;
|
||||
float ew = p_element->w;
|
||||
float eh = p_element->h;
|
||||
if (p_element->pflags & GuiElement::TOP) {
|
||||
ey = -h+ey-eh;
|
||||
} else if (p_element->pflags & GuiElement::BOTTOM) {
|
||||
ey = h-ey+eh;
|
||||
} else if (p_element->pflags & GuiElement::VCENTER) {
|
||||
ey = ey;
|
||||
}
|
||||
if (p_element->pflags & GuiElement::LEFT) {
|
||||
ex = -w+ex-ew;
|
||||
} else if (p_element->pflags & GuiElement::RIGHT) {
|
||||
ex = w-ex+ew;
|
||||
} else if (p_element->pflags & GuiElement::HCENTER) {
|
||||
ex = ex;
|
||||
}
|
||||
p_element->object->setTranslation(ex, 0.0f, ey);
|
||||
p_element->object->calcMatrix();
|
||||
}
|
||||
}
|
||||
int Gui::doRender() {
|
||||
scene->doRender();
|
||||
return 0;
|
||||
}
|
||||
|
|
13
src/Gui.hpp
13
src/Gui.hpp
|
@ -7,6 +7,9 @@ Gui.cpp/Gui.hpp provide the class responsible for creating, destroying, and hand
|
|||
#define GUI_HPP
|
||||
#include "SDL.h"
|
||||
#include "GuiElement.hpp"
|
||||
#include "RenderScene.hpp"
|
||||
#include "RenderCamera.hpp"
|
||||
#include "AssetManager.hpp"
|
||||
#include <vector>
|
||||
class Gui {
|
||||
friend class Core;
|
||||
|
@ -14,9 +17,19 @@ class Gui {
|
|||
Gui();
|
||||
~Gui();
|
||||
int addElement(GuiElement *element);
|
||||
void updateElements();
|
||||
protected:
|
||||
int onEvent(SDL_Event event);
|
||||
int doRender();
|
||||
int setView(int width, int height);
|
||||
int w, h;
|
||||
// Scene/Camera
|
||||
RenderScene *scene;
|
||||
RenderCamera *camera;
|
||||
RenderSet *set_basic;
|
||||
private:
|
||||
std::vector<GuiElement*> elements;
|
||||
AssetManager *asset_manager;
|
||||
Mesh *element_mesh;
|
||||
};
|
||||
#endif
|
||||
|
|
|
@ -33,7 +33,7 @@ template<class T> class HashEntry {
|
|||
|
||||
template<class T> class HashTable {
|
||||
public:
|
||||
HashTable() { HashTable(128); };
|
||||
HashTable() { HashTable<T>(128); };
|
||||
HashTable(int hash_size) {
|
||||
pos = 0; // iterator pos
|
||||
size = hash_size;
|
||||
|
|
|
@ -16,8 +16,8 @@ This header file defines the RenderCamera class.
|
|||
RenderCamera::RenderCamera() {
|
||||
fov_angle = M_PI / 4.0f;
|
||||
size = 40.0f; // Default orthogonal size of 40 meters, vertically
|
||||
near = 0.1f;
|
||||
far = 512.0f;
|
||||
near = 10.0f;
|
||||
far = 100.0f;
|
||||
pitch = yaw = 0;
|
||||
render_mode = 0;
|
||||
flags = 0;
|
||||
|
@ -98,9 +98,18 @@ void RenderCamera::setYaw(float yaw_) {
|
|||
yaw = yaw_;
|
||||
flags |= DIRTY;
|
||||
}
|
||||
void RenderCamera::setSize(float s) {
|
||||
size = s;
|
||||
}
|
||||
void RenderCamera::setRenderFlags(int r_flags) {
|
||||
render_flags = r_flags;
|
||||
}
|
||||
void RenderCamera::setNear(float near_) {
|
||||
near = near_;
|
||||
}
|
||||
void RenderCamera::setFar(float far_) {
|
||||
far = far_;
|
||||
}
|
||||
/* ======== Getter functions ======== */
|
||||
RenderView* RenderCamera::getRenderView() {
|
||||
return render_view;
|
||||
|
|
|
@ -38,10 +38,13 @@ class RenderCamera {
|
|||
void setPitch(float pitch);
|
||||
float getYaw();
|
||||
void setYaw(float yaw);
|
||||
void setSize(float size);
|
||||
float getSize();
|
||||
Mat4 getModelview();
|
||||
void setRenderFlags(int r_flags);
|
||||
int getRenderFlags();
|
||||
void setNear(float near_);
|
||||
void setFar(float far_);
|
||||
protected:
|
||||
int render_mode; // 0 = perspective, 1 = orthogonal
|
||||
Vec3 position; // The camera's coordinates in 3D space
|
||||
|
|
|
@ -16,6 +16,16 @@ RenderView::RenderView(int width, int height) {
|
|||
program = NULL;
|
||||
createView(width, height);
|
||||
}
|
||||
RenderView::RenderView(GLuint fbo_, int width, int height) {
|
||||
fbo = fbo_;
|
||||
w = width;
|
||||
h = height;
|
||||
x = y = 0;
|
||||
flags = STATIC_FBO;
|
||||
flags |= ACTIVE;
|
||||
mesh = NULL;
|
||||
program = NULL;
|
||||
}
|
||||
RenderView::~RenderView() {
|
||||
destroyView();
|
||||
if (mesh != NULL) delete mesh;
|
||||
|
@ -72,7 +82,7 @@ int RenderView::createView(int width, int height) {
|
|||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
return 1;
|
||||
}
|
||||
flags |= RENDERVIEW_ACTIVE;
|
||||
flags |= ACTIVE;
|
||||
// unbind our frame buffer
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
// ================ create quad mesh
|
||||
|
@ -91,16 +101,21 @@ int RenderView::createView(int width, int height) {
|
|||
return 0;
|
||||
}
|
||||
int RenderView::destroyView() {
|
||||
if ((flags & RENDERVIEW_ACTIVE) == 0) return 1;
|
||||
if ((flags & ACTIVE) == 0) return 1;
|
||||
flags &= ~ACTIVE;
|
||||
if ((flags & STATIC_FBO)) return 2;
|
||||
glDeleteFramebuffers(1, &fbo);
|
||||
glDeleteTextures(1, &fbo_tex);
|
||||
glDeleteRenderbuffers(1, &fbo_depth);
|
||||
delete mesh;
|
||||
flags &= ~RENDERVIEW_ACTIVE;
|
||||
mesh = NULL;
|
||||
return 0;
|
||||
}
|
||||
int RenderView::setView(int width, int height) {
|
||||
if ((flags & RENDERVIEW_ACTIVE) == 0) return 1;
|
||||
if ((flags & ACTIVE) == 0) return 1;
|
||||
w = width;
|
||||
h = height;
|
||||
if ((flags & STATIC_FBO)) return 2;
|
||||
// resize depth buffer
|
||||
glBindRenderbuffer(GL_RENDERBUFFER, fbo_depth);
|
||||
#if defined(HAVE_OPENGLES)
|
||||
|
@ -114,8 +129,6 @@ int RenderView::setView(int width, int height) {
|
|||
glBindTexture(GL_TEXTURE_2D, fbo_tex);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
w = width;
|
||||
h = height;
|
||||
return 0;
|
||||
}
|
||||
int RenderView::setProgram(Program *program_) {
|
||||
|
|
|
@ -11,11 +11,15 @@ object. For all intents and purposes, it is a wrapper around a FBO.
|
|||
#include "common.hpp"
|
||||
#include "Mesh.hpp"
|
||||
#include "Program.hpp"
|
||||
#define RENDERVIEW_ACTIVE (1 << 1)
|
||||
class RenderView {
|
||||
friend class RenderScene;
|
||||
public:
|
||||
enum Flags {
|
||||
ACTIVE = (1 << 1),
|
||||
STATIC_FBO = (1 << 2)
|
||||
};
|
||||
RenderView(int width, int height);
|
||||
RenderView(GLuint fbo_, int width, int height);
|
||||
~RenderView();
|
||||
GLuint getFBO();
|
||||
GLuint getTex();
|
||||
|
@ -33,7 +37,7 @@ class RenderView {
|
|||
int w, h; // width and height of this view (also of fbo)
|
||||
int x, y; // x and y offsets of rendering to screen
|
||||
Mesh *mesh; // rendering Mesh, created as quad in CreateView
|
||||
unsigned int flags; // byte flag
|
||||
int flags; // byte flag
|
||||
Program *program; // program that this RenderView should be rendered with
|
||||
};
|
||||
#endif
|
||||
|
|
|
@ -70,8 +70,12 @@ GLuint Texture::buildTexture() {
|
|||
return 0;
|
||||
}
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
|
||||
/*glGenerateMipmap(GL_TEXTURE_2D);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);*/
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
flags |= BUILT;
|
||||
LOG(LOG_DEBUG) << FUNC_NAME << ": " << name << " built as " << texture;
|
||||
|
|
|
@ -20,11 +20,11 @@ class Texture {
|
|||
GLuint buildTexture();
|
||||
int destroyTexture();
|
||||
GLuint texture; //
|
||||
int w, h; // width and height of this image
|
||||
private:
|
||||
std::string name;
|
||||
unsigned int flags; // status flags
|
||||
SDL_Surface *image; // the SDL_Surface that contains the data for the texture - this is populated on loadImg and should be destroyed by unloadImg whenever is convenient (usually after buildTexture)
|
||||
int w, h; // width and height of this image
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
#include "GuiElement.hpp"
|
||||
#include "Core.hpp" // UGH
|
||||
#include "Gui.hpp" // UGH x2
|
||||
#include "Log.hpp"
|
||||
|
||||
GuiElement::GuiElement(const char *string, GuiElement *parent_) {
|
||||
|
@ -6,6 +8,9 @@ GuiElement::GuiElement(const char *string, GuiElement *parent_) {
|
|||
parent->addChild(this);
|
||||
type = DEFAULT;
|
||||
text.assign(string);
|
||||
object = NULL;
|
||||
view = NULL;
|
||||
pflags = 0;
|
||||
x = 0.25; y = 0.25;
|
||||
w = 0.25; h = 0.25;
|
||||
}
|
||||
|
@ -13,6 +18,9 @@ GuiElement::GuiElement(const char *string) {
|
|||
parent = NULL;
|
||||
type = DEFAULT;
|
||||
text.assign(string);
|
||||
object = NULL;
|
||||
pflags = 0;
|
||||
view = NULL;
|
||||
x = 0.25; y = 0.25;
|
||||
w = 0.25; h = 0.25;
|
||||
}
|
||||
|
@ -22,6 +30,9 @@ GuiElement::GuiElement(const char *string, int group_id, int this_id) {
|
|||
text.assign(string);
|
||||
gid = group_id;
|
||||
id = this_id;
|
||||
object = NULL;
|
||||
view = NULL;
|
||||
pflags = 0;
|
||||
x = 0.25; y = 0.25;
|
||||
w = 0.25; h = 0.25;
|
||||
}
|
||||
|
@ -30,8 +41,12 @@ GuiElement::GuiElement() {
|
|||
type = DEFAULT;
|
||||
x = 0.25; y = 0.25;
|
||||
w = 0.25; h = 0.25;
|
||||
object = NULL;
|
||||
view = NULL;
|
||||
pflags = 0;
|
||||
}
|
||||
GuiElement::~GuiElement() {
|
||||
if (view != NULL) delete view;
|
||||
}
|
||||
/* ======== Relationships ======== */
|
||||
int GuiElement::addChild(GuiElement *child) {
|
||||
|
@ -63,3 +78,25 @@ GuiElement* GuiElement::getHit(float xhit, float yhit) {
|
|||
}
|
||||
return element;
|
||||
}
|
||||
/* ======== Render Modifications ======== */
|
||||
int GuiElement::setGeometry(float x_, float y_, float w_, float h_) {
|
||||
x = x_; y = y_;
|
||||
w = w_; h = h_;
|
||||
if (object == NULL) return 1;
|
||||
object->setScale(w, 1.0, h);
|
||||
//object->setTranslation(x, 0.0, y);
|
||||
object->calcMatrix();
|
||||
return 0;
|
||||
}
|
||||
int GuiElement::setPFlags(int pflags_) {
|
||||
pflags = pflags_;
|
||||
return 0;
|
||||
}
|
||||
/* ======== Render, eugh ======== */
|
||||
int GuiElement::doRender() {
|
||||
// TEMP: this is gross - Gui should just manage us, but this almost makes more sense..
|
||||
Gui *gui = core.getGui();
|
||||
if (gui == NULL) return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -7,6 +7,8 @@ This file describes the GuiElement class. This is the base class for further GUI
|
|||
#define GUIELEMENT_HPP
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "RenderObject.hpp"
|
||||
#include "RenderView.hpp"
|
||||
class GuiElement {
|
||||
friend class Gui;
|
||||
public:
|
||||
|
@ -18,23 +20,36 @@ class GuiElement {
|
|||
TEXTAREA,
|
||||
CHECKBOX
|
||||
};
|
||||
enum Position {
|
||||
VCENTER = (1 << 1),
|
||||
HCENTER = (1 << 2),
|
||||
LEFT = (1 << 3),
|
||||
RIGHT = (1 << 4),
|
||||
TOP = (1 << 5),
|
||||
BOTTOM = (1 << 6)
|
||||
};
|
||||
GuiElement();
|
||||
GuiElement(const char *string);
|
||||
GuiElement(const char *string, GuiElement *parent_);
|
||||
GuiElement(const char *string, int group_id, int this_id);
|
||||
~GuiElement();
|
||||
//
|
||||
// int setGeometry(float x, float y, float w, float h);
|
||||
int setGeometry(float x, float y, float w, float h);
|
||||
// int setText(const char *string);
|
||||
GuiElement *getHit(float xhit, float yhit);
|
||||
int addChild(GuiElement *child);
|
||||
int setPFlags(int pflags);
|
||||
virtual int doRender();
|
||||
protected:
|
||||
std::vector<GuiElement*> children; // children of this element
|
||||
RenderView *view; // Ugh, we use FBOs for GUI elements cuz otherwise it's too annoying.
|
||||
GuiElement *parent; // parent of this element
|
||||
std::string text; // text of this element
|
||||
int type; // flag for this type
|
||||
int pflags; // positioning flags
|
||||
int gid; // group id for this element
|
||||
int id; // id for this element
|
||||
RenderObject *object;// render object, as set by manager class, Gui
|
||||
//
|
||||
float x, y, w, h; // position and dimensions for this element
|
||||
};
|
||||
|
|
|
@ -98,14 +98,16 @@ int TestState::onInit() {
|
|||
// TEMP: our framebuffer rendering program
|
||||
Program *program = new Program();
|
||||
// FIXME: check for GLSL version and automagically load the appropriate shader file
|
||||
#ifdef HAVE_OPENGLES
|
||||
Asset *shader_asset = asset_manager->loadFile("shaders/fb_fs.100.glsl");
|
||||
std::string shader_file;
|
||||
shader_file = "shaders/" + core.shader_version + "/fb_fs.glsl";
|
||||
Asset *shader_asset = asset_manager->loadFile(shader_file.c_str());
|
||||
if (program->addShader(shader_asset->getData(), shader_asset->getDataLength(), GL_FRAGMENT_SHADER) != 0) {
|
||||
LOG(LOG_ERROR) << "Failed to add Shader!";
|
||||
return 1;
|
||||
}
|
||||
shader_asset->unloadData();
|
||||
shader_asset = asset_manager->loadFile("shaders/fb_vs.100.glsl");
|
||||
shader_file = "shaders/" + core.shader_version + "/fb_vs.glsl";
|
||||
shader_asset = asset_manager->loadFile(shader_file.c_str());
|
||||
if (program->addShader(shader_asset->getData(), shader_asset->getDataLength(), GL_VERTEX_SHADER) != 0) {
|
||||
LOG(LOG_ERROR) << "Failed to add Shader!";
|
||||
return 1;
|
||||
|
@ -117,13 +119,15 @@ int TestState::onInit() {
|
|||
}
|
||||
// TEMP: our model rendering program
|
||||
Program *program_model = new Program();
|
||||
shader_asset = asset_manager->loadFile("shaders/default_fs.100.glsl");
|
||||
shader_file = "shaders/" + core.shader_version + "/default_fs.glsl";
|
||||
shader_asset = asset_manager->loadFile(shader_file.c_str());
|
||||
if (program_model->addShader(shader_asset->getData(), shader_asset->getDataLength(), GL_FRAGMENT_SHADER) != 0) {
|
||||
LOG(LOG_ERROR) << "Failed to add Shader!";
|
||||
return 1;
|
||||
}
|
||||
shader_asset->unloadData();
|
||||
shader_asset = asset_manager->loadFile("shaders/default_vs.100.glsl");
|
||||
shader_file = "shaders/" + core.shader_version + "/default_vs.glsl";
|
||||
shader_asset = asset_manager->loadFile(shader_file.c_str());
|
||||
if (program_model->addShader(shader_asset->getData(), shader_asset->getDataLength(), GL_VERTEX_SHADER) != 0) {
|
||||
LOG(LOG_ERROR) << "Failed to add Shdaer!";
|
||||
return 1;
|
||||
|
@ -133,42 +137,6 @@ int TestState::onInit() {
|
|||
LOG(LOG_ERROR) << "Failed to compile GLSL Program!";
|
||||
return 1;
|
||||
}
|
||||
#else
|
||||
Asset *shader_asset = asset_manager->loadFile("shaders/fb_fs.glsl");
|
||||
if (program->addShader(shader_asset->getData(), shader_asset->getDataLength(), GL_FRAGMENT_SHADER) != 0) {
|
||||
LOG(LOG_ERROR) << "Failed to add Shader!";
|
||||
return 1;
|
||||
}
|
||||
shader_asset->unloadData();
|
||||
shader_asset = asset_manager->loadFile("shaders/fb_vs.glsl");
|
||||
if (program->addShader(shader_asset->getData(), shader_asset->getDataLength(), GL_VERTEX_SHADER) != 0) {
|
||||
LOG(LOG_ERROR) << "Failed to add Shader!";
|
||||
return 1;
|
||||
}
|
||||
shader_asset->unloadData();
|
||||
if (program->doCompile() != 0) {
|
||||
LOG(LOG_ERROR) << "Failed to compile GLSL Program!";
|
||||
return 1;
|
||||
}
|
||||
// TEMP: our model rendering program
|
||||
Program *program_model = new Program();
|
||||
shader_asset = asset_manager->loadFile("shaders/default_fs.glsl");
|
||||
if (program_model->addShader(shader_asset->getData(), shader_asset->getDataLength(), GL_FRAGMENT_SHADER) != 0) {
|
||||
LOG(LOG_ERROR) << "Failed to add Shader!";
|
||||
return 1;
|
||||
}
|
||||
shader_asset->unloadData();
|
||||
shader_asset = asset_manager->loadFile("shaders/default_vs.glsl");
|
||||
if (program_model->addShader(shader_asset->getData(), shader_asset->getDataLength(), GL_VERTEX_SHADER) != 0) {
|
||||
LOG(LOG_ERROR) << "Failed to add Shader!";
|
||||
return 1;
|
||||
}
|
||||
shader_asset->unloadData();
|
||||
if (program_model->doCompile() != 0) {
|
||||
LOG(LOG_ERROR) << "Failed to compile GLSL Program!";
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
scene = core.getScene();
|
||||
int width = core.getWidth();
|
||||
int height = core.getHeight();
|
||||
|
@ -193,32 +161,24 @@ int TestState::onInit() {
|
|||
Asset *asset_texture;
|
||||
|
||||
RenderObject *objerct = new RenderObject();
|
||||
asset_mesh = asset_manager->loadFile("models/cube.obj");
|
||||
Mesh *mersh = new Mesh(asset_mesh->getData(), asset_mesh->getDataLength());
|
||||
asset_texture = asset_manager->loadFile("textures/cube.png");
|
||||
Texture *texture = new Texture("textures/cube.png", asset_texture->getData(), asset_texture->getDataLength());
|
||||
texture->buildTexture();
|
||||
asset_texture->unloadData();
|
||||
asset_mesh->unloadData();
|
||||
mersh->buildMesh();
|
||||
objerct->setMesh(mersh);
|
||||
objerct->setTexture(texture);
|
||||
sert->addObject(objerct);
|
||||
|
||||
objerct = new RenderObject();
|
||||
asset_mesh = asset_manager->loadFile("models/chest.obj");
|
||||
mersh = new Mesh(asset_mesh->getData(), asset_mesh->getDataLength());
|
||||
asset_mesh->unloadData();
|
||||
mersh->buildMesh();
|
||||
objerct->setMesh(mersh);
|
||||
objerct->setTranslation(-5.0f, 0.0f, 5.0f);
|
||||
objerct->calcMatrix();
|
||||
objerct->setMesh(core.getMesh("models/cube.obj"));
|
||||
objerct->setTexture(core.getTexture("textures/cube.png"));
|
||||
sert->addObject(objerct);
|
||||
|
||||
scene->addSet(sert);
|
||||
|
||||
// GUI testing
|
||||
gui->addElement(new GuiElement("test", 10, 1));
|
||||
GuiElement *element = new GuiElement("test", 10, 1);
|
||||
element->setPFlags(GuiElement::VCENTER|GuiElement::HCENTER);
|
||||
element->setGeometry(0.0f, 0.0f, 128.0f, 128.0f);
|
||||
gui->addElement(element);
|
||||
element = new GuiElement("test2", 10, 2);
|
||||
element->setPFlags(GuiElement::RIGHT|GuiElement::TOP);
|
||||
element->setGeometry(256.0f, 128.0f, 128.0f, 128.0f);
|
||||
gui->addElement(element);
|
||||
|
||||
// manually update GUI
|
||||
gui->updateElements();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue