opengl - (Java and LWJGL) using glTranslatef when loading textures? -


i'm trying make 2d basic game; sort of terraria, improve opengl knowledge (using java , lwjgl) mainly.

at moment, i'm drawing textures on quads , everything's working fine, however, can't seem window scroll (from can gather, should done gltranslate..

code:

private void init() {     try {         display.setdisplaymode(new displaymode(window_width, window_height));         display.settitle(window_title);         display.setvsyncenabled(true);         display.create();     } catch (lwjglexception e) {         e.printstacktrace();     }      translatex = 0f;      w = new world();     w.placeblock(4, 5, blocks.stone);      glmatrixmode(gl_projection);     glloadidentity();     glortho(0, window_width, window_height, 0, 1, -1);     glmatrixmode(gl_modelview);     glenable(gl_texture_2d);      font awtfont = new font("courier new", font.bold, 12);     font = new truetypefont(awtfont, antialias); } private void gameloop() {     while (!display.iscloserequested()) {         glclear(gl_color_buffer_bit);          glloadidentity();         glpushmatrix();          gltranslatef(translatex, 0.0f, 0.0f);         if (keyboard.iskeydown(keyboard.key_space)) {             translatex += 2.0f;         }         w.draw();          inputmanager();          glpopmatrix();          display.update();         display.sync(fps);     } } 

draw method

public void draw() {     texture.bind();     glloadidentity();     gltranslatef(x, y, 0);     glbegin(gl_quads); {         gltexcoord2f(0, 0);         glvertex2f(0, 0);         gltexcoord2f(1, 0);         glvertex2f(block_size, 0);         gltexcoord2f(1, 1);         glvertex2f(block_size, block_size);         gltexcoord2f(0, 1);         glvertex2f(0, block_size);     }     glend();     glloadidentity(); } 

it works fine without textures, can't make work save life.. code set temporarily game loop try , cause basic translation when pressing space bar

do need use different method? how work?

by way, using slick-util , lwjgl libraries...

............edit...........

didn't fix it, found alternative way round it. modified draw method take in offset variables, , used relative translatex offset quads of each texture

public void draw(int offset_x, int offset_y) {     texture.bind();     glloadidentity();     gltranslatef(x - offset_x, y - offset_y, 0);     glbegin(gl_quads); {         // drawing quads here     }     glend();     glloadidentity(); } 

and called through this:

w.draw(translatex, 0); 

this work around, works, so. if has actual solution, can't think it'll more efficient...

you calling glloadidentity in drawing function, reseting previous transformation state, translation want apply. seem prevent translations per object accumulate, should use matrix stack instead, e.g. use sequence

pushmatrix() translatelocaltile() draw() popmatrix() 

in draw function, local translation encapsulated , not affect transformation of next object, while outside transformations (like global transformation) still effective.


Comments