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
|
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include "sgi.h"
#include <math.h>
#define MGL_JPEG 1
#define MGL_RGB 2
float myangle = 0.0;
GLint id_tex1;
GLint id_tex2;
GLuint init_texture(char *nom,int format)
{
GLubyte *image; // pour stocker les pixels de l'image texture
GLuint tex_id;
GLint longueur,largeur,depthi;
switch (format) {
case MGL_JPEG:image=read_jpg(nom,&longueur,&largeur,&depthi);break;
case MGL_RGB:image=read_sgi(nom,&longueur,&largeur,&depthi);break;
default:exit(1);
}
printf("lecture image : %d x %d x %d\n",longueur,largeur,depthi);
glGenTextures(1,&tex_id); // alloue 1 identifiant à tex_id.
glBindTexture(GL_TEXTURE_2D,tex_id); // la texture courante 2D correspond à
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,longueur,largeur,0,GL_RGB,GL_UNSIGNED_BYTE,image);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
return tex_id;
}
void texture_coord(GLint idtex)
{
glBindTexture(GL_TEXTURE_2D,idtex);
glEnable(GL_TEXTURE_2D);
}
void texture_sphere(GLint idtex)
{
glBindTexture(GL_TEXTURE_2D,idtex);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE,GL_SPHERE_MAP);
glEnable(GL_TEXTURE_GEN_S);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE,GL_SPHERE_MAP);
glEnable(GL_TEXTURE_GEN_T);
glEnable(GL_TEXTURE_2D);
}
void desactiver_texture() {
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_2D);
}
void myIdle() {
myangle+=0.05;
glutPostRedisplay();
}
static void myReshape(int width, int height) {
glViewport(0,0,(GLint) width, (GLint) height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat) width/(GLfloat)height, 1.0,50.0);
glMatrixMode(GL_MODELVIEW);
}
void tracerSoleil() {
glColor3f(1,1,0);
glTranslatef(0.5,0.5,0);
glRotatef(myangle,0,1,0);
glutSolidSphere(3.0,50,50);
}
static void myDisplay() {
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(2,3,-20);
tracerSoleil();
glPopMatrix();
glutSwapBuffers();
}
void myInit() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
initLumiere(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK,GL_DIFFUSE);
id_tex1=init_texture("texture-sphere.jpg",MGL_JPEG);
id_tex2=init_texture("msmichel.jpg",MGL_JPEG);
}
int main(int argc, char **argv) {
glutInit(&argc,argv);
glutInitWindowSize(800,800);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutCreateWindow("premier prog");
glutReshapeFunc(myReshape);
glutDisplayFunc(myDisplay);
glutIdleFunc(myIdle);
myInit();
glutMainLoop();
return 0;
} |
Partager