Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (511)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  cant get a 2D Camera in LWJGL working (beginner)  (Read 1845 times)
0 Members and 1 Guest are viewing this topic.
Offline MonsieurBlutbad

Senior Newbie





« Posted 2013-07-18 13:29:54 »

Hi all,

I am working on a 2D Game and I try to set up a simple camera for it (just moving and zooming).
Unfortunatly, I don't get it working at all.
Since I am new to LWJGL and OpenGL in general I have no idea, if I am missing out on something very basic
or if the problem is more complex.

With my googling I found out that there are 3 approaches:
1.) Use glScalef and glTranslatef (this seems to be very easy... in theoriy...)
2.) Use gluLookAt and gluPerspective (this seems to be more sophisticated, but maybe too much for a 2D Game?)
3.) Hardcode the "camera" position and zoom level into each draw call (now that seems to be a terrible way to go, even though I am sure I could do that...)

The most effort so far I put into the glScalef and glTranslatef approach.
My code is compiling, but neither the scaling nor the translating has any
effect on the screen.

Maybe somebody can take a look at my code and help me out

So here is my code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
// called once at start
protected void create() {
      // Enable blending
      glEnable(GL_BLEND);
      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   // enable textures since we're going to use these for our sprites
      glEnable(GL_TEXTURE_2D);
       
      // disable the OpenGL depth test since we're rendering 2D graphics
      glDisable(GL_DEPTH_TEST);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
       
      glOrtho(0, WIDTH, HEIGHT, 0, -1, 1);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      glViewport(0, 0, WIDTH, HEIGHT);
      // Set clear to transparent black
      glClearColor(0f, 0f, 0f, 0f);
}

// called every frame
protected void render(){
    // Clear the screen
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    float centerX = WIDTH * 0.5f;
    float centerY = HEIGHT * 0.5f;
    //Move to center of scene
    glTranslatef(centerX,centerY,0);  
    //Scale at center
    glScalef(2,2,1);          
    //Move back to origin
    glTranslatef(-centerX,-centerY,0);
    glPushMatrix();
      // ... render our game here ...
      spriteBatch.begin();
                         // next line is Pseudo-Code, but this is the way i draw all my graphics.
                         // I pass the spriteBatch to all my Entities, and they are drawn in the fashion shown below
               spriteBatch.draw( textureRegion, x, y, width, height );
      spriteBatch.end();
     glPopMatrix();
}


The SpriteBatch I use is not by me.
I use this one here: https://github.com/mattdesl/lwjgl-basics/blob/master/src/mdesl/graphics/SpriteBatch.java
Might it be the source of my problem? I tried to wrap my head around what actually is happening in that SpriteBatch,
but it is way beyond me Sad

Offline davedes
« Reply #1 - Posted 2013-07-18 17:35:56 »

glScale/glTranslate are deprecated. GLU isn't recommended much these days, and for all intents and purposes you can also consider it deprecated.

glPushMatrix, glMatrixMode, glLoadIdentity, and glOrtho are also all deprecated, and won't work in conjunction with my SpriteBatch (or any modern GL frameworks).

Instead, you need to provide SpriteBatch its own matrices. Below is an example using lwjgl-basics, and the display initialization from the "test" package (which is pretty much the same from the Display tutorial)
http://www.java-gaming.org/?action=pastebin&id=650

More info on matrices and modern GL here:
http://tomdalling.com/blog/modern-opengl/03-matrices-depth-buffering-animation/
http://tomdalling.com/blog/modern-opengl/04-cameras-vectors-and-input/
http://open.gl/transformations
http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/
http://www.arcsynthesis.org/gltut/

Modern OpenGL is a very challenging path for a beginner; you can't really just copy + paste and hope things work out. This is why frameworks like LibGDX are usually recommended instead; it allows you to work with OpenGL just as easily as LWJGL, but also gives you a lot of high-level functionality so that you aren't struggling with complex problems every step of the way.

Offline MonsieurBlutbad

Senior Newbie





« Reply #2 - Posted 2013-07-18 17:52:42 »

Thank you very much davedes, that is some very useful information.
I will try to get my head around the matrix stuff and take LibGDX as an emergency exit, if it proves to be too hard Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline MonsieurBlutbad

Senior Newbie





« Reply #3 - Posted 2013-07-19 10:10:20 »

The code example you gave me works great! Also the sites you linked me seem to be amazing. It will take quite some time, to go through these, but at least I know, that this is the "real deal" (not some deprecatet concepts).
Also, I was told over at the LWJGL Forum, that you made the lwjgl-basics library including the spriteBatcher. I went through quite some of your tutorials before and they are really excellent. They helped me out a lot to set foot on this ground. So thank you very much for those, too Cheesy
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (50 views)
2014-10-17 03:59:02

Norakomi (40 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (36 views)
2014-10-15 16:18:58

TehJavaDev (67 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (57 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (44 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!