Java LWJGL Heightmap rendering - java

Hello I wan't to do heightmap based game and I have optimization problem. I acctualy rendering in DisplayList but when I want to render heightmap 512x512 I have only 20-30 fps. I tried something in vbo but that don't work. Here is my sourcecode to display lists rendering:
public class Heightmap {
public static final Vector2f CHUNK_XZ = new Vector2f(256, 256);
public int CHUNK_ID;
private float[][] chunk_data = null;
private Vector3f[][] chunk_color_data = null;
private Vector3f pos;
private Vector3f scale = new Vector3f(10, 10, 10);
private boolean visible = true;
private int displayID;
private Texture texture;
public Heightmap(Vector3f pos) {
this.pos = pos;
chunk_data = new float[(int) CHUNK_XZ.x][(int) CHUNK_XZ.y];
chunk_color_data = new Vector3f[(int) CHUNK_XZ.x][(int) CHUNK_XZ.y];
for(int x = 0; x < chunk_data.length; x++) {
for(int y = 0; y < chunk_data[0].length; y++) {
chunk_data[x][y] = 1.0f;
chunk_color_data[x][y] = new Vector3f(0f, 0f, (float) Math.random());
}
}
try {
texture = TextureLoader.getTexture("jpg", new FileInputStream(new File("grassy.jpg")));
} catch (IOException e) {
e.printStackTrace();
}
displayID = glGenLists(1);
GL11.glNewList(displayID, GL11.GL_COMPILE);
texture.bind();
GL11.glPushMatrix();
GL11.glScalef(scale.x, scale.y, scale.z);
GL11.glBegin(GL11.GL_QUADS);
int heightmapExaggeration = 10;
for(int x=0; x < chunk_data.length; x++) {
for(int y=0; y < chunk_data[x].length; y++) {
GL11.glColor3f(chunk_color_data[x][y].getX(), chunk_color_data[x][y].getY(), chunk_color_data[x][y].getZ());
GL11.glTexCoord2f(0, 0);
GL11.glVertex3f(x*0.25f, getHeightAt(x, y)*heightmapExaggeration, y*0.25f);
GL11.glTexCoord2f(1, 0);
GL11.glVertex3f((x+1)*0.25f, getHeightAt(x+1, y)*heightmapExaggeration, y*0.25f);
GL11.glTexCoord2f(1, 1);
GL11.glVertex3f((x+1)*0.25f, getHeightAt(x+1, y+1)*heightmapExaggeration, (y+1)*0.25f);
GL11.glTexCoord2f(0, 1);
GL11.glVertex3f(x*0.25f, getHeightAt(x, y+1)*heightmapExaggeration, (y+1)*0.25f);
}
}
GL11.glEnd();
GL11.glPopMatrix();
texture.release();
GL11.glEndList();
}
public Chunk(float[][] chunk_data, Vector3f[][] chunk_color_data, Vector3f pos) {
this.chunk_data = chunk_data;
this.chunk_color_data = chunk_color_data;
this.pos = pos;
try {
texture = TextureLoader.getTexture("jpg", new FileInputStream(new File("grassy.jpg")));
} catch (IOException e) {
e.printStackTrace();
}
displayID = glGenLists(1);
GL11.glNewList(displayID, GL11.GL_COMPILE);
texture.bind();
GL11.glPushMatrix();
GL11.glScalef(scale.x, scale.y, scale.z);
GL11.glBegin(GL11.GL_QUADS);
int heightmapExaggeration = 10;
for(int x=0; x < chunk_data.length; x++){
for(int y=0; y < chunk_data[x].length; y++){
GL11.glColor3f(chunk_color_data[x][y].getX(), chunk_color_data[x][y].getY(), chunk_color_data[x][y].getZ());
GL11.glTexCoord2f(0, 0);
GL11.glVertex3f(x*0.25f, getHeightAt(x, y)*heightmapExaggeration, y*0.25f);
GL11.glTexCoord2f(1, 0);
GL11.glVertex3f((x+1)*0.25f, getHeightAt(x+1, y)*heightmapExaggeration, y*0.25f);
GL11.glTexCoord2f(1, 1);
GL11.glVertex3f((x+1)*0.25f, getHeightAt(x+1, y+1)*heightmapExaggeration, (y+1)*0.25f);
GL11.glTexCoord2f(0, 1);
GL11.glVertex3f(x*0.25f, getHeightAt(x, y+1)*heightmapExaggeration, (y+1)*0.25f);
}
}
GL11.glEnd();
GL11.glPopMatrix();
texture.release();
GL11.glEndList();
}
public float getHeightAt(int x, int z){
try {
return chunk_data[x][z];
} catch(Exception e) {
return 0;
}
}
public void render() {
glPushMatrix();
glTranslatef(pos.x, pos.y, pos.z);
glCallList(displayID);
glPopMatrix();
}
public void setChunkDataAtCoords(int x, int y, float value) {
this.chunk_data[x][y] = value;
}
public float[][] getChunk_data() {
return chunk_data;
}
public Vector3f[][] getChunk_color_data() {
return chunk_color_data;
}
public Vector3f getPos() {
return pos;
}
public Vector3f getScale() {
return scale;
}
public void setChunk_data(float[][] chunk_data) {
this.chunk_data = chunk_data;
}
public void setChunk_color_data(Vector3f[][] chunk_color_data) {
this.chunk_color_data = chunk_color_data;
}
public void setChunkColorDataAtCoords(int x, int y, Vector3f color) {
chunk_color_data[x][y] = color;
}
public void setPos(Vector3f pos) {
this.pos = pos;
}
public void setScale(Vector3f scale) {
this.scale = scale;
}
public boolean isVisible() {
return visible;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
}
And vbo rendering code but that don't work properly:
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import javax.imageio.ImageIO;
import org.lwjgl.BufferUtils;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
public class Terrain {
private int width = 200;
private int height = 200;
private int verticies = (3*(2*height)*width);
private int indicies = ((height*2)*width);
private FloatBuffer vBuffer = BufferUtils.createFloatBuffer(verticies);
private IntBuffer iBuffer = BufferUtils.createIntBuffer(indicies);
private IntBuffer ib = BufferUtils.createIntBuffer(indicies);
private static float[][] data;
public Terrain() {
glPushMatrix();
glScalef(0.6f, 0.6f, 0.6f);
glTranslatef(0f, 10f, 0f);
loadHeightImage();
loadHeightVerticies();
drawHeightMap();
glPopMatrix();
}
public void loadHeightImage() {
try {
BufferedImage heightmapImage = ImageIO.read(new File("height.jpg"));
data = new float[heightmapImage.getWidth()][heightmapImage.getHeight()];
Color colour;
for (int x = 0; x < data.length; x++)
{
for (int z = 0; z < data[x].length; z++)
{
colour = new Color(heightmapImage.getRGB(x, z));
data[z][x] = colour.getRed();
}
}
} catch (IOException e){
e.printStackTrace();
}
}
public void loadHeightVerticies() {
for (int z = 0; z < this.data.length - 1; z++) {
for (int x = 0; x < this.data[z].length; x++) {
vBuffer.put(x).put(this.data[z][x]).put(z);
vBuffer.put(x).put(this.data[z + 1][x]).put(z + 1);
}
}
this.vBuffer.flip();
for (int n = 0; n < indicies; n++) {
this.iBuffer.put( n);
}
this.iBuffer.flip();
}
public void drawHeightMap() {
glGenBuffers(ib);
int vHandle = ib.get(0);
int iHandle = ib.get(1);
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vHandle);
glBufferData(GL_ARRAY_BUFFER, vBuffer, GL_STATIC_DRAW);
glVertexPointer(3, GL_FLOAT, 0, 0L);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iHandle);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, iBuffer, GL_STATIC_DRAW);
for (int x = 0; x < 400; x++) {
glDrawElements(GL_TRIANGLE_STRIP, 400, GL_UNSIGNED_SHORT, 1600 * x);
}
glDisableClientState(GL_VERTEX_ARRAY);
ib.put(0, vHandle);
glDeleteBuffers(ib);
}
}
And one more question how can I color heightmap like in the displaylist mode ? Sorry for my English.

How many chunks are you rendering? Maybe you should look at face culling if you don't need all sides of the quads.

Related

OpenGL rendering order (EXCEPTION_ACCESS_VIOLATION)

I am trying to render a 2D square, but my program crashes. I think my order of methods is incorrect. I am using LWJGL3, with the jars Lwjgl, GLFW, and OpenGL. The stacktrace shows the error is an EXCEPTION_ACCESS_VIOLATION, and the error is at the first openGL function depending on which one is first (with current code it is GL20.glGetAttribLocation() (following the guid here: https://github.com/SilverTiger/lwjgl3-tutorial/wiki/Rendering)
I am also not using legacyGL
If your having this problem:
add the lines GLFW.makeContextCurrent(window) and Gl.createCapabilities()
Main:
public class DungeonRunners {
private double lastTick;
private float timeCount;
private int fps;
private int fpsCount;
private int ups;
private int upsCount;
public static void main(String[] args) {
System.out.println("LWJGL Version: " + Version.getVersion());
int width = 1240;
int height = 720;
boolean legacyGL = false;
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if(arg.equals("width:")) {
try {
width = Integer.parseInt(args[i+1]);
i++;
} catch (NumberFormatException ignored) { }
} else if(arg.equals("height:")) {
try {
height = Integer.parseInt(args[i+1]);
} catch (NumberFormatException ignored) { }
} else if(arg.equals("-useLegacyGL")) {
legacyGL = true;
}
}
DungeonRunners dungeonRunners = new DungeonRunners();
dungeonRunners.start(width, height, legacyGL);
}
private void start(int width, int height, Boolean legacyGL) {
long window;
GLFW.glfwInit();
if(!legacyGL) {
GLFW.glfwDefaultWindowHints();
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 3);
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 2);
GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE);
GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT, GLFW.GLFW_TRUE);
window = GLFW.glfwCreateWindow(width, height, "Dungeon Runners", MemoryUtil.NULL, MemoryUtil.NULL);
} else {
GLFW.glfwDefaultWindowHints();
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 2);
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 1);
window = GLFW.glfwCreateWindow(width, height, "Dungeon Runners", MemoryUtil.NULL, MemoryUtil.NULL);
}
RenderEngine engine = new RenderEngine(legacyGL);
while(!GLFW.glfwWindowShouldClose(window)) {
update();
engine.frame();
}
}
private void update() {
if (timeCount > 1f) {
fps = fpsCount;
fpsCount = 0;
ups = upsCount;
upsCount = 0;
timeCount -= 1f;
}
}
private float getDelta() {
double time = getTime();
float delta = (float) (time - lastTick);
lastTick = time;
timeCount += delta;
return delta;
}
public double getTime() {
return System.nanoTime() / 1000000000.0;
}
private void updateFPS() {
fpsCount++;
}
private void updateUPS() {
upsCount++;
}
}
The code is pretty explanatory, here is my RenderEngine class:
public class RenderEngine {
private int shaderProgram;
public RenderEngine(Boolean legacyGL) {
setVertexAttribs();
bindImage();
int vao = GL30.glGenVertexArrays();
GL30.glBindVertexArray(vao);
setupShaders(legacyGL);
GL20.glUseProgram(shaderProgram);
setUniformVars();
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 3);
}
private void bindImage() {
try (MemoryStack stack = MemoryStack.stackPush()) {
FloatBuffer vertices = stack.mallocFloat(3 * 6);
vertices.put(-0.6f).put(-0.4f).put(0f).put(1f).put(0f).put(0f);
vertices.put(0.6f).put(-0.4f).put(0f).put(0f).put(1f).put(0f);
vertices.put(0f).put(0.6f).put(0f).put(0f).put(0f).put(1f);
vertices.flip();
int vbo = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertices, GL15.GL_STATIC_DRAW);
MemoryStack.stackPop();
}
}
public void frame() {
}
private void setUniformVars() {
int uniModel = GL20.glGetUniformLocation(shaderProgram, "model");
UtilMatrix4f model = new UtilMatrix4f();
GL20.glUniformMatrix4fv(uniModel, false, model.getBuffer());
int uniView = GL20.glGetUniformLocation(shaderProgram, "view");
UtilMatrix4f view = new UtilMatrix4f();
GL20.glUniformMatrix4fv(uniView, false, view.getBuffer());
int uniProjection = GL20.glGetUniformLocation(shaderProgram, "projection");
float ratio = 640f / 480f;
UtilMatrix4f projection = UtilMatrix4f.orthographic(-ratio, ratio, -1f, 1f, -1f, 1f);
GL20.glUniformMatrix4fv(uniProjection, false, projection.getBuffer());
}
private void setVertexAttribs() {
int floatSize = 4;
int posAttrib = GL20.glGetAttribLocation(shaderProgram, "position");
GL20.glEnableVertexAttribArray(posAttrib);
GL20.glVertexAttribPointer(posAttrib, 3, GL11.GL_FLOAT, false, 6 * floatSize, 0);
int colAttrib = GL20.glGetAttribLocation(shaderProgram, "color");
GL20.glEnableVertexAttribArray(colAttrib);
GL20.glVertexAttribPointer(colAttrib, 3, GL11.GL_FLOAT, false, 6 * floatSize, 3 * floatSize);
}
private void setupShaders(boolean legacyGL) {
int vertexShader = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
if(legacyGL)
GL20.glShaderSource(vertexShader, "src\\main\\java\\bigbade\\dungeonrunners\\renderer\\shaders\\legacyVertexShader.txt");
else
GL20.glShaderSource(vertexShader, "src\\main\\java\\bigbade\\dungeonrunners\\renderer\\shaders\\vertexShader.txt");
GL20.glCompileShader(vertexShader);
int status = GL20.glGetShaderi(vertexShader, GL20.GL_COMPILE_STATUS);
if (status != GL11.GL_TRUE) {
throw new RuntimeException(GL20.glGetShaderInfoLog(vertexShader));
}
int fragmentShader = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
if(legacyGL)
GL20.glShaderSource(vertexShader, "src\\main\\java\\bigbade\\dungeonrunners\\renderer\\shaders\\legacyFragmentShader.txt");
else
GL20.glShaderSource(fragmentShader, "src\\main\\java\\bigbade\\dungeonrunners\\renderer\\shaders\\fragmentShader.txt");
GL20.glCompileShader(fragmentShader);
status = GL20.glGetShaderi(fragmentShader, GL20.GL_COMPILE_STATUS);
if (status != GL11.GL_TRUE) {
throw new RuntimeException(GL20.glGetShaderInfoLog(fragmentShader));
}
shaderProgram = GL20.glCreateProgram();
GL20.glAttachShader(shaderProgram, vertexShader);
GL20.glAttachShader(shaderProgram, fragmentShader);
GL30.glBindFragDataLocation(shaderProgram, 0, "fragColor");
GL20.glLinkProgram(shaderProgram);
status = GL20.glGetProgrami(shaderProgram, GL20.GL_LINK_STATUS);
if (status != GL11.GL_TRUE) {
throw new RuntimeException(GL20.glGetProgramInfoLog(shaderProgram));
}
}
}

Unfreezing colliding rectangles?

I need help with a project. For this project we have created moving rectangles that "freeze" when clicked on. We are also supposed to make frozen rectangles unfrozen if they collide with another. I have gotten the first step done but I can't seem to get the rectangles to unfreeze if they collide. I apologize if this doesn't make sense. I am new to Java and stack overflow. Here is my class code.
import edu.princeton.cs.introcs.StdDraw;
import java.util.Random;
import java.awt.Color;
public class MovingRectangle {
//canvas = canvasSize;
public int width; //width of rectangle
public int height; //height of rectangle
public double xVelocity; //x velocity
public double yVelocity; //y velocity
public int xPosition; //x starting position
public int yPosition; //y starting position
private boolean isFrozen; //the rectangle is still moving
// private int R, G, B;
Random rng = new Random();
public int R = rng.nextInt(256);
public int G = rng.nextInt(256);
public int B = rng.nextInt(256); //RGB variables
public int canvas;
public Color c;//canvas size
public MovingRectangle(int x, int y, int w, int h, double xv, double yv, int canvasSize) {
xPosition = x;
yPosition = y;
width = w;
height = h;
xVelocity = xv;
yVelocity = yv;
canvas = canvasSize;
isFrozen = false;
c =new Color(R,G,B);
}
//draws rectangle
public void draw() {
// Random rng = new Random();
// R = rng.nextInt(256);
// G = rng.nextInt(256);
// B = rng.nextInt(256);
// c = new Color(R,G,B);
StdDraw.setPenColor(c);
StdDraw.filledRectangle(xPosition, yPosition, width/2, height/2);
}
//pick a new random color
// public void color() {
// Random rng = new Random();
//
// R = rng.nextInt(256);
// G = rng.nextInt(256);
// B = rng.nextInt(256);
// c = new Color(R,G,B);
// }
//updates position every frame
public void move() {
if (isFrozen == false) {
xPosition += xVelocity;
yPosition += yVelocity;
// xPosition += 0;
// yPosition += 0;
//Check for hitting sides of canvas
if (xPosition > canvas) {
xVelocity *= -1;
//color();
}
if( xPosition < 0) {
xVelocity *= -1;
//color();
}
if (yPosition > canvas ) {
yVelocity *= -1;
//color();
}
if(yPosition <= 0) {
yVelocity*=-1;
//color();
}
}
}
public boolean ifColliding(MovingRectangle rectangle) {
int rightedge = rectangle.xPosition + rectangle.width/2;
int leftedge = rectangle.xPosition - rectangle.width/2;
int topedge = rectangle.yPosition + rectangle.height/2;
int bottomedge = rectangle.yPosition - height/2;
int rightedge2 = this.xPosition + this.width/2;
int leftedge2 = this.xPosition - this.width/2;
int topedge2 = this.yPosition + this.height/2;
int bottomedge2 = this.yPosition - this.height/2;
if(leftedge<rightedge2 && leftedge2<rightedge && topedge>bottomedge2 && bottomedge<topedge2) {
return true;
}
else {
return false;
}
}
public void freeze() {
if(StdDraw.mousePressed() && StdDraw.mouseX() <= xPosition+width && StdDraw.mouseX() >= xPosition-width && StdDraw.mouseY() <=yPosition+height &&StdDraw.mouseY() >= yPosition-height) {
xVelocity=0;
yVelocity=0;
c= new Color(100,0,0);
isFrozen= true;
}
}
public boolean isFrozen() {
return isFrozen;
}
public void winState() {
StdDraw.clear();
}
public void setFrozen(boolean val) {
isFrozen = false;
if (isFrozen == false) {
xPosition += xVelocity;
yPosition += yVelocity;
}
}
// public void stop() {
// isMoving = false;
// }
}
And my driver.
import edu.princeton.cs.introcs.StdDraw;
import java.util.Random;
import java.util.Scanner;
import java.awt.Color.*;
import edu.princeton.cs.introcs.StdDraw;
public class RectangleDriver {
public static final int canvas = 500;
public static void main(String[] args) {
//set canvas size
StdDraw.setCanvasSize(canvas,canvas);
StdDraw.setXscale(canvas, 0);
StdDraw.setYscale(0, canvas);
//assign variables
Random rng = new Random();
//make a rectangle
MovingRectangle[] rectangles = new MovingRectangle[5]; //(x, y, w, h, xv, yv, canvas);
for(int i =0;i<rectangles.length;i++) {
int x = rng.nextInt(canvas); //xPosition
int y = rng.nextInt(canvas); //yPosition
int w = rng.nextInt(100)+20; //width
int h = rng.nextInt(100)+20; //height
double xv = rng.nextInt(5)+2; //xVelocity
double yv = rng.nextInt(5)+2; //yVelocity
rectangles[i]= new MovingRectangle(x,y,w,h,xv,yv,canvas);
}
//while isMoving, the rectangles move
while (true) {
StdDraw.clear();
for(int i =0; i<rectangles.length;i++) {
rectangles[i].move();
rectangles[i].draw();
rectangles[i].freeze();
System.out.println(rectangles[i].isFrozen());
//for 0
if(rectangles[i].ifColliding(rectangles[1])){
if(rectangles[0].isFrozen()) {
rectangles[0].setFrozen(false);
}
}
if(rectangles[i].ifColliding(rectangles[2])){
if(rectangles[0].isFrozen()) {
rectangles[0].setFrozen(false);
}
}
if(rectangles[i].ifColliding(rectangles[3])){
if(rectangles[0].isFrozen()) {
rectangles[0].setFrozen(false);
}
}
if(rectangles[i].ifColliding(rectangles[4])){
if(rectangles[0].isFrozen()) {
rectangles[0].setFrozen(false);
}
}
//for 1
if(rectangles[1].ifColliding(rectangles[0])){
if(rectangles[1].isFrozen()) {
rectangles[1].setFrozen(false);
}
}
if(rectangles[1].ifColliding(rectangles[2])){
if(rectangles[1].isFrozen()) {
rectangles[1].setFrozen(false);
}
}
if(rectangles[1].ifColliding(rectangles[3])){
if(rectangles[1].isFrozen()) {
rectangles[1].setFrozen(false);
}
}
if(rectangles[1].ifColliding(rectangles[4])){
if(rectangles[1].isFrozen()) {
rectangles[1].setFrozen(false);
}
}
// for 2
if(rectangles[2].ifColliding(rectangles[0])){
if(rectangles[2].isFrozen()) {
rectangles[2].setFrozen(false);
}
}
if(rectangles[2].ifColliding(rectangles[1])){
if(rectangles[2].isFrozen()) {
rectangles[2].setFrozen(false);
}
}
if(rectangles[2].ifColliding(rectangles[3])){
if(rectangles[2].isFrozen()) {
rectangles[2].setFrozen(false);
}
}
if(rectangles[2].ifColliding(rectangles[4])){
if(rectangles[2].isFrozen()) {
rectangles[2].setFrozen(false);
}
}
//for 3
if(rectangles[3].ifColliding(rectangles[1])){
if(rectangles[3].isFrozen()) {
rectangles[3].setFrozen(false);
}
}
if(rectangles[3].ifColliding(rectangles[2])){
if(rectangles[3].isFrozen()) {
rectangles[3].setFrozen(false);
}
}
if(rectangles[3].ifColliding(rectangles[0])){
if(rectangles[3].isFrozen()) {
rectangles[3].setFrozen(false);
}
}
if(rectangles[3].ifColliding(rectangles[4])){
if(rectangles[3].isFrozen()) {
rectangles[3].setFrozen(false);
}
}
//for 4
if(rectangles[4].ifColliding(rectangles[1])){
if(rectangles[4].isFrozen()) {
rectangles[4].setFrozen(false);
}
}
if(rectangles[4].ifColliding(rectangles[2])){
if(rectangles[4].isFrozen()) {
rectangles[4].setFrozen(false);
}
}
if(rectangles[4].ifColliding(rectangles[3])){
if(rectangles[4].isFrozen()) {
rectangles[4].setFrozen(false);
}
}
if(rectangles[4].ifColliding(rectangles[0])){
if(rectangles[4].isFrozen()) {
rectangles[4].setFrozen(false);
}
}
}
if(rectangles[0].isFrozen()==true && rectangles[1].isFrozen()==true && rectangles[2].isFrozen()==true && rectangles[3].isFrozen()==true && rectangles[4].isFrozen()==true) {
StdDraw.clear();
StdDraw.text(250, 250, "You win!");
}
StdDraw.show(100);
}
}
}

add score in Screen libgdx

I have a sprite that collides with obstacle, but i have a problem with the score in the top of my screen.
In the update of my class, The collisions are specified by an array, but when it collides with an object, the score is still growing and I can not stop it. this is my code:
private Array<Polen> polen;
private Score score;
public PlayState(GameStateManager gsm) {
super(gsm);
score = new Score(110, 310);
polen = new Array<Polen>();
for(int i = 1; i <= COUNT; i++){
polen.add(new Polen(i * (Polen.WIDTH)));
}
}
#Override
public void update(float dt) {
handleInput();
for(int i = 0; i < polen.size; i++){
Polen pol= polen.get(i);
if(pol.collides(aliado.getBounds())) {
pol.changeExplosion();
flagScore = 1;
}
if (pol.collides(aliado.getBounds())==false){
flagScore = 0;
}
}
if (flagScore == 1){
Score.count++;
flagScore=0;
//auxCount = Score.count +1;
}
score.update(dt);
updateGround();
cam.update();
}
Score Class:
public class Score {
private static final int MOVEMENT = 70;
private Vector3 position;
private Vector3 velocity;
private Rectangle bounds;
private Texture texture;
public Score(int x, int y){
position = new Vector3(x, y, 0);
velocity = new Vector3(0, 0, 0);
texture = new Texture("score0.png");
bounds = new Rectangle(x, y, ((texture.getWidth())), texture.getHeight());
}
public void update(float dt){//code for move the score for top of screen:
if(position.y > 0)
velocity.add(0, 0, 0);
velocity.scl(dt);
position.add(MOVEMENT * dt, velocity.y, 0);
if(position.y < 0)
position.y = 0;
velocity.scl(1/dt);
bounds.setPosition(position.x, position.y);
}
public Vector3 getPosition() {
return position;
}
public Texture getTexture() {
return texture;
}
public void setTexture(Texture texture) {
this.texture = texture;
}
public Vector3 getVelocity() {
return velocity;
}
public void setVelocity(Vector3 velocity) {
this.velocity = velocity;
}
public Rectangle getBounds(){
return bounds;
}
public void setBounds(Rectangle bounds) {
this.bounds = bounds;
}
public void dispose(){
texture.dispose();
}
}
In pol.changeExplosion() you should add a boolean to mark the object as done.
public class Polen {
private boolean exploded = false;
public void changeExplosion() {
// ...
exploded = true;
// ...
}
public boolean isExploded() {
return exploded;
}
}
Then, in your update function you can use this flag to determine if the score should stop being incremented.
for(int i = 0; i < polen.size; i++) {
Polen pol= polen.get(i);
if(pol.collides(aliado.getBounds()) && !pol.isExploded()) {
pol.changeExplosion();
flagScore = 1;
}
else if (!pol.collides(aliado.getBounds())) {
flagScore = 0;
}
}

Texture not rendering when using shaders?

I am currently testing something out with GLSL, but now whenever I use shaders, my texture doesn't show up on the screen. I have 2 classes. It works without the shaders though...
Main Class
public class Main {
private static final int WIDTH = 1280;
private static final int HEIGHT = 720;
private static int shaderProgram;
private static int vertexShader;
private static int fragmentShader;
private static Terrain terrain;
public static void main(String[] args) {
initDisplay();
initGL();
terrain = new Terrain();
terrain.createTerrain();
createShader();
gameLoop();
removeShader();
}
private static void gameLoop() {
while (!Display.isCloseRequested()) {
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
terrain.drawTerrain();
glUseProgram(0);
Display.update();
Display.sync(60);
}
}
private static void initGL() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WIDTH, 0, HEIGHT, -1, 1);
glMatrixMode(GL_MODELVIEW);
glDisable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glClearColor(0, 0, 0, 1);
}
private static void initDisplay() {
try {
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
}
}
private static void createShader() {
shaderProgram = glCreateProgram();
vertexShader = glCreateShader(GL_VERTEX_SHADER);
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
StringBuilder vertexShaderSource = new StringBuilder();
StringBuilder fragmentShaderSource = new StringBuilder();
try {
BufferedReader reader = new BufferedReader(new FileReader("src/me/mateo226/shaders/vertexShader.txt"));
String line;
while ((line = reader.readLine()) != null) {
vertexShaderSource.append(line).append("\n");
}
reader.close();
} catch (IOException e) {
System.err.println("Vertex shader wasn't loaded properly!");
Display.destroy();
System.exit(1);
}
try {
BufferedReader reader = new BufferedReader(new FileReader("src/me/mateo226/shaders/fragmentShader.txt"));
String line;
while ((line = reader.readLine()) != null) {
fragmentShaderSource.append(line).append("\n");
}
reader.close();
} catch (IOException e) {
System.err.println("Fragment shader wasn't loaded properly!");
Display.destroy();
System.exit(1);
}
glShaderSource(vertexShader, vertexShaderSource);
glCompileShader(vertexShader);
if (glGetShader(vertexShader, GL_COMPILE_STATUS) == GL_FALSE) {
System.err.println("Vertex shader not compiled!");
}
glShaderSource(fragmentShader, fragmentShaderSource);
glCompileShader(fragmentShader);
if (glGetShader(fragmentShader, GL_COMPILE_STATUS) == GL_FALSE) {
System.err.println("Fragment shader not compiled!");
}
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glValidateProgram(shaderProgram);
}
private static void removeShader() {
glDeleteProgram(shaderProgram);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
Display.destroy();
}
}
Terrain Class
public class Terrain {
private static List<Tile> tiles = new ArrayList<Tile>();
public Terrain() {
}
public void createTerrain() {
Random rand = new Random();
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 15; j++) {
int r = rand.nextInt(2);
tiles.add(new Tile(i * 64, j * 64, r));
}
}
}
public void drawTerrain() {
for (Tile t : tiles) {
t.draw();
}
}
}
class Tile {
int type = 0;
private float x = 0;
private float y = 0;
private float width = 64;
private float height = 64;
private Texture tex;
public Tile(float x, float y, int type) {
this.type = type;
this.x = x;
this.y = y;
try {
if (this.type == 1) {
tex = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/grass.png")));
} else {
tex = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/grass.png")));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void draw() {
tex.bind();
glBegin(GL_QUADS);
{
glTexCoord2f(0, 0);
glVertex2f(x, y);
glTexCoord2f(0, 1);
glVertex2f(x, y + height);
glTexCoord2f(1, 1);
glVertex2f(x + width, y + height);
glTexCoord2f(1, 0);
glVertex2f(x + width, y);
}
glEnd();
}
}
While it may seem like a lot of code, it really isn't, just a simple program. However I am still unable to figure out what makes it not work. My vertex shader just has:
gl_Position = ftransform();
I tried adding gl_FragColor to the fragment shader but it doesn't help.
Cheers!
If you don't want to do anything with the texture e.g. lighting, you just have to assign texture2D(TexSampler, TexCoord) to the gl_FragColor in your frag shader.
That's because fragment shader will take care of each pixel (fragment) on screen (in current shader program).
If you don't assign any value to the gl_FragColor variable, nothing will drawn. What you want is to assign your texture (a bunch of pixels) to the gl_FragColor and you're good to go.

JPanel tearing even though I double buffer?

EDIT: This is an SSCCE to demonstrate my problem.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
public class Main {
public static BufferedImage map, tileSand, tileSea;
public static void main(String[] args) {
map = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);
for(int x = 0; x < 50 ; x++){
for(int y = 0; y < 50 ; y++){
boolean colour = Math.random() < 0.5;
if (colour){
map.setRGB(x, y, -256);
} else {
map.setRGB(x, y, -16776961);
}
}
}
tileSand = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
Graphics g = tileSand.getGraphics();
g.setColor(Color.YELLOW);
g.fillRect(0, 0, 32, 32);
tileSea = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
g.setColor(Color.BLUE);
g = tileSea.getGraphics();
g.fillRect(0, 0, 32, 32);
Island test = new Main.Island();
test.start();
long start, sleep;
while(true) {
start = System.currentTimeMillis();
test.getCanvas().requestFocus();
test.getCanvas().repaint();
sleep = 15-(System.currentTimeMillis()-start);
try {
Thread.sleep(sleep > 0 ? sleep : 0);
} catch (InterruptedException e) {
}
}
}
static class Island implements Runnable {
private Tile[][] tiles;
private JFrame frame;
private JPanel panel;
private Thread logicThread;
private boolean running = false;
private boolean paused = false;
private Image image;
private Player player;
public Island() {
image = new BufferedImage(1027, 768, BufferedImage.TYPE_INT_ARGB);
player = new Player();
tiles = new Tile[map.getWidth()][map.getHeight()];
int rgb;
for(int x = 0; x < map.getWidth(); x++) {
for(int y = 0; y < map.getHeight(); y++) {
rgb = map.getRGB(x, y);
switch (rgb) {
case -16776961: tiles[x][y] = new Tile("sea");
break;
case -256: tiles[x][y] = new Tile("sand");
break;
}
}
}
makeMap();
makeFrame();
addBindings();
logicThread = new Thread(this);
}
public JPanel getCanvas() {
return panel;
}
public void start() {
running = true;
paused = false;
logicThread.start();
}
public void run() {
long sleep, before;
while(running){
before = System.currentTimeMillis();
player.move();
try {
sleep = 15-(System.currentTimeMillis()-before);
Thread.sleep(sleep > 0 ? sleep : 0);
} catch (InterruptedException ex) {
}
while(running && paused);
}
paused = false;
}
private void makeFrame() {
frame = new JFrame("Island");
panel = new JPanel(){
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) image.getGraphics();
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, 1024, 768);
long xl, yl;
xl = player.getLocation().x-512;
yl = player.getLocation().y-384;
int x2, y2;
x2 = (int) Math.floor(xl / 32);
y2 = (int) Math.floor(yl / 32);
int xoffset, yoffset;
xoffset = (int) (xl % 32);
yoffset = (int) (yl % 32);
for(int x = x2; x < x2+40; x++) {
for(int y = y2; y < y2+40; y++) {
if (x < tiles.length && x > 0 && y < tiles[0].length && y > 0) {
g2d.drawImage(tiles[x][y].getContent(), (x-x2)*32 - xoffset, (y-y2)*32 - yoffset, null);
}
}
}
g.drawImage(image, 0, 0, null);
}
};
panel.setPreferredSize(new Dimension(1024, 768));
panel.setIgnoreRepaint(true);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
private void addBindings() {
InputMap inputMap = panel.getInputMap();
ActionMap actionMap = panel.getActionMap();
inputMap.put(KeyStroke.getKeyStroke("Q"),"hold-sprint");
actionMap.put("hold-sprint", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
player.sprint(true);
}
});
inputMap.put(KeyStroke.getKeyStroke("released Q"),"release-sprint");
actionMap.put("release-sprint", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
player.sprint(false);
}
});
inputMap.put(KeyStroke.getKeyStroke("RIGHT"),"hold-right");
actionMap.put("hold-right", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
player.right(true);
}
});
inputMap.put(KeyStroke.getKeyStroke("released RIGHT"),"release-right");
actionMap.put("release-right", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
player.right(false);
}
});
inputMap.put(KeyStroke.getKeyStroke("DOWN"),"hold-down");
actionMap.put("hold-down", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
player.down(true);
}
});
inputMap.put(KeyStroke.getKeyStroke("released DOWN"),"release-down");
actionMap.put("release-down", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
player.down(false);
}
});
inputMap.put(KeyStroke.getKeyStroke("LEFT"),"hold-left");
actionMap.put("hold-left", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
player.left(true);
}
});
inputMap.put(KeyStroke.getKeyStroke("released LEFT"),"release-left");
actionMap.put("release-left", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
player.left(false);
}
});
inputMap.put(KeyStroke.getKeyStroke("UP"),"hold-up");
actionMap.put("hold-up", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
player.up(true);
}
});
inputMap.put(KeyStroke.getKeyStroke("released UP"),"release-up");
actionMap.put("release-up", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
player.up(false);
}
});
}
private void makeMap() {
for(int x = 0; x < tiles.length; x++) {
for(int y = 0; y < tiles[0].length; y++) {
switch (tiles[x][y].getType()) {
case "sea": tiles[x][y].setContent(tileSea);
break;
case "sand": tiles[x][y].setContent(tileSand);
break;
}
}
}
}
}
static class Player{
private Point location;
private int xspeed, yspeed;
private boolean left, up, right, down, sprint;
public Player() {
location = new Point(0, 0);
left = false;
up = false;
right = false;
down = false;
sprint = false;
xspeed = yspeed = 0;
}
public void left(boolean state){
left = state;
}
public void up(boolean state){
up = state;
}
public void right(boolean state){
right = state;
}
public void down(boolean state){
down = state;
}
public void sprint(boolean state) {
sprint = state;
}
public void move() {
if (sprint) {
if (left) {
if(xspeed>-10)
xspeed--;
} if (right) {
if(xspeed<10)
xspeed++;
} if (up) {
if(yspeed>-10)
yspeed--;
} if (down) {
if(yspeed<10)
yspeed++;
}
} else {
if (left) {
if(xspeed>-5)
xspeed--;
} if (right) {
if(xspeed<5)
xspeed++;
} if (up) {
if(yspeed>-5)
yspeed--;
} if (down) {
if(yspeed<5)
yspeed++;
}
}
if (!sprint) {
if (xspeed > 5) {
xspeed--;
} if (xspeed < -5) {
xspeed++;
} if (yspeed > 5) {
yspeed--;
} if (yspeed < -5) {
yspeed++;
}
}
if (!left && !right) {
if (xspeed > 0) {
xspeed--;
} if (xspeed < 0) {
xspeed++;
}
} if (!up && !down) {
if (yspeed > 0) {
yspeed--;
} if (yspeed < 0) {
yspeed++;
}
}
location.x = location.x + xspeed;
location.y = location.y + yspeed;
}
public Point getLocation() {
return location;
}
}
static class Tile {
private String type;
private BufferedImage tile;
public Tile(String type) {
this.type = type;
}
public String getType() {
return type;
}
public void setContent(BufferedImage newTile) {
tile = newTile;
}
public BufferedImage getContent() {
return tile;
}
}
}
I have a JPanel with a modified paintComponent method. In this method I draw the relevant tiles from a 2d array to the screen based on the player location. I draw all my tiles to a BufferedImage and then I apply it to the screen at the end of the paint method. In theory this should not create tearing as I am double buffering? Here is the appropriate code, image is just a BufferedImage with the dimensions of the screen:
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) image.getGraphics();
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, 1024, 768);
long xl, yl;
xl = player.getLocation().x-512;
yl = player.getLocation().y-384;
int x2, y2;
x2 = (int) Math.floor(xl / 32);
y2 = (int) Math.floor(yl / 32);
int xoffset, yoffset;
xoffset = (int) (xl % 32);
yoffset = (int) (yl % 32);
for(int x = x2; x < x2+40; x++) {
for(int y = y2; y < y2+40; y++) {
if (x < tiles.length && x > 0 && y < tiles[0].length && y > 0) {
g2d.drawImage(tiles[x][y].getContent(), (x-x2)*32 - xoffset, (y-y2)*32 - yoffset, null);
}
}
}
g.drawImage(image, 0, 0, null);
}
I think the tearing could possibly be caused by the player's location changing over the duration of the paint method causing the tiles not to match up, the faster the player goes the more obvious the effect is. However I get the players location at the start and store it in two longs, I was under the impression that longs are passed by value not reference so the player location should be constant whilst the method runs.
I am happy to provide more code :), and thanks in advance.
Just so if people find my question and wonder what I ended up doing to "fix" it, switch over to c++ and SDL or some other image library before you are to far into your project, it's simply better for this kind of thing.

Categories