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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
package ru.olamedia.olacraft.scene;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryUsage;
import java.util.HashMap;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.glu.GLU;
import javax.media.opengl.glu.GLUquadric;
import org.ode4j.ode.DBody;
import com.jogamp.opengl.util.PMVMatrix;
import ru.olamedia.Options;
import ru.olamedia.game.GameTime;
import ru.olamedia.liveEntity.LiveEntity;
import ru.olamedia.olacraft.game.Game;
import ru.olamedia.olacraft.physics.GamePhysicsWorld;
import ru.olamedia.olacraft.render.jogl.ChunkRenderer;
import ru.olamedia.olacraft.render.jogl.InventoryRenderer;
import ru.olamedia.olacraft.render.jogl.joglViewport;
import ru.olamedia.olacraft.weapon.Bullet;
import ru.olamedia.olacraft.weapon.BulletScene;
import ru.olamedia.olacraft.world.block.Block;
import ru.olamedia.olacraft.world.blockTypes.AbstractBlockType;
import ru.olamedia.olacraft.world.blockTypes.GrassBlockType;
import ru.olamedia.olacraft.world.chunk.BlockSlice;
import ru.olamedia.olacraft.world.chunk.ChunkSlice;
import ru.olamedia.olacraft.world.chunk.ChunkUnavailableException;
import ru.olamedia.olacraft.world.provider.WorldProvider;
import ru.olamedia.player.Player;
import ru.olamedia.vbo.VBO;
public class GameScene {
private PMVMatrix matrix;
private HashMap<Integer, LiveEntity> liveEntities = new HashMap<Integer, LiveEntity>();
WorldProvider provider;
private int renderDistance = Options.renderDistance;
private joglViewport viewport;
private BulletScene bullets = new BulletScene();
private GamePhysicsWorld physics = new GamePhysicsWorld();
private VBO testObject;
private boolean isInitialized = false;
BlockSlice viewSlice;
public GameTime time = new GameTime();
public GameScene(WorldProvider provider) {
this.provider = provider;
setRenderDistance(renderDistance);
}
public void addBullet(Bullet b) {
bullets.add(b);
DBody body = physics.createBody();
body.setPosition(b.location.x, b.location.y, b.location.z);
body.setLinearVel(b.velocity.x, b.velocity.y, b.velocity.z);
/*
* DMass mass = OdeHelper.createMass();
* mass.setMass(10);
* mass.setI(OdeHelper.c);
* body.setMass(mass);
*/
b.body = body;
}
public int getBulletsCount() {
return bullets.getCount();
}
public void init(GLAutoDrawable drawable) {
if (isInitialized) {
return;
}
isInitialized = true;
time.init();
registerTextures();
viewport = new joglViewport(drawable);
testObject = new VBO(drawable);
}
private void registerTextures() {
AbstractBlockType t;
t = new GrassBlockType();
t.register();
}
/**
* @return the renderDistance
*/
public int getRenderDistance() {
return renderDistance;
}
/**
* @param renderDistance
* the renderDistance to set
*/
public void setRenderDistance(int renderDistance) {
this.renderDistance = renderDistance;
viewSlice = new BlockSlice(provider, renderDistance, renderDistance, renderDistance);
blockRenderer = new ChunkRenderer(viewSlice);
}
ChunkRenderer blockRenderer = new ChunkRenderer(viewSlice);
GLU glu = new GLU();
public void registerLiveEntity(LiveEntity entity) {
// liveEntityIncrement++;
// entity.setId(liveEntityIncrement);
liveEntities.put(entity.getConnectionId(), entity);
}
private InventoryRenderer inventoryRenderer;
private Player player;
public void registerPlayer(LiveEntity player) {
inventoryRenderer = new InventoryRenderer(player.getInventory());
this.player = (Player) player;
}
public LiveEntity getLiveEntity(int connectionId) {
if (liveEntities.containsKey(connectionId)) {
return liveEntities.get(connectionId);
}
return null;
}
public HashMap<Integer, LiveEntity> getLiveEntities() {
return liveEntities;
}
Block nearestBlock = null;
public void tick() {
time.tick();
Game.instance.tick();
float aspect = Game.Display.getAspect();
Game.instance.camera.setAspect(aspect);
// bullets.update(Game.instance.getDelta());
physics.getWorld().step(Game.instance.getDelta());
BlockSlice pickSlice = new BlockSlice(provider, 10, 10, 10);
pickSlice.setCenter(player.getCameraX(), player.getCameraY(), player.getCameraZ());
nearestBlock = pickSlice.getNearest(Game.instance.camera);
}
public void render(GLAutoDrawable drawable) {
if (!Game.instance.isRunning()) {
// not running, just clear screen
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glClearColor(49f / 255f, 49f / 255f, 49f / 255f, 1);
return;
}
init(drawable);
float[] clearColor = time.getClearColor();
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glClearColor(clearColor[0], clearColor[1], clearColor[2], 1);
// gl.glClearColor(49f / 255f, 119f / 255f, 243f / 255f, 1);
// GOING 3D
gl.glPushMatrix();
gl.glPushAttrib(GL2.GL_ALL_ATTRIB_BITS);
Game.instance.camera.setUp(drawable);
// RENDER SUN
gl.glPushAttrib(GL2.GL_ALL_ATTRIB_BITS);
gl.glEnable(GL2.GL_BLEND); // Enable Blending
gl.glBlendFunc(GL2.GL_SRC_ALPHA, GL2.GL_ONE); // Set Blending Mode To
// Mix Based On SRC
// Alpha
GLUquadric sun = glu.gluNewQuadric();
glu.gluQuadricDrawStyle(sun, GLU.GLU_FILL);
glu.gluQuadricNormals(sun, GLU.GLU_SMOOTH);
gl.glPushMatrix();
gl.glTranslatef(time.sun.getX() + player.getCameraX(), time.sun.getY(), time.sun.getZ() + player.getCameraZ());
gl.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_FILL);
gl.glColor4f(1, 1, 1, 0.02f);
glu.gluSphere(sun, 100f, 10, 10);
glu.gluSphere(sun, 90f, 10, 10);
glu.gluSphere(sun, 80f, 10, 10);
glu.gluSphere(sun, 70f, 10, 10);
glu.gluSphere(sun, 60f, 10, 10);
glu.gluSphere(sun, 50f, 10, 10);
glu.gluSphere(sun, 40f, 10, 10);
glu.gluSphere(sun, 35f, 10, 10);
gl.glColor4f(1, 1, 1, 1f);
glu.gluSphere(sun, 30f, 10, 10);
gl.glPopMatrix();
gl.glPopAttrib();
viewSlice.setCenter((int) Game.instance.camera.getX(), (int) Game.instance.camera.getY(),
(int) Game.instance.camera.getZ());
// RENDER BLOCKS
gl.glPopAttrib();
gl.glPushAttrib(GL2.GL_ALL_ATTRIB_BITS);
gl.glColor4f(0f, 1f, 0, 1);
gl.glEnable(GL2.GL_DEPTH_TEST);
gl.glShadeModel(GL2.GL_FLAT);
gl.glCullFace(GL2.GL_BACK);
// gl.glEnable(GL2.GL_FOG);
// gl.glFogf(GL2.GL_FOG_MODE, GL2.GL_LINEAR);
// gl.glFogf(GL2.GL_FOG_MODE, GL2.GL_EXP);
// gl.glFogf(GL2.GL_FOG_START, renderDistance / 2 - renderDistance /
// 10);
// gl.glFogf(GL2.GL_FOG_END, renderDistance / 2);
// gl.glFogf(GL2.GL_FOG_DENSITY, 0.002f);
// new float[] { 49f / 255f, 119f / 255f, 243f / 255f }
// gl.glFogfv(GL2.GL_FOG_COLOR, new float[] { 1, 1, 1, 0.2f }, 0);
blockRenderer.render(drawable);
gl.glPopAttrib();
// RENDER ANYTHING ELSE
gl.glPushAttrib(GL2.GL_ALL_ATTRIB_BITS);
GLUquadric qobj0 = glu.gluNewQuadric();
glu.gluQuadricDrawStyle(qobj0, GLU.GLU_FILL);
glu.gluQuadricNormals(qobj0, GLU.GLU_SMOOTH);
for (LiveEntity entity : liveEntities.values()) {
gl.glPushMatrix();
gl.glTranslatef(entity.getX(), entity.getCameraY(), entity.getZ());
gl.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_LINE);
glu.gluSphere(qobj0, 0.1f, 10, 10);
gl.glPopMatrix();
}
gl.glPopAttrib();
// bullets.render(drawable);
gl.glPopMatrix();
// testObject.render();
// GOIND 2D
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
int width = Game.Display.getWidth();
int height = Game.Display.getHeight();
glu.gluOrtho2D(0, width, height, 0);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glPushMatrix();
// renderHUD();
// MAP++
gl.glPushAttrib(GL2.GL_ALL_ATTRIB_BITS);
gl.glPolygonMode(GL2.GL_FRONT_AND_BACK, GL2.GL_FILL);
int msz = 100;
gl.glColor4f(0.3f, 0.3f, 0.3f, 0.7f);
gl.glRectf(width - msz - 12, 8, width - 8, msz + 12);
gl.glColor4f(0.9f, 0.9f, 0.9f, 1);
gl.glRectf(width - msz - 10, 10, width - 10, msz + 10);
gl.glColor4f(0.0f, 0.0f, 0.0f, 0.9f);
/*
* for (int mx = 0; mx < msz; mx++) {
* for (int mz = 0; mz < msz; mz++) {
* float h = (float) viewSlice
* .getHighest((int) (mx - msz / 2 + player.getX()), (int) (mz - msz / 2
* + player.getZ()));
* gl.glColor4f(h / 128, h / 128, h / 128, 1f);
* gl.glRectf(width - msz - 10 + mx, 10 + mz, width - msz - 10 + mx + 1,
* 10 + mz + 1);
* }
* }
*/
// MAP--
// crosshair
gl.glColor4f(1f, 1f, 1f, 0.7f);
gl.glRectf(width / 2 - 1, height / 2 - 10, width / 2 + 1, height / 2 + 10); // vertical
gl.glRectf(width / 2 - 10, height / 2 - 1, width / 2 + 10, height / 2 + 1); // horizontal
// inventoryprivate PMVMatrix matrix;
if (null != inventoryRenderer) {
// inventoryRenderer.render(drawable);
}
viewport.drawText("avg fps: " + (int) Game.timer.getAvgFps(), 10, height - 20);
viewport.drawText("fps: " + (int) Game.timer.getFps(), 10, height - 35);
MemoryUsage heap = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
viewport.drawText("mem: " + (heap.getUsed() / (1024 * 1024)) + "/" + (heap.getMax() / (1024 * 1024)), 10,
height - 50);
viewport.drawText("y: " + Game.instance.player.getY(), width - msz - 10, height - msz - 20);
viewport.drawText("y: " + Game.instance.player.getCameraY() + " (cam)", width - msz - 10, height - msz - 30);
viewport.drawText("x: " + Game.instance.player.getX(), width - msz - 10, height - msz - 40);
viewport.drawText("z: " + Game.instance.player.getZ(), width - msz - 10, height - msz - 50);
viewport.drawText("players: " + liveEntities.size(), width - msz - 10, height - msz - 70);
viewport.drawText("bullets: " + getBulletsCount(), width - msz - 10, height - msz - 95);
viewport.drawText("y velocity: " + player.velocity.y + " y accel: " + player.acceleration.y + " inJump: "
+ player.inJump + " onGround: " + player.onGround, width - msz - 350 - 10, height - msz - 110);
viewport.drawText("rdistance: " + renderDistance, width - msz - 10, height - msz - 155);
ChunkSlice cs = viewSlice.getChunkSlice();
viewport.drawText("slice x: " + cs.getX() + ".." + (cs.getX() + cs.getWidth() - 1) + " y: " + cs.getY() + ".."
+ (cs.getY() + cs.getHeight() - 1) + " z: " + cs.getZ() + ".." + (cs.getZ() + cs.getDepth() - 1), width
- msz * 2 - 10, height - msz - 170);
viewport.drawText("time: " + time.getDateTimeString(), width - msz * 2 - 10, height - msz - 185);
if (nearestBlock != null) {
viewport.drawText("block: " + nearestBlock.getX() + ":" + nearestBlock.getY() + ":" + nearestBlock.getZ()
+ " d " + nearestBlock.getDistance(Game.instance.camera), width - msz * 2 - 10, height - msz - 200);
}
gl.glPopAttrib();
gl.glPopMatrix();
gl.glFlush();
}
public Player getPlayer() {
return player;
}
}
|