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

KTX Texture mipmaps now loading correctly.

--HG--
extra : source : 35ac38101008024955ffa4d4c6e66f8e5be026a2
This commit is contained in:
2014-01-04 03:36:56 -08:00
parent a6fdde5978
commit 896311f0ac
6 changed files with 80 additions and 34 deletions

View File

@@ -42,7 +42,7 @@ void KRMeshStreamer::startStreamer()
#elif TARGET_OS_MAC
NSOpenGLPixelFormatAttribute pixelFormatAttributes[] =
{
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersionLegacy,
// NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersionLegacy,
0
};
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 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;
data->copy(&blockLength, blockStart, 4);
blockStart += 4;
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)
{
memcpy(_KTXFileIdentifier, m_header.identifier, 12);
memcpy(m_header.identifier, _KTXFileIdentifier, 12);
m_header.endianness = 0x04030201;
m_header.glType = 0;
m_header.glTypeSize = 1;

View File

@@ -47,7 +47,7 @@ void KRTextureStreamer::startStreamer()
NSOpenGLPixelFormatAttribute pixelFormatAttributes[] =
{
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersionLegacy,
// NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersionLegacy,
0
};
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;
}
//#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);
err = glGetError();
if (err != GL_NO_ERROR) {
@@ -168,10 +168,24 @@ KRTexture *KRTextureTGA::compress()
}
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_HEIGHT, &height));
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;

View File

@@ -25,11 +25,11 @@
// or implied, of Kearwood Gilbert.
//
// #extension GL_EXT_shadow_samplers : require
//#extension GL_EXT_shadow_samplers : require
#if ENABLE_RIM_COLOR == 1
uniform lowp vec3 rim_color;
uniform mediump float rim_power;
uniform lowp vec3 rim_color;
uniform mediump float rim_power;
#endif
#if FOG_TYPE > 0
@@ -116,18 +116,27 @@ uniform mediump float rim_power;
uniform sampler2D reflectionTexture;
#endif
#if ENABLE_RIM_COLOR == 1
#define NEED_EYEVEC
#endif
#if HAS_REFLECTION_CUBE_MAP == 1
uniform lowp vec3 material_reflection;
uniform samplerCube reflectionCubeTexture;
#if HAS_NORMAL_MAP == 1
varying highp mat3 tangent_to_world_matrix;
varying mediump vec3 eyeVec;
#define NEED_EYEVEC
uniform highp mat4 model_matrix;
#else
varying mediump vec3 reflectionVec;
#endif
#endif
#ifdef NEED_EYEVEC
varying mediump vec3 eyeVec;
#endif
#if SHADOW_QUALITY >= 1
#ifdef GL_EXT_shadow_samplers
@@ -398,4 +407,14 @@ void main()
#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.
//
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;
uniform highp mat4 mvp_matrix; // mvp_matrix is the result of multiplying the model, view, and projection matrices
#if BONE_COUNT > 0
attribute highp vec4 bone_weights;
attribute mediump vec4 bone_indexes;
attribute highp vec4 bone_indexes;
uniform highp mat4 bone_transforms[BONE_COUNT];
#else
#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;
#endif
#if ENABLE_RIM_COLOR == 1
#define NEED_EYEVEC
#endif
#if HAS_REFLECTION_CUBE_MAP == 1
#if HAS_NORMAL_MAP == 1
#define NEED_EYEVEC
uniform highp mat4 model_inverse_transpose_matrix;
varying mediump vec3 eyeVec;
varying highp mat3 tangent_to_world_matrix;
#else
uniform highp mat4 model_matrix;
@@ -146,6 +153,10 @@ uniform highp mat4 mvp_matrix; // mvp_matrix is the result of multiplying t
#endif
#endif
#ifdef NEED_EYEVEC
varying mediump vec3 eyeVec;
#endif
#if HAS_DIFFUSE_MAP_SCALE == 1
uniform highp vec2 diffuseTexture_Scale;
#endif
@@ -168,30 +179,28 @@ void main()
mediump vec4 scaled_bone_indexes = bone_indexes;
mediump vec4 scaled_bone_weights = bone_weights;
// scaled_bone_indexes = 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(
((bone_transforms[ int(scaled_bone_indexes.x) ] * vec4(vertex_normal, 1.0)).xyz * 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.z) ] * vec4(vertex_normal, 1.0)).xyz * scaled_bone_weights.z) +
((bone_transforms[ int(scaled_bone_indexes.w) ] * vec4(vertex_normal, 1.0)).xyz * scaled_bone_weights.w));
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
//scaled_bone_indexes = vec4(0.0, 0.0, 0.0, 0.0);
//scaled_bone_weights = vec4(1.0, 0.0, 0.0, 0.0);
highp mat4 skin_matrix =
bone_transforms[ int(scaled_bone_indexes.x) ] * scaled_bone_weights.x +
bone_transforms[ int(scaled_bone_indexes.y) ] * scaled_bone_weights.y +
bone_transforms[ int(scaled_bone_indexes.z) ] * scaled_bone_weights.z +
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
#endif
// Transform position
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)
// Pass UV co-ordinates
@@ -244,7 +253,7 @@ void main()
#if HAS_REFLECTION_CUBE_MAP == 1
#if HAS_NORMAL_MAP == 1
eyeVec = normalize(camera_position_model_space - vertex_position_skinned);
#else
// Calculate reflection vector as I - 2.0 * dot(N, I) * N
mediump vec3 eyeVec = normalize(camera_position_model_space - vertex_position_skinned);
@@ -253,6 +262,9 @@ void main()
#endif
#endif
#ifdef NEED_EYEVEC
eyeVec = normalize(camera_position_model_space - vertex_position_skinned);
#endif
#if HAS_LIGHT_MAP == 1
// Pass shadow UV co-ordinates