Model + Texture
This commit is contained in:
@@ -40,17 +40,17 @@
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<LibraryPath>$(SolutionDir)\Libraries\glew-1.10.0\lib\Debug\Win32;$(SolutionDir)\Libraries\glfw-3.0.4\lib\Debug;$(LibraryPath)</LibraryPath>
|
||||
<LibraryPath>H:\Högskolan\Design projekt\Escape the Dawn\Escape-the-Dawn\Libraries\SOIL\lib\Debug;$(SolutionDir)\Libraries\glew-1.10.0\lib\Debug\Win32;$(SolutionDir)\Libraries\glfw-3.0.4\lib\Debug;$(LibraryPath)</LibraryPath>
|
||||
<OutDir>$(SolutionDir)bin\</OutDir>
|
||||
<IntDir>$(SolutionDir)obj\$(ProjectName)\</IntDir>
|
||||
<TargetName>$(ProjectName)-$(Configuration)</TargetName>
|
||||
<IncludePath>$(ProjectDir);$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath)</IncludePath>
|
||||
<IncludePath>H:\Högskolan\Design projekt\Escape the Dawn\Escape-the-Dawn\Libraries\SOIL\src;$(ProjectDir);$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath)</IncludePath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<IncludePath>$(ProjectDir);$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath)</IncludePath>
|
||||
<IncludePath>H:\Högskolan\Design projekt\Escape the Dawn\Escape-the-Dawn\Libraries\SOIL\src;$(ProjectDir);$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath)</IncludePath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<LibraryPath>$(SolutionDir)\Libraries\glew-1.10.0\lib\Release\Win32;$(SolutionDir)\Libraries\glfw-3.0.4\lib\Release;$(LibraryPath)</LibraryPath>
|
||||
<LibraryPath>H:\Högskolan\Design projekt\Escape the Dawn\Escape-the-Dawn\Libraries\SOIL\lib\Release;$(SolutionDir)\Libraries\glew-1.10.0\lib\Release\Win32;$(SolutionDir)\Libraries\glfw-3.0.4\lib\Release;$(LibraryPath)</LibraryPath>
|
||||
<OutDir>$(SolutionDir)bin\</OutDir>
|
||||
<IntDir>$(SolutionDir)obj\$(ProjectName)\</IntDir>
|
||||
<TargetName>$(ProjectName)-$(Configuration)</TargetName>
|
||||
@@ -70,7 +70,7 @@
|
||||
<PostBuildEvent />
|
||||
<PostBuildEvent />
|
||||
<Lib>
|
||||
<AdditionalDependencies>opengl32.lib;glu32.lib;glew32sd.lib;glfw3.lib</AdditionalDependencies>
|
||||
<AdditionalDependencies>opengl32.lib;glu32.lib;soil.lib;glew32sd.lib;glfw3.lib</AdditionalDependencies>
|
||||
<ForceSymbolReferences>
|
||||
</ForceSymbolReferences>
|
||||
</Lib>
|
||||
@@ -93,7 +93,7 @@
|
||||
<AdditionalDependencies>opengl32.lib;glu32.lib;glew32s.lib;glfw3.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
<Lib>
|
||||
<AdditionalDependencies>opengl32.lib;glu32.lib;glew32s.lib;glfw3.lib;</AdditionalDependencies>
|
||||
<AdditionalDependencies>opengl32.lib;glu32.lib;soil.lib;glew32s.lib;glfw3.lib;</AdditionalDependencies>
|
||||
</Lib>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
@@ -101,6 +101,7 @@
|
||||
<ClCompile Include="Model.cpp" />
|
||||
<ClCompile Include="Renderer\Renderer.cpp" />
|
||||
<ClCompile Include="Systems\CollisionSystem.cpp" />
|
||||
<ClCompile Include="Texture.cpp" />
|
||||
<ClCompile Include="World.cpp" />
|
||||
<ClCompile Include="Systems\InputSystem.cpp" />
|
||||
<ClCompile Include="Systems\LevelGenerationSystem.cpp" />
|
||||
@@ -142,6 +143,7 @@
|
||||
<ClInclude Include="Systems\PlayerSystem.h" />
|
||||
<ClInclude Include="Systems\RenderSystem.h" />
|
||||
<ClInclude Include="Systems\SoundSystem.h" />
|
||||
<ClInclude Include="Texture.h" />
|
||||
<ClInclude Include="World.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
</ClCompile>
|
||||
<ClCompile Include="Model.cpp" />
|
||||
<ClCompile Include="Engine.h" />
|
||||
<ClCompile Include="Texture.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Component.h" />
|
||||
@@ -107,6 +108,7 @@
|
||||
<Filter>Util</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Renderer.h" />
|
||||
<ClInclude Include="Texture.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Shaders\Fragment.glsl">
|
||||
|
||||
+148
-42
@@ -1,72 +1,177 @@
|
||||
#include "Model.h"
|
||||
|
||||
|
||||
Model::Model()
|
||||
Model::Model(const char* path)
|
||||
{
|
||||
temp_CreateModelShit();
|
||||
|
||||
|
||||
Loadobj(path, Vertices, Normals, TextureCoords);
|
||||
CreateBuffers(Vertices, Normals, TextureCoords);
|
||||
}
|
||||
|
||||
|
||||
void Model::temp_CreateModelShit()
|
||||
bool Model::Loadobj(const char* path, std::vector <glm::vec3> & out_vertices, std::vector <glm::vec3> &out_normals, std::vector <glm::vec2> & out_TextureCoords)
|
||||
{
|
||||
Vertices.push_back(0.0f);
|
||||
Vertices.push_back(0.0f);
|
||||
Vertices.push_back(2.0f);
|
||||
std::vector< unsigned int > vertexIndices, TextureCoordIndices, normalIndices;
|
||||
std::vector< glm::vec3 > temp_vertices;
|
||||
std::vector< glm::vec2 > temp_TextureCoords;
|
||||
std::vector< glm::vec3 > temp_normals;
|
||||
|
||||
Vertices.push_back(0.0f);
|
||||
Vertices.push_back(1.0f);
|
||||
Vertices.push_back(2.0f);
|
||||
FILE * file = fopen(path, "r");
|
||||
LOG_INFO("Loading .obj file");
|
||||
if( file == NULL )
|
||||
{
|
||||
LOG_INFO("Load .obj file: failed");
|
||||
return false;
|
||||
}
|
||||
char lineHeader[512];
|
||||
|
||||
Vertices.push_back(1.0f);
|
||||
Vertices.push_back(0.0f);
|
||||
Vertices.push_back(2.0f);
|
||||
while(true)
|
||||
{
|
||||
|
||||
//read the first word of the line
|
||||
int res = fscanf(file, "%s", lineHeader);
|
||||
|
||||
Colors.push_back(1.0f);
|
||||
Colors.push_back(1.0f);
|
||||
Colors.push_back(1.0f);
|
||||
Colors.push_back(1.0f);
|
||||
if( res == EOF ) // EOF - End Of File
|
||||
{
|
||||
for( unsigned int i = 0; i < vertexIndices.size(); i++ )
|
||||
{
|
||||
unsigned int vertexIndex = vertexIndices[i];
|
||||
glm::vec3 vertex = temp_vertices[ vertexIndex-1];
|
||||
out_vertices.push_back(vertex);
|
||||
|
||||
Colors.push_back(1.0f);
|
||||
Colors.push_back(1.0f);
|
||||
Colors.push_back(1.0f);
|
||||
Colors.push_back(1.0f);
|
||||
}
|
||||
|
||||
Colors.push_back(1.0f);
|
||||
Colors.push_back(1.0f);
|
||||
Colors.push_back(1.0f);
|
||||
Colors.push_back(1.0f);
|
||||
for( unsigned int i = 0; i < TextureCoordIndices.size(); i++ )
|
||||
{
|
||||
unsigned int TextureCoordIndex = TextureCoordIndices[i];
|
||||
glm::vec2 TextureCoord = temp_TextureCoords[ TextureCoordIndex-1];
|
||||
out_TextureCoords.push_back(TextureCoord);
|
||||
}
|
||||
|
||||
for( unsigned int i = 0; i < normalIndices.size(); i++ )
|
||||
{
|
||||
unsigned int normalIndex = normalIndices[i];
|
||||
glm::vec3 normal = temp_normals[ normalIndex-1];
|
||||
out_normals.push_back(normal);
|
||||
}
|
||||
|
||||
|
||||
LOG_INFO("Model Loaded\n");
|
||||
break;
|
||||
}
|
||||
|
||||
if( strcmp( lineHeader, "v" ) == 0 ) // vertex
|
||||
{
|
||||
glm::vec3 vertex;
|
||||
fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z);
|
||||
temp_vertices.push_back(vertex);
|
||||
}
|
||||
else if ( strcmp( lineHeader, "vt" ) == 0 ) // texture coordinate
|
||||
{
|
||||
glm::vec2 TextureCoord;
|
||||
fscanf(file, "%f %f\n", &TextureCoord.x, &TextureCoord.y );
|
||||
temp_TextureCoords.push_back(TextureCoord);
|
||||
}
|
||||
else if( strcmp( lineHeader, "vn" ) == 0 ) // normal
|
||||
{
|
||||
glm::vec3 normal;
|
||||
fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z );
|
||||
temp_normals.push_back(normal);
|
||||
}
|
||||
else if( strcmp( lineHeader, "f" ) == 0)
|
||||
{
|
||||
unsigned int vertexIndex[3], TextureCoordIndex[3], normalIndex[3];
|
||||
int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIndex[0], &TextureCoordIndex[0], &normalIndex[0], &vertexIndex[1], &TextureCoordIndex[1], &normalIndex[1],&vertexIndex[2], &TextureCoordIndex[2], &normalIndex[2]);
|
||||
if(matches != 9)
|
||||
{
|
||||
printf("File can't be read, try exporting with other options\n");
|
||||
return false;
|
||||
}
|
||||
vertexIndices.push_back(vertexIndex[0]);
|
||||
vertexIndices.push_back(vertexIndex[1]);
|
||||
vertexIndices.push_back(vertexIndex[2]);
|
||||
TextureCoordIndices.push_back(TextureCoordIndex[0]);
|
||||
TextureCoordIndices.push_back(TextureCoordIndex[1]);
|
||||
TextureCoordIndices.push_back(TextureCoordIndex[2]);
|
||||
normalIndices.push_back(normalIndex[0]);
|
||||
normalIndices.push_back(normalIndex[1]);
|
||||
normalIndices.push_back(normalIndex[2]);
|
||||
|
||||
}
|
||||
else if ( strcmp( lineHeader, "mtllib" ) == 0 )
|
||||
{
|
||||
|
||||
const char* fileName;
|
||||
fscanf(file, "%s\n", &fileName);
|
||||
|
||||
|
||||
FILE * mtlfile = fopen(fileName, "r");
|
||||
LOG_INFO("Loading .mtl file");
|
||||
if( mtlfile == NULL )
|
||||
{
|
||||
LOG_INFO("Load .mtl file: failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
char mtllineHeader[512];
|
||||
//read the first word of the line
|
||||
|
||||
while (true)
|
||||
{
|
||||
int mtlres = fscanf(mtlfile, "%s", mtllineHeader);
|
||||
|
||||
if( mtlres == EOF ) // EOF - End Of File
|
||||
{
|
||||
|
||||
break;
|
||||
}
|
||||
else if ( strcmp( mtllineHeader, "map_Kd" ) == 0 )
|
||||
{
|
||||
const char* textureFileName;
|
||||
fscanf(mtlfile, "%s", textureFileName);
|
||||
texture.push_back(std::make_shared<Texture>(textureFileName));
|
||||
LOG_INFO("Texture Loaded\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
void Model::CreateBuffers( std::vector<float> _Vertices, std::vector<float> _Normals, std::vector<float> _TextureCoords )
|
||||
void Model::CreateBuffers( std::vector<glm::vec3> _Vertices, std::vector<glm::vec3> _Normals, std::vector<glm::vec2>_TextureCoords)
|
||||
{
|
||||
glGenBuffers(1, &VertBuffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VertBuffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, Vertices.size()*sizeof(float), Vertices.data(), GL_STATIC_DRAW);
|
||||
GLERROR("GLEW: BufferFail1");
|
||||
|
||||
glGenBuffers(1, &Colorbuffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, Colorbuffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, Colors.size()*sizeof(float), Colors.data(), GL_STATIC_DRAW);
|
||||
GLERROR("GLEW: BufferFail2");
|
||||
LOG_INFO("Generating VertexBuffer");
|
||||
glGenBuffers(1, &VertexBuffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VertexBuffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, _Vertices.size() * sizeof(glm::vec3), &_Vertices[0], GL_STATIC_DRAW);
|
||||
GLERROR("GLEW: BufferFail, VertexBuffer");
|
||||
|
||||
// glGenBuffers(1, &TextureCoordBuffer);
|
||||
// glBindBuffer(GL_ARRAY_BUFFER, TextureCoordBuffer);
|
||||
// glBufferData(GL_ARRAY_BUFFER, TextureCoords.size()*sizeof(float), TextureCoords.data(), GL_STATIC_DRAW);
|
||||
// GLERROR("GLEW: BufferFail3");
|
||||
LOG_INFO("Generating NormalBuffer");
|
||||
glGenBuffers(1, &NormalBuffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, NormalBuffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, _Normals.size() * sizeof(glm::vec3), &_Normals[0], GL_STATIC_DRAW);
|
||||
GLERROR("GLEW: BufferFail, NormalBuffer");
|
||||
|
||||
|
||||
LOG_INFO("Generating textureCoordBuffer");
|
||||
glGenBuffers(1, &TextureCoordBuffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, TextureCoordBuffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, _TextureCoords.size() * sizeof(glm::vec2), &_TextureCoords[0], GL_STATIC_DRAW);
|
||||
GLERROR("GLEW: BufferFail, TextureCoordBuffer");
|
||||
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glBindVertexArray(VAO);
|
||||
GLERROR("GLEW: BufferFail4");
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VertBuffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VertexBuffer);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
GLERROR("GLEW: BufferFail5");
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, Colorbuffer);
|
||||
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
|
||||
// glBindBuffer(GL_ARRAY_BUFFER, TextureCoordBuffer);
|
||||
// glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
@@ -77,3 +182,4 @@ void Model::CreateBuffers( std::vector<float> _Vertices, std::vector<float> _Nor
|
||||
GLERROR("GLEW: BufferFail5");
|
||||
}
|
||||
|
||||
|
||||
|
||||
+21
-10
@@ -18,24 +18,35 @@
|
||||
#include "glerror.h"
|
||||
#include <cstdlib>
|
||||
|
||||
#include "Texture.h"
|
||||
|
||||
class Model
|
||||
{
|
||||
public:
|
||||
glm::vec3 Position;
|
||||
std::vector<float> Colors;
|
||||
std::vector<float> Vertices;
|
||||
std::vector<float> Normals;
|
||||
std::vector<float> TextureCoords;
|
||||
std::vector<glm::vec3> Vertices;
|
||||
std::vector<glm::vec3> Normals;
|
||||
std::vector<glm::vec2> TextureCoords;
|
||||
|
||||
GLuint Colorbuffer;
|
||||
GLuint VertBuffer;
|
||||
GLuint VertexBuffer;
|
||||
GLuint NormalBuffer;
|
||||
GLuint TextureCoordBuffer;
|
||||
GLuint VAO;
|
||||
|
||||
Model();
|
||||
std::vector<std::shared_ptr<Texture>> texture;
|
||||
|
||||
void temp_CreateModelShit();
|
||||
void CreateBuffers(std::vector<float>, std::vector<float>, std::vector<float>);
|
||||
Model(const char* path);
|
||||
|
||||
bool Loadobj(
|
||||
const char* path,
|
||||
std::vector <glm::vec3> & out_vertices,
|
||||
std::vector <glm::vec3> &out_normals,
|
||||
std::vector <glm::vec2> & out_TextureCoords
|
||||
);
|
||||
|
||||
void CreateBuffers(
|
||||
std::vector<glm::vec3> _Vertices,
|
||||
std::vector<glm::vec3> _Normals,
|
||||
std::vector<glm::vec2>_TextureCoords
|
||||
);
|
||||
|
||||
};
|
||||
@@ -5,7 +5,7 @@ uniform sampler2D texture0;
|
||||
|
||||
in VertexData {
|
||||
vec3 Position;
|
||||
vec3 Color;
|
||||
vec3 Normal;
|
||||
vec2 TextureCoord;
|
||||
} Input;
|
||||
|
||||
@@ -14,5 +14,5 @@ out vec4 FragmentColor;
|
||||
void main()
|
||||
{
|
||||
vec4 texel = texture2D(texture0, Input.TextureCoord);
|
||||
FragmentColor = texel * vec4(Input.Color, 0);
|
||||
FragmentColor = texel;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#include "Texture.h"
|
||||
|
||||
Texture::Texture( const char* path )
|
||||
{
|
||||
Load(path);
|
||||
}
|
||||
|
||||
|
||||
void Texture::Load( const char* path )
|
||||
{
|
||||
texture = SOIL_load_OGL_texture(path, 0, 0, SOIL_FLAG_INVERT_Y);
|
||||
}
|
||||
|
||||
void Texture::Bind()
|
||||
{
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
}
|
||||
|
||||
Texture::~Texture()
|
||||
{
|
||||
glDeleteTextures(1, &texture);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
#include <SOIL.h>
|
||||
#include "Renderer.h"
|
||||
|
||||
|
||||
class Texture
|
||||
{
|
||||
public:
|
||||
|
||||
Texture(const char* path);
|
||||
|
||||
~Texture();
|
||||
|
||||
GLuint texture;
|
||||
|
||||
void Load(const char* path);
|
||||
void Bind();
|
||||
};
|
||||
Binary file not shown.
@@ -0,0 +1,320 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
||||
<HTML>
|
||||
<HEAD>
|
||||
<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=windows-1252">
|
||||
<TITLE>lonesock.net: SOIL</TITLE>
|
||||
<style type="text/css">
|
||||
<!--
|
||||
body { color: #000000; background-color: #FFFFFF; }
|
||||
.style1 { color: #505050; }
|
||||
.style2 { color: #A0A0A0; }
|
||||
.style3 { color: #8080FF; font-weight: bold; }
|
||||
.style15 { color: #8080FF; font-weight: bold; }
|
||||
.style17 { color: #008080; }
|
||||
.style18 { color: #800000; }
|
||||
.style4 { color: #F000F0; }
|
||||
.style5 { color: #0000A0; font-weight: bold; }
|
||||
.style16 { color: #00A000; font-weight: bold; }
|
||||
.style6 { color: #0000FF; }
|
||||
.style12 { color: #0000FF; }
|
||||
.style7 { color: #E0A000; }
|
||||
.style8 { color: #000000; }
|
||||
.style9 { color: #00A000; }
|
||||
.style10 { color: #FF0000; }
|
||||
.style34 { color: #000000; background-color: #80FFFF; font-weight: bold; }
|
||||
.style35 { color: #FFFFFF; background-color: #FF0000; font-weight: bold; }
|
||||
--></style>
|
||||
</HEAD>
|
||||
<BODY LANG="en-US" DIR="LTR">
|
||||
<PRE STYLE="margin-bottom: 0.2in; text-align: center">
|
||||
<FONT SIZE=4 STYLE="font-size: 16pt"><B>Simple OpenGL Image Library</B></FONT>
|
||||
</PRE>
|
||||
<P>
|
||||
<B>Introduction:</B><BR><BR>SOIL is a tiny C library used primarily
|
||||
for uploading textures into OpenGL. It is based on stb_image version
|
||||
1.16, the public domain code from Sean Barrett (found <A HREF="http://www.nothings.org/stb_image.c">here</A>).
|
||||
I have extended it to load TGA and DDS files, and to perform common
|
||||
functions needed in loading OpenGL textures. SOIL can also be used
|
||||
to save and load images in a variety of formats (useful for loading
|
||||
height maps, non-OpenGL applications, etc.)<BR>
|
||||
<BR>
|
||||
<B>Download:</B>
|
||||
<BR>
|
||||
<BR>
|
||||
You can grab the latest version of SOIL <A HREF="http://www.lonesock.net/files/soil.zip">here</A>.
|
||||
(July 7, 2008: see the change log at the bottom of this page.)<BR>
|
||||
You can also checkout the latest code from the new SVN repository, login as guest/guest:<BR>
|
||||
<A HREF="svn://www.twisted-works.com/jdummer/public/SOIL">svn://www.twisted-works.com/jdummer/public/SOIL</A><BR>
|
||||
(thanks for the SVN hosting, Sherief!)
|
||||
<BR>
|
||||
<BR>
|
||||
<B>License:</B>
|
||||
<BR>
|
||||
<BR>
|
||||
Public Domain
|
||||
<BR>
|
||||
<BR>
|
||||
<B>Features:</B>
|
||||
</P>
|
||||
<UL>
|
||||
<LI>Readable Image Formats:
|
||||
<UL>
|
||||
<LI>BMP - non-1bpp, non-RLE (from stb_image documentation)
|
||||
<LI>PNG - non-interlaced (from stb_image documentation)
|
||||
<LI>JPG - JPEG baseline (from stb_image documentation)
|
||||
<LI>TGA - greyscale or RGB or RGBA or indexed, uncompressed or RLE
|
||||
<LI>DDS - DXT1/2/3/4/5, uncompressed, cubemaps (can't read 3D DDS files yet)
|
||||
<LI>PSD - (from stb_image documentation)
|
||||
<LI>HDR - converted to LDR, unless loaded with *HDR* functions (RGBE or RGBdivA or RGBdivA2)
|
||||
</UL>
|
||||
<LI>Writeable Image Formats:
|
||||
<UL>
|
||||
<LI>TGA - Greyscale or RGB or RGBA, uncompressed
|
||||
<LI>BMP - RGB, uncompressed
|
||||
<LI>DDS - RGB as DXT1, or RGBA as DXT5
|
||||
</UL>
|
||||
<LI>Can load an image file directly into a 2D OpenGL texture, optionally performing the following functions:
|
||||
<UL>
|
||||
<LI>Can generate a new texture handle, or reuse one specified
|
||||
<LI>Can automatically rescale the image to the next largest power-of-two size
|
||||
<LI>Can automatically create MIPmaps
|
||||
<LI>Can scale (not simply clamp) the RGB values into the "safe range" for NTSC displays (16 to 235, as recommended <a href="http://msdn2.microsoft.com/en-us/library/bb174608.aspx#NTSC_Suggestions">here</a>)
|
||||
<LI>Can multiply alpha on load (for more correct blending / compositing)
|
||||
<LI>Can flip the image vertically
|
||||
<LI>Can compress and upload any image as DXT1 or DXT5 (if EXT_texture_compression_s3tc is available), using an internal (very fast!) compressor
|
||||
<LI>Can convert the RGB to YCoCg color space (useful with DXT5 compression: see <a href="http://developer.nvidia.com/object/real-time-ycocg-dxt-compression.html">this link</a> from NVIDIA)
|
||||
<LI>Will automatically downsize a texture if it is larger than GL_MAX_TEXTURE_SIZE
|
||||
<LI>Can directly upload DDS files (DXT1/3/5/uncompressed/cubemap, with or without MIPmaps). Note: directly uploading the compressed DDS image will disable the other options (no flipping, no pre-multiplying alpha, no rescaling, no creation of MIPmaps, no auto-downsizing)
|
||||
<LI>Can load rectangluar textures for GUI elements or splash screens (requires GL_ARB/EXT/NV_texture_rectangle)
|
||||
</UL>
|
||||
<LI>Can decompress images from RAM (e.g. via <a href="http://icculus.org/physfs/">PhysicsFS</a> or similar) into an OpenGL texture (same features as regular 2D textures, above)
|
||||
<LI>Can load cube maps directly into an OpenGL texture (same features as regular 2D textures, above)
|
||||
<UL>
|
||||
<LI>Can take six image files directly into an OpenGL cube map texture
|
||||
<LI>Can take a single image file where width = 6*height (or vice versa), split it into an OpenGL cube map texture
|
||||
</UL>
|
||||
<LI>No external dependencies
|
||||
<LI>Tiny
|
||||
<LI>Cross platform (Windows, *nix, Mac OS X)
|
||||
<LI>Public Domain
|
||||
</UL>
|
||||
|
||||
<PRE STYLE="margin-bottom: 0.2in"><B>ToDo:</B></PRE>
|
||||
<UL>
|
||||
<LI>More testing
|
||||
<LI>add HDR functions to load from memory and load to RGBE unsigned char*
|
||||
</UL>
|
||||
<P><BR><B>Usage:</B><BR><BR>SOIL is meant to be used as a static
|
||||
library (as it's tiny and in the public domain). You can use the static
|
||||
library file included in the zip (libSOIL.a works for MinGW and Microsoft
|
||||
compilers...feel free to rename it to SOIL.lib if that makes you happy),
|
||||
or compile the library yourself. The code is cross-platform and has been
|
||||
tested on Windows, Linux, and Mac. (The heaviest testing has been on the
|
||||
Windows platform, so feel free to email me if you find any issues with
|
||||
other platforms.)
|
||||
<BR><BR>Simply include SOIL.h in your C or C++ file,
|
||||
link in the static library, and then use any of SOIL's functions. The
|
||||
file SOIL.h contains simple doxygen style documentation. (If you use the
|
||||
static library, no other header files are needed besides SOIL.h) Below
|
||||
are some simple usage examples:
|
||||
</P>
|
||||
<pre>
|
||||
<code><span style="font: 8pt Courier New;"><span class="style1">/* load an image file directly as a new OpenGL texture */
|
||||
</span><span class="style11">GLuint tex_2d </span><span class="style10">= </span><span class="style11">SOIL_load_OGL_texture
|
||||
</span><span class="style10">(
|
||||
</span><span class="style6">"img.png"</span><span class="style10">,
|
||||
</span><span class="style11">SOIL_LOAD_AUTO</span><span class="style10">,
|
||||
</span><span class="style11">SOIL_CREATE_NEW_ID</span><span class="style10">,
|
||||
</span><span class="style11">SOIL_FLAG_MIPMAPS </span><span class="style10">| </span><span class="style11">SOIL_FLAG_INVERT_Y </span><span class="style10">| </span><span class="style11">SOIL_FLAG_NTSC_SAFE_RGB </span><span class="style10">| </span><span class="style11">SOIL_FLAG_COMPRESS_TO_DXT
|
||||
</span><span class="style10">);
|
||||
|
||||
</span><span class="style1">/* check for an error during the load process */
|
||||
</span><span class="style5">if</span><span class="style10">( </span><span class="style4">0 </span><span class="style10">== </span><span class="style11">tex_2d </span><span class="style10">)
|
||||
{
|
||||
</span><span class="style11">printf</span><span class="style10">( </span><span class="style6">"SOIL loading error: '%s'\n"</span><span class="style10">, </span><span class="style11">SOIL_last_result</span><span class="style10">() );
|
||||
}
|
||||
|
||||
</span><span class="style1">/* load another image, but into the same texture ID, overwriting the last one */
|
||||
</span><span class="style11">tex_2d </span><span class="style10">= </span><span class="style11">SOIL_load_OGL_texture
|
||||
</span><span class="style10">(
|
||||
</span><span class="style6">"some_other_img.dds"</span><span class="style10">,
|
||||
</span><span class="style11">SOIL_LOAD_AUTO</span><span class="style10">,
|
||||
</span><span class="style11">tex_2d</span><span class="style10">,
|
||||
</span><span class="style11">SOIL_FLAG_DDS_LOAD_DIRECT
|
||||
</span><span class="style10">);
|
||||
|
||||
</span><span class="style1">/* load 6 images into a new OpenGL cube map, forcing RGB */
|
||||
</span><span class="style11">GLuint tex_cube </span><span class="style10">= </span><span class="style11">SOIL_load_OGL_cubemap
|
||||
</span><span class="style10">(
|
||||
</span><span class="style6">"xp.jpg"</span><span class="style10">,
|
||||
</span><span class="style6">"xn.jpg"</span><span class="style10">,
|
||||
</span><span class="style6">"yp.jpg"</span><span class="style10">,
|
||||
</span><span class="style6">"yn.jpg"</span><span class="style10">,
|
||||
</span><span class="style6">"zp.jpg"</span><span class="style10">,
|
||||
</span><span class="style6">"zn.jpg"</span><span class="style10">,
|
||||
</span><span class="style11">SOIL_LOAD_RGB</span><span class="style10">,
|
||||
</span><span class="style11">SOIL_CREATE_NEW_ID</span><span class="style10">,
|
||||
</span><span class="style11">SOIL_FLAG_MIPMAPS
|
||||
</span><span class="style10">);
|
||||
|
||||
</span><span class="style1">/* load and split a single image into a new OpenGL cube map, default format */
|
||||
</span><span class="style1">/* face order = East South West North Up Down => "ESWNUD", case sensitive! */
|
||||
</span><span class="style11">GLuint single_tex_cube </span><span class="style10">= </span><span class="style11">SOIL_load_OGL_single_cubemap
|
||||
</span><span class="style10">(
|
||||
</span><span class="style6">"split_cubemap.png"</span><span class="style10">,
|
||||
</span><span class="style6">"EWUDNS"</span><span class="style10">,
|
||||
</span><span class="style11">SOIL_LOAD_AUTO</span><span class="style10">,
|
||||
</span><span class="style11">SOIL_CREATE_NEW_ID</span><span class="style10">,
|
||||
</span><span class="style11">SOIL_FLAG_MIPMAPS
|
||||
</span><span class="style10">);
|
||||
|
||||
</span><span class="style1">/* actually, load a DDS cubemap over the last OpenGL cube map, default format */
|
||||
</span><span class="style1">/* try to load it directly, but give the order of the faces in case that fails */
|
||||
</span><span class="style1">/* the DDS cubemap face order is pre-defined as SOIL_DDS_CUBEMAP_FACE_ORDER */
|
||||
</span><span class="style11">single_tex_cube </span><span class="style10">= </span><span class="style11">SOIL_load_OGL_single_cubemap
|
||||
</span><span class="style10">(
|
||||
</span><span class="style6">"overwrite_cubemap.dds"</span><span class="style10">,
|
||||
</span><span class="style11">SOIL_DDS_CUBEMAP_FACE_ORDER</span><span class="style10">,
|
||||
</span><span class="style11">SOIL_LOAD_AUTO</span><span class="style10">,
|
||||
</span><span class="style11">single_tex_cube</span><span class="style10">,
|
||||
</span><span class="style11">SOIL_FLAG_MIPMAPS | SOIL_FLAG_DDS_LOAD_DIRECT
|
||||
</span><span class="style10">);
|
||||
|
||||
</span><span class="style1">/* load an image as a heightmap, forcing greyscale (so channels should be 1) */
|
||||
</span><span class="style5">int </span><span class="style11">width</span><span class="style10">, </span><span class="style11">height</span><span class="style10">, </span><span class="style11">channels</span><span class="style10">;
|
||||
</span><span class="style5">unsigned char </span><span class="style10">*</span><span class="style11">ht_map </span><span class="style10">= </span><span class="style11">SOIL_load_image
|
||||
</span><span class="style10">(
|
||||
</span><span class="style6">"terrain.tga"</span><span class="style10">,
|
||||
&</span><span class="style11">width</span><span class="style10">, &</span><span class="style11">height</span><span class="style10">, &</span><span class="style11">channels</span><span class="style10">,
|
||||
</span><span class="style11">SOIL_LOAD_L
|
||||
</span><span class="style10">);
|
||||
|
||||
</span><span class="style1">/* save that image as another type */
|
||||
</span><span class="style5">int </span><span class="style11">save_result </span><span class="style10">= </span><span class="style11">SOIL_save_image
|
||||
</span><span class="style10">(
|
||||
</span><span class="style6">"new_terrain.dds"</span><span class="style10">,
|
||||
</span><span class="style11">SOIL_SAVE_TYPE_DDS</span><span class="style10">,
|
||||
</span><span class="style11">width</span><span class="style10">, </span><span class="style11">height</span><span class="style10">, </span><span class="style11">channels</span><span class="style10">,
|
||||
</span><span class="style11">ht_map
|
||||
</span><span class="style10">);
|
||||
|
||||
</span><span class="style1">/* save a screenshot of your awesome OpenGL game engine, running at 1024x768 */
|
||||
</span><span class="style11">save_result </span><span class="style10">= </span><span class="style11">SOIL_save_screenshot
|
||||
</span><span class="style10">(
|
||||
</span><span class="style6">"awesomenessity.bmp"</span><span class="style10">,
|
||||
</span><span class="style11">SOIL_SAVE_TYPE_BMP</span><span class="style10">,
|
||||
</span><span class="style4">0</span><span class="style10">, </span><span class="style4">0</span><span class="style10">, </span><span class="style4">1024</span><span class="style10">, </span><span class="style4">768
|
||||
</span><span class="style10">);
|
||||
|
||||
</span><span class="style1">/* loaded a file via PhysicsFS, need to decompress the image from RAM, */
|
||||
</span><span class="style1">/* where it's in a buffer: unsigned char *image_in_RAM */
|
||||
</span><span class="style11">GLuint tex_2d_from_RAM </span><span class="style10">= </span><span class="style11">SOIL_load_OGL_texture_from_memory
|
||||
</span><span class="style10">(
|
||||
</span><span class="style11">image_in_RAM</span><span class="style10">,
|
||||
</span><span class="style11">image_in_RAM_bytes</span><span class="style10">,
|
||||
</span><span class="style11">SOIL_LOAD_AUTO</span><span class="style10">,
|
||||
</span><span class="style11">SOIL_CREATE_NEW_ID</span><span class="style10">,
|
||||
</span><span class="style11">SOIL_FLAG_MIPMAPS </span><span class="style10">| </span><span class="style11">SOIL_FLAG_INVERT_Y </span><span class="style10">| </span><span class="style11">SOIL_FLAG_COMPRESS_TO_DXT
|
||||
</span><span class="style10">);
|
||||
|
||||
</span><span class="style1">/* done with the heightmap, free up the RAM */
|
||||
</span><span class="style11">SOIL_free_image_data</span><span class="style10">( </span><span class="style11">ht_map </span><span class="style10">);</span></span>
|
||||
</code></pre>
|
||||
<BR>
|
||||
<BR>
|
||||
<B>Change Log:</B>
|
||||
<UL>
|
||||
<LI>July 7, 2008
|
||||
<UL>
|
||||
<LI>upgraded to stb_image 1.16 (threadsafe! loads PSD and HDR formats)
|
||||
<LI>removed <B>inline</B> keyword from native SOIL functions (thanks Sherief, Boder, Amnesiac5!)
|
||||
<LI>added SOIL_load_OGL_HDR_texture (loads a Radience HDR file into RGBE, RGB/a, RGB/A^2)
|
||||
<LI>fixed a potential bug loading DDS files with a filename
|
||||
<LI>added a VC9 project file (thanks Sherief!)
|
||||
</UL>
|
||||
<LI>November 10, 2007: added SOIL_FLAG_TEXTURE_RECTANGLE (pixel addressed non POT, useful for GUI, splash screens, etc.). Not useful with cubemaps, and disables repeating and MIPmaps.
|
||||
<LI>November 8, 2007
|
||||
<UL>
|
||||
<LI>upgraded to stb_image 1.07
|
||||
<LI>fixed some includes and defines for compiling on OS X (thanks Mogui and swiftcoder!)
|
||||
</UL>
|
||||
<LI>October 30, 2007
|
||||
<UL>
|
||||
<LI>upgraded to stb_image 1.04, some tiny bug fixes
|
||||
<LI>there is now a makefile (under projects) for ease of building under Linux (thanks D J Peters!)
|
||||
<LI>Visual Studio 6/2003/2005 projects are working again
|
||||
<LI>patched SOIL for better pointer handling of the glCompressedTexImage2D extension (thanks Peter Sperl!)
|
||||
<LI>fixed DDS loading when force_channels=4 but there was no alpha; it was returning 3 channels. (Thanks LaurentGom!)
|
||||
<LI>fixed a bunch of channel issues in general. (Thanks Sean Barrett!)
|
||||
</UL>
|
||||
<LI>October 27, 2007
|
||||
<UL>
|
||||
<LI>correctly reports when there is no OpenGL context (thanks Merick Zero!)
|
||||
<LI>upgraded to stb_image 1.03 with support for loading the HDR image format
|
||||
<LI>fixed loading JPEG images while forcing the number of channels (e.g. to RGBA)
|
||||
<LI>changed SOIL_DDS_CUBEMAP_FACE_ORDER to a #define (thanks Dancho!)
|
||||
<LI>reorganized my additions to stb_image (you can define STBI_NO_DDS to compile SOIL without DDS support)
|
||||
<LI>added SOIL_FLAG_CoCg_Y, will convert RGB or RGBA to YCoCg color space (<a href="http://developer.nvidia.com/object/real-time-ycocg-dxt-compression.html">link</a>)
|
||||
</UL>
|
||||
<LI>October 5, 2007
|
||||
<UL>
|
||||
<LI>added SOIL_FLAG_NTSC_SAFE_RGB
|
||||
<LI>bugfixed & optimized up_scale_image (used with SOIL_FLAG_POWER_OF_TWO and SOIL_FLAG_MIPMAPS)
|
||||
</UL>
|
||||
<LI>September 20, 2007
|
||||
<UL>
|
||||
<LI>upgraded to stb_image 1.0
|
||||
<LI>added the DXT source files to the MSVS projects
|
||||
<LI>removed sqrtf() calls (VS2k3 could not handle them)
|
||||
<LI>distributing only 1 library file (libSOIL.a, compiled with MinGW 4.2.1 tech preview!) for all windows compilers
|
||||
<LI>added an example of the *_from_memory() functions to the Usage section
|
||||
</UL>
|
||||
<LI>September 6, 2007
|
||||
<UL>
|
||||
<LI>added a slew of SOIL_load_*_from_memory() functions for people using PhysicsFS or similar
|
||||
<LI>more robust loading of non-compliant DDS files (thanks Dan!)
|
||||
</UL>
|
||||
<LI>September 1, 2007 - fixed bugs from the last update [8^)
|
||||
<LI>August 31, 2007
|
||||
<UL>
|
||||
<LI>can load uncompressed and cubemap DDS files
|
||||
<LI>can create a cubemap texture from a single (stitched) image file of any type
|
||||
<LI>sped up the image resizing code
|
||||
</UL>
|
||||
<LI>August 24, 2007 - updated the documentation examples (at the bottom of this page)
|
||||
<LI>August 22, 2007
|
||||
<UL>
|
||||
<LI>can load cube maps (needs serious testing)
|
||||
<LI>can compress 1- or 2-channel images to DXT1/5
|
||||
<LI>fixed some malloc() casts
|
||||
<LI>fixed C++ style comments
|
||||
<LI>fixed includes to compile under *nix or Mac (hopefully, needs testing...any volunteers?)
|
||||
</UL>
|
||||
<LI>August 16, 2007
|
||||
<UL>
|
||||
<LI>Will now downsize the image if necessary to fit GL_MAX_TEXTURE_SIZE
|
||||
<LI>added SOIL_create_OGL_texture() to upload raw image data that isn't from an image file
|
||||
</UL>
|
||||
<LI>August 14, 2007 (PM) - Can now load indexed TGA
|
||||
<LI>August 14, 2007 (AM)
|
||||
<UL>
|
||||
<LI>Updated to stb_image 0.97
|
||||
<LI>added result messages
|
||||
<LI>can now decompress DDS files (DXT1/2/3/4/5)
|
||||
</UL>
|
||||
<LI>August 11, 2007 - MIPmaps can now handle non-square textures
|
||||
<LI>August 7, 2007
|
||||
<UL>
|
||||
<LI>Can directly upload DXT1/3/5 DDS files (with or w/o MIPmaps)
|
||||
<LI>can compress any image to DXT1/5 (using a new & fast & simple compression scheme) and upload
|
||||
<LI>can save as DDS
|
||||
</UL>
|
||||
<LI>July 31, 2007 - added compressing to DXT and flipping about Y
|
||||
<LI>July 30, 2007 - initial release
|
||||
</UL>
|
||||
<BR>
|
||||
<BR>
|
||||
<PRE STYLE="text-align: center">back to
|
||||
<A HREF="http://www.lonesock.net/">www.lonesock.net</A></PRE>
|
||||
</BODY>
|
||||
</HTML>
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,433 @@
|
||||
/**
|
||||
@mainpage SOIL
|
||||
|
||||
Jonathan Dummer
|
||||
2007-07-26-10.36
|
||||
|
||||
Simple OpenGL Image Library
|
||||
|
||||
A tiny c library for uploading images as
|
||||
textures into OpenGL. Also saving and
|
||||
loading of images is supported.
|
||||
|
||||
I'm using Sean's Tool Box image loader as a base:
|
||||
http://www.nothings.org/
|
||||
|
||||
I'm upgrading it to load TGA and DDS files, and a direct
|
||||
path for loading DDS files straight into OpenGL textures,
|
||||
when applicable.
|
||||
|
||||
Image Formats:
|
||||
- BMP load & save
|
||||
- TGA load & save
|
||||
- DDS load & save
|
||||
- PNG load
|
||||
- JPG load
|
||||
|
||||
OpenGL Texture Features:
|
||||
- resample to power-of-two sizes
|
||||
- MIPmap generation
|
||||
- compressed texture S3TC formats (if supported)
|
||||
- can pre-multiply alpha for you, for better compositing
|
||||
- can flip image about the y-axis (except pre-compressed DDS files)
|
||||
|
||||
Thanks to:
|
||||
* Sean Barret - for the awesome stb_image
|
||||
* Dan Venkitachalam - for finding some non-compliant DDS files, and patching some explicit casts
|
||||
* everybody at gamedev.net
|
||||
**/
|
||||
|
||||
#ifndef HEADER_SIMPLE_OPENGL_IMAGE_LIBRARY
|
||||
#define HEADER_SIMPLE_OPENGL_IMAGE_LIBRARY
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
The format of images that may be loaded (force_channels).
|
||||
SOIL_LOAD_AUTO leaves the image in whatever format it was found.
|
||||
SOIL_LOAD_L forces the image to load as Luminous (greyscale)
|
||||
SOIL_LOAD_LA forces the image to load as Luminous with Alpha
|
||||
SOIL_LOAD_RGB forces the image to load as Red Green Blue
|
||||
SOIL_LOAD_RGBA forces the image to load as Red Green Blue Alpha
|
||||
**/
|
||||
enum
|
||||
{
|
||||
SOIL_LOAD_AUTO = 0,
|
||||
SOIL_LOAD_L = 1,
|
||||
SOIL_LOAD_LA = 2,
|
||||
SOIL_LOAD_RGB = 3,
|
||||
SOIL_LOAD_RGBA = 4
|
||||
};
|
||||
|
||||
/**
|
||||
Passed in as reuse_texture_ID, will cause SOIL to
|
||||
register a new texture ID using glGenTextures().
|
||||
If the value passed into reuse_texture_ID > 0 then
|
||||
SOIL will just re-use that texture ID (great for
|
||||
reloading image assets in-game!)
|
||||
**/
|
||||
enum
|
||||
{
|
||||
SOIL_CREATE_NEW_ID = 0
|
||||
};
|
||||
|
||||
/**
|
||||
flags you can pass into SOIL_load_OGL_texture()
|
||||
and SOIL_create_OGL_texture().
|
||||
(note that if SOIL_FLAG_DDS_LOAD_DIRECT is used
|
||||
the rest of the flags with the exception of
|
||||
SOIL_FLAG_TEXTURE_REPEATS will be ignored while
|
||||
loading already-compressed DDS files.)
|
||||
|
||||
SOIL_FLAG_POWER_OF_TWO: force the image to be POT
|
||||
SOIL_FLAG_MIPMAPS: generate mipmaps for the texture
|
||||
SOIL_FLAG_TEXTURE_REPEATS: otherwise will clamp
|
||||
SOIL_FLAG_MULTIPLY_ALPHA: for using (GL_ONE,GL_ONE_MINUS_SRC_ALPHA) blending
|
||||
SOIL_FLAG_INVERT_Y: flip the image vertically
|
||||
SOIL_FLAG_COMPRESS_TO_DXT: if the card can display them, will convert RGB to DXT1, RGBA to DXT5
|
||||
SOIL_FLAG_DDS_LOAD_DIRECT: will load DDS files directly without _ANY_ additional processing
|
||||
SOIL_FLAG_NTSC_SAFE_RGB: clamps RGB components to the range [16,235]
|
||||
SOIL_FLAG_CoCg_Y: Google YCoCg; RGB=>CoYCg, RGBA=>CoCgAY
|
||||
SOIL_FLAG_TEXTURE_RECTANGE: uses ARB_texture_rectangle ; pixel indexed & no repeat or MIPmaps or cubemaps
|
||||
**/
|
||||
enum
|
||||
{
|
||||
SOIL_FLAG_POWER_OF_TWO = 1,
|
||||
SOIL_FLAG_MIPMAPS = 2,
|
||||
SOIL_FLAG_TEXTURE_REPEATS = 4,
|
||||
SOIL_FLAG_MULTIPLY_ALPHA = 8,
|
||||
SOIL_FLAG_INVERT_Y = 16,
|
||||
SOIL_FLAG_COMPRESS_TO_DXT = 32,
|
||||
SOIL_FLAG_DDS_LOAD_DIRECT = 64,
|
||||
SOIL_FLAG_NTSC_SAFE_RGB = 128,
|
||||
SOIL_FLAG_CoCg_Y = 256,
|
||||
SOIL_FLAG_TEXTURE_RECTANGLE = 512
|
||||
};
|
||||
|
||||
/**
|
||||
The types of images that may be saved.
|
||||
(TGA supports uncompressed RGB / RGBA)
|
||||
(BMP supports uncompressed RGB)
|
||||
(DDS supports DXT1 and DXT5)
|
||||
**/
|
||||
enum
|
||||
{
|
||||
SOIL_SAVE_TYPE_TGA = 0,
|
||||
SOIL_SAVE_TYPE_BMP = 1,
|
||||
SOIL_SAVE_TYPE_DDS = 2
|
||||
};
|
||||
|
||||
/**
|
||||
Defines the order of faces in a DDS cubemap.
|
||||
I recommend that you use the same order in single
|
||||
image cubemap files, so they will be interchangeable
|
||||
with DDS cubemaps when using SOIL.
|
||||
**/
|
||||
#define SOIL_DDS_CUBEMAP_FACE_ORDER "EWUDNS"
|
||||
|
||||
/**
|
||||
The types of internal fake HDR representations
|
||||
|
||||
SOIL_HDR_RGBE: RGB * pow( 2.0, A - 128.0 )
|
||||
SOIL_HDR_RGBdivA: RGB / A
|
||||
SOIL_HDR_RGBdivA2: RGB / (A*A)
|
||||
**/
|
||||
enum
|
||||
{
|
||||
SOIL_HDR_RGBE = 0,
|
||||
SOIL_HDR_RGBdivA = 1,
|
||||
SOIL_HDR_RGBdivA2 = 2
|
||||
};
|
||||
|
||||
/**
|
||||
Loads an image from disk into an OpenGL texture.
|
||||
\param filename the name of the file to upload as a texture
|
||||
\param force_channels 0-image format, 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA
|
||||
\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
|
||||
\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT
|
||||
\return 0-failed, otherwise returns the OpenGL texture handle
|
||||
**/
|
||||
unsigned int
|
||||
SOIL_load_OGL_texture
|
||||
(
|
||||
const char *filename,
|
||||
int force_channels,
|
||||
unsigned int reuse_texture_ID,
|
||||
unsigned int flags
|
||||
);
|
||||
|
||||
/**
|
||||
Loads 6 images from disk into an OpenGL cubemap texture.
|
||||
\param x_pos_file the name of the file to upload as the +x cube face
|
||||
\param x_neg_file the name of the file to upload as the -x cube face
|
||||
\param y_pos_file the name of the file to upload as the +y cube face
|
||||
\param y_neg_file the name of the file to upload as the -y cube face
|
||||
\param z_pos_file the name of the file to upload as the +z cube face
|
||||
\param z_neg_file the name of the file to upload as the -z cube face
|
||||
\param force_channels 0-image format, 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA
|
||||
\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
|
||||
\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT
|
||||
\return 0-failed, otherwise returns the OpenGL texture handle
|
||||
**/
|
||||
unsigned int
|
||||
SOIL_load_OGL_cubemap
|
||||
(
|
||||
const char *x_pos_file,
|
||||
const char *x_neg_file,
|
||||
const char *y_pos_file,
|
||||
const char *y_neg_file,
|
||||
const char *z_pos_file,
|
||||
const char *z_neg_file,
|
||||
int force_channels,
|
||||
unsigned int reuse_texture_ID,
|
||||
unsigned int flags
|
||||
);
|
||||
|
||||
/**
|
||||
Loads 1 image from disk and splits it into an OpenGL cubemap texture.
|
||||
\param filename the name of the file to upload as a texture
|
||||
\param face_order the order of the faces in the file, any combination of NSWEUD, for North, South, Up, etc.
|
||||
\param force_channels 0-image format, 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA
|
||||
\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
|
||||
\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT
|
||||
\return 0-failed, otherwise returns the OpenGL texture handle
|
||||
**/
|
||||
unsigned int
|
||||
SOIL_load_OGL_single_cubemap
|
||||
(
|
||||
const char *filename,
|
||||
const char face_order[6],
|
||||
int force_channels,
|
||||
unsigned int reuse_texture_ID,
|
||||
unsigned int flags
|
||||
);
|
||||
|
||||
/**
|
||||
Loads an HDR image from disk into an OpenGL texture.
|
||||
\param filename the name of the file to upload as a texture
|
||||
\param fake_HDR_format SOIL_HDR_RGBE, SOIL_HDR_RGBdivA, SOIL_HDR_RGBdivA2
|
||||
\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
|
||||
\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT
|
||||
\return 0-failed, otherwise returns the OpenGL texture handle
|
||||
**/
|
||||
unsigned int
|
||||
SOIL_load_OGL_HDR_texture
|
||||
(
|
||||
const char *filename,
|
||||
int fake_HDR_format,
|
||||
int rescale_to_max,
|
||||
unsigned int reuse_texture_ID,
|
||||
unsigned int flags
|
||||
);
|
||||
|
||||
/**
|
||||
Loads an image from RAM into an OpenGL texture.
|
||||
\param buffer the image data in RAM just as if it were still in a file
|
||||
\param buffer_length the size of the buffer in bytes
|
||||
\param force_channels 0-image format, 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA
|
||||
\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
|
||||
\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT
|
||||
\return 0-failed, otherwise returns the OpenGL texture handle
|
||||
**/
|
||||
unsigned int
|
||||
SOIL_load_OGL_texture_from_memory
|
||||
(
|
||||
const unsigned char *const buffer,
|
||||
int buffer_length,
|
||||
int force_channels,
|
||||
unsigned int reuse_texture_ID,
|
||||
unsigned int flags
|
||||
);
|
||||
|
||||
/**
|
||||
Loads 6 images from memory into an OpenGL cubemap texture.
|
||||
\param x_pos_buffer the image data in RAM to upload as the +x cube face
|
||||
\param x_pos_buffer_length the size of the above buffer
|
||||
\param x_neg_buffer the image data in RAM to upload as the +x cube face
|
||||
\param x_neg_buffer_length the size of the above buffer
|
||||
\param y_pos_buffer the image data in RAM to upload as the +x cube face
|
||||
\param y_pos_buffer_length the size of the above buffer
|
||||
\param y_neg_buffer the image data in RAM to upload as the +x cube face
|
||||
\param y_neg_buffer_length the size of the above buffer
|
||||
\param z_pos_buffer the image data in RAM to upload as the +x cube face
|
||||
\param z_pos_buffer_length the size of the above buffer
|
||||
\param z_neg_buffer the image data in RAM to upload as the +x cube face
|
||||
\param z_neg_buffer_length the size of the above buffer
|
||||
\param force_channels 0-image format, 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA
|
||||
\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
|
||||
\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT
|
||||
\return 0-failed, otherwise returns the OpenGL texture handle
|
||||
**/
|
||||
unsigned int
|
||||
SOIL_load_OGL_cubemap_from_memory
|
||||
(
|
||||
const unsigned char *const x_pos_buffer,
|
||||
int x_pos_buffer_length,
|
||||
const unsigned char *const x_neg_buffer,
|
||||
int x_neg_buffer_length,
|
||||
const unsigned char *const y_pos_buffer,
|
||||
int y_pos_buffer_length,
|
||||
const unsigned char *const y_neg_buffer,
|
||||
int y_neg_buffer_length,
|
||||
const unsigned char *const z_pos_buffer,
|
||||
int z_pos_buffer_length,
|
||||
const unsigned char *const z_neg_buffer,
|
||||
int z_neg_buffer_length,
|
||||
int force_channels,
|
||||
unsigned int reuse_texture_ID,
|
||||
unsigned int flags
|
||||
);
|
||||
|
||||
/**
|
||||
Loads 1 image from RAM and splits it into an OpenGL cubemap texture.
|
||||
\param buffer the image data in RAM just as if it were still in a file
|
||||
\param buffer_length the size of the buffer in bytes
|
||||
\param face_order the order of the faces in the file, any combination of NSWEUD, for North, South, Up, etc.
|
||||
\param force_channels 0-image format, 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA
|
||||
\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
|
||||
\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT
|
||||
\return 0-failed, otherwise returns the OpenGL texture handle
|
||||
**/
|
||||
unsigned int
|
||||
SOIL_load_OGL_single_cubemap_from_memory
|
||||
(
|
||||
const unsigned char *const buffer,
|
||||
int buffer_length,
|
||||
const char face_order[6],
|
||||
int force_channels,
|
||||
unsigned int reuse_texture_ID,
|
||||
unsigned int flags
|
||||
);
|
||||
|
||||
/**
|
||||
Creates a 2D OpenGL texture from raw image data. Note that the raw data is
|
||||
_NOT_ freed after the upload (so the user can load various versions).
|
||||
\param data the raw data to be uploaded as an OpenGL texture
|
||||
\param width the width of the image in pixels
|
||||
\param height the height of the image in pixels
|
||||
\param channels the number of channels: 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA
|
||||
\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
|
||||
\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT
|
||||
\return 0-failed, otherwise returns the OpenGL texture handle
|
||||
**/
|
||||
unsigned int
|
||||
SOIL_create_OGL_texture
|
||||
(
|
||||
const unsigned char *const data,
|
||||
int width, int height, int channels,
|
||||
unsigned int reuse_texture_ID,
|
||||
unsigned int flags
|
||||
);
|
||||
|
||||
/**
|
||||
Creates an OpenGL cubemap texture by splitting up 1 image into 6 parts.
|
||||
\param data the raw data to be uploaded as an OpenGL texture
|
||||
\param width the width of the image in pixels
|
||||
\param height the height of the image in pixels
|
||||
\param channels the number of channels: 1-luminous, 2-luminous/alpha, 3-RGB, 4-RGBA
|
||||
\param face_order the order of the faces in the file, and combination of NSWEUD, for North, South, Up, etc.
|
||||
\param reuse_texture_ID 0-generate a new texture ID, otherwise reuse the texture ID (overwriting the old texture)
|
||||
\param flags can be any of SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS | SOIL_FLAG_TEXTURE_REPEATS | SOIL_FLAG_MULTIPLY_ALPHA | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_DDS_LOAD_DIRECT
|
||||
\return 0-failed, otherwise returns the OpenGL texture handle
|
||||
**/
|
||||
unsigned int
|
||||
SOIL_create_OGL_single_cubemap
|
||||
(
|
||||
const unsigned char *const data,
|
||||
int width, int height, int channels,
|
||||
const char face_order[6],
|
||||
unsigned int reuse_texture_ID,
|
||||
unsigned int flags
|
||||
);
|
||||
|
||||
/**
|
||||
Captures the OpenGL window (RGB) and saves it to disk
|
||||
\return 0 if it failed, otherwise returns 1
|
||||
**/
|
||||
int
|
||||
SOIL_save_screenshot
|
||||
(
|
||||
const char *filename,
|
||||
int image_type,
|
||||
int x, int y,
|
||||
int width, int height
|
||||
);
|
||||
|
||||
/**
|
||||
Loads an image from disk into an array of unsigned chars.
|
||||
Note that *channels return the original channel count of the
|
||||
image. If force_channels was other than SOIL_LOAD_AUTO,
|
||||
the resulting image has force_channels, but *channels may be
|
||||
different (if the original image had a different channel
|
||||
count).
|
||||
\return 0 if failed, otherwise returns 1
|
||||
**/
|
||||
unsigned char*
|
||||
SOIL_load_image
|
||||
(
|
||||
const char *filename,
|
||||
int *width, int *height, int *channels,
|
||||
int force_channels
|
||||
);
|
||||
|
||||
/**
|
||||
Loads an image from memory into an array of unsigned chars.
|
||||
Note that *channels return the original channel count of the
|
||||
image. If force_channels was other than SOIL_LOAD_AUTO,
|
||||
the resulting image has force_channels, but *channels may be
|
||||
different (if the original image had a different channel
|
||||
count).
|
||||
\return 0 if failed, otherwise returns 1
|
||||
**/
|
||||
unsigned char*
|
||||
SOIL_load_image_from_memory
|
||||
(
|
||||
const unsigned char *const buffer,
|
||||
int buffer_length,
|
||||
int *width, int *height, int *channels,
|
||||
int force_channels
|
||||
);
|
||||
|
||||
/**
|
||||
Saves an image from an array of unsigned chars (RGBA) to disk
|
||||
\return 0 if failed, otherwise returns 1
|
||||
**/
|
||||
int
|
||||
SOIL_save_image
|
||||
(
|
||||
const char *filename,
|
||||
int image_type,
|
||||
int width, int height, int channels,
|
||||
const unsigned char *const data
|
||||
);
|
||||
|
||||
/**
|
||||
Frees the image data (note, this is just C's "free()"...this function is
|
||||
present mostly so C++ programmers don't forget to use "free()" and call
|
||||
"delete []" instead [8^)
|
||||
**/
|
||||
void
|
||||
SOIL_free_image_data
|
||||
(
|
||||
unsigned char *img_data
|
||||
);
|
||||
|
||||
/**
|
||||
This function resturn a pointer to a string describing the last thing
|
||||
that happened inside SOIL. It can be used to determine why an image
|
||||
failed to load.
|
||||
**/
|
||||
const char*
|
||||
SOIL_last_result
|
||||
(
|
||||
void
|
||||
);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* HEADER_SIMPLE_OPENGL_IMAGE_LIBRARY */
|
||||
@@ -0,0 +1,632 @@
|
||||
/*
|
||||
Jonathan Dummer
|
||||
2007-07-31-10.32
|
||||
|
||||
simple DXT compression / decompression code
|
||||
|
||||
public domain
|
||||
*/
|
||||
|
||||
#include "image_DXT.h"
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/* set this =1 if you want to use the covarince matrix method...
|
||||
which is better than my method of using standard deviations
|
||||
overall, except on the infintesimal chance that the power
|
||||
method fails for finding the largest eigenvector */
|
||||
#define USE_COV_MAT 1
|
||||
|
||||
/********* Function Prototypes *********/
|
||||
/*
|
||||
Takes a 4x4 block of pixels and compresses it into 8 bytes
|
||||
in DXT1 format (color only, no alpha). Speed is valued
|
||||
over prettyness, at least for now.
|
||||
*/
|
||||
void compress_DDS_color_block(
|
||||
int channels,
|
||||
const unsigned char *const uncompressed,
|
||||
unsigned char compressed[8] );
|
||||
/*
|
||||
Takes a 4x4 block of pixels and compresses the alpha
|
||||
component it into 8 bytes for use in DXT5 DDS files.
|
||||
Speed is valued over prettyness, at least for now.
|
||||
*/
|
||||
void compress_DDS_alpha_block(
|
||||
const unsigned char *const uncompressed,
|
||||
unsigned char compressed[8] );
|
||||
|
||||
/********* Actual Exposed Functions *********/
|
||||
int
|
||||
save_image_as_DDS
|
||||
(
|
||||
const char *filename,
|
||||
int width, int height, int channels,
|
||||
const unsigned char *const data
|
||||
)
|
||||
{
|
||||
/* variables */
|
||||
FILE *fout;
|
||||
unsigned char *DDS_data;
|
||||
DDS_header header;
|
||||
int DDS_size;
|
||||
/* error check */
|
||||
if( (NULL == filename) ||
|
||||
(width < 1) || (height < 1) ||
|
||||
(channels < 1) || (channels > 4) ||
|
||||
(data == NULL ) )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
/* Convert the image */
|
||||
if( (channels & 1) == 1 )
|
||||
{
|
||||
/* no alpha, just use DXT1 */
|
||||
DDS_data = convert_image_to_DXT1( data, width, height, channels, &DDS_size );
|
||||
} else
|
||||
{
|
||||
/* has alpha, so use DXT5 */
|
||||
DDS_data = convert_image_to_DXT5( data, width, height, channels, &DDS_size );
|
||||
}
|
||||
/* save it */
|
||||
memset( &header, 0, sizeof( DDS_header ) );
|
||||
header.dwMagic = ('D' << 0) | ('D' << 8) | ('S' << 16) | (' ' << 24);
|
||||
header.dwSize = 124;
|
||||
header.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_LINEARSIZE;
|
||||
header.dwWidth = width;
|
||||
header.dwHeight = height;
|
||||
header.dwPitchOrLinearSize = DDS_size;
|
||||
header.sPixelFormat.dwSize = 32;
|
||||
header.sPixelFormat.dwFlags = DDPF_FOURCC;
|
||||
if( (channels & 1) == 1 )
|
||||
{
|
||||
header.sPixelFormat.dwFourCC = ('D' << 0) | ('X' << 8) | ('T' << 16) | ('1' << 24);
|
||||
} else
|
||||
{
|
||||
header.sPixelFormat.dwFourCC = ('D' << 0) | ('X' << 8) | ('T' << 16) | ('5' << 24);
|
||||
}
|
||||
header.sCaps.dwCaps1 = DDSCAPS_TEXTURE;
|
||||
/* write it out */
|
||||
fout = fopen( filename, "wb");
|
||||
fwrite( &header, sizeof( DDS_header ), 1, fout );
|
||||
fwrite( DDS_data, 1, DDS_size, fout );
|
||||
fclose( fout );
|
||||
/* done */
|
||||
free( DDS_data );
|
||||
return 1;
|
||||
}
|
||||
|
||||
unsigned char* convert_image_to_DXT1(
|
||||
const unsigned char *const uncompressed,
|
||||
int width, int height, int channels,
|
||||
int *out_size )
|
||||
{
|
||||
unsigned char *compressed;
|
||||
int i, j, x, y;
|
||||
unsigned char ublock[16*3];
|
||||
unsigned char cblock[8];
|
||||
int index = 0, chan_step = 1;
|
||||
int block_count = 0;
|
||||
/* error check */
|
||||
*out_size = 0;
|
||||
if( (width < 1) || (height < 1) ||
|
||||
(NULL == uncompressed) ||
|
||||
(channels < 1) || (channels > 4) )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
/* for channels == 1 or 2, I do not step forward for R,G,B values */
|
||||
if( channels < 3 )
|
||||
{
|
||||
chan_step = 0;
|
||||
}
|
||||
/* get the RAM for the compressed image
|
||||
(8 bytes per 4x4 pixel block) */
|
||||
*out_size = ((width+3) >> 2) * ((height+3) >> 2) * 8;
|
||||
compressed = (unsigned char*)malloc( *out_size );
|
||||
/* go through each block */
|
||||
for( j = 0; j < height; j += 4 )
|
||||
{
|
||||
for( i = 0; i < width; i += 4 )
|
||||
{
|
||||
/* copy this block into a new one */
|
||||
int idx = 0;
|
||||
int mx = 4, my = 4;
|
||||
if( j+4 >= height )
|
||||
{
|
||||
my = height - j;
|
||||
}
|
||||
if( i+4 >= width )
|
||||
{
|
||||
mx = width - i;
|
||||
}
|
||||
for( y = 0; y < my; ++y )
|
||||
{
|
||||
for( x = 0; x < mx; ++x )
|
||||
{
|
||||
ublock[idx++] = uncompressed[(j+y)*width*channels+(i+x)*channels];
|
||||
ublock[idx++] = uncompressed[(j+y)*width*channels+(i+x)*channels+chan_step];
|
||||
ublock[idx++] = uncompressed[(j+y)*width*channels+(i+x)*channels+chan_step+chan_step];
|
||||
}
|
||||
for( x = mx; x < 4; ++x )
|
||||
{
|
||||
ublock[idx++] = ublock[0];
|
||||
ublock[idx++] = ublock[1];
|
||||
ublock[idx++] = ublock[2];
|
||||
}
|
||||
}
|
||||
for( y = my; y < 4; ++y )
|
||||
{
|
||||
for( x = 0; x < 4; ++x )
|
||||
{
|
||||
ublock[idx++] = ublock[0];
|
||||
ublock[idx++] = ublock[1];
|
||||
ublock[idx++] = ublock[2];
|
||||
}
|
||||
}
|
||||
/* compress the block */
|
||||
++block_count;
|
||||
compress_DDS_color_block( 3, ublock, cblock );
|
||||
/* copy the data from the block into the main block */
|
||||
for( x = 0; x < 8; ++x )
|
||||
{
|
||||
compressed[index++] = cblock[x];
|
||||
}
|
||||
}
|
||||
}
|
||||
return compressed;
|
||||
}
|
||||
|
||||
unsigned char* convert_image_to_DXT5(
|
||||
const unsigned char *const uncompressed,
|
||||
int width, int height, int channels,
|
||||
int *out_size )
|
||||
{
|
||||
unsigned char *compressed;
|
||||
int i, j, x, y;
|
||||
unsigned char ublock[16*4];
|
||||
unsigned char cblock[8];
|
||||
int index = 0, chan_step = 1;
|
||||
int block_count = 0, has_alpha;
|
||||
/* error check */
|
||||
*out_size = 0;
|
||||
if( (width < 1) || (height < 1) ||
|
||||
(NULL == uncompressed) ||
|
||||
(channels < 1) || ( channels > 4) )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
/* for channels == 1 or 2, I do not step forward for R,G,B vales */
|
||||
if( channels < 3 )
|
||||
{
|
||||
chan_step = 0;
|
||||
}
|
||||
/* # channels = 1 or 3 have no alpha, 2 & 4 do have alpha */
|
||||
has_alpha = 1 - (channels & 1);
|
||||
/* get the RAM for the compressed image
|
||||
(16 bytes per 4x4 pixel block) */
|
||||
*out_size = ((width+3) >> 2) * ((height+3) >> 2) * 16;
|
||||
compressed = (unsigned char*)malloc( *out_size );
|
||||
/* go through each block */
|
||||
for( j = 0; j < height; j += 4 )
|
||||
{
|
||||
for( i = 0; i < width; i += 4 )
|
||||
{
|
||||
/* local variables, and my block counter */
|
||||
int idx = 0;
|
||||
int mx = 4, my = 4;
|
||||
if( j+4 >= height )
|
||||
{
|
||||
my = height - j;
|
||||
}
|
||||
if( i+4 >= width )
|
||||
{
|
||||
mx = width - i;
|
||||
}
|
||||
for( y = 0; y < my; ++y )
|
||||
{
|
||||
for( x = 0; x < mx; ++x )
|
||||
{
|
||||
ublock[idx++] = uncompressed[(j+y)*width*channels+(i+x)*channels];
|
||||
ublock[idx++] = uncompressed[(j+y)*width*channels+(i+x)*channels+chan_step];
|
||||
ublock[idx++] = uncompressed[(j+y)*width*channels+(i+x)*channels+chan_step+chan_step];
|
||||
ublock[idx++] =
|
||||
has_alpha * uncompressed[(j+y)*width*channels+(i+x)*channels+channels-1]
|
||||
+ (1-has_alpha)*255;
|
||||
}
|
||||
for( x = mx; x < 4; ++x )
|
||||
{
|
||||
ublock[idx++] = ublock[0];
|
||||
ublock[idx++] = ublock[1];
|
||||
ublock[idx++] = ublock[2];
|
||||
ublock[idx++] = ublock[3];
|
||||
}
|
||||
}
|
||||
for( y = my; y < 4; ++y )
|
||||
{
|
||||
for( x = 0; x < 4; ++x )
|
||||
{
|
||||
ublock[idx++] = ublock[0];
|
||||
ublock[idx++] = ublock[1];
|
||||
ublock[idx++] = ublock[2];
|
||||
ublock[idx++] = ublock[3];
|
||||
}
|
||||
}
|
||||
/* now compress the alpha block */
|
||||
compress_DDS_alpha_block( ublock, cblock );
|
||||
/* copy the data from the compressed alpha block into the main buffer */
|
||||
for( x = 0; x < 8; ++x )
|
||||
{
|
||||
compressed[index++] = cblock[x];
|
||||
}
|
||||
/* then compress the color block */
|
||||
++block_count;
|
||||
compress_DDS_color_block( 4, ublock, cblock );
|
||||
/* copy the data from the compressed color block into the main buffer */
|
||||
for( x = 0; x < 8; ++x )
|
||||
{
|
||||
compressed[index++] = cblock[x];
|
||||
}
|
||||
}
|
||||
}
|
||||
return compressed;
|
||||
}
|
||||
|
||||
/********* Helper Functions *********/
|
||||
int convert_bit_range( int c, int from_bits, int to_bits )
|
||||
{
|
||||
int b = (1 << (from_bits - 1)) + c * ((1 << to_bits) - 1);
|
||||
return (b + (b >> from_bits)) >> from_bits;
|
||||
}
|
||||
|
||||
int rgb_to_565( int r, int g, int b )
|
||||
{
|
||||
return
|
||||
(convert_bit_range( r, 8, 5 ) << 11) |
|
||||
(convert_bit_range( g, 8, 6 ) << 05) |
|
||||
(convert_bit_range( b, 8, 5 ) << 00);
|
||||
}
|
||||
|
||||
void rgb_888_from_565( unsigned int c, int *r, int *g, int *b )
|
||||
{
|
||||
*r = convert_bit_range( (c >> 11) & 31, 5, 8 );
|
||||
*g = convert_bit_range( (c >> 05) & 63, 6, 8 );
|
||||
*b = convert_bit_range( (c >> 00) & 31, 5, 8 );
|
||||
}
|
||||
|
||||
void compute_color_line_STDEV(
|
||||
const unsigned char *const uncompressed,
|
||||
int channels,
|
||||
float point[3], float direction[3] )
|
||||
{
|
||||
const float inv_16 = 1.0f / 16.0f;
|
||||
int i;
|
||||
float sum_r = 0.0f, sum_g = 0.0f, sum_b = 0.0f;
|
||||
float sum_rr = 0.0f, sum_gg = 0.0f, sum_bb = 0.0f;
|
||||
float sum_rg = 0.0f, sum_rb = 0.0f, sum_gb = 0.0f;
|
||||
/* calculate all data needed for the covariance matrix
|
||||
( to compare with _rygdxt code) */
|
||||
for( i = 0; i < 16*channels; i += channels )
|
||||
{
|
||||
sum_r += uncompressed[i+0];
|
||||
sum_rr += uncompressed[i+0] * uncompressed[i+0];
|
||||
sum_g += uncompressed[i+1];
|
||||
sum_gg += uncompressed[i+1] * uncompressed[i+1];
|
||||
sum_b += uncompressed[i+2];
|
||||
sum_bb += uncompressed[i+2] * uncompressed[i+2];
|
||||
sum_rg += uncompressed[i+0] * uncompressed[i+1];
|
||||
sum_rb += uncompressed[i+0] * uncompressed[i+2];
|
||||
sum_gb += uncompressed[i+1] * uncompressed[i+2];
|
||||
}
|
||||
/* convert the sums to averages */
|
||||
sum_r *= inv_16;
|
||||
sum_g *= inv_16;
|
||||
sum_b *= inv_16;
|
||||
/* and convert the squares to the squares of the value - avg_value */
|
||||
sum_rr -= 16.0f * sum_r * sum_r;
|
||||
sum_gg -= 16.0f * sum_g * sum_g;
|
||||
sum_bb -= 16.0f * sum_b * sum_b;
|
||||
sum_rg -= 16.0f * sum_r * sum_g;
|
||||
sum_rb -= 16.0f * sum_r * sum_b;
|
||||
sum_gb -= 16.0f * sum_g * sum_b;
|
||||
/* the point on the color line is the average */
|
||||
point[0] = sum_r;
|
||||
point[1] = sum_g;
|
||||
point[2] = sum_b;
|
||||
#if USE_COV_MAT
|
||||
/*
|
||||
The following idea was from ryg.
|
||||
(https://mollyrocket.com/forums/viewtopic.php?t=392)
|
||||
The method worked great (less RMSE than mine) most of
|
||||
the time, but had some issues handling some simple
|
||||
boundary cases, like full green next to full red,
|
||||
which would generate a covariance matrix like this:
|
||||
|
||||
| 1 -1 0 |
|
||||
| -1 1 0 |
|
||||
| 0 0 0 |
|
||||
|
||||
For a given starting vector, the power method can
|
||||
generate all zeros! So no starting with {1,1,1}
|
||||
as I was doing! This kind of error is still a
|
||||
slight posibillity, but will be very rare.
|
||||
*/
|
||||
/* use the covariance matrix directly
|
||||
(1st iteration, don't use all 1.0 values!) */
|
||||
sum_r = 1.0f;
|
||||
sum_g = 2.718281828f;
|
||||
sum_b = 3.141592654f;
|
||||
direction[0] = sum_r*sum_rr + sum_g*sum_rg + sum_b*sum_rb;
|
||||
direction[1] = sum_r*sum_rg + sum_g*sum_gg + sum_b*sum_gb;
|
||||
direction[2] = sum_r*sum_rb + sum_g*sum_gb + sum_b*sum_bb;
|
||||
/* 2nd iteration, use results from the 1st guy */
|
||||
sum_r = direction[0];
|
||||
sum_g = direction[1];
|
||||
sum_b = direction[2];
|
||||
direction[0] = sum_r*sum_rr + sum_g*sum_rg + sum_b*sum_rb;
|
||||
direction[1] = sum_r*sum_rg + sum_g*sum_gg + sum_b*sum_gb;
|
||||
direction[2] = sum_r*sum_rb + sum_g*sum_gb + sum_b*sum_bb;
|
||||
/* 3rd iteration, use results from the 2nd guy */
|
||||
sum_r = direction[0];
|
||||
sum_g = direction[1];
|
||||
sum_b = direction[2];
|
||||
direction[0] = sum_r*sum_rr + sum_g*sum_rg + sum_b*sum_rb;
|
||||
direction[1] = sum_r*sum_rg + sum_g*sum_gg + sum_b*sum_gb;
|
||||
direction[2] = sum_r*sum_rb + sum_g*sum_gb + sum_b*sum_bb;
|
||||
#else
|
||||
/* use my standard deviation method
|
||||
(very robust, a tiny bit slower and less accurate) */
|
||||
direction[0] = sqrt( sum_rr );
|
||||
direction[1] = sqrt( sum_gg );
|
||||
direction[2] = sqrt( sum_bb );
|
||||
/* which has a greater component */
|
||||
if( sum_gg > sum_rr )
|
||||
{
|
||||
/* green has greater component, so base the other signs off of green */
|
||||
if( sum_rg < 0.0f )
|
||||
{
|
||||
direction[0] = -direction[0];
|
||||
}
|
||||
if( sum_gb < 0.0f )
|
||||
{
|
||||
direction[2] = -direction[2];
|
||||
}
|
||||
} else
|
||||
{
|
||||
/* red has a greater component */
|
||||
if( sum_rg < 0.0f )
|
||||
{
|
||||
direction[1] = -direction[1];
|
||||
}
|
||||
if( sum_rb < 0.0f )
|
||||
{
|
||||
direction[2] = -direction[2];
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void LSE_master_colors_max_min(
|
||||
int *cmax, int *cmin,
|
||||
int channels,
|
||||
const unsigned char *const uncompressed )
|
||||
{
|
||||
int i, j;
|
||||
/* the master colors */
|
||||
int c0[3], c1[3];
|
||||
/* used for fitting the line */
|
||||
float sum_x[] = { 0.0f, 0.0f, 0.0f };
|
||||
float sum_x2[] = { 0.0f, 0.0f, 0.0f };
|
||||
float dot_max = 1.0f, dot_min = -1.0f;
|
||||
float vec_len2 = 0.0f;
|
||||
float dot;
|
||||
/* error check */
|
||||
if( (channels < 3) || (channels > 4) )
|
||||
{
|
||||
return;
|
||||
}
|
||||
compute_color_line_STDEV( uncompressed, channels, sum_x, sum_x2 );
|
||||
vec_len2 = 1.0f / ( 0.00001f +
|
||||
sum_x2[0]*sum_x2[0] + sum_x2[1]*sum_x2[1] + sum_x2[2]*sum_x2[2] );
|
||||
/* finding the max and min vector values */
|
||||
dot_max =
|
||||
(
|
||||
sum_x2[0] * uncompressed[0] +
|
||||
sum_x2[1] * uncompressed[1] +
|
||||
sum_x2[2] * uncompressed[2]
|
||||
);
|
||||
dot_min = dot_max;
|
||||
for( i = 1; i < 16; ++i )
|
||||
{
|
||||
dot =
|
||||
(
|
||||
sum_x2[0] * uncompressed[i*channels+0] +
|
||||
sum_x2[1] * uncompressed[i*channels+1] +
|
||||
sum_x2[2] * uncompressed[i*channels+2]
|
||||
);
|
||||
if( dot < dot_min )
|
||||
{
|
||||
dot_min = dot;
|
||||
} else if( dot > dot_max )
|
||||
{
|
||||
dot_max = dot;
|
||||
}
|
||||
}
|
||||
/* and the offset (from the average location) */
|
||||
dot = sum_x2[0]*sum_x[0] + sum_x2[1]*sum_x[1] + sum_x2[2]*sum_x[2];
|
||||
dot_min -= dot;
|
||||
dot_max -= dot;
|
||||
/* post multiply by the scaling factor */
|
||||
dot_min *= vec_len2;
|
||||
dot_max *= vec_len2;
|
||||
/* OK, build the master colors */
|
||||
for( i = 0; i < 3; ++i )
|
||||
{
|
||||
/* color 0 */
|
||||
c0[i] = (int)(0.5f + sum_x[i] + dot_max * sum_x2[i]);
|
||||
if( c0[i] < 0 )
|
||||
{
|
||||
c0[i] = 0;
|
||||
} else if( c0[i] > 255 )
|
||||
{
|
||||
c0[i] = 255;
|
||||
}
|
||||
/* color 1 */
|
||||
c1[i] = (int)(0.5f + sum_x[i] + dot_min * sum_x2[i]);
|
||||
if( c1[i] < 0 )
|
||||
{
|
||||
c1[i] = 0;
|
||||
} else if( c1[i] > 255 )
|
||||
{
|
||||
c1[i] = 255;
|
||||
}
|
||||
}
|
||||
/* down_sample (with rounding?) */
|
||||
i = rgb_to_565( c0[0], c0[1], c0[2] );
|
||||
j = rgb_to_565( c1[0], c1[1], c1[2] );
|
||||
if( i > j )
|
||||
{
|
||||
*cmax = i;
|
||||
*cmin = j;
|
||||
} else
|
||||
{
|
||||
*cmax = j;
|
||||
*cmin = i;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
compress_DDS_color_block
|
||||
(
|
||||
int channels,
|
||||
const unsigned char *const uncompressed,
|
||||
unsigned char compressed[8]
|
||||
)
|
||||
{
|
||||
/* variables */
|
||||
int i;
|
||||
int next_bit;
|
||||
int enc_c0, enc_c1;
|
||||
int c0[4], c1[4];
|
||||
float color_line[] = { 0.0f, 0.0f, 0.0f, 0.0f };
|
||||
float vec_len2 = 0.0f, dot_offset = 0.0f;
|
||||
/* stupid order */
|
||||
int swizzle4[] = { 0, 2, 3, 1 };
|
||||
/* get the master colors */
|
||||
LSE_master_colors_max_min( &enc_c0, &enc_c1, channels, uncompressed );
|
||||
/* store the 565 color 0 and color 1 */
|
||||
compressed[0] = (enc_c0 >> 0) & 255;
|
||||
compressed[1] = (enc_c0 >> 8) & 255;
|
||||
compressed[2] = (enc_c1 >> 0) & 255;
|
||||
compressed[3] = (enc_c1 >> 8) & 255;
|
||||
/* zero out the compressed data */
|
||||
compressed[4] = 0;
|
||||
compressed[5] = 0;
|
||||
compressed[6] = 0;
|
||||
compressed[7] = 0;
|
||||
/* reconstitute the master color vectors */
|
||||
rgb_888_from_565( enc_c0, &c0[0], &c0[1], &c0[2] );
|
||||
rgb_888_from_565( enc_c1, &c1[0], &c1[1], &c1[2] );
|
||||
/* the new vector */
|
||||
vec_len2 = 0.0f;
|
||||
for( i = 0; i < 3; ++i )
|
||||
{
|
||||
color_line[i] = (float)(c1[i] - c0[i]);
|
||||
vec_len2 += color_line[i] * color_line[i];
|
||||
}
|
||||
if( vec_len2 > 0.0f )
|
||||
{
|
||||
vec_len2 = 1.0f / vec_len2;
|
||||
}
|
||||
/* pre-proform the scaling */
|
||||
color_line[0] *= vec_len2;
|
||||
color_line[1] *= vec_len2;
|
||||
color_line[2] *= vec_len2;
|
||||
/* compute the offset (constant) portion of the dot product */
|
||||
dot_offset = color_line[0]*c0[0] + color_line[1]*c0[1] + color_line[2]*c0[2];
|
||||
/* store the rest of the bits */
|
||||
next_bit = 8*4;
|
||||
for( i = 0; i < 16; ++i )
|
||||
{
|
||||
/* find the dot product of this color, to place it on the line
|
||||
(should be [-1,1]) */
|
||||
int next_value = 0;
|
||||
float dot_product =
|
||||
color_line[0] * uncompressed[i*channels+0] +
|
||||
color_line[1] * uncompressed[i*channels+1] +
|
||||
color_line[2] * uncompressed[i*channels+2] -
|
||||
dot_offset;
|
||||
/* map to [0,3] */
|
||||
next_value = (int)( dot_product * 3.0f + 0.5f );
|
||||
if( next_value > 3 )
|
||||
{
|
||||
next_value = 3;
|
||||
} else if( next_value < 0 )
|
||||
{
|
||||
next_value = 0;
|
||||
}
|
||||
/* OK, store this value */
|
||||
compressed[next_bit >> 3] |= swizzle4[ next_value ] << (next_bit & 7);
|
||||
next_bit += 2;
|
||||
}
|
||||
/* done compressing to DXT1 */
|
||||
}
|
||||
|
||||
void
|
||||
compress_DDS_alpha_block
|
||||
(
|
||||
const unsigned char *const uncompressed,
|
||||
unsigned char compressed[8]
|
||||
)
|
||||
{
|
||||
/* variables */
|
||||
int i;
|
||||
int next_bit;
|
||||
int a0, a1;
|
||||
float scale_me;
|
||||
/* stupid order */
|
||||
int swizzle8[] = { 1, 7, 6, 5, 4, 3, 2, 0 };
|
||||
/* get the alpha limits (a0 > a1) */
|
||||
a0 = a1 = uncompressed[3];
|
||||
for( i = 4+3; i < 16*4; i += 4 )
|
||||
{
|
||||
if( uncompressed[i] > a0 )
|
||||
{
|
||||
a0 = uncompressed[i];
|
||||
} else if( uncompressed[i] < a1 )
|
||||
{
|
||||
a1 = uncompressed[i];
|
||||
}
|
||||
}
|
||||
/* store those limits, and zero the rest of the compressed dataset */
|
||||
compressed[0] = a0;
|
||||
compressed[1] = a1;
|
||||
/* zero out the compressed data */
|
||||
compressed[2] = 0;
|
||||
compressed[3] = 0;
|
||||
compressed[4] = 0;
|
||||
compressed[5] = 0;
|
||||
compressed[6] = 0;
|
||||
compressed[7] = 0;
|
||||
/* store the all of the alpha values */
|
||||
next_bit = 8*2;
|
||||
scale_me = 7.9999f / (a0 - a1);
|
||||
for( i = 3; i < 16*4; i += 4 )
|
||||
{
|
||||
/* convert this alpha value to a 3 bit number */
|
||||
int svalue;
|
||||
int value = (int)((uncompressed[i] - a1) * scale_me);
|
||||
svalue = swizzle8[ value&7 ];
|
||||
/* OK, store this value, start with the 1st byte */
|
||||
compressed[next_bit >> 3] |= svalue << (next_bit & 7);
|
||||
if( (next_bit & 7) > 5 )
|
||||
{
|
||||
/* spans 2 bytes, fill in the start of the 2nd byte */
|
||||
compressed[1 + (next_bit >> 3)] |= svalue >> (8 - (next_bit & 7) );
|
||||
}
|
||||
next_bit += 3;
|
||||
}
|
||||
/* done compressing to DXT1 */
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
Jonathan Dummer
|
||||
2007-07-31-10.32
|
||||
|
||||
simple DXT compression / decompression code
|
||||
|
||||
public domain
|
||||
*/
|
||||
|
||||
#ifndef HEADER_IMAGE_DXT
|
||||
#define HEADER_IMAGE_DXT
|
||||
|
||||
/**
|
||||
Converts an image from an array of unsigned chars (RGB or RGBA) to
|
||||
DXT1 or DXT5, then saves the converted image to disk.
|
||||
\return 0 if failed, otherwise returns 1
|
||||
**/
|
||||
int
|
||||
save_image_as_DDS
|
||||
(
|
||||
const char *filename,
|
||||
int width, int height, int channels,
|
||||
const unsigned char *const data
|
||||
);
|
||||
|
||||
/**
|
||||
take an image and convert it to DXT1 (no alpha)
|
||||
**/
|
||||
unsigned char*
|
||||
convert_image_to_DXT1
|
||||
(
|
||||
const unsigned char *const uncompressed,
|
||||
int width, int height, int channels,
|
||||
int *out_size
|
||||
);
|
||||
|
||||
/**
|
||||
take an image and convert it to DXT5 (with alpha)
|
||||
**/
|
||||
unsigned char*
|
||||
convert_image_to_DXT5
|
||||
(
|
||||
const unsigned char *const uncompressed,
|
||||
int width, int height, int channels,
|
||||
int *out_size
|
||||
);
|
||||
|
||||
/** A bunch of DirectDraw Surface structures and flags **/
|
||||
typedef struct
|
||||
{
|
||||
unsigned int dwMagic;
|
||||
unsigned int dwSize;
|
||||
unsigned int dwFlags;
|
||||
unsigned int dwHeight;
|
||||
unsigned int dwWidth;
|
||||
unsigned int dwPitchOrLinearSize;
|
||||
unsigned int dwDepth;
|
||||
unsigned int dwMipMapCount;
|
||||
unsigned int dwReserved1[ 11 ];
|
||||
|
||||
/* DDPIXELFORMAT */
|
||||
struct
|
||||
{
|
||||
unsigned int dwSize;
|
||||
unsigned int dwFlags;
|
||||
unsigned int dwFourCC;
|
||||
unsigned int dwRGBBitCount;
|
||||
unsigned int dwRBitMask;
|
||||
unsigned int dwGBitMask;
|
||||
unsigned int dwBBitMask;
|
||||
unsigned int dwAlphaBitMask;
|
||||
}
|
||||
sPixelFormat;
|
||||
|
||||
/* DDCAPS2 */
|
||||
struct
|
||||
{
|
||||
unsigned int dwCaps1;
|
||||
unsigned int dwCaps2;
|
||||
unsigned int dwDDSX;
|
||||
unsigned int dwReserved;
|
||||
}
|
||||
sCaps;
|
||||
unsigned int dwReserved2;
|
||||
}
|
||||
DDS_header ;
|
||||
|
||||
/* the following constants were copied directly off the MSDN website */
|
||||
|
||||
/* The dwFlags member of the original DDSURFACEDESC2 structure
|
||||
can be set to one or more of the following values. */
|
||||
#define DDSD_CAPS 0x00000001
|
||||
#define DDSD_HEIGHT 0x00000002
|
||||
#define DDSD_WIDTH 0x00000004
|
||||
#define DDSD_PITCH 0x00000008
|
||||
#define DDSD_PIXELFORMAT 0x00001000
|
||||
#define DDSD_MIPMAPCOUNT 0x00020000
|
||||
#define DDSD_LINEARSIZE 0x00080000
|
||||
#define DDSD_DEPTH 0x00800000
|
||||
|
||||
/* DirectDraw Pixel Format */
|
||||
#define DDPF_ALPHAPIXELS 0x00000001
|
||||
#define DDPF_FOURCC 0x00000004
|
||||
#define DDPF_RGB 0x00000040
|
||||
|
||||
/* The dwCaps1 member of the DDSCAPS2 structure can be
|
||||
set to one or more of the following values. */
|
||||
#define DDSCAPS_COMPLEX 0x00000008
|
||||
#define DDSCAPS_TEXTURE 0x00001000
|
||||
#define DDSCAPS_MIPMAP 0x00400000
|
||||
|
||||
/* The dwCaps2 member of the DDSCAPS2 structure can be
|
||||
set to one or more of the following values. */
|
||||
#define DDSCAPS2_CUBEMAP 0x00000200
|
||||
#define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400
|
||||
#define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800
|
||||
#define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000
|
||||
#define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000
|
||||
#define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000
|
||||
#define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000
|
||||
#define DDSCAPS2_VOLUME 0x00200000
|
||||
|
||||
#endif /* HEADER_IMAGE_DXT */
|
||||
@@ -0,0 +1,435 @@
|
||||
/*
|
||||
Jonathan Dummer
|
||||
|
||||
image helper functions
|
||||
|
||||
MIT license
|
||||
*/
|
||||
|
||||
#include "image_helper.h"
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
/* Upscaling the image uses simple bilinear interpolation */
|
||||
int
|
||||
up_scale_image
|
||||
(
|
||||
const unsigned char* const orig,
|
||||
int width, int height, int channels,
|
||||
unsigned char* resampled,
|
||||
int resampled_width, int resampled_height
|
||||
)
|
||||
{
|
||||
float dx, dy;
|
||||
int x, y, c;
|
||||
|
||||
/* error(s) check */
|
||||
if ( (width < 1) || (height < 1) ||
|
||||
(resampled_width < 2) || (resampled_height < 2) ||
|
||||
(channels < 1) ||
|
||||
(NULL == orig) || (NULL == resampled) )
|
||||
{
|
||||
/* signify badness */
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
for each given pixel in the new map, find the exact location
|
||||
from the original map which would contribute to this guy
|
||||
*/
|
||||
dx = (width - 1.0f) / (resampled_width - 1.0f);
|
||||
dy = (height - 1.0f) / (resampled_height - 1.0f);
|
||||
for ( y = 0; y < resampled_height; ++y )
|
||||
{
|
||||
/* find the base y index and fractional offset from that */
|
||||
float sampley = y * dy;
|
||||
int inty = (int)sampley;
|
||||
/* if( inty < 0 ) { inty = 0; } else */
|
||||
if( inty > height - 2 ) { inty = height - 2; }
|
||||
sampley -= inty;
|
||||
for ( x = 0; x < resampled_width; ++x )
|
||||
{
|
||||
float samplex = x * dx;
|
||||
int intx = (int)samplex;
|
||||
int base_index;
|
||||
/* find the base x index and fractional offset from that */
|
||||
/* if( intx < 0 ) { intx = 0; } else */
|
||||
if( intx > width - 2 ) { intx = width - 2; }
|
||||
samplex -= intx;
|
||||
/* base index into the original image */
|
||||
base_index = (inty * width + intx) * channels;
|
||||
for ( c = 0; c < channels; ++c )
|
||||
{
|
||||
/* do the sampling */
|
||||
float value = 0.5f;
|
||||
value += orig[base_index]
|
||||
*(1.0f-samplex)*(1.0f-sampley);
|
||||
value += orig[base_index+channels]
|
||||
*(samplex)*(1.0f-sampley);
|
||||
value += orig[base_index+width*channels]
|
||||
*(1.0f-samplex)*(sampley);
|
||||
value += orig[base_index+width*channels+channels]
|
||||
*(samplex)*(sampley);
|
||||
/* move to the next channel */
|
||||
++base_index;
|
||||
/* save the new value */
|
||||
resampled[y*resampled_width*channels+x*channels+c] =
|
||||
(unsigned char)(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
/* done */
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
mipmap_image
|
||||
(
|
||||
const unsigned char* const orig,
|
||||
int width, int height, int channels,
|
||||
unsigned char* resampled,
|
||||
int block_size_x, int block_size_y
|
||||
)
|
||||
{
|
||||
int mip_width, mip_height;
|
||||
int i, j, c;
|
||||
|
||||
/* error check */
|
||||
if( (width < 1) || (height < 1) ||
|
||||
(channels < 1) || (orig == NULL) ||
|
||||
(resampled == NULL) ||
|
||||
(block_size_x < 1) || (block_size_y < 1) )
|
||||
{
|
||||
/* nothing to do */
|
||||
return 0;
|
||||
}
|
||||
mip_width = width / block_size_x;
|
||||
mip_height = height / block_size_y;
|
||||
if( mip_width < 1 )
|
||||
{
|
||||
mip_width = 1;
|
||||
}
|
||||
if( mip_height < 1 )
|
||||
{
|
||||
mip_height = 1;
|
||||
}
|
||||
for( j = 0; j < mip_height; ++j )
|
||||
{
|
||||
for( i = 0; i < mip_width; ++i )
|
||||
{
|
||||
for( c = 0; c < channels; ++c )
|
||||
{
|
||||
const int index = (j*block_size_y)*width*channels + (i*block_size_x)*channels + c;
|
||||
int sum_value;
|
||||
int u,v;
|
||||
int u_block = block_size_x;
|
||||
int v_block = block_size_y;
|
||||
int block_area;
|
||||
/* do a bit of checking so we don't over-run the boundaries
|
||||
(necessary for non-square textures!) */
|
||||
if( block_size_x * (i+1) > width )
|
||||
{
|
||||
u_block = width - i*block_size_y;
|
||||
}
|
||||
if( block_size_y * (j+1) > height )
|
||||
{
|
||||
v_block = height - j*block_size_y;
|
||||
}
|
||||
block_area = u_block*v_block;
|
||||
/* for this pixel, see what the average
|
||||
of all the values in the block are.
|
||||
note: start the sum at the rounding value, not at 0 */
|
||||
sum_value = block_area >> 1;
|
||||
for( v = 0; v < v_block; ++v )
|
||||
for( u = 0; u < u_block; ++u )
|
||||
{
|
||||
sum_value += orig[index + v*width*channels + u*channels];
|
||||
}
|
||||
resampled[j*mip_width*channels + i*channels + c] = sum_value / block_area;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
scale_image_RGB_to_NTSC_safe
|
||||
(
|
||||
unsigned char* orig,
|
||||
int width, int height, int channels
|
||||
)
|
||||
{
|
||||
const float scale_lo = 16.0f - 0.499f;
|
||||
const float scale_hi = 235.0f + 0.499f;
|
||||
int i, j;
|
||||
int nc = channels;
|
||||
unsigned char scale_LUT[256];
|
||||
/* error check */
|
||||
if( (width < 1) || (height < 1) ||
|
||||
(channels < 1) || (orig == NULL) )
|
||||
{
|
||||
/* nothing to do */
|
||||
return 0;
|
||||
}
|
||||
/* set up the scaling Look Up Table */
|
||||
for( i = 0; i < 256; ++i )
|
||||
{
|
||||
scale_LUT[i] = (unsigned char)((scale_hi - scale_lo) * i / 255.0f + scale_lo);
|
||||
}
|
||||
/* for channels = 2 or 4, ignore the alpha component */
|
||||
nc -= 1 - (channels & 1);
|
||||
/* OK, go through the image and scale any non-alpha components */
|
||||
for( i = 0; i < width*height*channels; i += channels )
|
||||
{
|
||||
for( j = 0; j < nc; ++j )
|
||||
{
|
||||
orig[i+j] = scale_LUT[orig[i+j]];
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
unsigned char clamp_byte( int x ) { return ( (x) < 0 ? (0) : ( (x) > 255 ? 255 : (x) ) ); }
|
||||
|
||||
/*
|
||||
This function takes the RGB components of the image
|
||||
and converts them into YCoCg. 3 components will be
|
||||
re-ordered to CoYCg (for optimum DXT1 compression),
|
||||
while 4 components will be ordered CoCgAY (for DXT5
|
||||
compression).
|
||||
*/
|
||||
int
|
||||
convert_RGB_to_YCoCg
|
||||
(
|
||||
unsigned char* orig,
|
||||
int width, int height, int channels
|
||||
)
|
||||
{
|
||||
int i;
|
||||
/* error check */
|
||||
if( (width < 1) || (height < 1) ||
|
||||
(channels < 3) || (channels > 4) ||
|
||||
(orig == NULL) )
|
||||
{
|
||||
/* nothing to do */
|
||||
return -1;
|
||||
}
|
||||
/* do the conversion */
|
||||
if( channels == 3 )
|
||||
{
|
||||
for( i = 0; i < width*height*3; i += 3 )
|
||||
{
|
||||
int r = orig[i+0];
|
||||
int g = (orig[i+1] + 1) >> 1;
|
||||
int b = orig[i+2];
|
||||
int tmp = (2 + r + b) >> 2;
|
||||
/* Co */
|
||||
orig[i+0] = clamp_byte( 128 + ((r - b + 1) >> 1) );
|
||||
/* Y */
|
||||
orig[i+1] = clamp_byte( g + tmp );
|
||||
/* Cg */
|
||||
orig[i+2] = clamp_byte( 128 + g - tmp );
|
||||
}
|
||||
} else
|
||||
{
|
||||
for( i = 0; i < width*height*4; i += 4 )
|
||||
{
|
||||
int r = orig[i+0];
|
||||
int g = (orig[i+1] + 1) >> 1;
|
||||
int b = orig[i+2];
|
||||
unsigned char a = orig[i+3];
|
||||
int tmp = (2 + r + b) >> 2;
|
||||
/* Co */
|
||||
orig[i+0] = clamp_byte( 128 + ((r - b + 1) >> 1) );
|
||||
/* Cg */
|
||||
orig[i+1] = clamp_byte( 128 + g - tmp );
|
||||
/* Alpha */
|
||||
orig[i+2] = a;
|
||||
/* Y */
|
||||
orig[i+3] = clamp_byte( g + tmp );
|
||||
}
|
||||
}
|
||||
/* done */
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
This function takes the YCoCg components of the image
|
||||
and converts them into RGB. See above.
|
||||
*/
|
||||
int
|
||||
convert_YCoCg_to_RGB
|
||||
(
|
||||
unsigned char* orig,
|
||||
int width, int height, int channels
|
||||
)
|
||||
{
|
||||
int i;
|
||||
/* error check */
|
||||
if( (width < 1) || (height < 1) ||
|
||||
(channels < 3) || (channels > 4) ||
|
||||
(orig == NULL) )
|
||||
{
|
||||
/* nothing to do */
|
||||
return -1;
|
||||
}
|
||||
/* do the conversion */
|
||||
if( channels == 3 )
|
||||
{
|
||||
for( i = 0; i < width*height*3; i += 3 )
|
||||
{
|
||||
int co = orig[i+0] - 128;
|
||||
int y = orig[i+1];
|
||||
int cg = orig[i+2] - 128;
|
||||
/* R */
|
||||
orig[i+0] = clamp_byte( y + co - cg );
|
||||
/* G */
|
||||
orig[i+1] = clamp_byte( y + cg );
|
||||
/* B */
|
||||
orig[i+2] = clamp_byte( y - co - cg );
|
||||
}
|
||||
} else
|
||||
{
|
||||
for( i = 0; i < width*height*4; i += 4 )
|
||||
{
|
||||
int co = orig[i+0] - 128;
|
||||
int cg = orig[i+1] - 128;
|
||||
unsigned char a = orig[i+2];
|
||||
int y = orig[i+3];
|
||||
/* R */
|
||||
orig[i+0] = clamp_byte( y + co - cg );
|
||||
/* G */
|
||||
orig[i+1] = clamp_byte( y + cg );
|
||||
/* B */
|
||||
orig[i+2] = clamp_byte( y - co - cg );
|
||||
/* A */
|
||||
orig[i+3] = a;
|
||||
}
|
||||
}
|
||||
/* done */
|
||||
return 0;
|
||||
}
|
||||
|
||||
float
|
||||
find_max_RGBE
|
||||
(
|
||||
unsigned char *image,
|
||||
int width, int height
|
||||
)
|
||||
{
|
||||
float max_val = 0.0f;
|
||||
unsigned char *img = image;
|
||||
int i, j;
|
||||
for( i = width * height; i > 0; --i )
|
||||
{
|
||||
/* float scale = powf( 2.0f, img[3] - 128.0f ) / 255.0f; */
|
||||
float scale = ldexp( 1.0f / 255.0f, (int)(img[3]) - 128 );
|
||||
for( j = 0; j < 3; ++j )
|
||||
{
|
||||
if( img[j] * scale > max_val )
|
||||
{
|
||||
max_val = img[j] * scale;
|
||||
}
|
||||
}
|
||||
/* next pixel */
|
||||
img += 4;
|
||||
}
|
||||
return max_val;
|
||||
}
|
||||
|
||||
int
|
||||
RGBE_to_RGBdivA
|
||||
(
|
||||
unsigned char *image,
|
||||
int width, int height,
|
||||
int rescale_to_max
|
||||
)
|
||||
{
|
||||
/* local variables */
|
||||
int i, iv;
|
||||
unsigned char *img = image;
|
||||
float scale = 1.0f;
|
||||
/* error check */
|
||||
if( (!image) || (width < 1) || (height < 1) )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
/* convert (note: no negative numbers, but 0.0 is possible) */
|
||||
if( rescale_to_max )
|
||||
{
|
||||
scale = 255.0f / find_max_RGBE( image, width, height );
|
||||
}
|
||||
for( i = width * height; i > 0; --i )
|
||||
{
|
||||
/* decode this pixel, and find the max */
|
||||
float r,g,b,e, m;
|
||||
/* e = scale * powf( 2.0f, img[3] - 128.0f ) / 255.0f; */
|
||||
e = scale * ldexp( 1.0f / 255.0f, (int)(img[3]) - 128 );
|
||||
r = e * img[0];
|
||||
g = e * img[1];
|
||||
b = e * img[2];
|
||||
m = (r > g) ? r : g;
|
||||
m = (b > m) ? b : m;
|
||||
/* and encode it into RGBdivA */
|
||||
iv = (m != 0.0f) ? (int)(255.0f / m) : 1.0f;
|
||||
iv = (iv < 1) ? 1 : iv;
|
||||
img[3] = (iv > 255) ? 255 : iv;
|
||||
iv = (int)(img[3] * r + 0.5f);
|
||||
img[0] = (iv > 255) ? 255 : iv;
|
||||
iv = (int)(img[3] * g + 0.5f);
|
||||
img[1] = (iv > 255) ? 255 : iv;
|
||||
iv = (int)(img[3] * b + 0.5f);
|
||||
img[2] = (iv > 255) ? 255 : iv;
|
||||
/* and on to the next pixel */
|
||||
img += 4;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
RGBE_to_RGBdivA2
|
||||
(
|
||||
unsigned char *image,
|
||||
int width, int height,
|
||||
int rescale_to_max
|
||||
)
|
||||
{
|
||||
/* local variables */
|
||||
int i, iv;
|
||||
unsigned char *img = image;
|
||||
float scale = 1.0f;
|
||||
/* error check */
|
||||
if( (!image) || (width < 1) || (height < 1) )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
/* convert (note: no negative numbers, but 0.0 is possible) */
|
||||
if( rescale_to_max )
|
||||
{
|
||||
scale = 255.0f * 255.0f / find_max_RGBE( image, width, height );
|
||||
}
|
||||
for( i = width * height; i > 0; --i )
|
||||
{
|
||||
/* decode this pixel, and find the max */
|
||||
float r,g,b,e, m;
|
||||
/* e = scale * powf( 2.0f, img[3] - 128.0f ) / 255.0f; */
|
||||
e = scale * ldexp( 1.0f / 255.0f, (int)(img[3]) - 128 );
|
||||
r = e * img[0];
|
||||
g = e * img[1];
|
||||
b = e * img[2];
|
||||
m = (r > g) ? r : g;
|
||||
m = (b > m) ? b : m;
|
||||
/* and encode it into RGBdivA */
|
||||
iv = (m != 0.0f) ? (int)sqrtf( 255.0f * 255.0f / m ) : 1.0f;
|
||||
iv = (iv < 1) ? 1 : iv;
|
||||
img[3] = (iv > 255) ? 255 : iv;
|
||||
iv = (int)(img[3] * img[3] * r / 255.0f + 0.5f);
|
||||
img[0] = (iv > 255) ? 255 : iv;
|
||||
iv = (int)(img[3] * img[3] * g / 255.0f + 0.5f);
|
||||
img[1] = (iv > 255) ? 255 : iv;
|
||||
iv = (int)(img[3] * img[3] * b / 255.0f + 0.5f);
|
||||
img[2] = (iv > 255) ? 255 : iv;
|
||||
/* and on to the next pixel */
|
||||
img += 4;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
Jonathan Dummer
|
||||
|
||||
Image helper functions
|
||||
|
||||
MIT license
|
||||
*/
|
||||
|
||||
#ifndef HEADER_IMAGE_HELPER
|
||||
#define HEADER_IMAGE_HELPER
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
This function upscales an image.
|
||||
Not to be used to create MIPmaps,
|
||||
but to make it square,
|
||||
or to make it a power-of-two sized.
|
||||
**/
|
||||
int
|
||||
up_scale_image
|
||||
(
|
||||
const unsigned char* const orig,
|
||||
int width, int height, int channels,
|
||||
unsigned char* resampled,
|
||||
int resampled_width, int resampled_height
|
||||
);
|
||||
|
||||
/**
|
||||
This function downscales an image.
|
||||
Used for creating MIPmaps,
|
||||
the incoming image should be a
|
||||
power-of-two sized.
|
||||
**/
|
||||
int
|
||||
mipmap_image
|
||||
(
|
||||
const unsigned char* const orig,
|
||||
int width, int height, int channels,
|
||||
unsigned char* resampled,
|
||||
int block_size_x, int block_size_y
|
||||
);
|
||||
|
||||
/**
|
||||
This function takes the RGB components of the image
|
||||
and scales each channel from [0,255] to [16,235].
|
||||
This makes the colors "Safe" for display on NTSC
|
||||
displays. Note that this is _NOT_ a good idea for
|
||||
loading images like normal- or height-maps!
|
||||
**/
|
||||
int
|
||||
scale_image_RGB_to_NTSC_safe
|
||||
(
|
||||
unsigned char* orig,
|
||||
int width, int height, int channels
|
||||
);
|
||||
|
||||
/**
|
||||
This function takes the RGB components of the image
|
||||
and converts them into YCoCg. 3 components will be
|
||||
re-ordered to CoYCg (for optimum DXT1 compression),
|
||||
while 4 components will be ordered CoCgAY (for DXT5
|
||||
compression).
|
||||
**/
|
||||
int
|
||||
convert_RGB_to_YCoCg
|
||||
(
|
||||
unsigned char* orig,
|
||||
int width, int height, int channels
|
||||
);
|
||||
|
||||
/**
|
||||
This function takes the YCoCg components of the image
|
||||
and converts them into RGB. See above.
|
||||
**/
|
||||
int
|
||||
convert_YCoCg_to_RGB
|
||||
(
|
||||
unsigned char* orig,
|
||||
int width, int height, int channels
|
||||
);
|
||||
|
||||
/**
|
||||
Converts an HDR image from an array
|
||||
of unsigned chars (RGBE) to RGBdivA
|
||||
\return 0 if failed, otherwise returns 1
|
||||
**/
|
||||
int
|
||||
RGBE_to_RGBdivA
|
||||
(
|
||||
unsigned char *image,
|
||||
int width, int height,
|
||||
int rescale_to_max
|
||||
);
|
||||
|
||||
/**
|
||||
Converts an HDR image from an array
|
||||
of unsigned chars (RGBE) to RGBdivA2
|
||||
\return 0 if failed, otherwise returns 1
|
||||
**/
|
||||
int
|
||||
RGBE_to_RGBdivA2
|
||||
(
|
||||
unsigned char *image,
|
||||
int width, int height,
|
||||
int rescale_to_max
|
||||
);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* HEADER_IMAGE_HELPER */
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,354 @@
|
||||
/* stbi-1.16 - public domain JPEG/PNG reader - http://nothings.org/stb_image.c
|
||||
when you control the images you're loading
|
||||
|
||||
QUICK NOTES:
|
||||
Primarily of interest to game developers and other people who can
|
||||
avoid problematic images and only need the trivial interface
|
||||
|
||||
JPEG baseline (no JPEG progressive, no oddball channel decimations)
|
||||
PNG non-interlaced
|
||||
BMP non-1bpp, non-RLE
|
||||
TGA (not sure what subset, if a subset)
|
||||
PSD (composited view only, no extra channels)
|
||||
HDR (radiance rgbE format)
|
||||
writes BMP,TGA (define STBI_NO_WRITE to remove code)
|
||||
decoded from memory or through stdio FILE (define STBI_NO_STDIO to remove code)
|
||||
supports installable dequantizing-IDCT, YCbCr-to-RGB conversion (define STBI_SIMD)
|
||||
|
||||
TODO:
|
||||
stbi_info_*
|
||||
|
||||
history:
|
||||
1.16 major bugfix - convert_format converted one too many pixels
|
||||
1.15 initialize some fields for thread safety
|
||||
1.14 fix threadsafe conversion bug; header-file-only version (#define STBI_HEADER_FILE_ONLY before including)
|
||||
1.13 threadsafe
|
||||
1.12 const qualifiers in the API
|
||||
1.11 Support installable IDCT, colorspace conversion routines
|
||||
1.10 Fixes for 64-bit (don't use "unsigned long")
|
||||
optimized upsampling by Fabian "ryg" Giesen
|
||||
1.09 Fix format-conversion for PSD code (bad global variables!)
|
||||
1.08 Thatcher Ulrich's PSD code integrated by Nicolas Schulz
|
||||
1.07 attempt to fix C++ warning/errors again
|
||||
1.06 attempt to fix C++ warning/errors again
|
||||
1.05 fix TGA loading to return correct *comp and use good luminance calc
|
||||
1.04 default float alpha is 1, not 255; use 'void *' for stbi_image_free
|
||||
1.03 bugfixes to STBI_NO_STDIO, STBI_NO_HDR
|
||||
1.02 support for (subset of) HDR files, float interface for preferred access to them
|
||||
1.01 fix bug: possible bug in handling right-side up bmps... not sure
|
||||
fix bug: the stbi_bmp_load() and stbi_tga_load() functions didn't work at all
|
||||
1.00 interface to zlib that skips zlib header
|
||||
0.99 correct handling of alpha in palette
|
||||
0.98 TGA loader by lonesock; dynamically add loaders (untested)
|
||||
0.97 jpeg errors on too large a file; also catch another malloc failure
|
||||
0.96 fix detection of invalid v value - particleman@mollyrocket forum
|
||||
0.95 during header scan, seek to markers in case of padding
|
||||
0.94 STBI_NO_STDIO to disable stdio usage; rename all #defines the same
|
||||
0.93 handle jpegtran output; verbose errors
|
||||
0.92 read 4,8,16,24,32-bit BMP files of several formats
|
||||
0.91 output 24-bit Windows 3.0 BMP files
|
||||
0.90 fix a few more warnings; bump version number to approach 1.0
|
||||
0.61 bugfixes due to Marc LeBlanc, Christopher Lloyd
|
||||
0.60 fix compiling as c++
|
||||
0.59 fix warnings: merge Dave Moore's -Wall fixes
|
||||
0.58 fix bug: zlib uncompressed mode len/nlen was wrong endian
|
||||
0.57 fix bug: jpg last huffman symbol before marker was >9 bits but less
|
||||
than 16 available
|
||||
0.56 fix bug: zlib uncompressed mode len vs. nlen
|
||||
0.55 fix bug: restart_interval not initialized to 0
|
||||
0.54 allow NULL for 'int *comp'
|
||||
0.53 fix bug in png 3->4; speedup png decoding
|
||||
0.52 png handles req_comp=3,4 directly; minor cleanup; jpeg comments
|
||||
0.51 obey req_comp requests, 1-component jpegs return as 1-component,
|
||||
on 'test' only check type, not whether we support this variant
|
||||
*/
|
||||
|
||||
#ifndef HEADER_STB_IMAGE_AUGMENTED
|
||||
#define HEADER_STB_IMAGE_AUGMENTED
|
||||
|
||||
//// begin header file ////////////////////////////////////////////////////
|
||||
//
|
||||
// Limitations:
|
||||
// - no progressive/interlaced support (jpeg, png)
|
||||
// - 8-bit samples only (jpeg, png)
|
||||
// - not threadsafe
|
||||
// - channel subsampling of at most 2 in each dimension (jpeg)
|
||||
// - no delayed line count (jpeg) -- IJG doesn't support either
|
||||
//
|
||||
// Basic usage (see HDR discussion below):
|
||||
// int x,y,n;
|
||||
// unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
|
||||
// // ... process data if not NULL ...
|
||||
// // ... x = width, y = height, n = # 8-bit components per pixel ...
|
||||
// // ... replace '0' with '1'..'4' to force that many components per pixel
|
||||
// stbi_image_free(data)
|
||||
//
|
||||
// Standard parameters:
|
||||
// int *x -- outputs image width in pixels
|
||||
// int *y -- outputs image height in pixels
|
||||
// int *comp -- outputs # of image components in image file
|
||||
// int req_comp -- if non-zero, # of image components requested in result
|
||||
//
|
||||
// The return value from an image loader is an 'unsigned char *' which points
|
||||
// to the pixel data. The pixel data consists of *y scanlines of *x pixels,
|
||||
// with each pixel consisting of N interleaved 8-bit components; the first
|
||||
// pixel pointed to is top-left-most in the image. There is no padding between
|
||||
// image scanlines or between pixels, regardless of format. The number of
|
||||
// components N is 'req_comp' if req_comp is non-zero, or *comp otherwise.
|
||||
// If req_comp is non-zero, *comp has the number of components that _would_
|
||||
// have been output otherwise. E.g. if you set req_comp to 4, you will always
|
||||
// get RGBA output, but you can check *comp to easily see if it's opaque.
|
||||
//
|
||||
// An output image with N components has the following components interleaved
|
||||
// in this order in each pixel:
|
||||
//
|
||||
// N=#comp components
|
||||
// 1 grey
|
||||
// 2 grey, alpha
|
||||
// 3 red, green, blue
|
||||
// 4 red, green, blue, alpha
|
||||
//
|
||||
// If image loading fails for any reason, the return value will be NULL,
|
||||
// and *x, *y, *comp will be unchanged. The function stbi_failure_reason()
|
||||
// can be queried for an extremely brief, end-user unfriendly explanation
|
||||
// of why the load failed. Define STBI_NO_FAILURE_STRINGS to avoid
|
||||
// compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly
|
||||
// more user-friendly ones.
|
||||
//
|
||||
// Paletted PNG and BMP images are automatically depalettized.
|
||||
//
|
||||
//
|
||||
// ===========================================================================
|
||||
//
|
||||
// HDR image support (disable by defining STBI_NO_HDR)
|
||||
//
|
||||
// stb_image now supports loading HDR images in general, and currently
|
||||
// the Radiance .HDR file format, although the support is provided
|
||||
// generically. You can still load any file through the existing interface;
|
||||
// if you attempt to load an HDR file, it will be automatically remapped to
|
||||
// LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;
|
||||
// both of these constants can be reconfigured through this interface:
|
||||
//
|
||||
// stbi_hdr_to_ldr_gamma(2.2f);
|
||||
// stbi_hdr_to_ldr_scale(1.0f);
|
||||
//
|
||||
// (note, do not use _inverse_ constants; stbi_image will invert them
|
||||
// appropriately).
|
||||
//
|
||||
// Additionally, there is a new, parallel interface for loading files as
|
||||
// (linear) floats to preserve the full dynamic range:
|
||||
//
|
||||
// float *data = stbi_loadf(filename, &x, &y, &n, 0);
|
||||
//
|
||||
// If you load LDR images through this interface, those images will
|
||||
// be promoted to floating point values, run through the inverse of
|
||||
// constants corresponding to the above:
|
||||
//
|
||||
// stbi_ldr_to_hdr_scale(1.0f);
|
||||
// stbi_ldr_to_hdr_gamma(2.2f);
|
||||
//
|
||||
// Finally, given a filename (or an open file or memory block--see header
|
||||
// file for details) containing image data, you can query for the "most
|
||||
// appropriate" interface to use (that is, whether the image is HDR or
|
||||
// not), using:
|
||||
//
|
||||
// stbi_is_hdr(char *filename);
|
||||
|
||||
#ifndef STBI_NO_STDIO
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
#define STBI_VERSION 1
|
||||
|
||||
enum
|
||||
{
|
||||
STBI_default = 0, // only used for req_comp
|
||||
|
||||
STBI_grey = 1,
|
||||
STBI_grey_alpha = 2,
|
||||
STBI_rgb = 3,
|
||||
STBI_rgb_alpha = 4,
|
||||
};
|
||||
|
||||
typedef unsigned char stbi_uc;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// WRITING API
|
||||
|
||||
#if !defined(STBI_NO_WRITE) && !defined(STBI_NO_STDIO)
|
||||
// write a BMP/TGA file given tightly packed 'comp' channels (no padding, nor bmp-stride-padding)
|
||||
// (you must include the appropriate extension in the filename).
|
||||
// returns TRUE on success, FALSE if couldn't open file, error writing file
|
||||
extern int stbi_write_bmp (char const *filename, int x, int y, int comp, void *data);
|
||||
extern int stbi_write_tga (char const *filename, int x, int y, int comp, void *data);
|
||||
#endif
|
||||
|
||||
// PRIMARY API - works on images of any type
|
||||
|
||||
// load image by filename, open file, or memory buffer
|
||||
#ifndef STBI_NO_STDIO
|
||||
extern stbi_uc *stbi_load (char const *filename, int *x, int *y, int *comp, int req_comp);
|
||||
extern stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
||||
extern int stbi_info_from_file (FILE *f, int *x, int *y, int *comp);
|
||||
#endif
|
||||
extern stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
||||
// for stbi_load_from_file, file pointer is left pointing immediately after image
|
||||
|
||||
#ifndef STBI_NO_HDR
|
||||
#ifndef STBI_NO_STDIO
|
||||
extern float *stbi_loadf (char const *filename, int *x, int *y, int *comp, int req_comp);
|
||||
extern float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
||||
#endif
|
||||
extern float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
||||
|
||||
extern void stbi_hdr_to_ldr_gamma(float gamma);
|
||||
extern void stbi_hdr_to_ldr_scale(float scale);
|
||||
|
||||
extern void stbi_ldr_to_hdr_gamma(float gamma);
|
||||
extern void stbi_ldr_to_hdr_scale(float scale);
|
||||
|
||||
#endif // STBI_NO_HDR
|
||||
|
||||
// get a VERY brief reason for failure
|
||||
// NOT THREADSAFE
|
||||
extern char *stbi_failure_reason (void);
|
||||
|
||||
// free the loaded image -- this is just free()
|
||||
extern void stbi_image_free (void *retval_from_stbi_load);
|
||||
|
||||
// get image dimensions & components without fully decoding
|
||||
extern int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
|
||||
extern int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len);
|
||||
#ifndef STBI_NO_STDIO
|
||||
extern int stbi_info (char const *filename, int *x, int *y, int *comp);
|
||||
extern int stbi_is_hdr (char const *filename);
|
||||
extern int stbi_is_hdr_from_file(FILE *f);
|
||||
#endif
|
||||
|
||||
// ZLIB client - used by PNG, available for other purposes
|
||||
|
||||
extern char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen);
|
||||
extern char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen);
|
||||
extern int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
|
||||
|
||||
extern char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen);
|
||||
extern int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
|
||||
|
||||
// TYPE-SPECIFIC ACCESS
|
||||
|
||||
// is it a jpeg?
|
||||
extern int stbi_jpeg_test_memory (stbi_uc const *buffer, int len);
|
||||
extern stbi_uc *stbi_jpeg_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
||||
extern int stbi_jpeg_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
|
||||
|
||||
#ifndef STBI_NO_STDIO
|
||||
extern stbi_uc *stbi_jpeg_load (char const *filename, int *x, int *y, int *comp, int req_comp);
|
||||
extern int stbi_jpeg_test_file (FILE *f);
|
||||
extern stbi_uc *stbi_jpeg_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
||||
|
||||
extern int stbi_jpeg_info (char const *filename, int *x, int *y, int *comp);
|
||||
extern int stbi_jpeg_info_from_file (FILE *f, int *x, int *y, int *comp);
|
||||
#endif
|
||||
|
||||
// is it a png?
|
||||
extern int stbi_png_test_memory (stbi_uc const *buffer, int len);
|
||||
extern stbi_uc *stbi_png_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
||||
extern int stbi_png_info_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp);
|
||||
|
||||
#ifndef STBI_NO_STDIO
|
||||
extern stbi_uc *stbi_png_load (char const *filename, int *x, int *y, int *comp, int req_comp);
|
||||
extern int stbi_png_info (char const *filename, int *x, int *y, int *comp);
|
||||
extern int stbi_png_test_file (FILE *f);
|
||||
extern stbi_uc *stbi_png_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
||||
extern int stbi_png_info_from_file (FILE *f, int *x, int *y, int *comp);
|
||||
#endif
|
||||
|
||||
// is it a bmp?
|
||||
extern int stbi_bmp_test_memory (stbi_uc const *buffer, int len);
|
||||
|
||||
extern stbi_uc *stbi_bmp_load (char const *filename, int *x, int *y, int *comp, int req_comp);
|
||||
extern stbi_uc *stbi_bmp_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
||||
#ifndef STBI_NO_STDIO
|
||||
extern int stbi_bmp_test_file (FILE *f);
|
||||
extern stbi_uc *stbi_bmp_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
||||
#endif
|
||||
|
||||
// is it a tga?
|
||||
extern int stbi_tga_test_memory (stbi_uc const *buffer, int len);
|
||||
|
||||
extern stbi_uc *stbi_tga_load (char const *filename, int *x, int *y, int *comp, int req_comp);
|
||||
extern stbi_uc *stbi_tga_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
||||
#ifndef STBI_NO_STDIO
|
||||
extern int stbi_tga_test_file (FILE *f);
|
||||
extern stbi_uc *stbi_tga_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
||||
#endif
|
||||
|
||||
// is it a psd?
|
||||
extern int stbi_psd_test_memory (stbi_uc const *buffer, int len);
|
||||
|
||||
extern stbi_uc *stbi_psd_load (char const *filename, int *x, int *y, int *comp, int req_comp);
|
||||
extern stbi_uc *stbi_psd_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
||||
#ifndef STBI_NO_STDIO
|
||||
extern int stbi_psd_test_file (FILE *f);
|
||||
extern stbi_uc *stbi_psd_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
||||
#endif
|
||||
|
||||
// is it an hdr?
|
||||
extern int stbi_hdr_test_memory (stbi_uc const *buffer, int len);
|
||||
|
||||
extern float * stbi_hdr_load (char const *filename, int *x, int *y, int *comp, int req_comp);
|
||||
extern float * stbi_hdr_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
||||
extern stbi_uc *stbi_hdr_load_rgbe (char const *filename, int *x, int *y, int *comp, int req_comp);
|
||||
extern float * stbi_hdr_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
||||
#ifndef STBI_NO_STDIO
|
||||
extern int stbi_hdr_test_file (FILE *f);
|
||||
extern float * stbi_hdr_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
||||
extern stbi_uc *stbi_hdr_load_rgbe_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
||||
#endif
|
||||
|
||||
// define new loaders
|
||||
typedef struct
|
||||
{
|
||||
int (*test_memory)(stbi_uc const *buffer, int len);
|
||||
stbi_uc * (*load_from_memory)(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
||||
#ifndef STBI_NO_STDIO
|
||||
int (*test_file)(FILE *f);
|
||||
stbi_uc * (*load_from_file)(FILE *f, int *x, int *y, int *comp, int req_comp);
|
||||
#endif
|
||||
} stbi_loader;
|
||||
|
||||
// register a loader by filling out the above structure (you must defined ALL functions)
|
||||
// returns 1 if added or already added, 0 if not added (too many loaders)
|
||||
// NOT THREADSAFE
|
||||
extern int stbi_register_loader(stbi_loader *loader);
|
||||
|
||||
// define faster low-level operations (typically SIMD support)
|
||||
#if STBI_SIMD
|
||||
typedef void (*stbi_idct_8x8)(uint8 *out, int out_stride, short data[64], unsigned short *dequantize);
|
||||
// compute an integer IDCT on "input"
|
||||
// input[x] = data[x] * dequantize[x]
|
||||
// write results to 'out': 64 samples, each run of 8 spaced by 'out_stride'
|
||||
// CLAMP results to 0..255
|
||||
typedef void (*stbi_YCbCr_to_RGB_run)(uint8 *output, uint8 const *y, uint8 const *cb, uint8 const *cr, int count, int step);
|
||||
// compute a conversion from YCbCr to RGB
|
||||
// 'count' pixels
|
||||
// write pixels to 'output'; each pixel is 'step' bytes (either 3 or 4; if 4, write '255' as 4th), order R,G,B
|
||||
// y: Y input channel
|
||||
// cb: Cb input channel; scale/biased to be 0..255
|
||||
// cr: Cr input channel; scale/biased to be 0..255
|
||||
|
||||
extern void stbi_install_idct(stbi_idct_8x8 func);
|
||||
extern void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func);
|
||||
#endif // STBI_SIMD
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
//
|
||||
//
|
||||
//// end header file /////////////////////////////////////////////////////
|
||||
#endif // STBI_INCLUDE_STB_IMAGE_H
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
adding DDS loading support to stbi
|
||||
*/
|
||||
|
||||
#ifndef HEADER_STB_IMAGE_DDS_AUGMENTATION
|
||||
#define HEADER_STB_IMAGE_DDS_AUGMENTATION
|
||||
|
||||
// is it a DDS file?
|
||||
extern int stbi_dds_test_memory (stbi_uc const *buffer, int len);
|
||||
|
||||
extern stbi_uc *stbi_dds_load (char *filename, int *x, int *y, int *comp, int req_comp);
|
||||
extern stbi_uc *stbi_dds_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
||||
#ifndef STBI_NO_STDIO
|
||||
extern int stbi_dds_test_file (FILE *f);
|
||||
extern stbi_uc *stbi_dds_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
||||
#endif
|
||||
|
||||
//
|
||||
//
|
||||
//// end header file /////////////////////////////////////////////////////
|
||||
#endif // HEADER_STB_IMAGE_DDS_AUGMENTATION
|
||||
@@ -0,0 +1,511 @@
|
||||
|
||||
/// DDS file support, does decoding, _not_ direct uploading
|
||||
/// (use SOIL for that ;-)
|
||||
|
||||
/// A bunch of DirectDraw Surface structures and flags
|
||||
typedef struct {
|
||||
unsigned int dwMagic;
|
||||
unsigned int dwSize;
|
||||
unsigned int dwFlags;
|
||||
unsigned int dwHeight;
|
||||
unsigned int dwWidth;
|
||||
unsigned int dwPitchOrLinearSize;
|
||||
unsigned int dwDepth;
|
||||
unsigned int dwMipMapCount;
|
||||
unsigned int dwReserved1[ 11 ];
|
||||
|
||||
// DDPIXELFORMAT
|
||||
struct {
|
||||
unsigned int dwSize;
|
||||
unsigned int dwFlags;
|
||||
unsigned int dwFourCC;
|
||||
unsigned int dwRGBBitCount;
|
||||
unsigned int dwRBitMask;
|
||||
unsigned int dwGBitMask;
|
||||
unsigned int dwBBitMask;
|
||||
unsigned int dwAlphaBitMask;
|
||||
} sPixelFormat;
|
||||
|
||||
// DDCAPS2
|
||||
struct {
|
||||
unsigned int dwCaps1;
|
||||
unsigned int dwCaps2;
|
||||
unsigned int dwDDSX;
|
||||
unsigned int dwReserved;
|
||||
} sCaps;
|
||||
unsigned int dwReserved2;
|
||||
} DDS_header ;
|
||||
|
||||
// the following constants were copied directly off the MSDN website
|
||||
|
||||
// The dwFlags member of the original DDSURFACEDESC2 structure
|
||||
// can be set to one or more of the following values.
|
||||
#define DDSD_CAPS 0x00000001
|
||||
#define DDSD_HEIGHT 0x00000002
|
||||
#define DDSD_WIDTH 0x00000004
|
||||
#define DDSD_PITCH 0x00000008
|
||||
#define DDSD_PIXELFORMAT 0x00001000
|
||||
#define DDSD_MIPMAPCOUNT 0x00020000
|
||||
#define DDSD_LINEARSIZE 0x00080000
|
||||
#define DDSD_DEPTH 0x00800000
|
||||
|
||||
// DirectDraw Pixel Format
|
||||
#define DDPF_ALPHAPIXELS 0x00000001
|
||||
#define DDPF_FOURCC 0x00000004
|
||||
#define DDPF_RGB 0x00000040
|
||||
|
||||
// The dwCaps1 member of the DDSCAPS2 structure can be
|
||||
// set to one or more of the following values.
|
||||
#define DDSCAPS_COMPLEX 0x00000008
|
||||
#define DDSCAPS_TEXTURE 0x00001000
|
||||
#define DDSCAPS_MIPMAP 0x00400000
|
||||
|
||||
// The dwCaps2 member of the DDSCAPS2 structure can be
|
||||
// set to one or more of the following values.
|
||||
#define DDSCAPS2_CUBEMAP 0x00000200
|
||||
#define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400
|
||||
#define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800
|
||||
#define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000
|
||||
#define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000
|
||||
#define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000
|
||||
#define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000
|
||||
#define DDSCAPS2_VOLUME 0x00200000
|
||||
|
||||
static int dds_test(stbi *s)
|
||||
{
|
||||
// check the magic number
|
||||
if (get8(s) != 'D') return 0;
|
||||
if (get8(s) != 'D') return 0;
|
||||
if (get8(s) != 'S') return 0;
|
||||
if (get8(s) != ' ') return 0;
|
||||
// check header size
|
||||
if (get32le(s) != 124) return 0;
|
||||
return 1;
|
||||
}
|
||||
#ifndef STBI_NO_STDIO
|
||||
int stbi_dds_test_file (FILE *f)
|
||||
{
|
||||
stbi s;
|
||||
int r,n = ftell(f);
|
||||
start_file(&s,f);
|
||||
r = dds_test(&s);
|
||||
fseek(f,n,SEEK_SET);
|
||||
return r;
|
||||
}
|
||||
#endif
|
||||
|
||||
int stbi_dds_test_memory (stbi_uc const *buffer, int len)
|
||||
{
|
||||
stbi s;
|
||||
start_mem(&s,buffer, len);
|
||||
return dds_test(&s);
|
||||
}
|
||||
|
||||
// helper functions
|
||||
int stbi_convert_bit_range( int c, int from_bits, int to_bits )
|
||||
{
|
||||
int b = (1 << (from_bits - 1)) + c * ((1 << to_bits) - 1);
|
||||
return (b + (b >> from_bits)) >> from_bits;
|
||||
}
|
||||
void stbi_rgb_888_from_565( unsigned int c, int *r, int *g, int *b )
|
||||
{
|
||||
*r = stbi_convert_bit_range( (c >> 11) & 31, 5, 8 );
|
||||
*g = stbi_convert_bit_range( (c >> 05) & 63, 6, 8 );
|
||||
*b = stbi_convert_bit_range( (c >> 00) & 31, 5, 8 );
|
||||
}
|
||||
void stbi_decode_DXT1_block(
|
||||
unsigned char uncompressed[16*4],
|
||||
unsigned char compressed[8] )
|
||||
{
|
||||
int next_bit = 4*8;
|
||||
int i, r, g, b;
|
||||
int c0, c1;
|
||||
unsigned char decode_colors[4*4];
|
||||
// find the 2 primary colors
|
||||
c0 = compressed[0] + (compressed[1] << 8);
|
||||
c1 = compressed[2] + (compressed[3] << 8);
|
||||
stbi_rgb_888_from_565( c0, &r, &g, &b );
|
||||
decode_colors[0] = r;
|
||||
decode_colors[1] = g;
|
||||
decode_colors[2] = b;
|
||||
decode_colors[3] = 255;
|
||||
stbi_rgb_888_from_565( c1, &r, &g, &b );
|
||||
decode_colors[4] = r;
|
||||
decode_colors[5] = g;
|
||||
decode_colors[6] = b;
|
||||
decode_colors[7] = 255;
|
||||
if( c0 > c1 )
|
||||
{
|
||||
// no alpha, 2 interpolated colors
|
||||
decode_colors[8] = (2*decode_colors[0] + decode_colors[4]) / 3;
|
||||
decode_colors[9] = (2*decode_colors[1] + decode_colors[5]) / 3;
|
||||
decode_colors[10] = (2*decode_colors[2] + decode_colors[6]) / 3;
|
||||
decode_colors[11] = 255;
|
||||
decode_colors[12] = (decode_colors[0] + 2*decode_colors[4]) / 3;
|
||||
decode_colors[13] = (decode_colors[1] + 2*decode_colors[5]) / 3;
|
||||
decode_colors[14] = (decode_colors[2] + 2*decode_colors[6]) / 3;
|
||||
decode_colors[15] = 255;
|
||||
} else
|
||||
{
|
||||
// 1 interpolated color, alpha
|
||||
decode_colors[8] = (decode_colors[0] + decode_colors[4]) / 2;
|
||||
decode_colors[9] = (decode_colors[1] + decode_colors[5]) / 2;
|
||||
decode_colors[10] = (decode_colors[2] + decode_colors[6]) / 2;
|
||||
decode_colors[11] = 255;
|
||||
decode_colors[12] = 0;
|
||||
decode_colors[13] = 0;
|
||||
decode_colors[14] = 0;
|
||||
decode_colors[15] = 0;
|
||||
}
|
||||
// decode the block
|
||||
for( i = 0; i < 16*4; i += 4 )
|
||||
{
|
||||
int idx = ((compressed[next_bit>>3] >> (next_bit & 7)) & 3) * 4;
|
||||
next_bit += 2;
|
||||
uncompressed[i+0] = decode_colors[idx+0];
|
||||
uncompressed[i+1] = decode_colors[idx+1];
|
||||
uncompressed[i+2] = decode_colors[idx+2];
|
||||
uncompressed[i+3] = decode_colors[idx+3];
|
||||
}
|
||||
// done
|
||||
}
|
||||
void stbi_decode_DXT23_alpha_block(
|
||||
unsigned char uncompressed[16*4],
|
||||
unsigned char compressed[8] )
|
||||
{
|
||||
int i, next_bit = 0;
|
||||
// each alpha value gets 4 bits
|
||||
for( i = 3; i < 16*4; i += 4 )
|
||||
{
|
||||
uncompressed[i] = stbi_convert_bit_range(
|
||||
(compressed[next_bit>>3] >> (next_bit&7)) & 15,
|
||||
4, 8 );
|
||||
next_bit += 4;
|
||||
}
|
||||
}
|
||||
void stbi_decode_DXT45_alpha_block(
|
||||
unsigned char uncompressed[16*4],
|
||||
unsigned char compressed[8] )
|
||||
{
|
||||
int i, next_bit = 8*2;
|
||||
unsigned char decode_alpha[8];
|
||||
// each alpha value gets 3 bits, and the 1st 2 bytes are the range
|
||||
decode_alpha[0] = compressed[0];
|
||||
decode_alpha[1] = compressed[1];
|
||||
if( decode_alpha[0] > decode_alpha[1] )
|
||||
{
|
||||
// 6 step intermediate
|
||||
decode_alpha[2] = (6*decode_alpha[0] + 1*decode_alpha[1]) / 7;
|
||||
decode_alpha[3] = (5*decode_alpha[0] + 2*decode_alpha[1]) / 7;
|
||||
decode_alpha[4] = (4*decode_alpha[0] + 3*decode_alpha[1]) / 7;
|
||||
decode_alpha[5] = (3*decode_alpha[0] + 4*decode_alpha[1]) / 7;
|
||||
decode_alpha[6] = (2*decode_alpha[0] + 5*decode_alpha[1]) / 7;
|
||||
decode_alpha[7] = (1*decode_alpha[0] + 6*decode_alpha[1]) / 7;
|
||||
} else
|
||||
{
|
||||
// 4 step intermediate, pluss full and none
|
||||
decode_alpha[2] = (4*decode_alpha[0] + 1*decode_alpha[1]) / 5;
|
||||
decode_alpha[3] = (3*decode_alpha[0] + 2*decode_alpha[1]) / 5;
|
||||
decode_alpha[4] = (2*decode_alpha[0] + 3*decode_alpha[1]) / 5;
|
||||
decode_alpha[5] = (1*decode_alpha[0] + 4*decode_alpha[1]) / 5;
|
||||
decode_alpha[6] = 0;
|
||||
decode_alpha[7] = 255;
|
||||
}
|
||||
for( i = 3; i < 16*4; i += 4 )
|
||||
{
|
||||
int idx = 0, bit;
|
||||
bit = (compressed[next_bit>>3] >> (next_bit&7)) & 1;
|
||||
idx += bit << 0;
|
||||
++next_bit;
|
||||
bit = (compressed[next_bit>>3] >> (next_bit&7)) & 1;
|
||||
idx += bit << 1;
|
||||
++next_bit;
|
||||
bit = (compressed[next_bit>>3] >> (next_bit&7)) & 1;
|
||||
idx += bit << 2;
|
||||
++next_bit;
|
||||
uncompressed[i] = decode_alpha[idx & 7];
|
||||
}
|
||||
// done
|
||||
}
|
||||
void stbi_decode_DXT_color_block(
|
||||
unsigned char uncompressed[16*4],
|
||||
unsigned char compressed[8] )
|
||||
{
|
||||
int next_bit = 4*8;
|
||||
int i, r, g, b;
|
||||
int c0, c1;
|
||||
unsigned char decode_colors[4*3];
|
||||
// find the 2 primary colors
|
||||
c0 = compressed[0] + (compressed[1] << 8);
|
||||
c1 = compressed[2] + (compressed[3] << 8);
|
||||
stbi_rgb_888_from_565( c0, &r, &g, &b );
|
||||
decode_colors[0] = r;
|
||||
decode_colors[1] = g;
|
||||
decode_colors[2] = b;
|
||||
stbi_rgb_888_from_565( c1, &r, &g, &b );
|
||||
decode_colors[3] = r;
|
||||
decode_colors[4] = g;
|
||||
decode_colors[5] = b;
|
||||
// Like DXT1, but no choicees:
|
||||
// no alpha, 2 interpolated colors
|
||||
decode_colors[6] = (2*decode_colors[0] + decode_colors[3]) / 3;
|
||||
decode_colors[7] = (2*decode_colors[1] + decode_colors[4]) / 3;
|
||||
decode_colors[8] = (2*decode_colors[2] + decode_colors[5]) / 3;
|
||||
decode_colors[9] = (decode_colors[0] + 2*decode_colors[3]) / 3;
|
||||
decode_colors[10] = (decode_colors[1] + 2*decode_colors[4]) / 3;
|
||||
decode_colors[11] = (decode_colors[2] + 2*decode_colors[5]) / 3;
|
||||
// decode the block
|
||||
for( i = 0; i < 16*4; i += 4 )
|
||||
{
|
||||
int idx = ((compressed[next_bit>>3] >> (next_bit & 7)) & 3) * 3;
|
||||
next_bit += 2;
|
||||
uncompressed[i+0] = decode_colors[idx+0];
|
||||
uncompressed[i+1] = decode_colors[idx+1];
|
||||
uncompressed[i+2] = decode_colors[idx+2];
|
||||
}
|
||||
// done
|
||||
}
|
||||
static stbi_uc *dds_load(stbi *s, int *x, int *y, int *comp, int req_comp)
|
||||
{
|
||||
// all variables go up front
|
||||
stbi_uc *dds_data = NULL;
|
||||
stbi_uc block[16*4];
|
||||
stbi_uc compressed[8];
|
||||
int flags, DXT_family;
|
||||
int has_alpha, has_mipmap;
|
||||
int is_compressed, cubemap_faces;
|
||||
int block_pitch, num_blocks;
|
||||
DDS_header header;
|
||||
int i, sz, cf;
|
||||
// load the header
|
||||
if( sizeof( DDS_header ) != 128 )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
getn( s, (stbi_uc*)(&header), 128 );
|
||||
// and do some checking
|
||||
if( header.dwMagic != (('D' << 0) | ('D' << 8) | ('S' << 16) | (' ' << 24)) ) return NULL;
|
||||
if( header.dwSize != 124 ) return NULL;
|
||||
flags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
|
||||
if( (header.dwFlags & flags) != flags ) return NULL;
|
||||
/* According to the MSDN spec, the dwFlags should contain
|
||||
DDSD_LINEARSIZE if it's compressed, or DDSD_PITCH if
|
||||
uncompressed. Some DDS writers do not conform to the
|
||||
spec, so I need to make my reader more tolerant */
|
||||
if( header.sPixelFormat.dwSize != 32 ) return NULL;
|
||||
flags = DDPF_FOURCC | DDPF_RGB;
|
||||
if( (header.sPixelFormat.dwFlags & flags) == 0 ) return NULL;
|
||||
if( (header.sCaps.dwCaps1 & DDSCAPS_TEXTURE) == 0 ) return NULL;
|
||||
// get the image data
|
||||
s->img_x = header.dwWidth;
|
||||
s->img_y = header.dwHeight;
|
||||
s->img_n = 4;
|
||||
is_compressed = (header.sPixelFormat.dwFlags & DDPF_FOURCC) / DDPF_FOURCC;
|
||||
has_alpha = (header.sPixelFormat.dwFlags & DDPF_ALPHAPIXELS) / DDPF_ALPHAPIXELS;
|
||||
has_mipmap = (header.sCaps.dwCaps1 & DDSCAPS_MIPMAP) && (header.dwMipMapCount > 1);
|
||||
cubemap_faces = (header.sCaps.dwCaps2 & DDSCAPS2_CUBEMAP) / DDSCAPS2_CUBEMAP;
|
||||
/* I need cubemaps to have square faces */
|
||||
cubemap_faces &= (s->img_x == s->img_y);
|
||||
cubemap_faces *= 5;
|
||||
cubemap_faces += 1;
|
||||
block_pitch = (s->img_x+3) >> 2;
|
||||
num_blocks = block_pitch * ((s->img_y+3) >> 2);
|
||||
/* let the user know what's going on */
|
||||
*x = s->img_x;
|
||||
*y = s->img_y;
|
||||
*comp = s->img_n;
|
||||
/* is this uncompressed? */
|
||||
if( is_compressed )
|
||||
{
|
||||
/* compressed */
|
||||
// note: header.sPixelFormat.dwFourCC is something like (('D'<<0)|('X'<<8)|('T'<<16)|('1'<<24))
|
||||
DXT_family = 1 + (header.sPixelFormat.dwFourCC >> 24) - '1';
|
||||
if( (DXT_family < 1) || (DXT_family > 5) ) return NULL;
|
||||
/* check the expected size...oops, nevermind...
|
||||
those non-compliant writers leave
|
||||
dwPitchOrLinearSize == 0 */
|
||||
// passed all the tests, get the RAM for decoding
|
||||
sz = (s->img_x)*(s->img_y)*4*cubemap_faces;
|
||||
dds_data = (unsigned char*)malloc( sz );
|
||||
/* do this once for each face */
|
||||
for( cf = 0; cf < cubemap_faces; ++ cf )
|
||||
{
|
||||
// now read and decode all the blocks
|
||||
for( i = 0; i < num_blocks; ++i )
|
||||
{
|
||||
// where are we?
|
||||
int bx, by, bw=4, bh=4;
|
||||
int ref_x = 4 * (i % block_pitch);
|
||||
int ref_y = 4 * (i / block_pitch);
|
||||
// get the next block's worth of compressed data, and decompress it
|
||||
if( DXT_family == 1 )
|
||||
{
|
||||
// DXT1
|
||||
getn( s, compressed, 8 );
|
||||
stbi_decode_DXT1_block( block, compressed );
|
||||
} else if( DXT_family < 4 )
|
||||
{
|
||||
// DXT2/3
|
||||
getn( s, compressed, 8 );
|
||||
stbi_decode_DXT23_alpha_block ( block, compressed );
|
||||
getn( s, compressed, 8 );
|
||||
stbi_decode_DXT_color_block ( block, compressed );
|
||||
} else
|
||||
{
|
||||
// DXT4/5
|
||||
getn( s, compressed, 8 );
|
||||
stbi_decode_DXT45_alpha_block ( block, compressed );
|
||||
getn( s, compressed, 8 );
|
||||
stbi_decode_DXT_color_block ( block, compressed );
|
||||
}
|
||||
// is this a partial block?
|
||||
if( ref_x + 4 > s->img_x )
|
||||
{
|
||||
bw = s->img_x - ref_x;
|
||||
}
|
||||
if( ref_y + 4 > s->img_y )
|
||||
{
|
||||
bh = s->img_y - ref_y;
|
||||
}
|
||||
// now drop our decompressed data into the buffer
|
||||
for( by = 0; by < bh; ++by )
|
||||
{
|
||||
int idx = 4*((ref_y+by+cf*s->img_x)*s->img_x + ref_x);
|
||||
for( bx = 0; bx < bw*4; ++bx )
|
||||
{
|
||||
|
||||
dds_data[idx+bx] = block[by*16+bx];
|
||||
}
|
||||
}
|
||||
}
|
||||
/* done reading and decoding the main image...
|
||||
skip MIPmaps if present */
|
||||
if( has_mipmap )
|
||||
{
|
||||
int block_size = 16;
|
||||
if( DXT_family == 1 )
|
||||
{
|
||||
block_size = 8;
|
||||
}
|
||||
for( i = 1; i < header.dwMipMapCount; ++i )
|
||||
{
|
||||
int mx = s->img_x >> (i + 2);
|
||||
int my = s->img_y >> (i + 2);
|
||||
if( mx < 1 )
|
||||
{
|
||||
mx = 1;
|
||||
}
|
||||
if( my < 1 )
|
||||
{
|
||||
my = 1;
|
||||
}
|
||||
skip( s, mx*my*block_size );
|
||||
}
|
||||
}
|
||||
}/* per cubemap face */
|
||||
} else
|
||||
{
|
||||
/* uncompressed */
|
||||
DXT_family = 0;
|
||||
s->img_n = 3;
|
||||
if( has_alpha )
|
||||
{
|
||||
s->img_n = 4;
|
||||
}
|
||||
*comp = s->img_n;
|
||||
sz = s->img_x*s->img_y*s->img_n*cubemap_faces;
|
||||
dds_data = (unsigned char*)malloc( sz );
|
||||
/* do this once for each face */
|
||||
for( cf = 0; cf < cubemap_faces; ++ cf )
|
||||
{
|
||||
/* read the main image for this face */
|
||||
getn( s, &dds_data[cf*s->img_x*s->img_y*s->img_n], s->img_x*s->img_y*s->img_n );
|
||||
/* done reading and decoding the main image...
|
||||
skip MIPmaps if present */
|
||||
if( has_mipmap )
|
||||
{
|
||||
for( i = 1; i < header.dwMipMapCount; ++i )
|
||||
{
|
||||
int mx = s->img_x >> i;
|
||||
int my = s->img_y >> i;
|
||||
if( mx < 1 )
|
||||
{
|
||||
mx = 1;
|
||||
}
|
||||
if( my < 1 )
|
||||
{
|
||||
my = 1;
|
||||
}
|
||||
skip( s, mx*my*s->img_n );
|
||||
}
|
||||
}
|
||||
}
|
||||
/* data was BGR, I need it RGB */
|
||||
for( i = 0; i < sz; i += s->img_n )
|
||||
{
|
||||
unsigned char temp = dds_data[i];
|
||||
dds_data[i] = dds_data[i+2];
|
||||
dds_data[i+2] = temp;
|
||||
}
|
||||
}
|
||||
/* finished decompressing into RGBA,
|
||||
adjust the y size if we have a cubemap
|
||||
note: sz is already up to date */
|
||||
s->img_y *= cubemap_faces;
|
||||
*y = s->img_y;
|
||||
// did the user want something else, or
|
||||
// see if all the alpha values are 255 (i.e. no transparency)
|
||||
has_alpha = 0;
|
||||
if( s->img_n == 4)
|
||||
{
|
||||
for( i = 3; (i < sz) && (has_alpha == 0); i += 4 )
|
||||
{
|
||||
has_alpha |= (dds_data[i] < 255);
|
||||
}
|
||||
}
|
||||
if( (req_comp <= 4) && (req_comp >= 1) )
|
||||
{
|
||||
// user has some requirements, meet them
|
||||
if( req_comp != s->img_n )
|
||||
{
|
||||
dds_data = convert_format( dds_data, s->img_n, req_comp, s->img_x, s->img_y );
|
||||
*comp = s->img_n;
|
||||
}
|
||||
} else
|
||||
{
|
||||
// user had no requirements, only drop to RGB is no alpha
|
||||
if( (has_alpha == 0) && (s->img_n == 4) )
|
||||
{
|
||||
dds_data = convert_format( dds_data, 4, 3, s->img_x, s->img_y );
|
||||
*comp = 3;
|
||||
}
|
||||
}
|
||||
// OK, done
|
||||
return dds_data;
|
||||
}
|
||||
|
||||
#ifndef STBI_NO_STDIO
|
||||
stbi_uc *stbi_dds_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp)
|
||||
{
|
||||
stbi s;
|
||||
start_file(&s,f);
|
||||
return dds_load(&s,x,y,comp,req_comp);
|
||||
}
|
||||
|
||||
stbi_uc *stbi_dds_load (char *filename, int *x, int *y, int *comp, int req_comp)
|
||||
{
|
||||
stbi_uc *data;
|
||||
FILE *f = fopen(filename, "rb");
|
||||
if (!f) return NULL;
|
||||
data = stbi_dds_load_from_file(f,x,y,comp,req_comp);
|
||||
fclose(f);
|
||||
return data;
|
||||
}
|
||||
#endif
|
||||
|
||||
stbi_uc *stbi_dds_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
|
||||
{
|
||||
stbi s;
|
||||
start_mem(&s,buffer, len);
|
||||
return dds_load(&s,x,y,comp,req_comp);
|
||||
}
|
||||
@@ -0,0 +1,379 @@
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#include <windows.h>
|
||||
#include <shellapi.h>
|
||||
#include <gl/gl.h>
|
||||
#include <gl/glext.h>
|
||||
|
||||
#include "SOIL.h"
|
||||
|
||||
LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
|
||||
void EnableOpenGL(HWND hwnd, HDC*, HGLRC*);
|
||||
void DisableOpenGL(HWND, HDC, HGLRC);
|
||||
|
||||
int WINAPI WinMain(HINSTANCE hInstance,
|
||||
HINSTANCE hPrevInstance,
|
||||
LPSTR lpCmdLine,
|
||||
int nCmdShow)
|
||||
{
|
||||
WNDCLASSEX wcex;
|
||||
HWND hwnd;
|
||||
HDC hDC;
|
||||
HGLRC hRC;
|
||||
MSG msg;
|
||||
BOOL bQuit = FALSE;
|
||||
float theta = 0.0f;
|
||||
|
||||
// register window class
|
||||
wcex.cbSize = sizeof(WNDCLASSEX);
|
||||
wcex.style = CS_OWNDC;
|
||||
wcex.lpfnWndProc = WindowProc;
|
||||
wcex.cbClsExtra = 0;
|
||||
wcex.cbWndExtra = 0;
|
||||
wcex.hInstance = hInstance;
|
||||
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
|
||||
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
|
||||
wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
|
||||
wcex.lpszMenuName = NULL;
|
||||
wcex.lpszClassName = "GLSample";
|
||||
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
|
||||
|
||||
|
||||
if (!RegisterClassEx(&wcex))
|
||||
return 0;
|
||||
|
||||
// create main window
|
||||
hwnd = CreateWindowEx(0,
|
||||
"GLSample",
|
||||
"SOIL Sample",
|
||||
WS_OVERLAPPEDWINDOW,
|
||||
CW_USEDEFAULT,
|
||||
CW_USEDEFAULT,
|
||||
512,
|
||||
512,
|
||||
NULL,
|
||||
NULL,
|
||||
hInstance,
|
||||
NULL);
|
||||
|
||||
ShowWindow(hwnd, nCmdShow);
|
||||
|
||||
// check my error handling
|
||||
/*
|
||||
SOIL_load_OGL_texture( "img_test.png", SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, 0 );
|
||||
std::cout << "'" << SOIL_last_result() << "'" << std::endl;
|
||||
*/
|
||||
|
||||
|
||||
// enable OpenGL for the window
|
||||
EnableOpenGL(hwnd, &hDC, &hRC);
|
||||
|
||||
glEnable( GL_BLEND );
|
||||
//glDisable( GL_BLEND );
|
||||
// straight alpha
|
||||
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
|
||||
// premultiplied alpha (remember to do the same in glColor!!)
|
||||
//glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );
|
||||
|
||||
// do I want alpha thresholding?
|
||||
glEnable( GL_ALPHA_TEST );
|
||||
glAlphaFunc( GL_GREATER, 0.5f );
|
||||
|
||||
// log what the use is asking us to load
|
||||
std::string load_me = lpCmdLine;
|
||||
if( load_me.length() > 2 )
|
||||
{
|
||||
//load_me = load_me.substr( 1, load_me.length() - 2 );
|
||||
load_me = load_me.substr( 0, load_me.length() - 0 );
|
||||
} else
|
||||
{
|
||||
//load_me = "img_test_uncompressed.dds";
|
||||
//load_me = "img_test_indexed.tga";
|
||||
//load_me = "img_test.dds";
|
||||
load_me = "img_test.png";
|
||||
//load_me = "odd_size.jpg";
|
||||
//load_me = "img_cheryl.jpg";
|
||||
//load_me = "oak_odd.png";
|
||||
//load_me = "field_128_cube.dds";
|
||||
//load_me = "field_128_cube_nomip.dds";
|
||||
//load_me = "field_128_cube_uc.dds";
|
||||
//load_me = "field_128_cube_uc_nomip.dds";
|
||||
//load_me = "Goblin.dds";
|
||||
//load_me = "parquet.dds";
|
||||
//load_me = "stpeters_probe.hdr";
|
||||
//load_me = "VeraMoBI_sdf.png";
|
||||
|
||||
// for testing the texture rectangle code
|
||||
//load_me = "test_rect.png";
|
||||
}
|
||||
std::cout << "'" << load_me << "'" << std::endl;
|
||||
|
||||
// 1st try to load it as a single-image-cubemap
|
||||
// (note, need DDS ordered faces: "EWUDNS")
|
||||
GLuint tex_ID;
|
||||
int time_me;
|
||||
|
||||
std::cout << "Attempting to load as a cubemap" << std::endl;
|
||||
time_me = clock();
|
||||
tex_ID = SOIL_load_OGL_single_cubemap(
|
||||
load_me.c_str(),
|
||||
SOIL_DDS_CUBEMAP_FACE_ORDER,
|
||||
SOIL_LOAD_AUTO,
|
||||
SOIL_CREATE_NEW_ID,
|
||||
SOIL_FLAG_POWER_OF_TWO
|
||||
| SOIL_FLAG_MIPMAPS
|
||||
//| SOIL_FLAG_COMPRESS_TO_DXT
|
||||
//| SOIL_FLAG_TEXTURE_REPEATS
|
||||
//| SOIL_FLAG_INVERT_Y
|
||||
| SOIL_FLAG_DDS_LOAD_DIRECT
|
||||
);
|
||||
time_me = clock() - time_me;
|
||||
std::cout << "the load time was " << 0.001f * time_me << " seconds (warning: low resolution timer)" << std::endl;
|
||||
if( tex_ID > 0 )
|
||||
{
|
||||
glEnable( GL_TEXTURE_CUBE_MAP );
|
||||
glEnable( GL_TEXTURE_GEN_S );
|
||||
glEnable( GL_TEXTURE_GEN_T );
|
||||
glEnable( GL_TEXTURE_GEN_R );
|
||||
glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );
|
||||
glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );
|
||||
glTexGeni( GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP );
|
||||
glBindTexture( GL_TEXTURE_CUBE_MAP, tex_ID );
|
||||
// report
|
||||
std::cout << "the loaded single cube map ID was " << tex_ID << std::endl;
|
||||
//std::cout << "the load time was " << 0.001f * time_me << " seconds (warning: low resolution timer)" << std::endl;
|
||||
} else
|
||||
{
|
||||
std::cout << "Attempting to load as a HDR texture" << std::endl;
|
||||
time_me = clock();
|
||||
tex_ID = SOIL_load_OGL_HDR_texture(
|
||||
load_me.c_str(),
|
||||
//SOIL_HDR_RGBE,
|
||||
//SOIL_HDR_RGBdivA,
|
||||
SOIL_HDR_RGBdivA2,
|
||||
0,
|
||||
SOIL_CREATE_NEW_ID,
|
||||
SOIL_FLAG_POWER_OF_TWO
|
||||
| SOIL_FLAG_MIPMAPS
|
||||
//| SOIL_FLAG_COMPRESS_TO_DXT
|
||||
);
|
||||
time_me = clock() - time_me;
|
||||
std::cout << "the load time was " << 0.001f * time_me << " seconds (warning: low resolution timer)" << std::endl;
|
||||
|
||||
// did I fail?
|
||||
if( tex_ID < 1 )
|
||||
{
|
||||
// loading of the single-image-cubemap failed, try it as a simple texture
|
||||
std::cout << "Attempting to load as a simple 2D texture" << std::endl;
|
||||
// load the texture, if specified
|
||||
time_me = clock();
|
||||
tex_ID = SOIL_load_OGL_texture(
|
||||
load_me.c_str(),
|
||||
SOIL_LOAD_AUTO,
|
||||
SOIL_CREATE_NEW_ID,
|
||||
SOIL_FLAG_POWER_OF_TWO
|
||||
| SOIL_FLAG_MIPMAPS
|
||||
//| SOIL_FLAG_MULTIPLY_ALPHA
|
||||
//| SOIL_FLAG_COMPRESS_TO_DXT
|
||||
| SOIL_FLAG_DDS_LOAD_DIRECT
|
||||
//| SOIL_FLAG_NTSC_SAFE_RGB
|
||||
//| SOIL_FLAG_CoCg_Y
|
||||
//| SOIL_FLAG_TEXTURE_RECTANGLE
|
||||
);
|
||||
time_me = clock() - time_me;
|
||||
std::cout << "the load time was " << 0.001f * time_me << " seconds (warning: low resolution timer)" << std::endl;
|
||||
}
|
||||
|
||||
if( tex_ID > 0 )
|
||||
{
|
||||
// enable texturing
|
||||
glEnable( GL_TEXTURE_2D );
|
||||
//glEnable( 0x84F5 );// enables texture rectangle
|
||||
// bind an OpenGL texture ID
|
||||
glBindTexture( GL_TEXTURE_2D, tex_ID );
|
||||
// report
|
||||
std::cout << "the loaded texture ID was " << tex_ID << std::endl;
|
||||
//std::cout << "the load time was " << 0.001f * time_me << " seconds (warning: low resolution timer)" << std::endl;
|
||||
} else
|
||||
{
|
||||
// loading of the texture failed...why?
|
||||
glDisable( GL_TEXTURE_2D );
|
||||
std::cout << "Texture loading failed: '" << SOIL_last_result() << "'" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// program main loop
|
||||
const float ref_mag = 0.1f;
|
||||
while (!bQuit)
|
||||
{
|
||||
// check for messages
|
||||
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
|
||||
{
|
||||
// handle or dispatch messages
|
||||
if (msg.message == WM_QUIT)
|
||||
{
|
||||
bQuit = TRUE;
|
||||
}
|
||||
else
|
||||
{
|
||||
TranslateMessage(&msg);
|
||||
DispatchMessage(&msg);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// OpenGL animation code goes here
|
||||
theta = clock() * 0.1;
|
||||
|
||||
float tex_u_max = 1.0f;//0.2f;
|
||||
float tex_v_max = 1.0f;//0.2f;
|
||||
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glPushMatrix();
|
||||
glScalef( 0.8f, 0.8f, 0.8f );
|
||||
//glRotatef(-0.314159f*theta, 0.0f, 0.0f, 1.0f);
|
||||
glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
|
||||
glNormal3f( 0.0f, 0.0f, 1.0f );
|
||||
glBegin(GL_QUADS);
|
||||
glNormal3f( -ref_mag, -ref_mag, 1.0f );
|
||||
glTexCoord2f( 0.0f, tex_v_max );
|
||||
glVertex3f( -1.0f, -1.0f, -0.1f );
|
||||
|
||||
glNormal3f( ref_mag, -ref_mag, 1.0f );
|
||||
glTexCoord2f( tex_u_max, tex_v_max );
|
||||
glVertex3f( 1.0f, -1.0f, -0.1f );
|
||||
|
||||
glNormal3f( ref_mag, ref_mag, 1.0f );
|
||||
glTexCoord2f( tex_u_max, 0.0f );
|
||||
glVertex3f( 1.0f, 1.0f, -0.1f );
|
||||
|
||||
glNormal3f( -ref_mag, ref_mag, 1.0f );
|
||||
glTexCoord2f( 0.0f, 0.0f );
|
||||
glVertex3f( -1.0f, 1.0f, -0.1f );
|
||||
glEnd();
|
||||
glPopMatrix();
|
||||
|
||||
tex_u_max = 1.0f;
|
||||
tex_v_max = 1.0f;
|
||||
glPushMatrix();
|
||||
glScalef( 0.8f, 0.8f, 0.8f );
|
||||
glRotatef(theta, 0.0f, 0.0f, 1.0f);
|
||||
glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
|
||||
glNormal3f( 0.0f, 0.0f, 1.0f );
|
||||
glBegin(GL_QUADS);
|
||||
glTexCoord2f( 0.0f, tex_v_max ); glVertex3f( 0.0f, 0.0f, 0.1f );
|
||||
glTexCoord2f( tex_u_max, tex_v_max ); glVertex3f( 1.0f, 0.0f, 0.1f );
|
||||
glTexCoord2f( tex_u_max, 0.0f ); glVertex3f( 1.0f, 1.0f, 0.1f );
|
||||
glTexCoord2f( 0.0f, 0.0f ); glVertex3f( 0.0f, 1.0f, 0.1f );
|
||||
glEnd();
|
||||
glPopMatrix();
|
||||
|
||||
{
|
||||
/* check for errors */
|
||||
GLenum err_code = glGetError();
|
||||
while( GL_NO_ERROR != err_code )
|
||||
{
|
||||
printf( "OpenGL Error @ %s: %i", "drawing loop", err_code );
|
||||
err_code = glGetError();
|
||||
}
|
||||
}
|
||||
|
||||
SwapBuffers(hDC);
|
||||
|
||||
Sleep (1);
|
||||
}
|
||||
}
|
||||
|
||||
// and show off the screenshot capability
|
||||
/*
|
||||
load_me += "-screenshot.tga";
|
||||
SOIL_save_screenshot( load_me.c_str(), SOIL_SAVE_TYPE_TGA, 0, 0, 512, 512 );
|
||||
//*/
|
||||
//*
|
||||
load_me += "-screenshot.bmp";
|
||||
SOIL_save_screenshot( load_me.c_str(), SOIL_SAVE_TYPE_BMP, 0, 0, 512, 512 );
|
||||
//*/
|
||||
/*
|
||||
load_me += "-screenshot.dds";
|
||||
SOIL_save_screenshot( load_me.c_str(), SOIL_SAVE_TYPE_DDS, 0, 0, 512, 512 );
|
||||
//*/
|
||||
|
||||
// shutdown OpenGL
|
||||
DisableOpenGL(hwnd, hDC, hRC);
|
||||
|
||||
// destroy the window explicitly
|
||||
DestroyWindow(hwnd);
|
||||
|
||||
return msg.wParam;
|
||||
}
|
||||
|
||||
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
switch (uMsg)
|
||||
{
|
||||
case WM_CLOSE:
|
||||
PostQuitMessage(0);
|
||||
break;
|
||||
|
||||
case WM_DESTROY:
|
||||
return 0;
|
||||
|
||||
case WM_KEYDOWN:
|
||||
{
|
||||
switch (wParam)
|
||||
{
|
||||
case VK_ESCAPE:
|
||||
PostQuitMessage(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
return DefWindowProc(hwnd, uMsg, wParam, lParam);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void EnableOpenGL(HWND hwnd, HDC* hDC, HGLRC* hRC)
|
||||
{
|
||||
PIXELFORMATDESCRIPTOR pfd;
|
||||
|
||||
int iFormat;
|
||||
|
||||
/* get the device context (DC) */
|
||||
*hDC = GetDC(hwnd);
|
||||
|
||||
/* set the pixel format for the DC */
|
||||
ZeroMemory(&pfd, sizeof(pfd));
|
||||
|
||||
pfd.nSize = sizeof(pfd);
|
||||
pfd.nVersion = 1;
|
||||
pfd.dwFlags = PFD_DRAW_TO_WINDOW |
|
||||
PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
|
||||
pfd.iPixelType = PFD_TYPE_RGBA;
|
||||
pfd.cColorBits = 24;
|
||||
pfd.cDepthBits = 16;
|
||||
pfd.iLayerType = PFD_MAIN_PLANE;
|
||||
|
||||
iFormat = ChoosePixelFormat(*hDC, &pfd);
|
||||
|
||||
SetPixelFormat(*hDC, iFormat, &pfd);
|
||||
|
||||
/* create and enable the render context (RC) */
|
||||
*hRC = wglCreateContext(*hDC);
|
||||
|
||||
wglMakeCurrent(*hDC, *hRC);
|
||||
}
|
||||
|
||||
void DisableOpenGL (HWND hwnd, HDC hDC, HGLRC hRC)
|
||||
{
|
||||
wglMakeCurrent(NULL, NULL);
|
||||
wglDeleteContext(hRC);
|
||||
ReleaseDC(hwnd, hDC);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user