chasep255
chasep255

Reputation: 12175

OpenGL with Gtk+, shapes are not being drawn despite background being cleared

I am trying to get OpenGL to work with gtk+. It seemed to be working size I was able to clear the background color. However, when I went to draw something it is not there. Am I missing something. I put the eye at 10, 10, 10 and I am looking at the origin. I should see a back triangle near the origin.

#include <gtk/gtk.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <iostream>

GtkWidget* wnd;
GtkWidget* glarea;

static gboolean render(GtkGLArea *area, GdkGLContext *context)
{
    int w = gtk_widget_get_allocated_width(GTK_WIDGET(area));
    int h = gtk_widget_get_allocated_height(GTK_WIDGET(area));
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(180, (double)w / (double)h, 0.1, 100.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(10, 10, 10, 0, 0, 0, 0, 1, 0);

    glClearColor(1, 1, 1, 0);
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f(0, 0, 0);
    glBegin(GL_TRIANGLES);
    glVertex3f(0, 0, 0);
    glVertex3f(-1, 2, -1);
    glVertex3f(1, 3, 2);
    glEnd();

    return TRUE;
}

int main(int argc, char *argv[])
{
    gtk_init(&argc, &argv);
    wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    glarea = gtk_gl_area_new();

    gtk_container_add(GTK_CONTAINER(wnd), glarea);
    g_signal_connect(wnd, "destroy", gtk_main_quit, 0);

    g_signal_connect(glarea, "render", G_CALLBACK(render), NULL);

    gtk_widget_show_all(wnd);
    gtk_main();
    return 0;
}

Upvotes: 4

Views: 634

Answers (1)

Marco A.
Marco A.

Reputation: 43662

Source: Emanuele Bassi's blog - GTK+ developer

[...] The OpenGL support inside GTK+ requires core GL profiles, and thus it won’t work with the fixed pipeline API that was common until OpenGL 3.2 and later versions. this means that you won’t be able to use API like glRotatef(), or glBegin()/glEnd() pairs, or any of that stuff.

Solution: drop the fixed function pipeline.

Upvotes: 1

Related Questions