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.
You probably forgot to clear the depth buffer
Also all of the code in
initGLactually 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.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.