Hi,
ich habe da ein kleines Problem mit dem ich mich gerade sehr rumschlage.
Undzwar versuche ich meinen Würfel zu generieren (Verschiedene Typen) und er kann das Programm nicht starten weil:
So und die beiden Codes:
CubeTerrain:
Die Stelle die ich meine ist bei ca. Zeile 190
DefaultCube:
MfG:
Florilu
ich habe da ein kleines Problem mit dem ich mich gerade sehr rumschlage.
Undzwar versuche ich meinen Würfel zu generieren (Verschiedene Typen) und er kann das Programm nicht starten weil:
Code:
The constructor DefaultCube(Vector3f, Vector3f, Vector4f, Texture) is undefined
So und die beiden Codes:
CubeTerrain:
Java:
package src.Game;
import java.util.Random;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;
import Methods.Cubes.DefaultCube;
public class CubeTerrain {
// Size of the terrain measured in cubes
public Vector3 arraySize;
// Size of each cube
public Vector3f cubeSize;
// Optional translation
private Vector3f translation;
// The 3d array containing the cubes
public DefaultCube[][][] terrain;
// Textures
private Texture stoneTexture;
private Texture grassTexture;
private Texture waterTexture;
private Texture dirtTexture;
// Display list
private int displayList;
private TextureStore textureStore;
public CubeTerrain(Vector3 arraySize, Vector3f cubeSize, Vector3f translation, TextureStore textureStore) {
this.arraySize = arraySize;
this.cubeSize = cubeSize;
this.translation = translation;
this.textureStore = textureStore;
// Create the cube array
terrain = new DefaultCube[arraySize.x][arraySize.y][arraySize.z];
for(int z = 0; z < arraySize.z; z++) {
for(int y = 0; y < arraySize.y; y++) {
for(int x = 0; x < arraySize.x; x++) {
terrain[x][y][z] = null;
}
}
}
stoneTexture = textureStore.getTexture("res/stone.png");
grassTexture = textureStore.getTexture("res/grass.png");
waterTexture = textureStore.getTexture("res/water.png");
dirtTexture = textureStore.getTexture("res/dirt.png");
}
public void generateTerrain(int maxHeight, int minHeight, int smoothLevel, int seed, float noiseSize, float persistence, int octaves, boolean textures) {
// Stores the height of each x, z coordinate
int heightData[][] = new int[arraySize.x][arraySize.z];
// Make sure maxHeight and minHeight are within bounds of the cube array
if(maxHeight > arraySize.y)
maxHeight = arraySize.y;
if(maxHeight < 0)
maxHeight = 0;
if(minHeight > arraySize.y)
minHeight = arraySize.y;
if(minHeight < 0)
minHeight = 0;
// Randomize the heights using Perlin noise
for(int z = 0; z < arraySize.z; z++) {
for(int x = 0; x < arraySize.x; x++) {
heightData[x][z] = (int) (PerlinNoise2D.perlin2D(x, z, arraySize.x, arraySize.z, seed, 100.0f, 0.0001f, octaves) * (maxHeight - minHeight) + minHeight);
}
}
// Smoothen the terrain
while(smoothLevel > 0) {
for(int z = 0; z < arraySize.z; z += 1) {
for(int x = 0; x < arraySize.x; x += 1) {
float totalHeight = 0.0f;
float count = 0;
if(z > 0) {
totalHeight += heightData[x][z - 1];
count++;
}
if(z < arraySize.z - 1) {
totalHeight += heightData[x][z + 1];
count++;
}
if(x > 0) {
totalHeight += heightData[x - 1][z];
count++;
}
if(x < arraySize.x - 1) {
totalHeight += heightData[x + 1][z];
count++;
}
heightData[x][z] = Math.round(totalHeight / count);
}
}
smoothLevel--;
}
// Create the cubes
for(int z = 0; z < arraySize.z; z++) {
for(int x = 0; x < arraySize.x; x++) {
for(int y = heightData[x][z]; y >= 0; y--) {
terrain[x][y][z] = createCube(new Vector3(x, y, z), textures);
}
}
}
Random rand = new Random();
// Calculate which sides each cube needs to render
for(int z = 0; z < arraySize.z; z++) {
for(int x = 0; x < arraySize.x; x++) {
for(int y = heightData[x][z]; y >= 0; y--) {
boolean renderTop = (y == heightData[x][z]) || (y == 0);
boolean renderBottom = (y == 0) || (y == 3);
boolean renderFront = (z == arraySize.z - 1) || (terrain[x][y][z + 1] == null);
boolean renderBack = (z == 0) || (terrain[x][y][z - 1] == null);
boolean renderRight = (x == arraySize.x - 1) || (terrain[x + 1][y][z] == null);
boolean renderLeft = (x == 0) || (terrain[x - 1][y][z] == null);
terrain[x][y][z].setVisibleSides(renderTop, renderBottom, renderFront, renderBack, renderRight, renderLeft);
}
}
}
// Create the display list
displayList = GL11.glGenLists(1);
GL11.glNewList(displayList, GL11.GL_COMPILE);
for(int z = 0; z < arraySize.z; z++) {
for(int x = 0; x < arraySize.x; x++) {
for(int y = 0; y < arraySize.y; y++) {
if(terrain[x][y][z] != null)
terrain[x][y][z].render();
}
}
}
GL11.glEndList();
}
private DefaultCube createCube(Vector3 arrayPosition, boolean textures) {
// Calculate the coordinates
Vector3f pos1 = new Vector3f(arrayPosition.x * cubeSize.x, arrayPosition.y * cubeSize.y, arrayPosition.z * cubeSize.z);
Vector3f pos2 = Vector3f.add(pos1, cubeSize);
// Set texture depending on y
Vector4f color = null;
Texture texture = null;
if(arrayPosition.y == 0) {
// Dirt
color = new Vector4f(0.35f, 0.15f, 0.0f, 1.0f);
texture = dirtTexture;
} else if(arrayPosition.y < 3) {
// Water
color = new Vector4f(0.0f, 0.2f, 0.7f, 0.6f);
texture = waterTexture;
} else if(arrayPosition.y < 6) {
// Grass
color = new Vector4f(0.2f, 0.4f, 0.1f, 1.0f);
texture = grassTexture;
} else {
// Stone
color = new Vector4f(0.3f, 0.3f, 0.3f, 1.0f);
texture = stoneTexture;
}
if(!textures)
texture = null;
return new DefaultCube(pos1, pos2, color, texture);
}
public void render() {
// Save the current matrix
GL11.glPushMatrix();
// Add the translation matrix
GL11.glTranslatef(translation.x, translation.y, translation.z);
// Call the display list
GL11.glCallList(displayList);
// Restore the matrix
GL11.glPopMatrix();
}
/* Returns true if there is a solid cube at the given coordinates. */
public boolean solidAt(Vector3f coordinates) {
// Get the cube coordinates in the array
Vector3 arrayCoordinates = new Vector3((int)((coordinates.x - translation.x) / cubeSize.x), (int)((coordinates.y - translation.y) / cubeSize.y), (int)((coordinates.z - translation.z) / cubeSize.z));
// Is this within the array bounds?
if(arrayCoordinates.x >= 0 && arrayCoordinates.x < arraySize.x &&
arrayCoordinates.y >= 0 && arrayCoordinates.y < arraySize.y &&
arrayCoordinates.z >= 0 && arrayCoordinates.z < arraySize.z) {
// Is there a cube at this coordinate?
if(terrain[arrayCoordinates.x][arrayCoordinates.y][arrayCoordinates.z] != null) {
return true;
}
}
return false;
}
public void release() {
GL11.glDeleteLists(displayList, 1);
}
}
Die Stelle die ich meine ist bei ca. Zeile 190
DefaultCube:
Java:
package Methods.Cubes;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Vector3f;
import org.lwjgl.util.vector.Vector4f;
import org.newdawn.slick.opengl.Texture;
public class DefaultCube {
/* pos1 contains the lowest x, y, z. pos2 contains the heighest x, y, z */
public Vector3f pos1, pos2;
// Color to use if no texture is present
public Vector4f color;
// Texture class from Slick-Util library
public Texture texture;
// Determines which sides to draw
protected boolean renderTop, renderBottom, renderFront, renderBack, renderRight, renderLeft;
public DefaultCube(Vector3f pos1, Vector3f pos2, Vector4f color, Texture texture) {
this.pos1 = pos1;
this.pos2 = pos2;
this.color = color;
this.texture = texture;
// Default is to draw all sides
this.renderTop = true;
this.renderBottom = true;
this.renderFront = true;
this.renderBack = true;
this.renderRight = true;
this.renderLeft = true;
}
/* Sets information about which sides to draw. */
public void setVisibleSides(boolean drawTop, boolean drawBottom, boolean drawFront, boolean drawBack, boolean drawRight, boolean drawLeft) {
this.renderTop = drawTop;
this.renderBottom = drawBottom;
this.renderFront = drawFront;
this.renderBack = drawBack;
this.renderRight = drawRight;
this.renderLeft = drawLeft;
}
/* Renders the cube. */
public void render() {
if(texture != null) {
// Set the texture
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureID());
} else {
// Set the color
GL11.glColorMaterial(GL11.GL_FRONT_AND_BACK, GL11.GL_AMBIENT_AND_DIFFUSE);
GL11.glEnable(GL11.GL_COLOR_MATERIAL);
GL11.glColor4f(color.x, color.y, color.z, color.w);
}
GL11.glBegin(GL11.GL_QUADS);
// Top
if(renderTop) {
GL11.glNormal3f(1.0f, 1.0f, -1.0f);
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(pos2.x, pos2.y, pos1.z);
GL11.glNormal3f(-1.0f, 1.0f, -1.0f);
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(pos1.x, pos2.y, pos1.z);
GL11.glNormal3f(-1.0f, 1.0f, 1.0f);
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(pos1.x, pos2.y, pos2.z);
GL11.glNormal3f(1.0f, 1.0f, 1.0f);
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(pos2.x, pos2.y, pos2.z);
}
// Bottom
if(renderBottom) {
GL11.glNormal3f(1.0f, -1.0f, 1.0f);
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(pos2.x, pos1.y, pos2.z);
GL11.glNormal3f(-1.0f, -1.0f, 1.0f);
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(pos1.x, pos1.y, pos2.z);
GL11.glNormal3f(-1.0f, -1.0f, -1.0f);
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(pos1.x, pos1.y, pos1.z);
GL11.glNormal3f(1.0f, -1.0f, -1.0f);
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(pos2.x, pos1.y, pos1.z);
}
// Front
if(renderFront) {
GL11.glNormal3f(1.0f, 1.0f, 1.0f);
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(pos2.x, pos2.y, pos2.z);
GL11.glNormal3f(-1.0f, 1.0f, 1.0f);
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(pos1.x, pos2.y, pos2.z);
GL11.glNormal3f(-1.0f, -1.0f, 1.0f);
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(pos1.x, pos1.y, pos2.z);
GL11.glNormal3f(1.0f, -1.0f, 1.0f);
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(pos2.x, pos1.y, pos2.z);
}
// Back
if(renderBack) {
GL11.glNormal3f(-1.0f, 1.0f, -1.0f);
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(pos1.x, pos2.y, pos1.z);
GL11.glNormal3f(1.0f, 1.0f, -1.0f);
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(pos2.x, pos2.y, pos1.z);
GL11.glNormal3f(1.0f, -1.0f, -1.0f);
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(pos2.x, pos1.y, pos1.z);
GL11.glNormal3f(-1.0f, -1.0f, -1.0f);
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(pos1.x, pos1.y, pos1.z);
}
// Right
if(renderRight) {
GL11.glNormal3f(1.0f, 1.0f, -1.0f);
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(pos2.x, pos2.y, pos1.z);
GL11.glNormal3f(1.0f, 1.0f, 1.0f);
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(pos2.x, pos2.y, pos2.z);
GL11.glNormal3f(1.0f, -1.0f, 1.0f);
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(pos2.x, pos1.y, pos2.z);
GL11.glNormal3f(1.0f, -1.0f, -1.0f);
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(pos2.x, pos1.y, pos1.z);
}
// Left
if(renderLeft) {
GL11.glNormal3f(-1.0f, 1.0f, 1.0f);
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(pos1.x, pos2.y, pos2.z);
GL11.glNormal3f(-1.0f, 1.0f, -1.0f);
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(pos1.x, pos2.y, pos1.z);
GL11.glNormal3f(-1.0f, -1.0f, -1.0f);
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(pos1.x, pos1.y, pos1.z);
GL11.glNormal3f(-1.0f, -1.0f, 1.0f);
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(pos1.x, pos1.y, pos2.z);
}
GL11.glEnd();
// Reset color if color was used
GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
GL11.glDisable(GL11.GL_TEXTURE_2D);
GL11.glDisable(GL11.GL_COLOR_MATERIAL);
}
}
MfG:
Florilu