KTX Texture compression fixed for opaque textures without an alpha channel.

KTX Texture mipmaps now loading correctly.

--HG--
branch : nfb
This commit is contained in:
2014-01-04 03:36:56 -08:00
parent 5de7ef1d26
commit f6da62221f
6 changed files with 80 additions and 34 deletions

View File

@@ -42,7 +42,7 @@ void KRMeshStreamer::startStreamer()
#elif TARGET_OS_MAC #elif TARGET_OS_MAC
NSOpenGLPixelFormatAttribute pixelFormatAttributes[] = NSOpenGLPixelFormatAttribute pixelFormatAttributes[] =
{ {
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersionLegacy, // NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersionLegacy,
0 0
}; };
NSOpenGLPixelFormat *pixelFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttributes] autorelease]; NSOpenGLPixelFormat *pixelFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttributes] autorelease];

View File

@@ -59,9 +59,10 @@ KRTextureKTX::KRTextureKTX(KRContext &context, KRDataBlock *data, std::string na
uint32_t blockStart = sizeof(KTXHeader) + m_header.bytesOfKeyValueData; uint32_t blockStart = sizeof(KTXHeader) + m_header.bytesOfKeyValueData;
uint32_t width = m_header.pixelWidth, height = m_header.pixelHeight; uint32_t width = m_header.pixelWidth, height = m_header.pixelHeight;
for(int mipmap_level=0; mipmap_level < KRMIN(m_header.numberOfMipmapLevels, 1); mipmap_level++) { for(int mipmap_level=0; mipmap_level < KRMAX(m_header.numberOfMipmapLevels, 1); mipmap_level++) {
uint32_t blockLength; uint32_t blockLength;
data->copy(&blockLength, blockStart, 4); data->copy(&blockLength, blockStart, 4);
blockStart += 4;
m_blocks.push_back(m_pData->getSubBlock(blockStart, blockLength)); m_blocks.push_back(m_pData->getSubBlock(blockStart, blockLength));
@@ -84,7 +85,7 @@ KRTextureKTX::KRTextureKTX(KRContext &context, KRDataBlock *data, std::string na
KRTextureKTX::KRTextureKTX(KRContext &context, std::string name, GLenum internal_format, GLenum base_internal_format, int width, int height, const std::list<KRDataBlock *> &blocks) : KRTexture2D(context, new KRDataBlock(), name) KRTextureKTX::KRTextureKTX(KRContext &context, std::string name, GLenum internal_format, GLenum base_internal_format, int width, int height, const std::list<KRDataBlock *> &blocks) : KRTexture2D(context, new KRDataBlock(), name)
{ {
memcpy(_KTXFileIdentifier, m_header.identifier, 12); memcpy(m_header.identifier, _KTXFileIdentifier, 12);
m_header.endianness = 0x04030201; m_header.endianness = 0x04030201;
m_header.glType = 0; m_header.glType = 0;
m_header.glTypeSize = 1; m_header.glTypeSize = 1;

View File

@@ -47,7 +47,7 @@ void KRTextureStreamer::startStreamer()
NSOpenGLPixelFormatAttribute pixelFormatAttributes[] = NSOpenGLPixelFormatAttribute pixelFormatAttributes[] =
{ {
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersionLegacy, // NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersionLegacy,
0 0
}; };
NSOpenGLPixelFormat *pixelFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttributes] autorelease]; NSOpenGLPixelFormat *pixelFormat = [[[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttributes] autorelease];

View File

@@ -114,7 +114,7 @@ bool KRTextureTGA::uploadTexture(GLenum target, int lod_max_dim, int &current_lo
pSource += 3; pSource += 3;
} }
//#endif //#endif
glTexImage2D(target, 0, internal_format, pHeader->width, pHeader->height, 0, GL_BGR, GL_UNSIGNED_BYTE, (GLvoid *)converted_image); glTexImage2D(target, 0, internal_format, pHeader->width, pHeader->height, 0, GL_BGRA, GL_UNSIGNED_BYTE, (GLvoid *)converted_image);
free(converted_image); free(converted_image);
err = glGetError(); err = glGetError();
if (err != GL_NO_ERROR) { if (err != GL_NO_ERROR) {
@@ -168,11 +168,25 @@ KRTexture *KRTextureTGA::compress()
} }
GLDEBUG(glGenerateMipmap(GL_TEXTURE_2D)); GLDEBUG(glGenerateMipmap(GL_TEXTURE_2D));
GLint width = 0, height = 0, internal_format, base_internal_format = GL_BGRA; GLint width = 0, height = 0, internal_format, base_internal_format;
GLDEBUG(glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width)); GLDEBUG(glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width));
GLDEBUG(glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height)); GLDEBUG(glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height));
GLDEBUG(glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &internal_format)); GLDEBUG(glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &internal_format));
switch(internal_format)
{
case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
base_internal_format = GL_BGRA;
break;
case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
base_internal_format = GL_BGRA;
break;
default:
assert(false); // Not yet supported
break;
}
GLuint lod_level = 0; GLuint lod_level = 0;
GLint compressed_size = 0; GLint compressed_size = 0;

View File

@@ -25,11 +25,11 @@
// or implied, of Kearwood Gilbert. // or implied, of Kearwood Gilbert.
// //
// #extension GL_EXT_shadow_samplers : require //#extension GL_EXT_shadow_samplers : require
#if ENABLE_RIM_COLOR == 1 #if ENABLE_RIM_COLOR == 1
uniform lowp vec3 rim_color; uniform lowp vec3 rim_color;
uniform mediump float rim_power; uniform mediump float rim_power;
#endif #endif
#if FOG_TYPE > 0 #if FOG_TYPE > 0
@@ -116,18 +116,27 @@ uniform mediump float rim_power;
uniform sampler2D reflectionTexture; uniform sampler2D reflectionTexture;
#endif #endif
#if ENABLE_RIM_COLOR == 1
#define NEED_EYEVEC
#endif
#if HAS_REFLECTION_CUBE_MAP == 1 #if HAS_REFLECTION_CUBE_MAP == 1
uniform lowp vec3 material_reflection; uniform lowp vec3 material_reflection;
uniform samplerCube reflectionCubeTexture; uniform samplerCube reflectionCubeTexture;
#if HAS_NORMAL_MAP == 1 #if HAS_NORMAL_MAP == 1
varying highp mat3 tangent_to_world_matrix; varying highp mat3 tangent_to_world_matrix;
varying mediump vec3 eyeVec; #define NEED_EYEVEC
uniform highp mat4 model_matrix; uniform highp mat4 model_matrix;
#else #else
varying mediump vec3 reflectionVec; varying mediump vec3 reflectionVec;
#endif #endif
#endif #endif
#ifdef NEED_EYEVEC
varying mediump vec3 eyeVec;
#endif
#if SHADOW_QUALITY >= 1 #if SHADOW_QUALITY >= 1
#ifdef GL_EXT_shadow_samplers #ifdef GL_EXT_shadow_samplers
@@ -398,4 +407,14 @@ void main()
#endif #endif
#if ENABLE_RIM_COLOR == 1
lowp float rim = 1.0 - clamp(dot(normalize(eyeVec), normal), 0.0, 1.0);
gl_FragColor += vec4(rim_color, 1.0) * pow(rim, rim_power);
#endif
#if BONE_COUNT > 0
gl_FragColor.b = 1.0;
#endif
} }

View File

@@ -29,13 +29,17 @@
// or implied, of Kearwood Gilbert. // or implied, of Kearwood Gilbert.
// //
attribute highp vec3 vertex_position, vertex_normal, vertex_tangent;
attribute highp vec3 vertex_position, vertex_normal;
#if HAS_NORMAL_MAP == 1
attribute highp vec3 vertex_tangent;
#endif
attribute mediump vec2 vertex_uv; attribute mediump vec2 vertex_uv;
uniform highp mat4 mvp_matrix; // mvp_matrix is the result of multiplying the model, view, and projection matrices uniform highp mat4 mvp_matrix; // mvp_matrix is the result of multiplying the model, view, and projection matrices
#if BONE_COUNT > 0 #if BONE_COUNT > 0
attribute highp vec4 bone_weights; attribute highp vec4 bone_weights;
attribute mediump vec4 bone_indexes; attribute highp vec4 bone_indexes;
uniform highp mat4 bone_transforms[BONE_COUNT]; uniform highp mat4 bone_transforms[BONE_COUNT];
#else #else
#define vertex_position_skinned vertex_position #define vertex_position_skinned vertex_position
@@ -134,11 +138,14 @@ uniform highp mat4 mvp_matrix; // mvp_matrix is the result of multiplying t
varying mediump float specularFactor; varying mediump float specularFactor;
#endif #endif
#if ENABLE_RIM_COLOR == 1
#define NEED_EYEVEC
#endif
#if HAS_REFLECTION_CUBE_MAP == 1 #if HAS_REFLECTION_CUBE_MAP == 1
#if HAS_NORMAL_MAP == 1 #if HAS_NORMAL_MAP == 1
#define NEED_EYEVEC
uniform highp mat4 model_inverse_transpose_matrix; uniform highp mat4 model_inverse_transpose_matrix;
varying mediump vec3 eyeVec;
varying highp mat3 tangent_to_world_matrix; varying highp mat3 tangent_to_world_matrix;
#else #else
uniform highp mat4 model_matrix; uniform highp mat4 model_matrix;
@@ -146,6 +153,10 @@ uniform highp mat4 mvp_matrix; // mvp_matrix is the result of multiplying t
#endif #endif
#endif #endif
#ifdef NEED_EYEVEC
varying mediump vec3 eyeVec;
#endif
#if HAS_DIFFUSE_MAP_SCALE == 1 #if HAS_DIFFUSE_MAP_SCALE == 1
uniform highp vec2 diffuseTexture_Scale; uniform highp vec2 diffuseTexture_Scale;
#endif #endif
@@ -168,31 +179,29 @@ void main()
mediump vec4 scaled_bone_indexes = bone_indexes; mediump vec4 scaled_bone_indexes = bone_indexes;
mediump vec4 scaled_bone_weights = bone_weights; mediump vec4 scaled_bone_weights = bone_weights;
// scaled_bone_indexes = vec4(1.0, 0.0, 0.0, 0.0); //scaled_bone_indexes = vec4(0.0, 0.0, 0.0, 0.0);
// scaled_bone_weights = vec4(1.0, 0.0, 0.0, 0.0); //scaled_bone_weights = vec4(1.0, 0.0, 0.0, 0.0);
highp vec3 vertex_position_skinned =
((bone_transforms[ int(scaled_bone_indexes.x) ] * vec4(vertex_position, 1.0)).xyz * scaled_bone_weights.x) +
((bone_transforms[ int(scaled_bone_indexes.y) ] * vec4(vertex_position, 1.0)).xyz * scaled_bone_weights.y) +
((bone_transforms[ int(scaled_bone_indexes.z) ] * vec4(vertex_position, 1.0)).xyz * scaled_bone_weights.z) +
((bone_transforms[ int(scaled_bone_indexes.w) ] * vec4(vertex_position, 1.0)).xyz * scaled_bone_weights.w);
highp vec3 vertex_normal_skinned = normalize( highp mat4 skin_matrix =
((bone_transforms[ int(scaled_bone_indexes.x) ] * vec4(vertex_normal, 1.0)).xyz * scaled_bone_weights.x) + bone_transforms[ int(scaled_bone_indexes.x) ] * scaled_bone_weights.x +
((bone_transforms[ int(scaled_bone_indexes.y) ] * vec4(vertex_normal, 1.0)).xyz * scaled_bone_weights.y) + bone_transforms[ int(scaled_bone_indexes.y) ] * scaled_bone_weights.y +
((bone_transforms[ int(scaled_bone_indexes.z) ] * vec4(vertex_normal, 1.0)).xyz * scaled_bone_weights.z) + bone_transforms[ int(scaled_bone_indexes.z) ] * scaled_bone_weights.z +
((bone_transforms[ int(scaled_bone_indexes.w) ] * vec4(vertex_normal, 1.0)).xyz * scaled_bone_weights.w)); bone_transforms[ int(scaled_bone_indexes.w) ] * scaled_bone_weights.w;
//skin_matrix = bone_transforms[0];
highp vec3 vertex_position_skinned = (skin_matrix * vec4(vertex_position, 1)).xyz;
highp vec3 vertex_normal_skinned = normalize(mat3(skin_matrix) * vertex_normal);
#if HAS_NORMAL_MAP == 1
highp vec3 vertex_tangent_skinned = normalize(mat3(skin_matrix) * vertex_tangent);
#endif
highp vec3 vertex_tangent_skinned = normalize(
((bone_transforms[ int(scaled_bone_indexes.x) ] * vec4(vertex_tangent, 1.0)).xyz * scaled_bone_weights.x) +
((bone_transforms[ int(scaled_bone_indexes.y) ] * vec4(vertex_tangent, 1.0)).xyz * scaled_bone_weights.y) +
((bone_transforms[ int(scaled_bone_indexes.z) ] * vec4(vertex_tangent, 1.0)).xyz * scaled_bone_weights.z) +
((bone_transforms[ int(scaled_bone_indexes.w) ] * vec4(vertex_tangent, 1.0)).xyz * scaled_bone_weights.w));
#endif #endif
// Transform position // Transform position
gl_Position = mvp_matrix * vec4(vertex_position_skinned,1.0); gl_Position = mvp_matrix * vec4(vertex_position_skinned,1.0);
#if HAS_DIFFUSE_MAP == 1 || (HAS_NORMAL_MAP == 1 && ENABLE_PER_PIXEL == 1) || (HAS_SPEC_MAP == 1 && ENABLE_PER_PIXEL == 1) || (HAS_REFLECTION_MAP == 1 && ENABLE_PER_PIXEL == 1) #if HAS_DIFFUSE_MAP == 1 || (HAS_NORMAL_MAP == 1 && ENABLE_PER_PIXEL == 1) || (HAS_SPEC_MAP == 1 && ENABLE_PER_PIXEL == 1) || (HAS_REFLECTION_MAP == 1 && ENABLE_PER_PIXEL == 1)
// Pass UV co-ordinates // Pass UV co-ordinates
texCoord = vertex_uv.st; texCoord = vertex_uv.st;
@@ -244,7 +253,7 @@ void main()
#if HAS_REFLECTION_CUBE_MAP == 1 #if HAS_REFLECTION_CUBE_MAP == 1
#if HAS_NORMAL_MAP == 1 #if HAS_NORMAL_MAP == 1
eyeVec = normalize(camera_position_model_space - vertex_position_skinned);
#else #else
// Calculate reflection vector as I - 2.0 * dot(N, I) * N // Calculate reflection vector as I - 2.0 * dot(N, I) * N
mediump vec3 eyeVec = normalize(camera_position_model_space - vertex_position_skinned); mediump vec3 eyeVec = normalize(camera_position_model_space - vertex_position_skinned);
@@ -253,6 +262,9 @@ void main()
#endif #endif
#endif #endif
#ifdef NEED_EYEVEC
eyeVec = normalize(camera_position_model_space - vertex_position_skinned);
#endif
#if HAS_LIGHT_MAP == 1 #if HAS_LIGHT_MAP == 1
// Pass shadow UV co-ordinates // Pass shadow UV co-ordinates