Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

kiba

Alter Hase

  • »kiba« ist der Autor dieses Themas

Beiträge: 327

Wohnort: NRW

Beruf: Azubi: Fach-Info. Anw.

  • Private Nachricht senden

1

01.12.2008, 22:43

[erledigt]src rect in opengl

hi ich versuche eine src rect in meine Sprite klasse zu machen
(src rect = anzeige bereich des Sprites)

dabei hab ich 2 möglichkeiten aber auch 2 probleme:
1.

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
srcW = this->src_rect.width
srcH = this->src_rect.height 

glLoadIdentity();
glTranslated(this->x,this->y,0);
glScaled(srcW,srcH,0);

        glBegin(GL_QUADS);
            glTexCoord2f(static_cast<float> (this->src_rect.x) / texW,
                                static_cast<float> (this->src_rect.y) / texH);
            glVertex3d(0.0f,0.0f,this->z);

            glTexCoord2f(static_cast<float> (srcW+this->src_rect.x) / texW,
                                static_cast<float> (this->src_rect.y) / texH);
            glVertex3d(1.0f,0.0f,this->z);

            glTexCoord2f(static_cast<float> (srcW+this->src_rect.x) / texW,
                            static_cast<float> (srcH+this->src_rect.y) /  texH);
            glVertex3d(1.0f,1.0f,this->z);

            glTexCoord2f(static_cast<float> (this->src_rect.x) / texW,
                            static_cast<float> (srcH+this->src_rect.y) /  texH);
            glVertex3d(0.0f,1.0f,this->z ;
        glEnd();


wenn ich jetzt die werte habe:
z.b.
texW = 250
texH = 250
src_rect.x = 200
src_rect.y = 200
src_rect.width = 100
src_rect.height = 100
zeigt er die texture wiederholt an der (0|0) stelle an
200+100 = über der texture grösse,texture wird ab da von neu angezeigt
und das will ich nicht

2.
die lösung:

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
srcW = this->src_rect.width
srcH = this->src_rect.height 

glLoadIdentity();
glTranslated(this->x,this->y,0);
glScaled(srcW-this->src_rect.x,srcH-this->src_rect.y,0);

        glBegin(GL_QUADS);
            glTexCoord2f(static_cast<float> (this->src_rect.x) / texW,
                                static_cast<float> (this->src_rect.y) / texH);
            glVertex3d(0.0f,0.0f,this->z);

            glTexCoord2f(static_cast<float> (srcW) / texW,
                                static_cast<float> (this->src_rect.y) / texH);
            glVertex3d(1.0f,0.0f,this->z);

            glTexCoord2f(static_cast<float> (srcW) / texW,
                            static_cast<float> (srcH) /  texH);
            glVertex3d(1.0f,1.0f,this->z);

            glTexCoord2f(static_cast<float> (this->src_rect.x) / texW,
                            static_cast<float> (srcH) /  texH);
            glVertex3d(0.0f,1.0f,this->z ;
        glEnd();

100-200 = -100
jetzt ist hier das problem das bei glscaled negative zahlen raus kommen und es wir garnichts angezeigt

David_pb

Community-Fossil

Beiträge: 3 886

Beruf: 3D Graphics Programmer

  • Private Nachricht senden

2

02.12.2008, 18:34

Ich weiß garnicht was du hast. Du gibst Texturkoordinaten für die Ecken deines Quads an... Wo soll er denn sonst die Textur zeichnen, wenn nicht auf das komplette Rechteck?
@D13_Dreinig

kiba

Alter Hase

  • »kiba« ist der Autor dieses Themas

Beiträge: 327

Wohnort: NRW

Beruf: Azubi: Fach-Info. Anw.

  • Private Nachricht senden

3

02.12.2008, 18:40

Zitat


zeigt er die texture wiederholt an der (0|0) stelle an
200+100 = über der texture grösse,texture wird ab da von neu angezeigt
und das will ich nicht


beim ersten code
ist das problem das sich mein sprite wieholt
eben wie ein unendlicher scrollender hintergrund
und das will ich nicht

mit den 2 code löse ich das problem
aber dann kommt ein neues problem
glScaled(srcW-this->src_rect.x,srcH-this->src_rect.y,0);

wenn src_rect.x/y größer als srcW/H ist, dann entstehen negative zahlen

David_pb

Community-Fossil

Beiträge: 3 886

Beruf: 3D Graphics Programmer

  • Private Nachricht senden

4

02.12.2008, 18:58

Was für einen Effekt willst du überhaupt erzielen...
@D13_Dreinig

kiba

Alter Hase

  • »kiba« ist der Autor dieses Themas

Beiträge: 327

Wohnort: NRW

Beruf: Azubi: Fach-Info. Anw.

  • Private Nachricht senden

5

02.12.2008, 19:23

ich will die Sprite so anzeigen das diessich nicht wiederholt
eben nur ein Bild von der Sprite ohne wiederholungen

und das ganze mit
source rectangle

denn damit verschiede ich ja den punkt ab wo die Sprite angezeigt wird

bild:
http://www.file-upload.net/view-1291110/Unbenannt-1.png.html

David_pb

Community-Fossil

Beiträge: 3 886

Beruf: 3D Graphics Programmer

  • Private Nachricht senden

6

02.12.2008, 20:30

Der Trick ist, dass die Texturkoordinaten im Bereich 0-1 bleiben müssen:

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
float invw = 1.0f / g_pTex->GetWidth();
float invh = 1.0f / g_pTex->GetHeight();

float cx0 = Clamp( 0.0f, 1.0f, rect.x * invw );
float cy0 = Clamp( 0.0f, 1.0f, rect.y * invh );
float cx1 = Clamp( 0.0f, 1.0f, ( rect.x + rect.w ) * invw );
float cy1 = Clamp( 0.0f, 1.0f, ( rect.y + rect.h ) * invh );

if ( cx0 > cx1 )
    Swap( cx0, cx1 );

if ( cy0 > cy1 )
    Swap( cy0, cy1 );

glBegin( GL_QUADS );
glTexCoord2f( cx0, cy0 ); glVertex2f( -1.0f,  1.0f );
glTexCoord2f( cx0, cy1 ); glVertex2f( -1.0f, -1.0f );
glTexCoord2f( cx1, cy1 ); glVertex2f(  1.0f, -1.0f );
glTexCoord2f( cx1, cy0 ); glVertex2f(  1.0f,  1.0f );
glEnd();
@D13_Dreinig

kiba

Alter Hase

  • »kiba« ist der Autor dieses Themas

Beiträge: 327

Wohnort: NRW

Beruf: Azubi: Fach-Info. Anw.

  • Private Nachricht senden

7

02.12.2008, 21:30

hi ich hab das ausprobier aber irget was klappt es nicht
es taucht für einen augenblick etwas auf
und verschwindet ,glaube beim nächsten rendern

C-/C++-Quelltext

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
#define CLAMP(X,Min,Max) if( X > Max ){ X = Max;}else if( X < Min ){X = Min;}

float invW = 1.0f / static_cast<float>(texW);
float invH = 1.0f / static_cast<float>(texH);

clampf cx = 0.0f;
CLAMP( cx, 1.0f, this->src_rect.x * invW );

clampf cy = 0.0f;
CLAMP( cy, 1.0f, this->src_rect.y * invH );

clampf cwidth = 0.0f;
CLAMP( cwidth, 1.0f, static_cast<float>(( this->src_rect.x + this->src_rect.width ) * invW) );

clampf cheight = 0.0f;
CLAMP( cheight, 1.0f, static_cast<float>(( this->src_rect.y + this->src_rect.height ) * invH) );

if ( cx > cwidth )
   std::swap( cx, cwidth );

if ( cy > cwidth )
   std::swap( cy, cheight );

glLoadIdentity();
   glTranslated(this->x,this->y);
   glScalef(this->src_rect.height,this->src_rect.height,0);

      glBegin(GL_QUADS);
         glTexCoord2f(cx,cy);
         glColor4ub(this->vColor[0].red,this->vColor[0].green,this->vColor[0].blue,this->vColor[0].alpha);
         glVertex3f(-1.0f,  1.0f,this->z);

         glTexCoord2f(cx,cheight);
         glColor4ub(this->vColor[1].red,this->vColor[1].green,this->vColor[1].blue,this->vColor[1].alpha);
         glVertex3f(-1.0f, -1.0f,this->z);

         glTexCoord2f(cwidth,cheight);
         glColor4ub(this->vColor[2].red,this->vColor[2].green,this->vColor[2].blue,this->vColor[2].alpha);
         glVertex3f(1.0f, -1.0f,this->z);

         glTexCoord2f(cwidth,cy);
         glColor4ub(this->vColor[3].red,this->vColor[3].green,this->vColor[3].blue,this->vColor[3].alpha);
         glVertex3f(-1.0f,  1.0f,this->z);
      glEnd();

David_pb

Community-Fossil

Beiträge: 3 886

Beruf: 3D Graphics Programmer

  • Private Nachricht senden

8

02.12.2008, 21:37

C-/C++-Quelltext

1
#define CLAMP(X,Min,Max) if( X > Max ){ X = Max;}else if( X < Min ){X = Min;} 


Genial! Vorallem auch noch falsch verwendet...
@D13_Dreinig

Anonymous

unregistriert

9

02.12.2008, 21:49

Genial, auch noch ein Makro... Benutz Templates!

kiba

Alter Hase

  • »kiba« ist der Autor dieses Themas

Beiträge: 327

Wohnort: NRW

Beruf: Azubi: Fach-Info. Anw.

  • Private Nachricht senden

10

02.12.2008, 21:52

upps..so besser ^^

C-/C++-Quelltext

1
2
clampf cx = this->src_rect.x * invW;
CLAMP( cx, 0.0f, 1.0f );


und dein..

C-/C++-Quelltext

1
2
3
4
glBegin( GL_QUADS );
glTexCoord2f( cx0, cy0 ); glVertex2f( -1.0f,  1.0f ); 
glTexCoord2f( cx0, cy1 ); glVertex2f( -1.0f, -1.0f ); 
//...

..stimmt nicht so ganz

auf jeden fall funktionier bei mir das hier:

C-/C++-Quelltext

1
2
3
4
glBegin( GL_QUADS );
glTexCoord2f( cx0, cy0 ); glVertex2f( 0.0f,  0.0f ); 
glTexCoord2f( cx1, cy0 ); glVertex2f( 1.0f,  0.0f ); 
//...

Werbeanzeige