0

So, i have a problem with my engine that i am working on right now:

img

The point is, that it should generate 16x16 platform of them.

This is code that creates cubes:

 private void render(){



        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
        for(int x=0; x<16; x++){
            for(int z=0; z<16; z++) {


                b.renderBlock(x, 0, z, texture);

            }
        }

        glMatrixMode( GL_MODELVIEW );
        GL11.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
        GL11.glEnable(GL_CULL_FACE);
        GL11.glLoadIdentity();

    }

This should render 16x16 platform of blocks

Block.java (b.renderblock)

package net.themorfeus.awesum.adventure.game;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;

public class Block {

    private float rquad = 0f;

    public void renderBlock(int posx, int posy, int posz, Texture t) {
        t.bind();
        //GL11.glLoadIdentity();                          // Reset The Current Modelview Matrix
        GL11.glTranslatef(posx,posy,posz);             // Move Right 1.5 Units And Into The Screen 6.0
        GL11.glRotatef(rquad,0.0f,0.0f,1.0f);               // Rotate The Quad On The X axis ( NEW )
        GL11.glColor3f(1.0f,1.0f,1.0f);                 // Set The Color To Blue One Time Only
        GL11.glBegin(GL11.GL_QUADS);                  // Draw A Quad
      //GL11.glColor3f(0.5f, 0.4f, 0.4f);             // Set The Color To Green
        GL11.glTexCoord2f(0,0);
        GL11.glVertex3f( 1f, 1f,-1f);         // Top Right Of The Quad (Top)
        GL11.glTexCoord2f(1,0);
        GL11.glVertex3f(-1f, 1f,-1f);         // Top Left Of The Quad (Top)
        GL11.glTexCoord2f(1,1);
        GL11.glVertex3f(-1f, 1f, 1f);         // Bottom Left Of The Quad (Top)
        GL11.glTexCoord2f(0,1);
        GL11.glVertex3f( 1f, 1f, 1f);         // Bottom Right Of The Quad (Top)

        //GL11.glColor3f(1.2f,0.5f,0.9f);             // Set The Color To Orange
        GL11.glTexCoord2f(0,0);
        GL11.glVertex3f( 1f,-1f, 1f);         // Top Right Of The Quad (Bottom)
        GL11.glTexCoord2f(0,1);
        GL11.glVertex3f(-1f,-1f, 1f);         // Top Left Of The Quad (Bottom)
        GL11.glTexCoord2f(1,1);
        GL11.glVertex3f(-1f,-1f,-1f);         // Bottom Left Of The Quad (Bottom)
        GL11.glTexCoord2f(1,0);
        GL11.glVertex3f( 1f,-1f,-1f);         // Bottom Right Of The Quad (Bottom)

        //GL11.glColor3f(1.0f,0.0f,0.0f);             // Set The Color To Red
        GL11.glTexCoord2f(0,0);
        GL11.glVertex3f( 1f, 1f, 1f);         // Top Right Of The Quad (Front)
        GL11.glTexCoord2f(1,0);
        GL11.glVertex3f(-1f, 1f, 1f);         // Top Left Of The Quad (Front)
        GL11.glTexCoord2f(1,1);
        GL11.glVertex3f(-1f,-1f, 1f);         // Bottom Left Of The Quad (Front)
        GL11.glTexCoord2f(0,1);
        GL11.glVertex3f( 1f,-1f, 1f);         // Bottom Right Of The Quad (Front)

        //GL11.glColor3f(1f,0.5f,0.0f);             // Set The Color To Yellow
        GL11.glTexCoord2f(0,0);
        GL11.glVertex3f( 1f,-1f,-1f);         // Bottom Left Of The Quad (Back)
        GL11.glTexCoord2f(1,0);
        GL11.glVertex3f(-1f,-1f,-1f);         // Bottom Right Of The Quad (Back)
        GL11.glTexCoord2f(1,1);
        GL11.glVertex3f(-1f, 1f,-1f);         // Top Right Of The Quad (Back)
        GL11.glTexCoord2f(0,1);
        GL11.glVertex3f( 1f, 1f,-1f);         // Top Left Of The Quad (Back)

        //GL11.glColor3f(0.0f,0.0f,0.3f);             // Set The Color To Blue
        GL11.glTexCoord2f(0,1);
        GL11.glVertex3f(-1f, 1f, 1f);         // Top Right Of The Quad (Left)
        GL11.glTexCoord2f(1,1);
        GL11.glVertex3f(-1f, 1f,-1f);         // Top Left Of The Quad (Left)
        GL11.glTexCoord2f(1,0);
        GL11.glVertex3f(-1f,-1f,-1f);         // Bottom Left Of The Quad (Left)
        GL11.glTexCoord2f(0,0);
        GL11.glVertex3f(-1f,-1f, 1f);         // Bottom Right Of The Quad (Left)

        //GL11.glColor3f(0.5f,0.0f,0.5f);             // Set The Color To Violet
        GL11.glTexCoord2f(0,0);
        GL11.glVertex3f( 1f, 1f,-1f);         // Top Right Of The Quad (Right)
        GL11.glTexCoord2f(1,0);
        GL11.glVertex3f( 1f, 1f, 1f);         // Top Left Of The Quad (Right)
        GL11.glTexCoord2f(1,1);
        GL11.glVertex3f( 1f,-1f, 1f);         // Bottom Left Of The Quad (Right)
        GL11.glTexCoord2f(0,1);
        GL11.glVertex3f( 1f,-1f,-1f);         // Bottom Right Of The Quad (Right)
        GL11.glEnd();
        //rquad+=0.0000001f;

    }




}

It basically renders the block quads.

genpfault
  • 51,148
  • 11
  • 85
  • 139
themorfeus
  • 277
  • 1
  • 3
  • 17
  • 1
    Just a suggestion: You should really get rid of that immediate mode code (`glBegin()…glEnd()`). All this function call overhead is bad enough in native code, crossing the JNI boundary doesn't make it better. Use Vertex Arrays and Vertex Buffer Objects. – datenwolf Mar 31 '12 at 15:27

1 Answers1

2

You shoud surround your drawing/translation with a glPushMatrix-glPopMatrix pair.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • Hey, i also have another prblem - my blocks overlap. like, 2nd block starts in 1/3rd of first one etc.. how to fix this? – themorfeus Mar 31 '12 at 15:41
  • Your block have an extent of 2 units in each direction (-1 to +1), so you must move in multiples of 2. Also the blocks are centered around the local 0-point, so your glTranslate should move to positions 2[x,y,z] + 0.5. Pro Tip: Get a pencil and paper and draw those things down, with your hands. – datenwolf Mar 31 '12 at 15:47