Note that there are some explanatory texts on larger screens.

plurals
  1. PO(Java and LWJGL) using glTranslatef when loading textures?
    primarykey
    data
    text
    <p>I'm trying to make a 2D basic game; sort of like Terraria, to improve my openGL knowledge (using Java and LWJGL) mainly.</p> <p>At the moment, i'm drawing textures on quads and everything's working fine, however, I can't seem to get this window to scroll (From what I can gather, this should be done by glTranslate..</p> <p>Code:</p> <pre><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); } } </code></pre> <p>Draw method</p> <pre><code>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(); } </code></pre> <p>It works fine without the textures, but I can't make it work to save my life.. Code is set up temporarily the game loop to try and just cause some basic translation when pressing the space bar</p> <p>Do I need to use a different method? How do I get this to work? </p> <p>By the way, using slick-util and lwjgl libraries...</p> <p><strong>............Edit...........</strong></p> <p>Didn't exactly fix it, but found an alternative way round it. I modified to the draw method to take in some offset variables, and used the relative translatex to offset the quads of each texture</p> <pre><code>public void draw(int offset_x, int offset_y) { texture.bind(); glLoadIdentity(); glTranslatef(x - offset_x, y - offset_y, 0); glBegin(GL_QUADS); { // Drawing the quads here } glEnd(); glLoadIdentity(); } </code></pre> <p>And then called through this:</p> <pre><code>w.draw(translatex, 0); </code></pre> <p>This is only a work around, but it works, so. If anyone has an actual solution, I can't help but think it'll be more efficient...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload