You had errors because you didn’t reconcile the other image loading code using Devil when you converted to your use of libjpeg. The following code runs but you have to have three new images: logo.jpg and logo2.jpg of size 64x64 and a blank.jpg of size 128x128. I don’t claim this code is the best I just hacked your previous post until it worked You will have to understand it an clean it up to your preference of coding style. Note I moved genID into the texture_info structure and consequently remove the textures[] array.
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <jpeglib.h>
#include <jerror.h>
#include <IL/il.h>
#include <GL/glext.h>
#define NUM_TEXTURES 20
GLfloat gAngle = 0.0;
GLUquadricObj *IDquadric;
GLuint gState = 0;
typedef struct
{
const char * name;
int format;
unsigned int size;
GLuint genID;
} texture_info_t;
static texture_info_t textures_info[] =
{
{ "marble.jpg", GL_RGB, 64, 0 },
{ "chess.jpg", GL_LUMINANCE, 64, 0},
{ "chrome.jpg", GL_RGB, 64, 0},
{ "mercedes.jpg", GL_RGB, 64, 0},
{ "satin.jpg", GL_RGB, 64, 0},
{ "outline.jpg", GL_LUMINANCE, 64, 0},
{ "marble.jpg", GL_RGB, 64, 0},
{ "marble.jpg", GL_RGB, 64, 0},
{ "satin.jpg", GL_RGB, 64, 0},
{ "satin.jpg", GL_RGB, 64, 0}, //
{ "logo.jpg", GL_RGB, 64, 0}, // kludge to start at 10 for logo, logo2, blank
{ "logo2.jpg", GL_RGB, 64, 0},
{ "blank.jpg", GL_RGB, 128, 0},
{ NULL, 0, 0, 0}
};
static int left_click = GLUT_UP;
static int right_click = GLUT_UP;
static int xold;
static int yold;
static float rotate_x = 30;
static float rotate_y = 15;
static float translate_x = 0;
static float translate_y = 0;
static float plane_xy[3] = {1, 0, 0};
static float plane_yz[3] = {0, 0, 1};
int load_texture (const char * filename,unsigned char *
dest,const int format,const unsigned int size)
{
FILE *fd;
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
unsigned char * line;
cinfo.err = jpeg_std_error (&jerr);
jpeg_create_decompress (&cinfo);
if (0 == (fd = fopen(filename, "rb")))
return 1;
jpeg_stdio_src (&cinfo, fd);
jpeg_read_header (&cinfo, TRUE);
if ((cinfo.image_width != size) || (cinfo.image_height !=
size))
return 1;
if (GL_RGB == format)
{
if (cinfo.out_color_space == JCS_GRAYSCALE)
return 1;
}
else
if (cinfo.out_color_space != JCS_GRAYSCALE)
return 1;
jpeg_start_decompress (&cinfo);
while (cinfo.output_scanline < cinfo.output_height)
{
line = dest +
(GL_RGB == format ? 3 * size : size)
* cinfo.output_scanline;
jpeg_read_scanlines (&cinfo, &line, 1);
}
jpeg_finish_decompress (&cinfo);
jpeg_destroy_decompress (&cinfo);
fclose(fd);
return 0;
}
void timer(int value)
{
const int desiredFPS=120;
glutTimerFunc(1000/desiredFPS, timer, ++value);
GLfloat dt = 1./desiredFPS;
gAngle += dt*360./8.;
glutPostRedisplay();
}
void draw_cylinder()
{
glPushMatrix();
glTranslatef(-5.,0,-100);
glRotatef(gAngle,1.,1.,0.);
gluCylinder(IDquadric,10.0f,10.0f,10.0f,32,32);
glPopMatrix();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
if (gState)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_COLOR,GL_DST_COLOR);
glBlendColor(.5,.5,.5,.5);
glBindTexture ( GL_TEXTURE_2D, textures_info[10].genID);
draw_cylinder();
glBindTexture ( GL_TEXTURE_2D, textures_info[11].genID);
draw_cylinder();
glDisable(GL_BLEND);
}
else
{
glBindTexture ( GL_TEXTURE_2D, textures_info[12].genID);
draw_cylinder();
}
glutSwapBuffers();
}
void CreateMultiTexture()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
glViewport(0,0,textures_info[12].size,textures_info[12].size);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_COLOR,GL_DST_COLOR);
glBlendColor(.5,.5,.5,.5);
glBindTexture ( GL_TEXTURE_2D, textures_info[10].genID);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex2f(-1,-1);
glTexCoord2f(1,0); glVertex2f( 1,-1);
glTexCoord2f(1,1); glVertex2f( 1, 1);
glTexCoord2f(0,1); glVertex2f(-1, 1);
glEnd();
glBindTexture ( GL_TEXTURE_2D, textures_info[11].genID);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex2f(-1,-1);
glTexCoord2f(1,0); glVertex2f( 1,-1);
glTexCoord2f(1,1); glVertex2f( 1, 1);
glTexCoord2f(0,1); glVertex2f(-1, 1);
glEnd();
glDisable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D,textures_info[12].genID);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, textures_info[12].size, textures_info[12].size, 0);
glViewport(viewport[0], viewport[1], viewport[2] ,viewport[3]);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glutSwapBuffers();
glutDisplayFunc(display);
}
void cleanupQuadric(void)
{
gluDeleteQuadric(IDquadric);
printf( "cleanupQuadric completed
" );
}
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
IDquadric=gluNewQuadric();
gluQuadricNormals(IDquadric, GLU_SMOOTH);
gluQuadricTexture(IDquadric, GL_TRUE);
atexit(cleanupQuadric);
GLdouble Vol = 10*1.8;
GLdouble Left=-Vol;
GLdouble Right=Vol;
GLdouble Bottom=-Vol;
GLdouble Top=Vol;
GLdouble Near=0;
GLdouble Far=2*Vol;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(Left, Right, Bottom, Top, Near, Far);
GLdouble eyeX=0;
GLdouble eyeY=0;
GLdouble eyeZ=-100+Vol;
GLdouble centerX=0;
GLdouble centerY=0;
GLdouble centerZ=-100;
GLdouble upX=0;
GLdouble upY=1;
GLdouble upZ=0;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eyeX,eyeY,eyeZ,
centerX,centerY,centerZ,
upX,upY,upZ);
ilInit();
glEnable (GL_TEXTURE_2D);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 27:
exit(0);
break;
default:
gState++;
gState %= 2;
printf("%d
",gState);
break;
}
}
int main(int argc, char** argv)
{
unsigned char texture[NUM_TEXTURES][3 * 128 * 128];
unsigned int i;
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutCreateWindow ("Texture gen");
glClearColor (0, 0, 0, 0);
glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable (GL_TEXTURE_2D);
for (i = 0; textures_info[i].size != 0; ++i)
{
glGenTextures (1, &textures_info[i].genID);
if (load_texture (textures_info[i].name,texture[i],
textures_info[i].format,textures_info[i].size) != 0)
return 1;
glBindTexture (GL_TEXTURE_2D, textures_info[i].genID);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
GL_REPEAT);
gluBuild2DMipmaps (GL_TEXTURE_2D, textures_info[i].format,
textures_info[i].size,
textures_info[i].size,
textures_info[i].format,
GL_UNSIGNED_BYTE, texture[i]);
}
glutDisplayFunc (CreateMultiTexture);
glutTimerFunc(0,timer,0);
glutKeyboardFunc(keyboard);
init();
glutMainLoop();
return 0;
}