Example #1
0
void Terrain::createFromHeightmap(const std::string &filename)
{
    GLFWimage *img = new GLFWimage();
    
    if(glfwReadImage(filename.c_str(), img, 0)==GL_FALSE)
        return;
    
    for (int z=0; z<MAP_Z_SEG; ++z)
    {
        for (int x=0; x<MAP_X_SEG; ++x)
        {
            vertex.push_back( glm::vec3( 	MAP_X_OFFSET + (float)x/(float)(MAP_X_SEG-1) * MAP_X_SCALE,
                                        MAP_Y_OFFSET + getSample( img, (float)x/(float)(MAP_X_SEG-1), (float)z/(float)(MAP_Z_SEG-1)) * MAP_HEIGHT,
                                        MAP_Z_OFFSET + (float)z/(float)(MAP_Z_SEG-1) * MAP_Z_SCALE ));
            
        }
    }
    
    for (int z=0; z<MAP_Z_SEG-1; ++z)
    {
        for(int x=0; x<MAP_X_SEG-1; ++x)
        {
            face.push_back( glm::uvec3(z*MAP_X_SEG+x,(z+1)*MAP_X_SEG+x, z*MAP_X_SEG+x+1) );
            face.push_back( glm::uvec3((z+1)*MAP_X_SEG+x, (z+1)*MAP_X_SEG+x+1, z*MAP_X_SEG+x+1) );
        }
    }
    
    initGL();
    
    glfwFreeImage(img);
    delete img;
}
Example #2
0
void InitExample2()
{
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	g_pProgram = wolf::ProgramManager::CreateProgram("data/week6/one_texture.vsh", "data/week6/one_texture.fsh");
	g_pVB = wolf::BufferManager::CreateVertexBuffer(sizeof(Vertex) * 6);

	g_pDecl = new wolf::VertexDeclaration();
	g_pDecl->Begin();
	g_pDecl->AppendAttribute(wolf::AT_Position, 2, wolf::CT_Float);
	g_pDecl->AppendAttribute(wolf::AT_TexCoord1, 2, wolf::CT_Float);
	g_pDecl->SetVertexBuffer(g_pVB);
	g_pDecl->End();

	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, tex);

	GLFWimage img;
	glfwReadImage("data/week6/brick.tga", &img, 0);
	glTexImage2D(GL_TEXTURE_2D, 0, img.Format, img.Width, img.Height, 0, img.Format, GL_UNSIGNED_BYTE, img.Data);
	glfwFreeImage(&img);

	// These two lines are explained soon!
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

	glGenTextures(1, &tex2);
	glBindTexture(GL_TEXTURE_2D, tex2);


	glfwReadImage("data/week6/color.tga", &img, 0);
	glTexImage2D(GL_TEXTURE_2D, 0, img.Format, img.Width, img.Height, 0, img.Format, GL_UNSIGNED_BYTE, img.Data);
	glfwFreeImage(&img);

	// These two lines are explained soon!
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, tex);

	glActiveTexture(GL_TEXTURE1);
	glBindTexture(GL_TEXTURE_2D, tex2);
	
}
Example #3
0
GLuint workshopScene::textureFromFile(std::string name, std::string reldir)
{
    std::stringstream ss;
    ss << path << reldir << name;
    GLFWimage img;
    if (!glfwReadImage(ss.str().c_str(), &img, 0))
        std::cout << "Could not load texture: " << ss.str() << "\n";
    GLuint texhandle = makeTexture(img);
    glfwFreeImage(&img);
    return texhandle;
}
Example #4
0
void image_file::load_tga( const string256& str )
{
	GLFWimage img;
	i32 result = glfwReadImage( str.cstring() , &img, 0 );
	CRAP_ASSERT_DEBUG( result != GL_FALSE , "Not a valid BMP-file" );
	
	_dimension.x = img.Width;
	_dimension.y = img.Height;
	_format = img.Format;
	_bpp = img.BytesPerPixel;
	_size = img.Width * img.Height;
	_data = img.Data;
}
Example #5
0
//----------------------------------------------------------
// Builds the texture from the given TGA file. Mipmap levels
// are automatically generated
//----------------------------------------------------------
void Texture::LoadFromTGA(const std::string& p_strFile)
{
	glGenTextures(1,&m_uiTex);
	glBindTexture(GL_TEXTURE_2D, m_uiTex);

	GLFWimage img;
	glfwReadImage( p_strFile.c_str(), &img, 0 );
    glTexImage2D(GL_TEXTURE_2D, 0, img.Format, img.Width, img.Height, 0, img.Format, GL_UNSIGNED_BYTE, img.Data);
    
    m_uiWidth = img.Width;
	m_uiHeight = img.Height;
    
	glfwFreeImage(&img);

	glGenerateMipmap(GL_TEXTURE_2D);
	SetFilterMode(FM_TrilinearMipmap, FM_Linear);
}
GLuint gl4::TextureManager::_loadTextureFromTGA(const char *filename) {
	GLuint textureID = 0;
	GLFWimage img;

	if(!glfwReadImage(filename, &img, GLFW_NO_RESCALE_BIT))
		std::cerr << "   Failed to load texture from TGA file." << std::endl;

	glGenTextures(1, &textureID);
	glActiveTexture(GL_TEXTURE0);
	glBindTexture( GL_TEXTURE_2D, textureID );

	glfwLoadTextureImage2D( &img, 0 );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
	glfwFreeImage(&img); // Clean up the malloc()'ed data pointer
	return textureID;
}
Example #7
0
	TextureData2D * TextureData2D :: FromTGA ( const char * file )
	{
		TextureData2D * data = NULL;
		
		GLFWimage * image = new GLFWimage ( );

		if ( glfwReadImage ( file, image, 0 ) )
		{
			data = new TextureData2D ( image->Width, image->Height, image->BytesPerPixel );

			for ( int index = 0; index < data->Width * data->Height * data->Components; index++ )
			{
				( *data ) [index] = image->Data [index] / 255.0F;
			}
		}

		glfwFreeImage ( image );

		return data;
	}
Example #8
0
void Start_Menu_Textures::loadTexture(const char *filename, unsigned int &_id, bool pureAlpha) {


  GLFWimage image;
  assert(glfwReadImage(filename, &image, GLFW_ORIGIN_UL_BIT) != 0);

  // Make a texture object and bind it.
  glGenTextures(1, &_id);
  glBindTexture(GL_TEXTURE_2D, _id);

  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

  // For pure alpha textures, grayscale gets interpreted as alpha
  if (pureAlpha && image.Format == GL_LUMINANCE)
	image.Format = GL_ALPHA;

  // Upload the pixel data.
  glTexImage2D(GL_TEXTURE_2D, 0, image.Format, image.Width, image.Height,
			   0, image.Format, GL_UNSIGNED_BYTE, reinterpret_cast<void*>(image.Data));
  glfwFreeImage(&image);
}
/*
 * loadDistTexture - load 8-bit distance texture data
 * from a TGA file and set up the corresponding texture object.
 */
void loadDistTexture(char *filename, GLuint texID, int *texw, int *texh) {
  
  GLFWimage teximg; // Use intermediate GLFWimage to get width and height

  if(!glfwReadImage(filename, &teximg, GLFW_NO_RESCALE_BIT))
    printError("I/O error", "Failed to load distance texture from TGA file.");
  
  *texw = teximg.Width;
  *texh = teximg.Height;
  
  glActiveTextureARB(GL_TEXTURE0);
  glBindTexture( GL_TEXTURE_2D, texID );

  glfwLoadTextureImage2D( &teximg, 0 );
  // The special shader used to render this texture performs its own minification
  // and magnification. Specify nearest neighbor sampling to avoid trampling
  // over the distance values split over two channels as 8.8 fixed-point data.
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
  glfwFreeImage(&teximg); // Clean up the malloc()'ed data pointer
}
Example #10
0
Material CGraphicsModule::loadMaterial(const char *matName) {
    Material material = materials[matName];
    
    if (material.mat) {
        return useMaterial(matName);
    }
    
    GLuint textureID;
    glGenTextures( 1, &textureID );
    
    GLFWimage teximg; // Use intermediate GLFWimage to get width and height
    
    if(!glfwReadImage(matName, &teximg, GLFW_NO_RESCALE_BIT))
        printf("I/O error %s", "Failed to load distance texture from TGA file.");
    
    glActiveTexture(GL_TEXTURE0);
    glBindTexture( GL_TEXTURE_2D, textureID );
    
    glfwLoadTextureImage2D( &teximg, 0 );
    
    unsigned int width = teximg.Width, height = teximg.Height;
    
    // The special shader used to render this texture performs its own minification
    // and magnification. Specify nearest neighbor sampling to avoid trampling
    // over the distance values split over two channels as 8.8 fixed-point data.
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
    glfwFreeImage(&teximg); // Clean up the malloc()'ed data pointer
    
    Material ret = { (unsigned int)textureID, width, height };
    materials[matName] = ret;
    
    return ret;
}
Example #11
0
workshopScene::workshopScene(std::string path_)
:mouseRayCallback(btVector3(0, 0, 0), btVector3(0, 0, 0)),
 axisResult(btVector3(0, 0, 0), btVector3(0, 0, 0))
{
    path = path_;
    DIR *dir;
    dirent *ent;
    gWorld = new world();
    gWorld->btWorld->setGravity(btVector3(0, 0, 0));
    mouseConstraint = 0;
    std::stringstream ss;
    ss << path << "data/plane_10x10.bsm";
    gWorld->addObject(new physObj(0, btVector3(0, -1, 0), new btStaticPlaneShape(btVector3(0, 1, 0), 1), new model(ss.str())));
    ss.str("");
    ss << path << "assemblies";
    dir = opendir(ss.str().c_str());
    if (dir == NULL)
    {
        std::cout << "Could not open assemblies directory.\n";
        return;
    }
    int i = 0;
    while ((ent = readdir(dir)) != NULL)
    {
        if (ent->d_type == DT_DIR && ++i > 2)     //discard "." and ".."
        {
            partnames.push_back(ent->d_name);
        }
    }
    closedir(dir);

    for (unsigned int i = 0; i < partnames.size(); i++)
    {
        std::cout << "Part: " << partnames[i] << "\n";
        std::stringstream ss;
        ss << path << "assemblies/" << partnames[i] << "/thumb.tga";
        std::cout << ss.str() << "\n";
        GLFWimage img;
        if (glfwReadImage(ss.str().c_str(), &img, 0))
        {
            thumbnails.push_back(makeTexture(img));
            glfwFreeImage(&img);
        }
        else
        {
            thumbnails.push_back(0);
            std::cout << "Could not load thumbnail for " << partnames[i] << "\n";
        }
    }
    cursor = textureFromFile("cursor.tga");
    panel = ninePatch(textureFromFile("9patch.tga"));
    button = ninePatch(textureFromFile("button.tga"));
    bubble = ninePatch(textureFromFile("bubble.tga"), 8, 16, 12, 8, 0.25, 0.5, 0.375, 0.75);
    tooltextures.push_back(textureFromFile("drag.tga"));
    tooltextures.push_back(textureFromFile("axis.tga"));
    tooltextures.push_back(textureFromFile("delete.tga"));
    font = textureFromFile("font.tga");
    cursorx = 0;
    cursory = 0;
    mousevelx = 0;
    mousevely = 0;
    mouseWasCaptured = false;
    camera.position = btVector3(0, 0, 10);
    camera.pitch = 0;
    camera.yaw = 0;
    camera.orientationFromAngles();
    selectedItem = -1;
    selectedTool = -1;
    static unsigned char fontpixels[STB_SOMEFONT_BITMAP_HEIGHT][STB_SOMEFONT_BITMAP_WIDTH];
    STB_SOMEFONT_CREATE(fontdata, fontpixels, STB_SOMEFONT_BITMAP_HEIGHT);
    glGenTextures(1, &stbfont);
    glBindTexture(GL_TEXTURE_2D, stbfont);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_WRAP_BORDER);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_WRAP_BORDER);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, STB_SOMEFONT_BITMAP_HEIGHT, STB_SOMEFONT_BITMAP_WIDTH, 0, GL_ALPHA, GL_UNSIGNED_BYTE, fontpixels);
}