1

i'm learning opengl and I want to draw 50 texture(from one variable) this is what I do: in the update method:

public void update(){
    while(!Display.isCloseRequested()){
        input();
        for(int x = 0; x< 100; x++){
            block = new GrassBlock(x*32,10);
            block.draw();
        }
        Display.update();   
        Display.sync(60);
    }
}

this is how I init openGL:

    private void initGL(){
    glEnable(GL_TEXTURE_2D);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glViewport(0, 0,640 , 480);
    glMatrixMode(GL_MODELVIEW);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0,640 , 480, 0, 1, -1);
    glMatrixMode(GL_MODELVIEW);
}

this is the GrassBlock draw method:

@Override
public void draw() {
    grass.bind();
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0);
    glVertex2f(100, 100);
    glTexCoord2f(1, 0);
    glVertex2f(100+grass.getTextureWidth(),100);
    glTexCoord2f(1,1);
    glVertex2f(100+grass.getTextureWidth(),100+grass.getTextureHeight());
    glTexCoord2f(0,1);
    glVertex2f(100,100+grass.getTextureHeight());
    glEnd();
}

Also, I know that I cannot put the block creation in the update method because it loops, but how would I solve that I can draw multiple texture's from one variable?

Now I only get one and its always flickering.

1 Answers1

2

You probably forgot to clear the depth buffer

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                            ^^^^^^^^^^^^^^^^^^^^^

Also all of the code in initGL actually belongt at the beginning of your redraw function. The dimensions of the viewport should be taken by requesting the window's size from the graphics system.

glViewport(0, 0,640 , 480);
glMatrixMode(GL_MODELVIEW); <<<<<<<<
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

The switch to the modelview matrix there does nothing, as you switch back to the projection immediately after.

It's good practice to reset all matrices to identity at the beginning of the drawing function, to start of a well known state.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • Thanks, but the initGL code do I need to put it in the draw method of the GrassBlock? –  Jul 07 '12 at 11:03
  • @Arno: At the beginning of the rendering of the current layer (technically speaking). In your case into a to be created display function, called from the main loop, that will then also draw the GrassBlock. BTW: You should not allocate a new one each redraw. Otherwise you waste memory like hell, and your program will get stuck every so often, when the JVM garbage collector kicks in. – datenwolf Jul 07 '12 at 13:26