Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
elect86 committed May 1, 2017
1 parent af2855a commit 0fee411
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/build/
/.idea/
.gradle/
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ group = 'com.github.kotlin-graphics'

buildscript {

ext.kotlinVersion = '1.1.2'
ext.kotlinVersion = '1.1.2-2'

repositories {
mavenCentral()
Expand All @@ -25,8 +25,8 @@ dependencies {

compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"

compile 'com.github.kotlin-graphics:glm:c86a65913cdac916f3a202367b7a27b3495fc688'
compile 'com.github.kotlin-graphics:uno-sdk:f3d334f25207a1a513b10e1989946c74d364027f'
compile 'com.github.kotlin-graphics:glm:4711d0f44930262d53f4c3b9a799edb4f6a4401d'
compile 'com.github.kotlin-graphics:uno-sdk:8d450fb36790af4256b71e5ef6a1058459a18d08'

// compile('com.github.elect86:glm:c5c7ab9eb0') { changing = true }
// compile('com.github.elect86:uno-sdk:5d9be5c99411c4e6fe8bddb633f98330990813a4') { changing = true }
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/gli/gl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import gli.gl.ExternalFormat.NONE as NONE_
/** Translation class to convert GLI enums into OpenGL values */
object gl {

lateinit var profile: Profile
var profile = Profile.GL33

fun translate(target: gli.Target) = tableT[target.i]

Expand Down Expand Up @@ -410,7 +410,7 @@ object gl {

val i = ordinal

val hasSwizzle by lazy { this == ES30 || this == GL33 }
val hasSwizzle get() = this == ES30 || this == GL33
}

data class Swizzles(var r: Swizzle, var g: Swizzle, var b: Swizzle, var a: Swizzle) {
Expand Down
92 changes: 92 additions & 0 deletions src/main/kotlin/gli/load.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package gli

import glm.set
import glm.vec3.Vec3i
import uno.buffer.toByteBuffer
import java.awt.image.BufferedImage
import java.awt.image.BufferedImage.TYPE_3BYTE_BGR
import java.awt.image.BufferedImage.TYPE_4BYTE_ABGR
import java.awt.image.DataBufferByte
import java.io.File
import javax.imageio.ImageIO

/**
* Created by elect on 01/05/17.
*/

fun load(path: String) {

val ext = path.substringAfterLast('.').toLowerCase()

when (ext) {

"dds" -> loadDDS(path)
"png" -> loadPNG(path)
}
}

fun loadPNG(path: String): Texture {

val file = File(Texture::javaClass.javaClass.classLoader.getResource(path).toURI())

val image = ImageIO.read(file).flipY()

val data = (image.raster.dataBuffer as DataBufferByte).data

val format: Format

when (image.type) {

TYPE_3BYTE_BGR -> {

format = Format.RGB8_UNORM_PACK8

repeat(image.width * image.height) {
// swith blue and red
val tmp = data[it * 3] // save blue
data[it * 3] = data[it * 3 + 2] // write red
data[it * 3 + 2] = tmp // write blue
}
}
TYPE_4BYTE_ABGR -> {

format = Format.RGBA8_UNORM_PACK8

repeat(image.width * image.height) {
// swith alpha and red
var tmp = data[it * 4] // save alpha
data[it * 4] = data[it * 4 + 3] // write red
data[it * 4 + 3] = tmp // write alpha
// swith blue and green
tmp = data[it * 4 + 1] // save blue
data[it * 4 + 1] = data[it * 4 + 2] // write green
data[it * 4 + 2] = tmp // write blue
}
}
else -> throw Error("not yet supported")
}

val texture = Texture(Target._2D, format, Vec3i(image.width, image.height, 1), 1, 1, 1)

repeat(texture.size()) { texture.data()[it] = data[it]}

return texture
}

fun BufferedImage.toByteBuffer() = (raster.dataBuffer as DataBufferByte).data.toByteBuffer()

fun BufferedImage.flipY(): BufferedImage {

var scanline1: Any? = null
var scanline2: Any? = null

for (i in 0 until height / 2) {

scanline1 = raster.getDataElements(0, i, width, 1, scanline1)
scanline2 = raster.getDataElements(0, height - i - 1, width, 1, scanline2)
raster.setDataElements(0, i, width, 1, scanline2)
raster.setDataElements(0, height - i - 1, width, 1, scanline1)
}

return this
}
9 changes: 7 additions & 2 deletions src/main/kotlin/gli/loadDDS.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ import glm.vec3.Vec3i
*/


/** Loads a texture storage_linear from DDS memory. Returns an empty storage_linear in case of failure. */
/** Loads a texture storage_linear from DDS memory. Returns an empty storage_linear in case of failure.
* @param path Path of the file to open including filaname and filename extension */
fun loadDDS(path: String) = loadDDS(Texture::javaClass.javaClass.classLoader.getResource(path).toURI())

/** Loads a texture storage_linear from DDS memory. Returns an empty storage_linear in case of failure.
* @param uri Uri of the file to open including filaname and filename extension */
fun loadDDS(uri: URI): Texture {

val channel = RandomAccessFile(File(uri), "r").channel
Expand Down Expand Up @@ -122,7 +127,7 @@ fun loadDDS(data: ByteBuffer): Texture {

assert(data.capacity() == data.position() + texture.size())

repeat(texture.size()) { texture.data()[it] = data.get()}
repeat(texture.size()) { texture.data()[it] = data.get() }

return texture
}
Expand Down

0 comments on commit 0fee411

Please sign in to comment.