PDA

View Full Version : OpenGL with FLTK background image



pajoohesh
12-24-2016, 08:37 AM
Dear All,
I have this class inherited from Fl_Gl_Window but I do not manage to draw the background image. here is the code:

void MyGLWindow::draw()
{
if (!valid())
{
glGenTextures(1, &tex_id);
glBindTexture(GL_TEXTURE_2D, tex_id);

Fl_Shared_Image *img{ Fl_Shared_Image::get("picture.jpg") };
if (img->d() != 3)
std::cout << "Not 3 dimensions" << std::endl;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->w(), img->h(), 0,
GL_RGB, GL_UNSIGNED_BYTE, img->data()[0]);
img->release();

glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

glOrtho(-1, 1, -1, 1, -1, 1);
glClearColor(1., 1., 1., 1.f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
gl_font(FL_HELVETICA_BOLD_ITALIC, 30);
}
glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glColor4f(1., 1., 0., .7);
for (auto& b : balls)
b.draw();
glPopMatrix();

glPushMatrix();
glLoadIdentity();
glColor4f(1.f, 0.f, 0.0, 1.f);
std::string s{ "HELLO" };
gl_draw(s.c_str(), s.size(), 0.5f, 0.5f);
glPopMatrix();

glPushMatrix();
glLoadIdentity();
glBegin(GL_QUADS);
glColor4f(1.f, 1.f, 1.0, 0.f);
glTexCoord2f(0, 1.f); glVertex2f(-1, -1);
glTexCoord2f(1.f, 1.f); glVertex2f(1, -1);
glTexCoord2f(1.f, 0); glVertex2f(1, 1);
glTexCoord2f(0, 0); glVertex2f(-1, 1);
glEnd();
glPopMatrix();
}

The problem is that the text and the balls all show up nicely but no image is shown in the background. Could someone help me with this problem?

Carmine
12-25-2016, 07:43 PM
You might try the FLTK forum for this question.