Пример #1
0
// init programs here, this will be done only once:
void SoTriangles::init (const char *file, GLuint *textures)
 {
   // Build program:
   _vsh.load_and_compile ( GL_VERTEX_SHADER, "../shaders/vsh_smtl_tex_gouraud.glsl" );
   _fsh.load_and_compile ( GL_FRAGMENT_SHADER, "../shaders/fsh_tex_gouraud.glsl" );
   _prog.init_and_link ( _vsh, _fsh );

   // Define buffers needed:
   gen_vertex_arrays ( 1 ); // will use at least 1 vertex array
   gen_buffers ( 2 );       // will use at least 1 buffer
   _prog.uniform_locations (11); // declare here uniforms
   //_prog.uniform_location ( 0, "vTransf" ); // each name must appear in the shader
   //_prog.uniform_location ( 1, "vProj" );
   //...
   _prog.uniform_location(0, "vTransf");
   _prog.uniform_location(1, "vProj");
   _prog.uniform_location(2, "lPos");
   _prog.uniform_location(3, "la");
   _prog.uniform_location(4, "ld");
   _prog.uniform_location(5, "ls");
   _prog.uniform_location(6, "ka");
   _prog.uniform_location(7, "kd");
   _prog.uniform_location(8, "ks");
   _prog.uniform_location(9, "sh");
   _prog.uniform_location(10, "Tex1");

   GsImage I;
   if (!I.load(file))
	   std::cout << "COULD NOT LOAD IMAGE!\n";
   else
	   std::cout << "loaded\n";
   //glGenTextures(1, &id); // ids start at 1
   glGenTextures(2, textures);
   glBindTexture(GL_TEXTURE_2D, *textures);
   glTexImage2D(GL_TEXTURE_2D, 0, 4, I.w(), I.h(), 0, GL_RGBA, GL_UNSIGNED_BYTE, I.data());
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   glGenerateMipmap(GL_TEXTURE_2D);
   glBindTexture(GL_TEXTURE_2D, 0);
   glBindVertexArray(0);
   I.init(0, 0); // free image from CPU 
   if (!I.load("../texture/sides.bmp"))
	   std::cout << "COULD NOT LOAD IMAGE!\n";
   else
	   std::cout << "loaded\n";
   glBindTexture(GL_TEXTURE_2D, textures[1]);
   glTexImage2D(GL_TEXTURE_2D, 0, 4, I.w(), I.h(), 0, GL_RGBA, GL_UNSIGNED_BYTE, I.data());
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
   glGenerateMipmap(GL_TEXTURE_2D);
   glBindTexture(GL_TEXTURE_2D, 0);
   glBindVertexArray(0);
   I.init(0, 0); // free image from CPU 
 }
Пример #2
0
void AppWindow::initTextures()
{
    const int   textureCount = 2;
#ifdef WIN32
	const char *textures[] =
	{
		"../barrel_texture_oil.bmp",
		"../barrel_texture_wood.bmp"
	};
#else
    const char *textures[] =
    {
        "barrel_texture_oil.bmp",
        "barrel_texture_wood.bmp"
    };
#endif

    // Generate the texture handles
    glGenTextures(textureCount, &_textures[0]);
    
    // Load the textures
    for(int i = 0; i < textureCount; i++)
    {
        GsImage I;
        if(!I.load(textures[i]))
            exit(1);
        
        glBindTexture ( GL_TEXTURE_2D, _textures[i] );
        glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGBA, I.w(), I.h(), 0, GL_RGBA, GL_UNSIGNED_BYTE, I.data() );
        glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameterf ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
        glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
        glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
        glGenerateMipmap ( GL_TEXTURE_2D );
        glBindTexture(GL_TEXTURE_2D, 0);
        I.init(0,0);
    }
}