Esempio n. 1
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);
    }
}
Esempio n. 2
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 
 }
Esempio n. 3
0
void draw_solid_tube (double len, double rt, double rb, int nfaces, bool smooth, bool texture, GsImage& img)
{
	glEnable(GL_COLOR_MATERIAL);
	if(App->wireframe){glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);}
	else
		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	glEnable(GL_NORMALIZE);
	if(texture)
	{
		glEnable(GL_TEXTURE_2D);
		img.ogl_bind_texture();
		glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	} else
		glDisable(GL_TEXTURE_2D);
			

   if ( rb<=0 ) rb=MINRADIUS;
   if ( rt<=0 ) rt=MINRADIUS;
   if ( nfaces<3 ) nfaces = 3;
   glColor3f(1.0, 1.0, 1.0);
   
   // compute vertical axis:
   double dang = (2.0*PI)/double(nfaces);
   double ang = 0;

   // we will use len as half the length:
   len = len/2;

   // initial points:
   double ibx=rb, iby=-len, ibz=0; // bottom
   double itx=rt, ity=+len, itz=0; // top

   // current points start as the initial points:
   double cbx=ibx, cby=iby, cbz=ibz; // bottom
   double ctx=itx, cty=ity, ctz=itz; // top

   // declare the "next points" to form the cylinder lines:
   double nbx, nby, nbz; // bottom
   double ntx, nty, ntz; // top

   // declare variables for texture usage
   double tx = 1.0;
   double tnext =  -( 1.0 / nfaces );

   //declare vector variables
   Vec u, v, w, p, q, r;

   // compute the points all around body:
   int i=1;
   while(i <= nfaces)
   { // rotate the current points:
        if ( i<nfaces ) // normal rotation for intermediate points
         { ang += dang;
           double ca = cos(ang);
           double sa = sin(ang);
           nbx=ca*rb; nby=cby; nbz=sa*rb;
           ntx=ca*rt; nty=cty; ntz=sa*rt;
         }
        else // to make a perfect closure, the last points are exactly the first ones
         { nbx=ibx; nby=iby; nbz=ibz;
           ntx=itx; nty=ity; ntz=itz;
         }

		//Calculating vectors and normals for upper and lower parts
		u = Vec( (ntx - ctx), (nty - cty), (ntz - ctz) );
		v = Vec( (0 - ctx), (len - cty), (0 - ctz) );
		w = cross(v, u);
		w.normalize();

		//Normal for faces
		p = Vec( (nbx - cbx), (nby - cby), (nbz - cbz) );
		q = Vec( (ctx - cbx), (cty - cby), (ctz - cbz) );
		r = cross(q, p);
		r.normalize();

		//Start drawing
		glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);

		
		if(!smooth && !texture)
		{
			glBegin ( GL_TRIANGLES );
			//draw top polygons
			glNormal3d(w.x, w.y, w.z); 
			glVertex3d(ctx, cty, ctz); glVertex3d(ntx, nty, ntz); glVertex3d(0, len, 0);

			//draw bottom polygons
			glNormal3d(-w.x, -w.y, -w.z);
			glVertex3d(cbx, cby, cbz); glVertex3d(nbx, nby, nbz); glVertex3d(0, -len, 0);
			glEnd();

			//draw faces
			glBegin(GL_QUADS);
			glNormal3d(r.x, r.y, r.z);
			glVertex3d(ctx, cty, ctz); glVertex3d(ntx, nty, ntz); 
			glVertex3d(nbx, nby, nbz); glVertex3d(cbx, cby, cbz);
			glEnd();
		}

		if(smooth && !texture)
		{
			glBegin ( GL_TRIANGLES );
			//draw top polygons
			glNormal3d(ctx, cty, ctz); glVertex3d(ctx, cty, ctz);
			glNormal3d(ntx, nty, ntz); glVertex3d(ntx, nty, ntz);
			glNormal3d(0, len, 0); glVertex3d(0, len, 0);

			//draw bottom polygons
			glNormal3d(cbx, cby, cbz); glVertex3d(cbx, cby, cbz);
			glNormal3d(nbx, nby, nbz); glVertex3d(nbx, nby, nbz);
			glNormal3d(0, -len, 0); glVertex3d(0, -len, 0);
			
			//draw faces
			glNormal3d(ctx, cty, ctz); glVertex3d(ctx, cty, ctz);
			glNormal3d(ntx, nty, ntz); glVertex3d(ntx, nty, ntz);
			glNormal3d(cbx, cby, cbz); glVertex3d(cbx, cby, cbz);
			glNormal3d(cbx, cby, cbz); glVertex3d(cbx, cby, cbz);
			glNormal3d(nbx, nby, nbz); glVertex3d(nbx, nby, nbz);
			glNormal3d(ntx, nty, ntz); glVertex3d(ntx, nty, ntz);

			glEnd();
		}

		if(texture)
		{
			//draw faces
			glBegin(GL_QUADS);

			glTexCoord2f(tx, 0); 
			glVertex3d(ctx, cty, ctz);

			glTexCoord2f(tx + tnext, 0); 
			glVertex3d(ntx, nty, ntz);

			glTexCoord2f(tx + tnext, 1); 
			glVertex3d(nbx, nby, nbz);

			glTexCoord2f(tx, 1); 
			glVertex3d(cbx, cby, cbz);

			glEnd();

			glDisable(GL_TEXTURE_2D);
			glBegin ( GL_TRIANGLES );

			//draw top polygons
			glVertex3d(ctx, cty, ctz);
			glVertex3d(ntx, nty, ntz);
			glVertex3d(0, len, 0);

			//draw bottom polygons
			glVertex3d(cbx, cby, cbz);
			glVertex3d(nbx, nby, nbz);
			glVertex3d(0, -len, 0);
			glEnd();
			glEnable(GL_TEXTURE_2D);
		}


        // update current points:
        cbx=nbx; cby=nby; cbz=nbz;
        ctx=ntx; cty=nty; ctz=ntz;
		tx += tnext;

        // increment side counter:
        i++;
      }

   // done
   glEnd();
}