Skip to content

Instantly share code, notes, and snippets.

@RainWarrior
Last active August 29, 2015 13:58
Show Gist options
  • Save RainWarrior/9998879 to your computer and use it in GitHub Desktop.
Save RainWarrior/9998879 to your computer and use it in GitHub Desktop.
import org.lwjgl.{ BufferUtils, opengl }
import opengl.{ Display, DisplayMode, GL11 }
import GL11._
import java.nio.FloatBuffer
/*class BasicApplet {
val serialVersionUID = 1L;
def init() {
//setSize(16*17*3, 16*17*3);
}
def paint(g: Graphics) {
//setBackground(Color.WHITE)
}
}
}*/
object Main extends Application {
val size = 1000
Display.setDisplayMode(new DisplayMode(size,size))
Display.create()
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, size, 0, size, 1, -1)
glMatrixMode(GL_MODELVIEW)
val texture = glGenTextures()
val buffer = BufferUtils.createByteBuffer(0x10000 * 3)
@inline final def clamp(f: Float) = if(f<0F) 0F else { if (f>1F) 1F else f }
val kr = 0.2126f
val kb = 0.0722f
for {
t <- 0 until 16
s <- 0 until 16
cb <- 0 until 16
cr <- 0 until 16
} {
val tf = t / 15f
val sf = s / 15f
val cbf = cb / 15f - .5f
val crf = cr / 15f - .5f
val y = clamp(sf + tf)
val cb2 = (-0.003f * sf + cbf * tf) / (sf + tf)
val cr2 = ( 0.003f * sf + crf * tf) / (sf + tf)
val b = y + 2f * (1f - kb) * cb2
val r = y + 2f * (1f - kr) * cr2
val g = (y - kr * r - kb * b) / (1f - kr - kb)
val idx = (t * 0x1000 + s * 0x10 + cb * 0x100 + cr * 0x1) * 3
buffer.put(idx, (clamp(r) * 255).toByte)
buffer.put(idx + 1, (clamp(g) * 255).toByte)
buffer.put(idx + 2, (clamp(b) * 255).toByte)
}
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, texture)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 0x100, 0x100, 0, GL_RGB, GL_UNSIGNED_BYTE, buffer)
while (!Display.isCloseRequested) {
glClear(GL_COLOR_BUFFER_BIT)
glBindTexture(GL_TEXTURE_2D, texture)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glBegin(GL_QUADS); {
glTexCoord2f(0, 0); glVertex2f(0, 0 )
glTexCoord2f(1, 0); glVertex2f(size, 0 )
glTexCoord2f(1, 1); glVertex2f(size, size)
glTexCoord2f(0, 1); glVertex2f(0, size)
}
glEnd()
Display.update()
}
Display.destroy()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment