Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (800)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (867)
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  
  Fixing image stretching when rotating [LWJGL]  (Read 1911 times)
0 Members and 1 Guest are viewing this topic.
Offline lcass
« Posted 2014-12-23 01:41:57 »

So its been a long time since ive handled rotation code. I have never done it within GLSL before but ive given it a shot , the rotation calculates as it should , for a square viewpoint that is. When I rotate it stretches to fit the 1 by 1 image , how do I fix this?
Vertex shader 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  
attribute vec2 position;
attribute vec2 texin;
uniform vec2 transform;
uniform float zoom;
varying vec2 vecdata;
uniform vec2 rotpos;
uniform float rotation;
void main(){
   if(zoom <= 0){//if its negative then its not fun :(
         zoom = 1;
   }
   vec4 offset = vec4((position.x+transform.x) * zoom,(position.y+transform.y) *zoom,1,1);
   rotation = 60 * (3.1415926535897936264/180);
   rotpos = vec2(0
   ,0);
   offset = vec4((offset.x - rotpos.x),offset.y - rotpos.y,1,1);
   mat4 RotationMatrix = mat4( cos( rotation ), -sin( rotation ), 0.0, 0.0,
             sin( rotation ),  cos( rotation ), 0.0, 0.0,
                      0.0,           0.0, 1.0, 0.0,
                 0.0,           0.0, 0.0, 1.0 );
               
   offset *= RotationMatrix;
   offset = vec4((offset.x + rotpos.x) ,offset.y + rotpos.y,1,1);

   gl_Position = offset;
   vecdata = texin;


}

No stretching
at 0 degrees

total stretching at 90 degrees


Two solutions to this, I convert the coordinates back to pixel coordinates before rotation then convert them back afterward.
I switch the entire code to handle pixel coordinates and only convert them at the last second in the shader. Latter seems more difficult to achieve.
Offline KudoDEV

JGO Ninja


Medals: 79
Exp: 6 years


Game Dev Hobbyist


« Reply #1 - Posted 2014-12-23 01:47:38 »

Hmm I don't really understand what you're trying to do. I might just be crazy.

But here's a tip: Don't branch in GLSL unless you REALLY have to. For the "zoom" uniform validate it in your Java code updating the shader.

Offline orange451

JGO Kernel


Medals: 564
Projects: 8
Exp: 8 years


Your face? Your ass? What's the difference?


« Reply #2 - Posted 2014-12-23 01:52:04 »

Do as much computational stuff as you can outside of the shader (such as matrix math), then just pass in all the uniforms you need to draw the final scene.

First Recon. A java made online first person shooter!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SHC
« Reply #3 - Posted 2014-12-23 02:09:17 »

You have to transpose the matrix. The
mat4
constructor takes arguments in the column major order. Just reverse the negative signs on the sine functions.

Offline lcass
« Reply #4 - Posted 2014-12-23 12:49:07 »

What I mean is nothing to do with issue with rotation , the issue is with the image afterward it is stretched to the width of the screen how can I solve this?
Offline SHC
« Reply #5 - Posted 2014-12-23 13:47:24 »

The problem is your rotation matrix is in row-major format:

1  
2  
3  
4  
5  
6  
7  
mat4 RotationMatrix = mat4
(
    cos( rotation ), -sin( rotation ), 0.0, 0.0,  // Row 1
    sin( rotation ),  cos( rotation ), 0.0, 0.0,  // Row 2
    0.0,              0.0,             1.0, 0.0,  // Row 3
    0.0,              0.0,             0.0, 1.0   // Row 4
);

But the
mat4
constructor in GLSL expects the values in column major format. Instead of passing rows, you have to pass columns, so you have to transpose the matrix you are using. That can be simply achieved by reversing the minus sign on the
sin
terms, like this.

1  
2  
3  
4  
5  
6  
7  
mat4 RotationMatrix = mat4
(
     cos( rotation ), sin( rotation ), 0.0, 0.0,  // Column 1
    -sin( rotation ), cos( rotation ), 0.0, 0.0,  // Column 2
     0.0,             0.0,             1.0, 0.0,  // Column 3
     0.0,             0.0,             0.0, 1.0   // Column 4
);

Hope this clears up what I've said before.

Pages: [1]
  ignore  |  Print  
 
 

 
Riven (348 views)
2019-09-04 15:33:17

hadezbladez (5132 views)
2018-11-16 13:46:03

hadezbladez (2013 views)
2018-11-16 13:41:33

hadezbladez (5400 views)
2018-11-16 13:35:35

hadezbladez (1121 views)
2018-11-16 13:32:03

EgonOlsen (4539 views)
2018-06-10 19:43:48

EgonOlsen (5396 views)
2018-06-10 19:43:44

EgonOlsen (3083 views)
2018-06-10 19:43:20

DesertCoockie (3977 views)
2018-05-13 18:23:11

nelsongames (4527 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
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!