jister
jister

Reputation: 145

LWJGL 3D Rendering not working

I have set up here a main class which renders the blocks from an ArrayList however I only see the last one to be rendered. Why do I only see the last block in the ArrayList?

This is where I render my blocks in my Main Class:

glPushMatrix();
glTranslatef(0, 0, -10);
Brick.bind();
glBegin(GL_QUADS);
    Block b;
    for(int i = 0; i < blocks.size(); i++)
    {
        b = blocks.get(i);
        b.render();
    }
glEnd();

glPopMatrix(); Display.update();

This is where I generate my Blocks:

public static void generateMap()
    {
        Block b;
        for(float i = 0; i < 99; i += 3)
        {
            for(float r = 0; r < 99; r += 3)
            {
                b = new Block(i, 0f, r, 3f, 3f, 3f);
                blocks.add(b);
            }
        }
    }

Then I have this Block class that I use when making each block:

import static org.lwjgl.opengl.GL11.glTexCoord2f;
import static org.lwjgl.opengl.GL11.glVertex3f;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;


public class Block 
{
    protected static float x;
    protected static float y;
    protected static float z;
    protected static float sx;
    protected static float sy;
    protected static float sz;

    public Block(float x, float y, float z, float sx, float sy, float sz)
    {
        this.x = x;
        this.y = y;
        this.z = z;
        this.sx = sx;
        this.sy = sy;
        this.sz = sz;
    }
    public static void render()
    {
        glTexCoord2f(0,0); glVertex3f(x-sx,y,z-sz);
        glTexCoord2f(0,1); glVertex3f(x,y,z-sz);
        glTexCoord2f(1,1); glVertex3f(x,y,z);
        glTexCoord2f(1,0); glVertex3f(x-sx,y,z);
        //Bottom
        glTexCoord2f(0,0); glVertex3f(x-sx,y-sy,z-sz);
        glTexCoord2f(0,1); glVertex3f(x,y-sx,z-sz);
        glTexCoord2f(1,1); glVertex3f(x,y-sx,z);
        glTexCoord2f(1,0); glVertex3f(x-sx,y-sy,z);
        //Front
        glTexCoord2f(0,0); glVertex3f(x,y,z);               
        glTexCoord2f(0,1); glVertex3f(x,y-sy,z);
        glTexCoord2f(1,1); glVertex3f(x-sx,y-sy,z);
        glTexCoord2f(1,0); glVertex3f(x-sx,y,z);
        //Back
        glTexCoord2f(0,0); glVertex3f(x,y,z-sz);
        glTexCoord2f(0,1); glVertex3f(x,y-sy,z-sz);
        glTexCoord2f(1,1); glVertex3f(x-sx,y-sy,z-sz);
        glTexCoord2f(1,0); glVertex3f(x-sx,y,z-sz);
        //Left
        glTexCoord2f(0,0); glVertex3f(x-sx,y,z);
        glTexCoord2f(0,1); glVertex3f(x-sx,y-sy,z);
        glTexCoord2f(1,1); glVertex3f(x-sx,y-sy,z-sz);
        glTexCoord2f(1,0); glVertex3f(x-sx,y,z-sz);
        //Right
        glTexCoord2f(0,0); glVertex3f(x,y,z);
        glTexCoord2f(0,1); glVertex3f(x,y-sy,z);
        glTexCoord2f(1,1); glVertex3f(x,y-sy,z-sz);
        glTexCoord2f(1,0); glVertex3f(x,y,z-sz);
    }
}

Upvotes: 1

Views: 247

Answers (1)

vallentin
vallentin

Reputation: 26157

The reason you only see the last one, is because all the "Blocks" are on top of each other!

Within the Block class you've set the x, y, z, sx, sy, sz variables as static, which they shouldn't be!

Basically the static keyword mean that the value is shared thereby if you change the x at one place it will affect everything else also.

Advice learn all the Java keywords or atleast all the basic once, because you jump into something as advanced as OpenGL.

Upvotes: 1

Related Questions