[Fencommits] libvob: Start porting textures.

Matti J. Katila majukati at cc.jyu.fi
Tue Mar 7 02:05:22 EET 2006


Tue Mar  7 01:08:32 EET 2006  Matti J. Katila <majukati at cc.jyu.fi>
  * Start porting textures.

diff -rN -u libvob-old/org/nongnu/libvob/gl/impl/lwjgl/texture/GeometricTex.java libvob-new/org/nongnu/libvob/gl/impl/lwjgl/texture/GeometricTex.java
--- libvob-old/org/nongnu/libvob/gl/impl/lwjgl/texture/GeometricTex.java	1970-01-01 02:00:00.000000000 +0200
+++ libvob-new/org/nongnu/libvob/gl/impl/lwjgl/texture/GeometricTex.java	2006-03-07 01:34:37.000000000 +0200
@@ -0,0 +1,187 @@
+package org.nongnu.libvob.gl.impl.lwjgl.texture;
+
+import java.nio.FloatBuffer;
+import java.security.SecureRandom;
+
+public class GeometricTex extends NamedTexture {
+    /*-
+    geometric.texture
+     *    
+     *    Copyright (c) 2003, Janne Kujala and Tuomas J. Lukka
+     *    
+     *    This file is part of Gzz.
+     *    
+     *    Gzz is free software; you can redistribute it and/or modify it under
+     *    the terms of the GNU General Public License as published by
+     *    the Free Software Foundation; either version 2 of the License, or
+     *    (at your option) any later version.
+     *    
+     *    Gzz is distributed in the hope that it will be useful, but WITHOUT
+     *    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+     *    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
+     *    Public License for more details.
+     *    
+     *    You should have received a copy of the GNU General
+     *    Public License along with Gzz; if not, write to the Free
+     *    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+     *    MA  02111-1307  USA
+     *    
+     *    
+     */
+    /*
+     * Written by Janne Kujala and Tuomas J. Lukka
+     */
+
+//     -*-C++-*-
+//    #include <math.h>
+
+
+
+	//    #define FPARAM(name, default) float name = params->getFloat(#name, default);
+    	float SQR(float A) { return (float) ((A) * (A)); }
+
+    
+    
+    float pyramid(float x, float y) {
+      x -= Math.floor(x);
+      y -= Math.floor(y);
+
+      x = (float) Math.abs(x - .5);
+      y = (float) Math.abs(y - .5);
+
+      return 1 - 2 * (x > y ? x : y);
+    }
+
+    float checkerboard(float x, float y) {
+      x -= Math.floor(x);
+      y -= Math.floor(y);
+
+      return (x < .499999) ^ (y < .499999)? 1f:0;
+    }
+
+    float cone(float x, float y) {
+      x -= Math.floor(x);
+      y -= Math.floor(y);
+
+      x -= .5;
+      y -= .5;
+      
+      return (float) (1 - 2 * Math.sqrt(.5 * (x * x + y * y)));
+    }
+
+    float checkerboard2(float x, float y) {
+      x -= Math.floor(x);
+      y -= Math.floor(y);
+      return (x < .499999) && (y < .499999)?1f:0;
+    }
+
+    float saw(float x) {
+      return (float) (x - Math.floor(x));
+    }
+
+    float triangle(float x) {
+      x -= Math.floor(x);
+      return (float) (1 - 2 * Math.abs(x - .5));
+    }
+
+    float stripe(float x) {
+      x -= Math.floor(x);
+      return (x < .4999999)?1f:0;
+    }
+
+    float circle(float x, float y) {
+      return ((SQR(x - 0.5f) + SQR(y - 0.5f)) <= SQR(0.5f))? 1f: 0;
+    }
+
+    public GeometricTex() {}
+    public void render(TextureParam params, int width, int height, int depth, int components, FloatBuffer data) {
+
+	float type = params.get("type", 0);
+	float scale = params.get("params", 1f);
+	float bias = params.get("bias", 0);
+	float seed = params.get("seed", 1f);
+
+        if(components > 4) return;
+        
+        SecureRandom srand = new SecureRandom();
+        if (seed != 1f)
+            srand.setSeed((long)seed);
+//          srandom((unsigned)seed);
+ 
+
+        int ind=0, i, j;
+        float x, y;
+        float xstep = 1.0f / width;
+        float ystep = 1.0f / height;
+
+        for (j = 0, y = 0; j < height; j++, y += ystep) {	
+          for (i = 0, x = 0; i < width; i++, x += xstep) {
+
+    	switch ((int)type) {
+    	case 0: 
+    	  if (components >= 1) data.put(ind++, pyramid(x, y));
+    	  if (components >= 2) data.put(ind++, pyramid(x + .5f, y));
+    	  if (components >= 3) data.put(ind++, pyramid(x, y + .5f));
+    	  if (components >= 4) data.put(ind++, pyramid(x + .5f, y + .5f));
+    	  break;
+    	case 1:
+    	  if (components >= 1) data.put(ind++, checkerboard(x, y));
+    	  if (components >= 2) data.put(ind++, checkerboard(x + .25f, y));
+    	  if (components >= 3) data.put(ind++, checkerboard(x, y + .25f));
+    	  if (components >= 4) data.put(ind++, checkerboard(x + .25f, y + .25f));
+    	  break;
+    	case 2:
+    	  if (components >= 1) data.put(ind++, cone(x, y));
+    	  if (components >= 2) data.put(ind++, cone(x + .5f, y));
+    	  if (components >= 3) data.put(ind++, cone(x, y + .5f));
+    	  if (components >= 4) data.put(ind++, cone(x + .5f, y + .5f));
+    	  break;
+    	case 3:
+    	  if (components >= 1) data.put(ind++, checkerboard2(x, y));
+    	  if (components >= 2) data.put(ind++, checkerboard2(x + .5f, y));
+    	  if (components >= 3) data.put(ind++, checkerboard2(x, y + .5f));
+    	  if (components >= 4) data.put(ind++, checkerboard2(x + .5f, y + .5f));
+    	  break;
+    	case 4:
+    	  if (components >= 1) data.put(ind++, saw(x));
+    	  if (components >= 2) data.put(ind++, saw(y));
+    	  if (components >= 3) data.put(ind++, saw(1 - xstep - x));
+    	  if (components >= 4) data.put(ind++, saw(1 - ystep - y));
+    	  break;
+    	case 5:
+    	  if (components >= 1) data.put(ind++, triangle(x));
+    	  if (components >= 2) data.put(ind++, triangle(y));
+    	  if (components >= 3) data.put(ind++, triangle(.5f + x));
+    	  if (components >= 4) data.put(ind++, triangle(.5f + y));
+    	  break;
+    	case 6:
+    	  if (components >= 1) data.put(ind++, stripe(x));
+    	  if (components >= 2) data.put(ind++, stripe(y));
+    	  if (components >= 3) data.put(ind++, stripe(1 - xstep - x));
+    	  if (components >= 4) data.put(ind++, stripe(1 - ystep - y));
+    	  break;
+    	case 7:
+    	    throw new Error("argh");
+//    	  if (components >= 1) data[ind++] = srand.nextFloat / (RAND_MAX + 1.0);
+//    	  if (components >= 2) data[ind++] = srand.nextFloat / (RAND_MAX + 1.0);
+//    	  if (components >= 3) data[ind++] = srand.rand() / (RAND_MAX + 1.0);
+//    	  if (components >= 4) data[ind++] = rand() / (RAND_MAX + 1.0);
+//    	  break;
+    	case 8:
+    	  if (components >= 1) data.put(ind++, circle(x + 0.5f/width, y + 0.5f/height));
+    	  if (components >= 2) data.put(ind++, circle(x + 0.5f/width, y + 0.5f/height));
+    	  if (components >= 3) data.put(ind++, circle(x + 0.5f/width, y + 0.5f/height));
+    	  if (components >= 4) data.put(ind++, circle(x + 0.5f/width, y + 0.5f/height));
+    	  break;
+          	}
+          }	
+        }
+
+        for(i = 0; i < width * height * depth * components; i++) {
+          data.put(i, data.get(i) * scale + bias);
+        }
+
+//        data.flip();
+    }
+
+}
diff -rN -u libvob-old/org/nongnu/libvob/gl/impl/lwjgl/texture/NamedTexture.java libvob-new/org/nongnu/libvob/gl/impl/lwjgl/texture/NamedTexture.java
--- libvob-old/org/nongnu/libvob/gl/impl/lwjgl/texture/NamedTexture.java	1970-01-01 02:00:00.000000000 +0200
+++ libvob-new/org/nongnu/libvob/gl/impl/lwjgl/texture/NamedTexture.java	2006-03-07 01:34:37.000000000 +0200
@@ -0,0 +1,39 @@
+package org.nongnu.libvob.gl.impl.lwjgl.texture;
+
+import java.nio.FloatBuffer;
+
+public abstract class NamedTexture {
+
+    public abstract void render(TextureParam params, int width, int height, int depth, int components, FloatBuffer data);
+    
+
+    
+    static public NamedTexture getTexture(String name) {
+	
+	
+//	if (strcmp(type, "TubeConnector") == 0) return new TubeConnector;
+//	if (strcmp(type, "TubeFrame") == 0) return new TubeFrame;
+//	if (strcmp(type, "bgnoise") == 0) return new bgnoise;
+//	if (strcmp(type, "circle") == 0) return new circle;
+//	if (strcmp(type, "coordinates") == 0) return new coordinates;
+//	if (strcmp(type, "filereader") == 0) return new filereader;
+//	if (strcmp(type, "fnoise") == 0) return new fnoise;
+	if (name.equals("geometric")) return new GeometricTex(); //param, w,h,d,comps, data);
+//	if (strcmp(type, "irregu") == 0) return new irregu;
+//	if (strcmp(type, "irreguedge") == 0) return new irreguedge;
+//	if (strcmp(type, "line") == 0) return new line;
+//	if (strcmp(type, "lines1") == 0) return new lines1;
+//	if (strcmp(type, "noise") == 0) return new noise;
+//	if (strcmp(type, "palette1") == 0) return new palette1;
+//	if (strcmp(type, "ppbg") == 0) return new ppbg;
+//	if (strcmp(type, "ppbg1") == 0) return new ppbg1;
+//	if (strcmp(type, "rd1") == 0) return new rd1;
+//	if (strcmp(type, "sawnoise") == 0) return new sawnoise;
+//	if (strcmp(type, "shape1") == 0) return new shape1;
+//	if (strcmp(type, "shape2") == 0) return new shape2;
+//	if (strcmp(type, "waves") == 0) return new waves;
+	throw new Error("no texture implemented!");
+    }
+    
+    
+}
diff -rN -u libvob-old/org/nongnu/libvob/gl/impl/lwjgl/texture/TextureParam.java libvob-new/org/nongnu/libvob/gl/impl/lwjgl/texture/TextureParam.java
--- libvob-old/org/nongnu/libvob/gl/impl/lwjgl/texture/TextureParam.java	1970-01-01 02:00:00.000000000 +0200
+++ libvob-new/org/nongnu/libvob/gl/impl/lwjgl/texture/TextureParam.java	2006-03-07 01:34:37.000000000 +0200
@@ -0,0 +1,22 @@
+package org.nongnu.libvob.gl.impl.lwjgl.texture;
+
+import java.util.HashMap;
+
+public class TextureParam extends HashMap{
+
+    public TextureParam(String[] params) {
+	super();
+	for (int i = 0; i < params.length; i+=2) {
+	    String key = params[i];
+	    Float val = new Float(params[i+1]);
+	    put(key, val);
+	}
+    }
+
+    public float get(String key, float def) {
+	if (!containsKey(key))
+	    return def;
+	else return ((Float)get(key)).floatValue();
+    }
+
+}
diff -rN -u libvob-old/org/nongnu/libvob/gl/impl/lwjgl/texture/TextureUtils.java libvob-new/org/nongnu/libvob/gl/impl/lwjgl/texture/TextureUtils.java
--- libvob-old/org/nongnu/libvob/gl/impl/lwjgl/texture/TextureUtils.java	1970-01-01 02:00:00.000000000 +0200
+++ libvob-new/org/nongnu/libvob/gl/impl/lwjgl/texture/TextureUtils.java	2006-03-07 01:34:37.000000000 +0200
@@ -0,0 +1,122 @@
+package org.nongnu.libvob.gl.impl.lwjgl.texture;
+
+import java.nio.Buffer;
+import java.nio.CharBuffer;
+import java.nio.FloatBuffer;
+
+import org.lwjgl.BufferUtils;
+import org.lwjgl.opengl.GL11;
+import org.lwjgl.opengl.GL12;
+
+public class TextureUtils {
+
+    public static void buildMipmaps(int target, int intformat, int w, int h, int format, int type, FloatBuffer pixels) {
+
+	if (!((w - 1 & w) == 0 && (h - 1 & h) == 0)) throw new Error("aasert");
+
+	int comp = 0;
+	switch (format) {
+		default: 
+		case GL11.GL_COLOR_INDEX: 
+		    throw new Error();
+//		    assert(0); 
+//		    break;
+		case GL11.GL_DEPTH_COMPONENT:
+		case GL11.GL_RED:
+		case GL11.GL_GREEN:  
+		case GL11.GL_BLUE: 
+		case GL11.GL_ALPHA:
+		case GL11.GL_LUMINANCE:  
+		    comp = 1; break;
+		case GL11.GL_LUMINANCE_ALPHA: 
+		    comp = 2; break;
+		case GL11.GL_RGB:   
+		case GL12.GL_BGR:   
+		    comp = 3; break;
+		case GL11.GL_RGBA:   
+		case GL12.GL_BGRA:
+		    comp = 4; break;
+	}
+
+	int w2 = w, h2 = h;
+	int level = 0;
+
+	Buffer data = null;
+//	char *data = new char[w * h * comp * 4];
+
+	while (true) {
+	    int xf = w / w2;
+	    int yf = h / h2;
+	    switch (type) {
+//	    	case GL11.GL_UNSIGNED_BYTE: 
+//		  filter((unsigned char *)data, (unsigned char *)pixels, w, h, comp, xf, yf); break;
+//		case GL11.GL_BYTE: 
+//		  filter((char *)data, (char *)pixels, w, h, comp, xf, yf); break;
+//		case GL11.GL_UNSIGNED_SHORT: 
+//		  filter((unsigned short *)data, (unsigned short *)pixels, w, h, comp, xf, yf); break;
+//		case GL11.GL_SHORT: 
+//		  filter((short *)data, (short *)pixels, w, h, comp, xf, yf); break;
+//		case GL11.GL_UNSIGNED_INT: 
+//		  filter((unsigned int *)data, (unsigned int *)pixels, w, h, comp, xf, yf); break;
+//		case GL11.GL_INT: 
+//		  filter((int *)data, (int *)pixels, w, h, comp, xf, yf); break;
+		case GL11.GL_FLOAT: {
+		    if (data == null)
+			data = BufferUtils.createFloatBuffer(w * h * comp * 4);
+		    filter(data, pixels, w, h, comp, xf, yf); 
+		    break;
+		}
+		default:
+		    throw new Error();
+		    //  assert(0);
+	    }
+	    data.flip();
+	    
+	    if (data instanceof FloatBuffer)
+		GL11.glTexImage2D(target, level, intformat, w2, h2, 0, format, type, (FloatBuffer)data);
+	    level++;
+		
+	    if (w2 <= 1 && h2 <= 1) break;
+	    w2 = w2 + 1 >> 1;
+	    h2 = h2 + 1 >> 1;
+	}
+	      
+	GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_BASE_LEVEL, 0);
+	GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL12.GL_TEXTURE_MAX_LEVEL, level - 1);
+    }
+
+    static void filter(Buffer dst, Buffer src, int w, int h, int comp, int xfact, int yfact) {
+	for (int y = 0; y < h; y += yfact)
+	    for (int x = 0; x < w; x += xfact)
+		for (int c = 0; c < comp; c++) {
+		    float value = 0;
+		    for (int j = 0; j < xfact; j++)
+			for (int i = 0; i < yfact; i++)
+			    if (src instanceof FloatBuffer)
+				value += ((FloatBuffer) src).get(c + (x + i + w * (y + j)) * comp);
+		    //*dst++ = (TYPE)(value / (xfact * yfact) + (.5f - (TYPE).5f));
+		    if (dst instanceof FloatBuffer)
+			((FloatBuffer) dst).put(value / (xfact * yfact) + (.5f - .5f));
+		}
+    }
+    
+    
+    
+    /*
+     *     template <class TYPE>
+    void filter(TYPE *dst, TYPE *src, GLsizei w, GLsizei h, int comp, int xfact, int yfact) {
+      for (int y = 0; y < h; y += yfact)
+	for (int x = 0; x < w; x += xfact)
+	  for (int c = 0; c < comp; c++)
+	    {
+	      float value = 0;
+	      for (int j = 0; j < xfact; j++)
+		for (int i = 0; i < yfact; i++)
+		  value += src[c + (x + i + w * (y + j)) * comp];
+	      *dst++ = (TYPE)(value / (xfact * yfact) + (.5f - (TYPE).5f));
+	    }
+    }
+     * 
+     */
+    
+}




More information about the Fencommits mailing list