Browse Source

poking at renderer interface in assimp_loading example

testing
cinnaboot 6 years ago
parent
commit
d3e6dc1eba
  1. 38
      examples/assimp_loading/main.cpp
  2. 6
      src/renderer.cpp

38
examples/assimp_loading/main.cpp

@ -1,9 +1,11 @@
#include <SDL2/SDL.h>
#include <glm/glm.hpp>
#include "camera.h"
#include "dumbLog.h"
#include "renderer.h"
#include "mesh.h"
#include "entity.h"
typedef void (*frame_callback_fn) (render_state*);
@ -14,8 +16,9 @@ doFrameCallback(render_state* rs)
}
void
doRenderLoop(render_state* rs, frame_callback_fn callback_fn)
doRenderLoop(render_state* rs, frame_callback_fn callback_fn, Entity* entities, uint entity_count)
{
// TODO: move frame delay to renderFrame()
const uint TARGET_FPS = 60;
const uint FRAME_DELAY = 1000 / TARGET_FPS;
uint frameStart, frameTime;
@ -25,6 +28,7 @@ doRenderLoop(render_state* rs, frame_callback_fn callback_fn)
while (running) {
frameStart = SDL_GetTicks();
// TODO: add a better structure for input, and provide a callback to use here
while (SDL_PollEvent(&e)) {
if ((e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_ESCAPE)
|| e.type == SDL_QUIT) {
@ -33,7 +37,8 @@ doRenderLoop(render_state* rs, frame_callback_fn callback_fn)
}
callback_fn(rs);
renRenderFrame(rs, nullptr, 0);
// TODO: entity pointer and entity count should be stored somewhere on the render_state object
renRenderFrame(rs, entities, entity_count);
SDL_GL_SwapWindow(rs->handles.window);
frameTime = SDL_GetTicks() - frameStart;
@ -46,15 +51,34 @@ int
main()
{
render_state* rs = renInit();
meInitAssimp();
meMeshGroup mg;
if (meLoadFromFile(mg, "../data", "../data/spaceship.glb")) {
doRenderLoop(rs, doFrameCallback);
// TODO: entInit() doesn't allocate memory because we want to do the allocation in
// one big block, but this is really clunky to use
Entity spaceship;
// TODO: this should be handled in entInit
// TODO: utilCopyCStr() can fail, and returns false
utilCopyCStr(spaceship.model_filename, "../data/spaceship.glb", 256);
entSetWorldPosition(spaceship, 0, 0, 0);
// set entity scale, rotation, world_transform to defaults :( ...
// need to finish loading binary texture data in mesh::loadDiffuseTexture()
// setup camera
glm::vec3 cam_pos(0, -100, 100);
cameraInitPerspective(rs->cam, cam_pos, spaceship.translation, rs->cam.world_up);
if (entInit(spaceship, "../data", rs->default_shader)) {
doRenderLoop(rs, doFrameCallback, &spaceship, 1);
} else {
LOG(Error) << "Error loading mesh, exiting\n";
LOG(Error) << "Error initializing entity, exiting\n";
}
// TODO: way more cleanup to do :(, see hexgame::cleanUp()
meShutdownAssimp();
return 0;
}

6
src/renderer.cpp

@ -30,7 +30,7 @@ void openglDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity,
GLsizei length, const GLchar* message, const void* userParam);
bool initSDL(SDL_Handles& handles);
bool createWindow(SDL_Handles& handles, v2i& viewport_dims);
bool initGlContext(SDL_Handles& handles);
bool initContext(SDL_Handles& handles);
bool initGlOptions();
bool initDebugRenderGroup(render_state* rs);
void setDefaults(render_state* rs);
@ -46,7 +46,7 @@ renInit()
if (initSDL(rs->handles) &&
createWindow(rs->handles, rs->viewport_dims) &&
initGlContext(rs->handles) &&
initContext(rs->handles) &&
initGlOptions() &&
rgInitShaderProgram(rs->default_shader, DEFAULT_VERTEX_SHADER, DEFAULT_FRAGMENT_SHADER) &&
initDebugRenderGroup(rs) )
@ -167,7 +167,7 @@ createWindow(SDL_Handles& handles, v2i& viewport_dims)
}
bool
initGLContext(SDL_Handles& handles)
initContext(SDL_Handles& handles)
{
handles.glContext = SDL_GL_CreateContext(handles.window);

Loading…
Cancel
Save