Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
RGBATextureTest GL1.0
package mdesl.test;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.GL_RENDERER;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.GL_VERSION;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glClearColor;
import static org.lwjgl.opengl.GL11.glColor4f;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glGetString;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glOrtho;
import static org.lwjgl.opengl.GL11.glTexCoord2f;
import static org.lwjgl.opengl.GL11.glVertex2f;
import java.nio.ByteBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
public class RGBATextureTestGL10 extends SimpleGame {
public static void main(String[] args) throws LWJGLException {
Game game = new RGBATextureTestGL10();
game.setDisplayMode(640, 480, false);
final int TEX_SIZE = 256;
//our procedural texture
Texture tex;
ByteBuffer buffer;
float time = 0;
SoftwareShader shader;
protected void create() throws LWJGLException {
try {
System.out.println("OpenGL version: " + glGetString(GL_VERSION));
System.out.println("Graphic Card: " + glGetString(GL_RENDERER));
System.out.println("GLSL version: " + glGetString(GL20.GL_SHADING_LANGUAGE_VERSION));
} catch (Exception e) {
//create an empty texture
tex = new Texture(TEX_SIZE, TEX_SIZE, Texture.LINEAR, Texture.REPEAT);
buffer = BufferUtils.createByteBuffer(tex.getWidth() * tex.getHeight() * 4);
shader = new Kaliset();
glClearColor(0.5f, .5f, .5f, 1f);
glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
glEnable(GL_TEXTURE_2D); //likely redundant; will be removed upon migration to "modern GL"
public static void drawTexture(Texture tex, float x, float y, float width, float height) {
//bind the texture before rendering it
//setup our texture coordinates
//(u,v) is another common way of writing (s,t)
float u = 0f;
float v = 0f;
float u2 = 1f;
float v2 = 1f;
//immediate mode is deprecated -- we are only using it for quick debugging
//NOTE: If we want to render many textures, we should use texture atlases and
//combine many vertices between glBegin + glEnd --- or just use vertex arrays!
glColor4f(1f, 1f, 1f, 1f);
glTexCoord2f(u, v);
glVertex2f(x, y);
glTexCoord2f(u, v2);
glVertex2f(x, y + height);
glTexCoord2f(u2, v2);
glVertex2f(x + width, y + height);
glTexCoord2f(u2, v);
glVertex2f(x + width, y);
protected void render() throws LWJGLException {
time += this.getDeltaTime();
int w = tex.getWidth();
int h = tex.getHeight();
Color color = new Color();
buffer.clear(); //clear our RGBA buffer
for (int i=0; i<w * h; i++) {
//offset to (x, y)
int y = i / w;
int x = i - w*y;
shader.getPixelColor(x, y, w, h, time, color);
//place R, G, B, A bytes
buffer.put( (byte)(Math.min(255, Math.max(0, color.r*255))) )
.put( (byte)(Math.min(255, Math.max(0, color.g*255))) )
.put( (byte)(Math.min(255, Math.max(0, color.b*255))) )
.put( (byte)(Math.min(255, Math.max(0, color.a*255))) );
tex.upload(GL11.GL_RGBA, buffer);
drawTexture(tex, 0, 0, tex.getWidth(), tex.getHeight());
// called to resize the display
protected void resize() throws LWJGLException {
//emulates shaders in software
interface SoftwareShader {
* Gets the pixel color for the specified input values, placing it in the "out" param.
* @param x the x position of this pixel
* @param y the y position of this pixel
* @param width the width of the texture in pixels
* @param height the height of the texture in pixels
* @param time a time offset for effects
* @param the output color
public void getPixelColor(int x, int y, int width, int height, float time, Color out);
//Kaliset fractal
class Kaliset implements SoftwareShader {
public static final int ITERATIONS = 22;
public void getPixelColor(int x, int y, int width, int height, float time, Color out) {
float cx = (float)(-0.5 * (2.5 + Math.sin(time * 0.0025)) / 2.0 + 0.3);
float cy = (float)(-0.5 * (2.5 + Math.sin(time * 0.0025)) / 2.0 + 0.3);
float zx = (x / (float)width) * 3f - 1.5f;
float zy = (y / (float)height) * 3f - 1.5f;
float total = 0f;
float m = 0f;
for (int i=0; i<ITERATIONS; i++) {
float pm = m;
m = zx*zx + zy*zy;
zx = Math.abs(zx) / m + cx;
zy = Math.abs(zy) / m + cy;
total += (float)Math.exp(-1.0 / Math.abs(m - pm));
total = total / 20f;
out.r = total*.5f;
out.g = total;
out.b = (float)(total/Math.sin(Math.cos(time/10f)));
out.a = 1.0f;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment