QT GUI Programming Kit, C++ and OpenGL

I am attempting to use the tools availible in the QT Programming Kit to program a small graphical demostration in C++ using the OpenGL graphics library under the Linux Operating System. Until now, I haven’t had any difficulties using the QT Kit, however now I am encountering programs with texture mapping. I warn you, I am new to OpenGL, so perhaps I am simply overlooking something in all the documentation I’ve been sifting through.

I need to know how you go about loading an image into QT and then making it a texture that is usable by the OpenGL libraries.

-mike

The easiest way I find is to store textures as raw PPM files and use the following code:

/*

  • texture.c
  • Load and bind textures from PPM files.
  • Copyright © 2001 Ryan T. Sammartino
  • This program is free software; you can redistribute it and/or
  • modify it under the terms of the GNU General Public License
  • as published by the Free Software Foundation; either version 2
  • of the License, or (at your option) any later version.
  • This program is distributed in the hope that it will be useful,
  • but WITHOUT ANY WARRANTY; without even the implied warranty of
  • MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  • GNU General Public License for more details.
  • You should have received a copy of the GNU General Public License
  • along with this program; if not, write to the Free Software
  • Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

*/

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <GL/gl.h>
#include <GL/glu.h>

/*

  • Loads and binds a texture from a raw PPM file given by filename
  • Returns the GL name in texName
  • Return value: negative for failure, 0 for success

/
int LoadTextureFromPPM(const char filename, GLuint texName)
{
/
Load the image /
FILE
fp;
int i, w, h, d;
unsigned char
image;
char head[70]; /
max line <= 70 in PPM (per spec). */

fp = fopen(filename, "rb");
if (!fp) {
    perror(filename);
    return -1;
}

/* grab first two chars of the file and make sure that it has the
   correct magic cookie for a raw PPM file. */
fgets(head, 70, fp);

if (strncmp(head, "P6", 2)) {
    fprintf(stderr, "%s is not a raw PPM file

", filename);
return -1;
}

/* grab the three elements in the header (width, height, maxval). */
i = 0;
while (i < 3) {
    fgets(head, 70, fp);
    if (head[0] == '#') {
        /* skip comments. */
        continue;
    }
    switch (i) {
    case 0:
        i += sscanf(head, "%d %d %d", &w, &h, &d);
        break;
    case 1:
        i += sscanf(head, "%d %d", &h, &d);
        break;
    case 2:
        i += sscanf(head, "%d", &d);
        break;
   }
}

/* grab all the image data in one fell swoop. */
image = (unsigned char*)malloc(sizeof(unsigned char)*w*h*3);
fread(image, sizeof(unsigned char), w*h*3, fp);
fclose(fp);

/* Bind */
glGenTextures(1, texName);
glBindTexture(GL_TEXTURE_2D, *texName);

/* Set parameters */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

/* Build mipmaps */
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB,
                  w, h,
                  GL_RGB,
                  GL_UNSIGNED_BYTE,
                  image);

free(image);
return 0;

}

[This message has been edited by rts (edited 09-17-2001).]

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.