aboutsummaryrefslogtreecommitdiffstats
path: root/src/ru/olamedia/olacraft/render/jogl/BlockRenderer.java
blob: 6d964030e16673e29d768edd24fe1d59255bb59b (plain)
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
package ru.olamedia.olacraft.render.jogl;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;

import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureCoords;

import ru.olamedia.geom.SimpleQuadMesh;
import ru.olamedia.olacraft.world.chunk.BlockSlice;
import ru.olamedia.texture.TextureManager;

public class BlockRenderer {
	private BlockSlice slice;
	private SimpleQuadMesh mesh;

	public BlockRenderer(BlockSlice slice) {
		this.slice = slice;
	}

	public SimpleQuadMesh getMesh(GL glx) {
		GL2 gl = glx.getGL2();
		// 14739
		SimpleQuadMesh mesh = new SimpleQuadMesh(999999);
		mesh.useColor();
		mesh.useTexture();

		gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER,
				GL.GL_NEAREST_MIPMAP_NEAREST);
		gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER,
				GL.GL_NEAREST_MIPMAP_NEAREST);
		gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_FASTEST);
		gl.glHint(GL2.GL_LINE_SMOOTH_HINT, GL2.GL_NICEST);
		Texture grass = TextureManager.get("texture/grass.png");
		int tex = grass.getTextureObject(gl);

		TextureCoords tc = grass.getImageTexCoords();
		// mesh.setTextureSize(tc.right(), tc.top());
		System.out.println(grass.getWidth() + " " + grass.getHeight() + " | "
				+ tc.left() + " " + tc.right() + " " + tc.top() + " "
				+ tc.bottom());
		mesh.setTextureSize(grass.getWidth(), grass.getHeight());
		for (int x = 0; x < slice.getWidth(); x++) {
			for (int y = 0; y < slice.getHeight(); y++) {
				for (int z = 0; z < slice.getDepth(); z++) {
					mesh.setTranslation(x, y, z);
					// mesh.setColor4f(0, 1, 0, 1);
					mesh.setColor4f((float) Math.random(),
							(float) Math.random(), (float) Math.random(), 1);
					mesh.setGLTexture(tex);
					if (y == 0) {
						mesh.addBottomQuad();
					}
					if (y == 3) {
						// Math.random();
						mesh.addTopQuad();
					}
					if (y < 4) {
						if (x == 0) {
							mesh.addLeftQuad();
						}
						if (x == slice.getWidth() - 1) {
							mesh.addRightQuad();
						}
						if (z == 0) {
							mesh.addBackQuad();
						}
						if (z == slice.getDepth() - 1) {
							mesh.addFrontQuad();
						}
					}
				}
			}
		}
		mesh.endMesh();
		return mesh;
	}

	public void render(GL glx) {
		if (null == mesh) {
			mesh = getMesh(glx);
		}
		mesh.joglRender(glx);
	}

}