Gtk::GLAarea does not draw

Asked 2 months ago, Updated 2 months ago, 11 views

I am trying to program graphics with OpenGL using Gtkmm's GLArea, but when I call glBegin~glEnd or glDraw*, the contents are not drawn.DrawCall seems to be ignored when using Gtkmm, even though it is drawn with the same code using glfw.Below is the code for the problem.

 std::vector<glm::vec2>vtx{
            { 0, 1 },
            { -1, -1 },
            { 1, -1 }
};
const char*vertex_source=
            "#version 460"
            ""
            "invec2position;"
            ""
            "void main(){"
            "   gl_Position=vec4(position,0,1);"
            "}";
const char*fragment_source=
            "#version 460"
            ""
            "outvec4 outColor;"
            ""
            "void main(){"
            "   outColor=vec4(1,1,1,1);"
            "}";

class canvas:public Gtk::GLAarea{
public:
    canvas() {

    }
    void on_realize() override {
        Widget::on_realize();

        make_current();

        // create shader
        shader=glCreateProgram();
        // attach vertex and fragment shaders

        // create buffer
        glGenBuffers (1, & vbo);
        // glBindBuffer, glBufferData 

        glClearColor(0,0,0,1);
    }
    void on_unrealize() override {
        Widget::on_unrealize();

        // destroy buffer and shader
    }
    US>bool on_render(const Glib::RefPtr<Gdk::GLContext>&) override {
        glClear(GL_COLOR_BUFFER_BIT);
        glUseProgram (shader);
        glBindBuffer (vbo);
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,0,nullptr);
        glDrawArrays (GL_TRIANGLES, 0, 3);
        glDisableVertexAttribArray(0);

        glUseProgram(0);
        glFlush();
    }
private:
    GLuint shader;
    GLuint vbo;
};

int main(intargc, char*argv[]){
    auto app=Gtk::Application::create(argc,argv);
    Gtk::Window;
    window.set_default_size(1000,700);
    canvas cv;
    window.add(cv);
    window.show_all_children();

    return app ->run(window);
}

The above code ignores glDraw*, but the background seems to be painted out with glClearColor.
The expected action is to draw a large white triangle on a black background, but how do you draw it?

The environment is Ubuntu 18.04, Gtkmm 3.22.2.
Thank you for your cooperation.

c++ opengl gtk

2022-09-30 11:39

1 Answers

I've only done 2.0 worth of OpenGL, so I don't want to.

const char*vertex_source=
            "#version 460"
            ""
            "layout(location=0)invec2position;"
            ""
            "void main(){"
            "   gl_Position=vec4(position,0,1);"
            "}";

Isn't that right?

location of attribute variables as specified by the glEnableVertexAttribArray function, etc. If not explicitly specified, it is not certain which number will be located.
Therefore, you must investigate the number of locations in the glBindAttribLocation function, or the glGetAttribLocation function, either explicitly in the shader (if OpenGL 3.0 or later).


2022-09-30 11:39

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.