Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanecelis/2902146 to your computer and use it in GitHub Desktop.
Save shanecelis/2902146 to your computer and use it in GitHub Desktop.
Example of using the Visualization Library without a GUI wrapper
/* visualization-library-with-sdl-example.cpp
This is an example of using the Visualization Library (VL) [1]
without using any of its GUI wrappers. It will render a spinning
cube just like the basic scene setup example[2]. However, it does
not respond to mouse events.
As a suggestion for the future work on VL, I would recommend
separating the OpenGLContext class which is required for rendering
from what is required for providing a GUI abstraction that will
handle events.
Copyright (c) 2012, Shane Celis
You can do whatever you want with this code. No warranty.
[1]: http://www.visualizationlibrary.org
[2]: http://www.visualizationlibrary.org/documentation/pag_guide_rotating_cube.html
*/
#include <SDL/SDL.h>
#include <GL/mesa_gl.h>
#include <GL/khronos_glext.h>
#include <OpenGL/gl.h>
#include <unistd.h>
#include <vlCore/VisualizationLibrary.hpp>
#include <vlCore/Time.hpp>
#include <vlGraphics/Rendering.hpp>
#include <vlGraphics/SceneManagerActorTree.hpp>
#include <vlGraphics/Geometry.hpp>
#include <vlGraphics/OpenGLContext.hpp>
#include <vlGraphics/GeometryPrimitives.hpp>
#include <vlGraphics/Light.hpp>
vl::Rendering rendering;
vl::ref<vl::SceneManagerActorTree> scene_manager;
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240
#define SCREEN_DEPTH 8
/*
DummyOpenGLContext implements the required abstract methods of
OpenGLContext. My use case is that I do not want to use VL's GUI
wrappers. I want to use VL for rendering only.
*/
class DummyOpenGLContext : public vl::OpenGLContext {
public:
DummyOpenGLContext() : OpenGLContext(SCREEN_WIDTH, SCREEN_HEIGHT) {
initGLContext();
}
virtual void swapBuffers() { }
virtual void makeCurrent() { }
virtual void update() { }
};
int main(int argc, char *argv[]) {
SDL_Surface *screen;
short done = 0;
SDL_Event event;
/* Initialize SDL. */
SDL_Init(SDL_INIT_VIDEO);
/* Initialize the screen / window. */
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_OPENGL);
vl::VisualizationLibrary::init();
/* Initialize scene. */
vl::ref<vl::Transform> cubeTransform = new vl::Transform;
rendering.transform()->addChild(cubeTransform.get());
vl::ref<vl::Geometry> cube = vl::makeBox(vl::vec3(0,0,0), 10, 10, 10);
cube->computeNormals();
vl::ref<vl::Effect> effect = new vl::Effect;
effect->shader()->enable(vl::EN_DEPTH_TEST);
effect->shader()->setRenderState( new vl::Light, 0 );
effect->shader()->enable(vl::EN_LIGHTING);
effect->shader()->gocMaterial()->setDiffuse( vl::crimson );
scene_manager = new vl::SceneManagerActorTree;
rendering.sceneManagers()->push_back(scene_manager.get());
scene_manager->tree()->addActor(cube.get(), effect.get(), cubeTransform.get());
DummyOpenGLContext glcontext;
rendering.renderer()->setFramebuffer(glcontext.framebuffer());
/* define the camera position and orientation */
vl::vec3 eye = vl::vec3(0,10,35);
vl::vec3 center = vl::vec3(0,0,0);
vl::vec3 up = vl::vec3(0,1,0);
vl::mat4 view_mat = vl::mat4::getLookAt(eye, center, up);
rendering.camera()->setViewMatrix( view_mat );
rendering.camera()->viewport()->setClearColor( vl::black );
rendering.camera()->viewport()->setWidth(SCREEN_WIDTH);
rendering.camera()->viewport()->setHeight(SCREEN_HEIGHT);
while(! done) {
vl::real degrees = vl::Time::currentTime() * 45.0f;
vl::mat4 matrix = vl::mat4::getRotation( degrees, 0,1,0 );
cubeTransform->setLocalMatrix( matrix );
rendering.render();
SDL_GL_SwapBuffers();
VL_CHECK_OGL();
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case 'q':
done = 1;
break;
}
break;
case SDL_QUIT:
done = 1;
break;
}
}
}
vl::VisualizationLibrary::shutdown();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment