I’m very new to openGL and not good at math ( and I blame myself for that also trying to fix this), but here it is:
I have my simple 2d game engine written in java, now I want to rewrite it in c++ with opengl support.
Currently I stuck in the beginning, I’ve read some articles\posts about lack of FPU support in old hand-held devices (like dingoo and similar) and decided to switch from -1,1 coordinate system to something better and with fixed values.
One of solution appeared in my mind is this: (replace X(-1,1), Y(-1,1),Z (-1,1) coordinate system with new one like X(-160,160,Y(-120,120),Z(-100,100) so I will be able to use glVertex3iv
I dont know how to do that.
Actually I already received few advices like : use glOrtho(its quite good. and if object must be closer I can just scale it.), use glScalef(didnt worked for me. dont know how to scale it right) to modify projection view.
for now I found a perfect Z value so all of my “sprites” are moved right in a place where they exactly same size in pixel that they appear on screen. but I think that in future this can make a bad results. ( also when user will switch to full screen on monitor with ratio 16:9 he will get a stretched picture which is bad. )
so I’m a little bit lost for now.
also I have a picture that describes what I need:
i.imgur.com slash MqNhhb1.jpg
glOrtho is the best solution but you need to keep your ascept ratio the same when the user resizes the window.
For example if you want to display an area of 100 pixels wide and 200 high with 0,0 in the middle of the screen you would use glOrtho(-50,50,-100,100,-1,1);
To zoom in to x0.5 you would do glOrtho(-25,25,-50,50,-1,1);
To zoom out to x2 you would do glOrtho(-100,100,-200,200,-1,1);
This view has an ascept ratio of w:h 1:2. If the person resizes the window so it no longer 1:2 you can either just let your image stretch or change your w:h ratio to match the new size and change the glOrtho call to match.
Lets say they resize to a w:h ratio of 1:1 your glOrtho call can either be glOrtho(-50,50,-50,50,-1,1) so your width does not change, or glOrtho(-100,100,-100,100,-1,1) so your height does not change.
Well, I decided to stay with this:
float ratio = (float) width / (float) height;
glViewport( 0, 0, width, height );
glMatrixMode( GL_PROJECTION );
gluPerspective( 60.0, ratio, 1.0, 1024.0 );
glTranslatef( -160, -120, 0 );
with “perfect Z” value. as If I change window width and height everything stays same. Only one thing is bothers me right now: that if I put my sprite outside of my virtual scene rect it will be painted as I dont do anything to prevent that. I’ve read about glScissors, but I’m not sure if it fits to my problem and current conditions (like perfect z and stuff).
picture: i.imgur.com slash TmkQ0BV.jpg