public
Last active

RGBATextureTest GL1.0

  • Download Gist
RGBATextureTestGL10.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
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 mdesl.graphics.Color;
import mdesl.graphics.Texture;
 
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);
game.start();
}
 
final int TEX_SIZE = 256;
//our procedural texture
Texture tex;
ByteBuffer buffer;
float time = 0;
SoftwareShader shader;
protected void create() throws LWJGLException {
super.create();
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) {
e.printStackTrace();
}
//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);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
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
tex.bind();
 
//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);
glBegin(GL_QUADS);
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);
glEnd();
}
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))) );
}
buffer.flip();
tex.upload(GL11.GL_RGBA, buffer);
glClear(GL_COLOR_BUFFER_BIT);
drawTexture(tex, 0, 0, tex.getWidth(), tex.getHeight());
}
 
// called to resize the display
protected void resize() throws LWJGLException {
super.resize();
}
//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
//https://bitbucket.org/sharow/pijockey/src/tip/shaders/kaliset.glsl
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;
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.