Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
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  
  glViewport/glOrtho problem?  (Read 4642 times)
0 Members and 1 Guest are viewing this topic.
Offline SkyAphid
« Posted 2012-06-27 08:22:36 »

I have a resizable window programmed into my game, and oddly enough, the coordinate (0,0) isn't at the upper left of the screen when it's maximized. Here's a screenshot:



The mugshot of player should be in the corner, because it's translated to 0,0 every step. Obviously, it didn't move to the corner, so what's up?

The viewport is reset in the "core" of the game, like this:
1  
2  
3  
4  
         glViewport(0, 0, Display.getWidth(), Display.getHeight());
         glClearColor(0,0,0,1);
         glClearDepth(1f);
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


and the ortho projection is elsewhere, (after the above, though)

1  
2  
3  
4  
5  
6  
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glOrtho(0, Display.getWidth(), Display.getHeight(), 0, -CAMERA_NEARFAR, CAMERA_NEARFAR);
     
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();


I'm at wits end, not exactly sure why he won't draw in the corner, even after my demands to go there.

“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
Offline davedes
« Reply #1 - Posted 2012-06-27 11:14:11 »

Are you resizing glViewport/glOrtho every time the game window is resized? Use Display.wasResized.

Offline SkyAphid
« Reply #2 - Posted 2012-06-28 01:33:42 »

Are you resizing glViewport/glOrtho every time the game window is resized? Use Display.wasResized.
I managed to fix it. It seems that another transformation earlier was affecting it, even though I popped the matrix. Thanks anyway, though!

“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SkyAphid
« Reply #3 - Posted 2012-06-29 08:27:53 »

Okay, so I have another problem;

so let's say, we have a viewport like this:

1  
glOrtho(0, Display.getWidth(), Display.getHeight(), 0, -CAMERA_NEARFAR, CAMERA_NEARFAR);


And, we place a quad at these coordinates:

1  
int x = 0; int y = 0; int z = CAMERA_NEARFAR - 3;


DEPTH_TEST is functional, along with everything needed to make it work, other objects it works fine with it.

Now, my problem is, I have that quad placed there, but drawing anything ANYWHERE after it is drawn over, and Alpha/Z isn't taken into consideration.
Here's what I mean:



The code after all of this initialization that draws the above uses glPushMatrix(); glLoadIdentity(); Draws then glPopMatrix();

So, it does this:
DrawMugshot at Z = CAMERA_NEARFAR - 3;
DrawBubble at Z = glTranslatef(0,0,1);
DrawBackSlider at Z = glTranslatef(0,0,-3);

and I get the pictured effect with DepthTesting enabled.

I would just leave it off for this situation but I NEED to know what's wrong.

I did notice if I placed it's Z below 10 it worked fine.

Please help. This is bothering the absolute shit out of me.



“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
Offline theagentd

« JGO Bitwise Duke »


Medals: 365
Projects: 2
Exp: 8 years



« Reply #4 - Posted 2012-06-30 14:22:35 »

It's 100% normal. Depth testing doesn't care about what color it's actually drawing.

The main problem is that blending does not work with depth testing. Something you can do is enable alpha testing, which discards pixels that do not have enough alpha, but it has to be used instead of blending. The problem is that the z-buffer only tracks the closest pixel, so if you draw transparent stuff in the wrong order it will be unable to blend it in the correct order. While alpha testing allows you to discard pixels completely transparent pixels, it still doesn't really solve this problem since it will not allow you to have antialiased edges or semi-transparent surfaces. The best solution is probably to sort everything on the CPU, at least for a 2D game.

Myomyomyo.
Offline SkyAphid
« Reply #5 - Posted 2012-06-30 19:30:33 »

It's 100% normal. Depth testing doesn't care about what color it's actually drawing.

The main problem is that blending does not work with depth testing. Something you can do is enable alpha testing, which discards pixels that do not have enough alpha, but it has to be used instead of blending. The problem is that the z-buffer only tracks the closest pixel, so if you draw transparent stuff in the wrong order it will be unable to blend it in the correct order. While alpha testing allows you to discard pixels completely transparent pixels, it still doesn't really solve this problem since it will not allow you to have antialiased edges or semi-transparent surfaces. The best solution is probably to sort everything on the CPU, at least for a 2D game.

Well it's odd because this works perfectly fine everywhere else, like I could draw the player in the same position and he'd work fine, just the GUI is being weird about this.

I think I'll leave on Blending for drawing the entities, and just turn it off and do it the retarded way with the GUI like I did in the picture.

“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
Offline theagentd

« JGO Bitwise Duke »


Medals: 365
Projects: 2
Exp: 8 years



« Reply #6 - Posted 2012-07-01 11:22:17 »

Normally you wouldn't have depth testing enabled when drawing the GUI.

Myomyomyo.
Offline SkyAphid
« Reply #7 - Posted 2012-07-01 15:17:05 »

Normally you wouldn't have depth testing enabled when drawing the GUI.
Oh? Cool then. I'm doing right. That's even better.
It's really odd doing the "draw first, draw over," thing after all of this time, though. Hahah

“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
Offline JESTERRRRRR

Senior Devvie


Medals: 7
Exp: 1 year



« Reply #8 - Posted 2012-07-03 02:44:29 »

I didn't want to make a new thread for such a small question, so sorry for jacking yours


I draw a line that is x1 = 0, x2 = 800, so it spans 800 width across my window(display), which is also 800. Then I resize the window, to say 1000. I then figure I have 200 pixels of extra space, so I do
glViewport((width-800)/2,0,800,height)     (I'm just ignoring Y for now, keeping it constant; also I use floats and finally cast to an int for accuracy)

I figured this would move the 800 line to the center of the new window, but for me it is slightly off center (too far to the left) and also its been squashed inwards horizontally a bit. What am I missing?
Offline SkyAphid
« Reply #9 - Posted 2012-07-03 12:05:44 »

I didn't want to make a new thread for such a small question, so sorry for jacking yours


I draw a line that is x1 = 0, x2 = 800, so it spans 800 width across my window(display), which is also 800. Then I resize the window, to say 1000. I then figure I have 200 pixels of extra space, so I do
glViewport((width-800)/2,0,800,height)     (I'm just ignoring Y for now, keeping it constant; also I use floats and finally cast to an int for accuracy)

I figured this would move the 800 line to the center of the new window, but for me it is slightly off center (too far to the left) and also its been squashed inwards horizontally a bit. What am I missing?
Well, you need to be sure to recall the perspective too along with your viewport or else it's going to stretch.
That's all I got.

“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd

« JGO Bitwise Duke »


Medals: 365
Projects: 2
Exp: 8 years



« Reply #10 - Posted 2012-07-03 12:12:57 »

I didn't want to make a new thread for such a small question, so sorry for jacking yours


I draw a line that is x1 = 0, x2 = 800, so it spans 800 width across my window(display), which is also 800. Then I resize the window, to say 1000. I then figure I have 200 pixels of extra space, so I do
glViewport((width-800)/2,0,800,height)     (I'm just ignoring Y for now, keeping it constant; also I use floats and finally cast to an int for accuracy)

I figured this would move the 800 line to the center of the new window, but for me it is slightly off center (too far to the left) and also its been squashed inwards horizontally a bit. What am I missing?
glViewport() defines the part of the window that OpenGL can draw to. glOrtho() and gluPerspective() should not take the parts outside the viewport into account.
1  
2  
glViewport(100, 100, 800, 600);
glOrtho(0, 800, 600, 0, -1, 1); //Ignore that the viewport is offset by (100, 100)

That the screen is distorted could be due to an incorrect aspect ratio supplied to gluPerspective().

Myomyomyo.
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.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (75 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (46 views)
2014-11-29 21:32:03

toopeicgaming1999 (113 views)
2014-11-26 15:22:04

toopeicgaming1999 (100 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

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
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!