LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-17-2016, 09:06 AM   #1
anon112
Member
 
Registered: May 2008
Distribution: Arch, FreeBSD
Posts: 116

Rep: Reputation: 17
GtkGLArea not drawing colours from fragment shader; C++


I've mostly worked with GLFW in the past, however a recent project required me to implement an OpenGL draw area in a GTK. Configuring my rendering and shaders are all the same, the only real difference is that I'm setting up a GtkGLArea widget to handle my draw calls instead of a GLFW window.

I've built my UI using glade and I'm packing my GtkGLArea into a Paned window element within a Paned window element. As a test program I decided to try drawing a simple coloured triangle with a dark red background. However, whenever I try to render anything drawn with glDrawArrays(...), I just get a black silhouette of what I'm trying to draw. Even if I modify my fragment shader to only draw white, it draws black.

Fragment shader:
Code:
#version 330 core

in vec3 VertCol;
out vec4 color;

void main()
{
    //color = vec4(VertCol.xyz,1.0f);
    color = vec4(1.0f,1.0f,1.0f,1.0f);
}
Initializing my GtkGLArea:

Code:
//(in main)
gtk_builder_add_from_file(Builder,"GladeInterface.glade",NULL);
Win = gtk_builder_get_object(Builder,"RootWindow");
GObject* GLDrawArea = (GObject*)gtk_gl_area_new();
gtk_gl_area_set_required_version((GtkGLArea*)GLDrawArea,3,3);
ERR = g_signal_connect(GLDrawArea,"realize",G_CALLBACK(GLAreaRealize),NULL);
if (ERR < 0) fprintf(stderr,"[OpenGL]: Unable to realize OpenGL context\n");
ERR = g_signal_connect(GLDrawArea,"render",G_CALLBACK(GLAreaRender),NULL);
if (ERR < 0) fprintf(stderr,"[OpenGL]: Unable to initialize OpenGL render\n");
GObject* EventBox = gtk_builder_get_object(Builder,"EventBox");
gtk_container_add((GtkContainer*)EventBox,(GtkWidget*)GLDrawArea);
gtk_widget_show(GTK_WIDGET(Win));
gtk_widget_show((GtkWidget*)GLDrawArea);
"realize" and "render" callbacks:

Code:
void GLAreaRender(GtkGLArea *area, GdkGLContext *context, gpointer data)
{
    glClearColor (0.5f, 0.0f, 0.0f, 1.0f);
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    //Draw Functions Go Here
    glUseProgram(GLB_Shader.ProgID);
    glBindVertexArray(GLB_VAO);
    glBindBuffer(GL_ARRAY_BUFFER,GLB_VBO);
    GLfloat PN[] = {1.0f,1.0f,-1.0f,    1.0f,1.0f,1.0f,    -1.0f,-1.0f,0.0f,    0.5f,0.5f,0.5f,    0.3f,1.0f,0.3f,    0.3f,0.3f,0.3f};
    glBufferData(GL_ARRAY_BUFFER,sizeof(PN),PN,GL_STATIC_DRAW);
    glDrawArrays(GL_TRIANGLES,0,3);
    glFinish();
}

void GLAreaRealize(GtkGLArea* A,gpointer data)
{
    gtk_gl_area_set_has_depth_buffer(A,1);
    gtk_gl_area_set_has_alpha(A,0);
    gtk_gl_area_set_auto_render(A,0);

    gtk_gl_area_attach_buffers(A);

    if (gtk_gl_area_get_error (A) != NULL)
    {
        fprintf(stderr,"Could not initialize OpenGL Area\n");
        return;
    }

    GLenum Shaders[2] = {GL_VERTEX_SHADER,GL_FRAGMENT_SHADER};

    gtk_gl_area_make_current(A);

    //standard GL program builder tested with other projects
    GLB_Shader.BuildProgram("Shader",Shaders,2);

    glGenVertexArrays(1,&GLB_VAO);

    glGenBuffers(1,&GLB_VBO);
    glGenBuffers(1,&GLB_EBO);
    glBindBuffer(GL_ARRAY_BUFFER,GLB_VBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,GLB_EBO);

    glBindVertexArray(GLB_VAO);
    glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,6*sizeof(GLfloat),(GLvoid*)0);
    glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,6*sizeof(GLfloat),(GLvoid*)(3*sizeof(GLfloat)));
    glEnableVertexAttribArray(0); //Vert Position
    glEnableVertexAttribArray(1); //Vert colour
};
I know my render code works fine with GLFW, so my buffers and shaders are both fine. The issue would seem to be related to how GTK is handling the window. Does anyone know why I can't seem to get colours to draw on a GtkGLArea?

Note: this error persists even if I make a window in code without using the builder. The gtk-demo for GtkGLArea works fine. The output I get from this code though is just a red screen with a black triangle (and from my fragment shader, the triangle should be white)

Last edited by anon112; 04-17-2016 at 09:08 AM. Reason: info
 
  


Reply

Tags
c++, gtk3, opengl



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Trying to install gtkglarea usnf002 Linux - Software 3 02-02-2012 05:53 PM
Does EXT2/# Fragment? sirclown82 General 6 03-13-2007 08:51 PM
Any use for fragment shader HW under Linux? randomshinichi Linux - Software 0 09-28-2005 08:41 AM
gtkglarea compiling program problem vas_dee Linux - Software 2 03-19-2005 05:23 AM
BZedit won't compile because of gtkglarea derfaust Linux - Software 8 06-30-2004 03:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration