GLFW3 & Noesis
Posted: 16 Jan 2013, 22:38
I've been trying to get a Noesis on GLFW3 application running without any success so far. I took the GLFW triangle.c sample from GLFW3's repository and copied some of the code from the GLUT sample in what I hope is the correct way.
The code crashes on the "Update Renderer logic". If I comment out that section, I get the spinning triangle.
error.log exists and is empty and NS.Log.<username>.log shows a bunch of asset loads. The assets are properly in place, since I have an #ifdefed alternative GLUT build with the sample program which builds and works correctly (spinning cube with GUI).
The code follows (built on VS 2012):
Any clues as to what I might be doing wrong? Also, any chance of a minimal GLFW sample with Noesis (probably 2.x)?
Thanks!
The code crashes on the "Update Renderer logic". If I comment out that section, I get the spinning triangle.
error.log exists and is empty and NS.Log.<username>.log shows a bunch of asset loads. The assets are properly in place, since I have an #ifdefed alternative GLUT build with the sample program which builds and works correctly (spinning cube with GUI).
The code follows (built on VS 2012):
Code: Select all
#include <Windows.h>
#define GLFW_INCLUDE_GLU
#include <GL/glfw3.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#pragma warning(disable: 4275)
#pragma warning(disable: 4251)
#define NOESIS_GUI
#ifdef NOESIS_GUI
#include <Noesis.h>
#include <NsCore/Kernel.h>
#include <NsCore/Error.h>
#include <NsCore/NsSystem.h>
#include <NsCore/NsConfig.h>
#include <NsRender/IRenderTarget2D.h> // < falta en IGLRenderSystem
#include <NsRender/IGLRenderSystem.h>
#include <NsResource/IResourceSystem.h>
#include <NsDrawing/IVGLSurface.h>
#include <NsDrawing/IVGLSystem.h>
#include <NsGui/IUIResource.h>
#include <NsGui/Visual.h>
#include <NsGui/IRenderer.h>
#include <NsGui/Slider.h>
#include <NsGui/ComboBox.h>
#include <NsGui/RoutedEvent.h>
#include <NsGui/VisualTreeHelper.h>
using namespace Noesis;
using namespace Noesis::Core;
using namespace Noesis::Gui;
using namespace Noesis::Resource;
using namespace Noesis::Render;
using namespace Noesis::Drawing;
Ptr<IRenderer> gUIRenderer;
Ptr<UIElement> gUIRoot;
#endif
clock_t _startclock;
//#pragma comment(lib, "GLFWDLL.lib")
#ifdef NDEBUG
#pragma comment(lib, "GLFW3.lib")
#else
#pragma comment(lib, "GLFW3_D.lib")
#endif
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
#ifdef NOESIS_GUI
#pragma comment(lib, "Noesis.lib")
#endif
#ifdef NOESIS_GUI
void ErrorHandler(const NsChar* filename, NsInt line, const NsChar* desc)
{
printf("\nERROR: %s\n\n", desc);
exit(1);
}
void Shutdown(void)
{
// Free global resources and shutdown kernel
gUIRoot.Reset();
gUIRenderer.Reset();
NsGetKernel()->Shutdown();
}
#endif
static void error_callback(int error, const char* description)
{
fprintf(stderr, "Error: %s\n", description);
}
int main(void)
{
int width, height, x;
GLFWwindow* window;
glfwSetErrorCallback(error_callback);
// Initialise GLFW
if (!glfwInit())
exit(EXIT_FAILURE);
// Open a window and create its OpenGL context
window = glfwCreateWindow(640, 480, "Spinning Triangle", NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
_startclock = clock();
// Enable vertical sync (on cards that support it)
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
#ifdef NOESIS_GUI
{
// Install an error handler to catch any problem with NoesisGui
Noesis::Core::SetErrorHandler(ErrorHandler);
// Force the OpenGL renderer
NsConfigValue("Render.RenderSystem", "Render", "GL");
// Launch Noesis Kernel
NsGetKernel()->Init();
// Create a OpenGL context that will be used by NoesisGui.
HDC hDC = wglGetCurrentDC();
IGLRenderSystem::SetContext(hDC, wglCreateContext(hDC));
NsGetKernel()->InitSystems();
// Load the .xaml resource
Ptr<IUIResource> guiResource = NsDynamicCast< Ptr<IUIResource> >(NsGetSystem<IResourceSystem>()->Load("Gui/Samples/SDKTutorial/UIgl.xaml"));
gUIRoot.Reset(NsStaticCast<UIElement*>(guiResource->GetRoot()));
// Create the UI renderer
gUIRenderer = CreateRenderer(gUIRoot.GetPtr());
gUIRenderer->SetSurfaceClearMode(Noesis::Gui::SurfaceClearMode_Manual);
gUIRenderer->SetAntialiasingMode(Noesis::Gui::AntialiasingMode_PPAA);
atexit(Shutdown);
}
#endif
// Ensure we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
for (;;)
{
double t = glfwGetTime();
glfwGetCursorPos(window, &x, NULL);
// Get window size (may be different than the requested size)
glfwGetWindowSize(window, &width, &height);
// Special case: avoid division by zero below
height = height > 0 ? height : 1;
glViewport(0, 0, width, height);
// Clear color buffer to black
glClearColor(0.f, 0.f, 0.f, 0.f);
glClear(GL_COLOR_BUFFER_BIT);
// Select and setup the projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.f, (GLfloat) width / (GLfloat) height, 1.f, 100.f);
// Select and setup the modelview matrix
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt(0.f, 1.f, 0.f, // Eye-position
0.f, 20.f, 0.f, // View-point
0.f, 0.f, 1.f); // Up-vector
// Draw a rotating colorful triangle
glTranslatef(0.f, 14.f, 0.f);
glRotatef(0.3f * (GLfloat) x + (GLfloat) t * 100.f, 0.f, 0.f, 1.f);
glBegin(GL_TRIANGLES);
glColor3f(1.f, 0.f, 0.f);
glVertex3f(-5.f, 0.f, -4.f);
glColor3f(0.f, 1.f, 0.f);
glVertex3f(5.f, 0.f, -4.f);
glColor3f(0.f, 0.f, 1.f);
glVertex3f(0.f, 0.f, 6.f);
glEnd();
#ifdef NOESIS_GUI
// Tick kernel
NsGetKernel()->Tick();
// Update renderer
gUIRenderer->Update(float(clock() - _startclock)/ 1000.0f);
// ...Do something useful here because Update() is concurrent...
RenderCommands* commands = gUIRenderer->WaitForUpdate();
// Render
glClearStencil(1);
glClear(GL_STENCIL_BUFFER_BIT);
gUIRenderer->Render(commands);
// ...Do something useful here because Render() is concurrent...
gUIRenderer->WaitForRender();
#endif
// Swap buffers
glfwSwapBuffers(window);
glfwPollEvents();
// Check if the ESC key was pressed or the window should be closed
if (glfwGetKey(window, GLFW_KEY_ESCAPE))
break;
if (glfwGetWindowParam(window, GLFW_SHOULD_CLOSE))
break;
}
// Close OpenGL window and terminate GLFW
glfwTerminate();
exit(EXIT_SUCCESS);
}
Thanks!