@Overri<strong>de</strong>public void init ( GLAutoDrawable drawable ) {GL2 gl = drawable . getGL (). getGL2 ();}// Grayish backgroundgl. glClearColor ( fLowLight [0] , fLowLight [1] , fLowLight [2] , fLowLight [3]);// Cull backs of polygonsgl. glCullFace ( GL2 . GL_BACK );gl. glFrontFace ( GL2 . GL_CCW );gl. glEnable ( GL2 . GL_CULL_FACE );gl. glEnable ( GL2 . GL_DEPTH_TEST );setupLight (gl );spherePositions = GlUtil . generateRandomPositions ( FloorSize , NumSpheres );setupTextures (gl );setupCubeMap (gl );private TGAImage loadTexture ( String name ) {TGAImage img = null ;try {InputStream stream = Anisotropic . class . getResourceAsStream ( name );img = TGAImage . read ( stream );stream . close ();} catch ( IOException e) {e. printStackTrace ();System . exit (0);}return img ;}private void setupTextures ( GL2 gl) {// Activar as texturasgl. glEnable ( GL2 . GL_TEXTURE_2D );// Obter os i<strong>de</strong>ntificadorestextures = IntBuffer . allocate ( NumTextures ); // 2 texturas + 1 cubogl. glGenTextures ( NumTextures , textures );// Configuracao do Texture Environmentgl. glTexEnvi ( GL2 . GL_TEXTURE_ENV , GL2 . GL_TEXTURE_ENV_MODE , GL2 . GL_MODULATE );// Texturas da GeometriatextureNames . add (" earth . tga "); // Para os circulostextureNames . add (" grass . tga ");// Texturas do Cube MaptextureNames . add (" cubemap / right . tga ");textureNames . add (" cubemap / left . tga ");textureNames . add (" cubemap /up.tga ");textureNames . add (" cubemap / down . tga ");textureNames . add (" cubemap / backward . tga ");textureNames . add (" cubemap / forward . tga ");// Carregar as texturas da Geometriafor ( int i = 0; i < CubeStartIn<strong>de</strong>x ; i ++) {// Fazer o bind do estado da textura ao i<strong>de</strong>ntificadorgl. glBindTexture ( GL2 . GL_TEXTURE_2D , textures . get (i ));133
Carregar e Ler a imagem para a nossa texturaTGAImage img = loadTexture ( textureNames . get (i ));ByteBuffer bb = img . getData ();// Carregar a textura ( comprimida RGB )gl. glTexParameteri ( GL2 . GL_TEXTURE_2D , GL2 . GL_GENERATE_MIPMAP , GL2 . GL_TRUE );gl. glTexImage2D ( GL2 . GL_TEXTURE_2D , 0, GL2 . GL_COMPRESSED_RGB ,img . getWidth () , img . getHeight () , 0, img . getGLFormat () ,GL2 . GL_UNSIGNED_BYTE , bb );// Parametros da texturagl. glTexParameteri ( GL2 . GL_TEXTURE_2D , GL2 . GL_TEXTURE_MAG_FILTER ,GL2 . GL_LINEAR );gl. glTexParameteri ( GL2 . GL_TEXTURE_2D , GL2 . GL_TEXTURE_MIN_FILTER ,GL2 . GL_LINEAR_MIPMAP_LINEAR );gl. glTexParameteri ( GL2 . GL_TEXTURE_2D , GL2 . GL_TEXTURE_WRAP_S ,GL2 . GL_CLAMP_TO_EDGE );gl. glTexParameteri ( GL2 . GL_TEXTURE_2D , GL2 . GL_TEXTURE_WRAP_T ,GL2 . GL_CLAMP_TO_EDGE );}}private void setupCubeMap ( GL2 gl) {// Carregar as texturas do Cubogl. glBindTexture ( GL2 . GL_TEXTURE_CUBE_MAP , textures . get ( CubeStartIn<strong>de</strong>x ));gl. glTexParameteri ( GL2 . GL_TEXTURE_CUBE_MAP , GL2 . GL_TEXTURE_MAG_FILTER ,GL2 . GL_LINEAR );gl. glTexParameteri ( GL2 . GL_TEXTURE_CUBE_MAP , GL2 . GL_TEXTURE_MIN_FILTER ,GL2 . GL_LINEAR );gl. glTexParameteri ( GL2 . GL_TEXTURE_CUBE_MAP , GL2 . GL_TEXTURE_WRAP_S ,GL2 . GL_REPEAT );gl. glTexParameteri ( GL2 . GL_TEXTURE_CUBE_MAP , GL2 . GL_TEXTURE_WRAP_T ,GL2 . GL_REPEAT );gl. glTexParameteri ( GL2 . GL_TEXTURE_CUBE_MAP , GL2 . GL_TEXTURE_WRAP_R ,GL2 . GL_REPEAT );// Restantes texturas preenchem o cubofor ( int i = CubeStartIn<strong>de</strong>x ; i < textureNames . size (); i ++) {// Carregar e Ler a imagem para a nossa texturaTGAImage img = loadTexture ( textureNames . get (i ));ByteBuffer bb = img . getData ();}gl. glTexImage2D ( CubeDirection [i - CubeStartIn<strong>de</strong>x ], 0, GL2 . GL_RGBA ,img . getWidth () , img . getHeight () , 0, img . getGLFormat () ,GL2 . GL_UNSIGNED_BYTE , bb );}gl. glTexGeni ( GL2 .GL_S , GL2 . GL_TEXTURE_GEN_MODE , GL2 . GL_REFLECTION_MAP );gl. glTexGeni ( GL2 .GL_T , GL2 . GL_TEXTURE_GEN_MODE , GL2 . GL_REFLECTION_MAP );gl. glTexGeni ( GL2 .GL_R , GL2 . GL_TEXTURE_GEN_MODE , GL2 . GL_REFLECTION_MAP );private void setupLight ( GL2 gl) {// Setup light parametersgl. glLightMo<strong>de</strong>lfv ( GL2 . GL_LIGHT_MODEL_AMBIENT , fNoLight , 0);gl. glLightMo<strong>de</strong>li ( GL2 . GL_LIGHT_MODEL_COLOR_CONTROL ,GL2 . GL_SEPARATE_SPECULAR_COLOR );gl. glLightfv ( GL2 . GL_LIGHT0 , GL2 . GL_AMBIENT , fLowLight , 0);gl. glLightfv ( GL2 . GL_LIGHT0 , GL2 . GL_DIFFUSE , fBrightLight , 0);gl. glLightfv ( GL2 . GL_LIGHT0 , GL2 . GL_SPECULAR , fBrightLight , 0);gl. glEnable ( GL2 . GL_LIGHTING );134