Files |  Tutorials |  Articles |  Links |  Home |  Team |  Forum |  Wiki |  Impressum

Aktuelle Zeit: Mi Jul 09, 2025 20:09

Foren-Übersicht » English » English Programming Forum
Unbeantwortete Themen | Aktive Themen



Ein neues Thema erstellen Auf das Thema antworten  [ 6 Beiträge ] 
Autor Nachricht
 Betreff des Beitrags: calculate texture coords
BeitragVerfasst: Mi Aug 19, 2009 19:24 
Offline
DGL Member
Benutzeravatar

Registriert: Di Jul 01, 2003 18:59
Beiträge: 887
Wohnort: (The Netherlands)
Programmiersprache: fpc/delphi/java/c#
Hello i am having trouble with calculation st (uv) coords for vertex coords.

this is the 'default' coords:
Code:
  1. 0,1   1,1
  2.    ---
  3.    |/|
  4.    ---
  5. 0,0   1,0


this the vertexes i want to render:
Code:
  1. 30,30   60,30
  2.      --
  3.      | >60,40
  4.      --
  5. 30,60   60,60


the calculations (in pseudo code):

lowest x := 30
lowest y := 30

highest x := 60;
highest y := 60;

range = (lowest - highest) * -1
range x = (30 - 60) * -1 = 30
range y = (30 - 60) * -1 = 30
offset = 0 - lowest
offset x = 0 - 30 = -30
offset y = 0 - 30 = -30

now for all vertexes

abspos = pos + offset
S = abspos / range
T = abspos / range


for 30,30:
abspos = 30 + -30 = 0
s = 0 / 30 = 0
abspos = 30 + -30 = 0
t = 0 / 30 = 0 but this should have been 1 ?

for 60,40:
abspos = 60 + -30 = 30
s = 30 / 30 = 1
abspos = 40 + -30 = 10
t = 10 / 30 = 0,333333

for 60,60:
abspos = 60 + -30 = 30
s = 30 / 30 = 1
abspos = 60 + -30 = 30
t = 30 / 30 = 1 but this should have been 0 ?

Now i am doing some thing wrong as i dont even get the 4 outside points right.

Thanks for your answers in advance.

_________________
http://3das.noeska.com - create adventure games without programming


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Mi Aug 19, 2009 21:29 
Offline
DGL Member
Benutzeravatar

Registriert: Fr Jan 04, 2008 21:29
Beiträge: 419
Wohnort: Lübeck
in the first table with your texcoords, the x-coord is growing from left to right, like the x-coord of your vertices. But if you are looking in both tables on your y-coords, you can see, that your y-coord in texture space is gowing from bottom to top, while the y-coord in vertex-space grows from top to bottom. As a result from this, your "range" should be negative for the y-component. Every logic thinking guy thinks it must be absolute, because it's a distance, but the truth is, it's a direction with 1-dimension, so the sign is important for calculus.

Another note: ( range = (lowest - highest) * -1 ) == ( range = (highest - lowest) )

to calc your example:

left = 30
top = 30
right = 60
bottom = 60 <- thats the point: your bottom is bigger than your top, so it's reversed

range x = right - left = 60 - 30 = 30
range y = top - bottom = 30 - 60 = -30

offset x = 0 - left = 0 - 30 = -30
offset y = 0 - bottom = 0 - 60 = -60

for left,top:
s = (left + offset x) / range x = (30 + -30) / 30 = 0
t = (top + offset y) / range y = (30 + -30) / -30 = -0

for right,top
s = (right + offset x) / range x = (60 + -30) / 30 = 1
t = (top + offset y) / range y = (30 + -30) / -30 = -0

for right,bottom
s = (right + offset x) / range x = (60 + -30) / 30 = 1
t = (bottom + offset y) / range y = (60 + -30) / -30 = -1

for left,bottom
s = (left + offset x) / range x = (30 + -30) / 30 = 0
t = (bottom + offset y) / range y = (60 + -30) / -30 = -1

Now the values are the same, but the signs are different. While -0 == 0 the -1 != 1, that means we changed direction on the texture. If you want you can shift the t-coords, by adding 1, back to the positiv range. The graphical-result should be the same.

_________________
Klar Soweit?


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Do Aug 20, 2009 11:09 
Offline
DGL Member
Benutzeravatar

Registriert: Di Jul 01, 2003 18:59
Beiträge: 887
Wohnort: (The Netherlands)
Programmiersprache: fpc/delphi/java/c#
Thanks you are right. But i am back to my original calculations due to the svg coordinate system. I had a small typo there that did not end up in explanation here.
@SellMan Could you make a wiki entrance on calculation of texture coords?

Now i am on to the next challenge(s):
-Rotate the texture around the center of the polygon (almost done)
Code:
  1.     glMatrixMode(GL_TEXTURE);
  2.     glLoadIdentity();
  3.     glTranslatef(0.5,0.5,0.0); //no need to know the size of the texture
  4.     glRotatef(45.0,0.0,0.0,1.0); //angle 45.0
  5.     glTranslatef(-0.5,-0.5,0.0);
  6.     glMatrixMode(GL_MODELVIEW);

-Repeat the texture (done)
Code:
  1.     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
  2.                      GL_REPEAT  );
  3.     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
  4.                      GL_REPEAT );

-Keep the texture at its original size. It now stretches, and that is what i told it to do. But as i am working in 2D i would like to keep the texture at its original size.
So if the texture width is 128 and the rectangle width is 200 the texture should draw up to 128 and repeat after that, if the rectangle width is 50 the texture should draw up to 50.
Now here i am uncertain how to solve it. Should i take the texture size into account when calculating the texture coords? Or is there an easier way?

[EDIT]
Solved keeping the texture size at its original size by setting the range to be the texture width and height.
[/EDIT]

_________________
http://3das.noeska.com - create adventure games without programming


Zuletzt geändert von noeska am Do Aug 20, 2009 15:16, insgesamt 1-mal geändert.

Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Do Aug 20, 2009 14:48 
Offline
DGL Member
Benutzeravatar

Registriert: Di Jul 01, 2003 18:59
Beiträge: 887
Wohnort: (The Netherlands)
Programmiersprache: fpc/delphi/java/c#
Now that i look at your calculations again you get the same outcome as me.
E.g.

for the top left point 30,30 you also get 0,0 and that is incorrect. It should become 0,1 . So somehow the outcome needs to be mirrored over the y axis?

this is the 'default' coords:
Code:
  1. 0,1   1,1
  2.    ---
  3.    |/|
  4.    ---
  5. 0,0   1,0



this the vertexes i want to render:
Code:
  1. 30,30   60,30
  2.      --
  3.      | >60,40
  4.      --
  5. 30,60   60,60  

_________________
http://3das.noeska.com - create adventure games without programming


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Do Aug 20, 2009 15:29 
Offline
DGL Member
Benutzeravatar

Registriert: Fr Jan 04, 2008 21:29
Beiträge: 419
Wohnort: Lübeck
look closely on my last scentence. the t-coord of the texture must be shifted. to do so you have to add 1 to the t-coord, the result is excactly your expectet values.

After shifting the values of my calculation it looks like this:

for left,top (30,30 => 0,1)
s = 0
t = -0 + 1 = 1

for right,top (60,30 => 1,1)
s = 1
t = -0 + 1 = 1

for right,bottom (60,60 => 1,0)
s = 1
t = -1 + 1 = 0

for left,bottom (30,60 => 0,0)
s = 0
t = -1 + 1 = 0

_________________
Klar Soweit?


Nach oben
 Profil  
Mit Zitat antworten  
 Betreff des Beitrags:
BeitragVerfasst: Do Aug 20, 2009 16:32 
Offline
DGL Member
Benutzeravatar

Registriert: Di Jul 01, 2003 18:59
Beiträge: 887
Wohnort: (The Netherlands)
Programmiersprache: fpc/delphi/java/c#
:oops: You are right. My bad.

_________________
http://3das.noeska.com - create adventure games without programming


Nach oben
 Profil  
Mit Zitat antworten  
Beiträge der letzten Zeit anzeigen:  Sortiere nach  
Ein neues Thema erstellen Auf das Thema antworten  [ 6 Beiträge ] 
Foren-Übersicht » English » English Programming Forum


Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 4 Gäste


Du darfst keine neuen Themen in diesem Forum erstellen.
Du darfst keine Antworten zu Themen in diesem Forum erstellen.
Du darfst deine Beiträge in diesem Forum nicht ändern.
Du darfst deine Beiträge in diesem Forum nicht löschen.
Du darfst keine Dateianhänge in diesem Forum erstellen.

Suche nach:
Gehe zu:  
  Powered by phpBB® Forum Software © phpBB Group
Deutsche Übersetzung durch phpBB.de
[ Time : 0.008s | 14 Queries | GZIP : On ]