1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
int texture[20];//tableau pour le "multi-texturing"
/* OpenGL texture info */
typedef struct {
GLsizei width;
GLsizei height;
GLenum format;
GLint internalFormat;
GLuint id;
GLubyte *texels;
}
gl_texture_t;
//sample pour charger les textures images en .jpg
gl_texture_t*ReadJPEGFromFile (const char *filename) {
gl_texture_t *texinfo = NULL;
FILE *fp = NULL;
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
JSAMPROW j;
int i;
/* open image file */
fp = fopen (filename, "rb");
if (!fp)
{
fprintf (stderr, "error: couldn't open \"%s\"!\n", filename);
return NULL;
}
/* create and configure decompressor */
jpeg_create_decompress (&cinfo);
cinfo.err = jpeg_std_error (&jerr);
jpeg_stdio_src (&cinfo, fp);
/*
* NOTE: this is the simplest "readJpegFile" function. There
* is no advanced error handling. It would be a good idea to
* setup an error manager with a setjmp/longjmp mechanism.
* In this function, if an error occurs during reading the JPEG
* file, the libjpeg abords the program.
* See jpeg_mem.c (or RTFM) for an advanced error handling which
* prevent this kind of behavior (http://tfc.duke.free.fr)
*/
/* read header and prepare for decompression */
jpeg_read_header (&cinfo, TRUE);
jpeg_start_decompress (&cinfo);
/* initialize image's member variables */
texinfo = (gl_texture_t *)malloc (sizeof (gl_texture_t));
texinfo->width = cinfo.image_width;
texinfo->height = cinfo.image_height;
texinfo->internalFormat = cinfo.num_components;
if (cinfo.num_components == 1)
texinfo->format = GL_LUMINANCE;
else
texinfo->format = GL_RGB;
texinfo->texels = (GLubyte *)malloc (sizeof (GLubyte) * texinfo->width
* texinfo->height * texinfo->internalFormat);
/* extract each scanline of the image */
for (i = 0; i < texinfo->height; ++i)
{
j = (texinfo->texels +
((texinfo->height - (i + 1)) * texinfo->width * texinfo->internalFormat));
jpeg_read_scanlines (&cinfo, &j, 1);
}
/* finish decompression and release memory */
jpeg_finish_decompress (&cinfo);
jpeg_destroy_decompress (&cinfo);
fclose (fp);
return texinfo;
}
GLuint loadJPEGTexture (const char *filename) {
gl_texture_t *jpeg_tex = NULL;
GLuint tex_id = 0;
jpeg_tex = ReadJPEGFromFile(filename);
if (jpeg_tex && jpeg_tex->texels){
/* generate texture */
glGenTextures (1, &jpeg_tex->id);
glBindTexture (GL_TEXTURE_2D, jpeg_tex->id);
/* setup some parameters for texture filters and mipmapping */
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
/* glTexImage2D (GL_TEXTURE_2D, 0, jpeg_tex->internalFormat,
jpeg_tex->width, jpeg_tex->height, 0, jpeg_tex->format,
GL_UNSIGNED_BYTE, jpeg_tex->texels);*/
gluBuild2DMipmaps (GL_TEXTURE_2D, jpeg_tex->internalFormat, jpeg_tex->width, jpeg_tex->height, jpeg_tex->format, GL_UNSIGNED_BYTE, jpeg_tex->texels);
tex_id = jpeg_tex->id;
/* OpenGL has its own copy of texture data */
free (jpeg_tex->texels);
free (jpeg_tex);
}
return tex_id;
}
int main(int argc, char **argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
glutInitWindowSize(480, 272);
glutInitWindowPosition(0, 0);
window = glutCreateWindow("pspflashsystem");
loadobj(&model, "cube.obj", 1);
loadobj(&models, "cubes.obj", 0);
texture[0] = loadJPEGTexture("./data/cube.jpg");
texture[1] = loadJPEGTexture("./data/cube2.jpg");
initGL(480.0f, 272.0f);
glutDisplayFunc(Display);//fonction principale pour l'affichage des éléments
glutIdleFunc(Display);//pour raffraichir et exécuter à chaque fois les rotation et animation que l'on effectue à travers les touches ;)
glutKeyboardFunc(padGL);
glutSpecialFunc(padspecialGL);
glutMainLoop();//gestion par glut des différentes fonction dans cette boucle sorte de boucle principale :)
return (0);
} |
Partager