From 91b70e5978275cf46dc70158b4fd0ac451a94231 Mon Sep 17 00:00:00 2001
From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54>
Date: Wed, 2 Jun 2010 13:54:54 +0000
Subject: [PATCH] again

git-svn-id: http://svn.net-core.org/repos/t-engine4@740 51575b47-30f0-44d4-a5cc-537603b46e54
---
 src/noise.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 55 insertions(+), 4 deletions(-)

diff --git a/src/noise.c b/src/noise.c
index 4d304c5f02..a79d3cee12 100644
--- a/src/noise.c
+++ b/src/noise.c
@@ -25,6 +25,7 @@
 #include "types.h"
 #include "script.h"
 #include <math.h>
+#include <stdlib.h>
 #include "libtcod.h"
 #include "noise.h"
 #include "tgl.h"
@@ -178,10 +179,10 @@ static int noise_texture2d(lua_State *L)
 	const char *type = luaL_checkstring(L, 2);
 	int w = luaL_checknumber(L, 3);
 	int h = luaL_checknumber(L, 4);
-	float zoom = luaL_checknumber(L, 4);
-	float x = luaL_checknumber(L, 5);
-	float y = luaL_checknumber(L, 6);
-	float octave = lua_tonumber(L, 7);
+	float zoom = luaL_checknumber(L, 5);
+	float x = luaL_checknumber(L, 6);
+	float y = luaL_checknumber(L, 7);
+	float octave = lua_tonumber(L, 8);
 	GLubyte *map = malloc(w * h * 3 * sizeof(GLubyte));
 
 	float p[2];
@@ -215,6 +216,56 @@ static int noise_texture2d(lua_State *L)
 	return 1;
 }
 
+static int noise_texture3d(lua_State *L)
+{
+	noise_t *n = (noise_t*)auxiliar_checkclass(L, "noise{core}", 1);
+	const char *type = luaL_checkstring(L, 2);
+	int w = luaL_checknumber(L, 3);
+	int h = luaL_checknumber(L, 4);
+	int d = luaL_checknumber(L, 5);
+	float zoom = luaL_checknumber(L, 6);
+	float x = luaL_checknumber(L, 7);
+	float y = luaL_checknumber(L, 8);
+	float z = luaL_checknumber(L, 9);
+	float octave = lua_tonumber(L, 10);
+	GLubyte *map = malloc(w * h * d * 3 * sizeof(GLubyte));
+
+	float p[3];
+	int i, j, k;
+	for (i = 0; i < w; i++)
+	{
+		for (j = 0; j < h; j++)
+		{
+			for (k = 0; k < d; k++)
+			{
+				p[0] = zoom * ((float)(i+x)) / w;
+				p[1] = zoom * ((float)(j+y)) / h;
+				p[2] = zoom * ((float)(k+z)) / d;
+				float v = ((TCOD_noise_simplex(n->noise, p) + 1) / 2) * 255;
+				map[TEXEL3(i, j, k)] = (GLubyte)v;
+				map[TEXEL3(i, j, k)+1] = (GLubyte)v;
+				map[TEXEL3(i, j, k)+2] = (GLubyte)v;
+			}
+		}
+	}
+
+	GLuint *t = (GLuint*)lua_newuserdata(L, sizeof(GLuint));
+	auxiliar_setclass(L, "gl{texture}", -1);
+
+	glGenTextures(1, t);
+	glBindTexture(GL_TEXTURE_3D, *t);
+	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
+	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+	glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB8, w, h, d, 0, GL_RGB, GL_UNSIGNED_BYTE, map);
+
+	free(map);
+
+	return 1;
+}
+
 static const struct luaL_reg noiselib[] =
 {
 	{"new", noise_new},
-- 
GitLab