Giter Club home page Giter Club logo

Comments (11)

AlwaysGeeky avatar AlwaysGeeky commented on June 20, 2024 2

Just wondering if this issue was ever solved? I have very much the same problem...

Do you have any examples of anyone or another project that is able to get this working, by also handling GLFW and GLEW themselves?

from nanogui.

wjakob avatar wjakob commented on June 20, 2024

maybe you didn't initialize GLFW?

from nanogui.

Krystex avatar Krystex commented on June 20, 2024

No, I definately initialized it and worked with it.
Sample code:

#include <GLFW/glfw3.h>
#include <stdlib.h>
#include <stdio.h>
#include <nanogui/nanogui.h>
static void error_callback(int error, const char* description)
{
    fputs(description, stderr);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);
}
int main(void)
{
    GLFWwindow* window;
    glfwSetErrorCallback(error_callback);
    if (!glfwInit())
        exit(EXIT_FAILURE);
    window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    // Here it begins
    int testvariable = 5;
    using namespace nanogui;
    Screen *screen = new Screen();

    screen->initialize(window, true); // <- and it crashes right here
    FormHelper *gui = new FormHelper(screen);
    ref<Window> wnd = gui->addWindow(Eigen::Vector2i(10, 10), "Form helper example");
    gui->addGroup("Basic types");
    gui->addVariable("bool", testvariable);
    screen->setVisible(true);
    screen->performLayout();
    wnd->center();

    // ---
    glfwMakeContextCurrent(window);
    glfwSwapInterval(1);
    glfwSetKeyCallback(window, key_callback);
    while (!glfwWindowShouldClose(window))
    {
        float ratio;
        int width, height;
        glfwGetFramebufferSize(window, &width, &height);
        ratio = width / (float)height;
        glViewport(0, 0, width, height);
        glClear(GL_COLOR_BUFFER_BIT);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glRotatef((float)glfwGetTime() * 50.f, 0.f, 0.f, 1.f);
        glBegin(GL_TRIANGLES);
        glColor3f(1.f, 0.f, 0.f);
        glVertex3f(-0.6f, -0.4f, 0.f);
        glColor3f(0.f, 1.f, 0.f);
        glVertex3f(0.6f, -0.4f, 0.f);
        glColor3f(0.f, 0.f, 1.f);
        glVertex3f(0.f, 0.6f, 0.f);
        glEnd();
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwDestroyWindow(window);
    glfwTerminate();
    exit(EXIT_SUCCESS);
}

I also tried to put the code right before the main loop, and even there it crashes.

from nanogui.

wjakob avatar wjakob commented on June 20, 2024

sorry -- I meant GLEW, not GLFW. Looks like you did not initialize it -- see the code of screen.cpp to see how to do it.

from nanogui.

Krystex avatar Krystex commented on June 20, 2024

Thank you for your fast response!
But still, after I initialized GLEW, it crashes, with the same Call Stack as seen in my first post.
My code:

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdlib.h>
#include <stdio.h>
#include <nanogui/nanogui.h>
static void error_callback(int error, const char* description)
{
    fputs(description, stderr);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);
}
int main(void)
{
    GLFWwindow* window;
    glfwSetErrorCallback(error_callback);
    if (!glfwInit())
        exit(EXIT_FAILURE);
    window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }
    /* Request a forward compatible OpenGL 3.3 core profile context */
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    /* Request a RGBA8 buffer without MSAA */
    glfwWindowHint(GLFW_SAMPLES, 16);
    glfwWindowHint(GLFW_RED_BITS, 8);
    glfwWindowHint(GLFW_GREEN_BITS, 8);
    glfwWindowHint(GLFW_BLUE_BITS, 8);
    glfwWindowHint(GLFW_ALPHA_BITS, 8);
    glfwWindowHint(GLFW_STENCIL_BITS, 8);
    glfwWindowHint(GLFW_DEPTH_BITS, 24);
    glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);


    glfwMakeContextCurrent(window);
    glfwSwapInterval(1);

    glewExperimental = true; // Needed in core profile
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Failed to initialize GLEW\n");
        return -1;
    }
    glViewport(0, 0, 640, 480);
    // Here it begins
    int testvariable = 5;
    using namespace nanogui;
    Screen *screen = new Screen();

    screen->initialize(window, true); // <- and it crashes right here
    FormHelper *gui = new FormHelper(screen);
    ref<Window> wnd = gui->addWindow(Eigen::Vector2i(10, 10), "Form helper example");
    gui->addGroup("Basic types");
    gui->addVariable("bool", testvariable);
    screen->setVisible(true);
    screen->performLayout();
    wnd->center();

    // ---

    glfwSetKeyCallback(window, key_callback);
    while (!glfwWindowShouldClose(window))
    {
        float ratio;
        int width, height;
        glfwGetFramebufferSize(window, &width, &height);
        ratio = width / (float)height;
        glViewport(0, 0, width, height);
        glClear(GL_COLOR_BUFFER_BIT);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glRotatef((float)glfwGetTime() * 50.f, 0.f, 0.f, 1.f);
        glBegin(GL_TRIANGLES);
        glColor3f(1.f, 0.f, 0.f);
        glVertex3f(-0.6f, -0.4f, 0.f);
        glColor3f(0.f, 1.f, 0.f);
        glVertex3f(0.6f, -0.4f, 0.f);
        glColor3f(0.f, 0.f, 1.f);
        glVertex3f(0.f, 0.6f, 0.f);
        glEnd();
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwDestroyWindow(window);
    glfwTerminate();
    exit(EXIT_SUCCESS);
}

2016-02-13 12_27_27-start

Also, Im not sure which code I should use from the screen.cpp.

Thank you for your time!

from nanogui.

wjakob avatar wjakob commented on June 20, 2024

Sorry, I am afraid you will have to debug this on your own (my time for supporting this project is very limited). All the initialization code is available in screen.cpp, so it's just a matter of following the approach shown there.

from nanogui.

danielepanozzo avatar danielepanozzo commented on June 20, 2024

I have an example here: http://libigl.github.io/libigl/tutorial/tutorial.html#viewermenu

from nanogui.

AlwaysGeeky avatar AlwaysGeeky commented on June 20, 2024

Thanks for the reply, do you have any easy access or examples of the actual GLFW init and creation code and how you pass this to nanogui and the root screen setup/init.

I pretty much have the exact same problem as the author of this issue. I create my own glfw window and context and init glew, etc.. but when I pass the glfwwindow pointer to a new 'screen' object to call initialize(...) I get a hard crash inside the initialize(...) function, because of an access violation. During this call:

glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER,
GL_STENCIL, GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE, &nStencilBits);

But if I remove my own glfw init and windows creation code and call the screen constructors like in the nanogui examples, it seems to create the window/screen properly and it works.

from nanogui.

svenevs avatar svenevs commented on June 20, 2024

Given the error you are having with nStencilBits, I think this is a Screen::Screen() initialization error:

https://github.com/wjakob/nanogui/blob/master/src/screen.cpp#L73

It seems there are a couple of fields not initialized, so the values of nStencilBits and nSamples used in the initialize method haven't been set yet, so their values won't be reliable. The default values in the other constructor are int stencilBits = 8, int nSamples = 0. I don't know which is more appropriate, but two options would be

  1. Hard-set values in the Screen::Screen constructor
  2. Make stencilBits and nSamples parameters to initialize, and in the explicit Screen constructor call it with the member variables.
    • With this approach, these could also be default parameters so that other frameworks using this method will still work: void initialize(GLFWwindow *window, bool shutdownGLFWOnDestruct, int stencilBits = 8, int nSamples = 0);?

from nanogui.

AlwaysGeeky avatar AlwaysGeeky commented on June 20, 2024

Thanks for the additional info. I don't think that is right though, I think there is a problem generally with the pass GLFWwindow* pointer. since these two calls don't even populate the right values in the size variables:

glfwGetWindowSize(mGLFWWindow, &mSize[0], &mSize[1]);
glfwGetFramebufferSize(mGLFWWindow, &mFBSize[0], &mFBSize[1]);

It could be to do with the included version of glfw in nanogui, or some other incompatability, but its hard to say since I'm not fully familiar with how nanogui is built and having to use a dll. So far I have had little trouble getting the source of nanogui to compile natively in my engine, but will be working on that soon.

Also I'm gonna write a simple cut down sample code that easily demonstrates that the Screen functionality doesnt work properly when you switch from letting nanogui manage the glfw itself and when you create it yourself and pass it into the Initialize function.

Something fishy is going on, but I will be investigating more.

from nanogui.

AlwaysGeeky avatar AlwaysGeeky commented on June 20, 2024

I have a fix for this in the pull request: #91

from nanogui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.