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

11

26.07.2007, 20:13

So. Das ist der Code von meinem ersten Texturprogramm. Ich hab es noch einmal kompiliert und getestet und es funktioniert einwandfrei.

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <d3dx9.h>
#include <D3D9.h>
#include <TriBase.h>

#define TRIANGLE_FVF ( D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)

struct SVertex
{
    tbVector3           vPosition;  // Position des Vertex

    D3DCOLOR            Color;      // Farbe des Vertex

    tbVector2           vTexture;   //Texturkoordinaten

};


float               g_fTime = 0.0f;         // Zeitzähler

PDIRECT3DTEXTURE9   g_pTexture;             // Die Textur

    tbDirect3D& D3D = tbDirect3D::Instance();

SVertex             g_aVertex[3];

tbConfig g_Config;


// Move-Funktion

tbResult Move(float fTime)
{
    
    // Zeitzähler erhöhen

    g_fTime += fTime;
    return TB_OK;
}


// Render-Funktion

tbResult Render(float fTime)
{
    HRESULT     hResult;
    
    
    tbMatrix    mRotation;      // Rotationsmatrix

    tbMatrix    mWorld;         // Weltmatrix

    tbMatrix    mTranslation;   // Translationsmatrix


    // Puffer leeren und Szene beginnen

    
    D3D->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, tbColor(0.0f, 0.0f, 0.0f), 1.0f, 0);
    D3D->BeginScene();
    
    // ------------------------------------------------------------------

    
    // Rotations- und Translationsmatrix des Dreiecks erzeugen

    mRotation = tbMatrixRotationY(TB_DEG_TO_RAD(g_fTime * 90.0f));
    mTranslation = tbMatrixTranslation(tbVector3(0.0f, 0.0f, 2.0f));
    
    // Beide Matrizen kombinieren und als Weltmatrix einsetzen

    mWorld = mRotation * mTranslation;
    D3D->SetTransform(D3DTS_WORLD, (D3DMATRIX*)(&mWorld));

    // ------------------------------------------------------------------


    // Als Dreiecksfolge zeichnen

    D3D->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 1, g_aVertex, sizeof(SVertex));

    // ------------------------------------------------------------------

    // Szene beenden

    D3D->EndScene();
    
    D3D->Present (NULL, NULL, NULL, NULL);
    return TB_OK;
}


tbResult InitGame(char *WindowTitle)
{
    tbResult r;
    
    // Die TriBase-Engine initialisieren und den Konfigurationsdialog aufrufen

    if(tbInit()) return TB_ERROR;
    r = tbDoConfigDialog(&g_Config);
    if(r == TB_CANCELED) return TB_CANCELED;
    else if(r) TB_ERROR("Engine konnte nicht initialisiert werden!", r);

    if (tbDirect3D::Instance().Init(&g_Config, WindowTitle, NULL, NULL))
    {
        TB_ERROR("Direct3D konnte nicht initialisiert werden.", TB_ERROR);
    }
    
    // Texturmanager erstellen

    if(tbTextureManager::Instance().Init())
    {
        // Fehler!

        TB_ERROR("Texturmanager konnte nicht initialisiert werden!", TB_ERROR);
    }

    // Die Textur laden

    
    g_pTexture = tbTextureManager::Instance().GetTexture("Texture.jpg");
    if(g_pTexture == NULL) TB_ERROR("Fehler beim Laden von Texture.jpg!", TB_ERROR);
    
    // Die drei Vertizes des Dreiecks erstellen

    // 1.

    g_aVertex[0].vPosition = tbVector3(0.0f, 1.0f, 0.0f);
    g_aVertex[0].Color = tbColor(1.0f, 1.0f, 1.0f);
    g_aVertex[0].vTexture = tbVector2(0.5f, 0.0f);
    
    // 2.

    g_aVertex[1].vPosition = tbVector3(1.0f, -1.0f, 0.0f);
    g_aVertex[1].Color = g_aVertex[0].Color;
    g_aVertex[1].vTexture = tbVector2(1.0f, 1.0f);

    // 3.

    g_aVertex[2].vPosition = tbVector3(-1.0f, -1.0f, 0.0f);
    g_aVertex[2].Color = g_aVertex[0].Color;
    g_aVertex[2].vTexture = tbVector2(0.0f, 1.0f);

    // Vertexformat setzen

    D3D.SetFVF(TRIANGLE_FVF);
    
    // Textur einsetzen

    D3D->SetTexture(0, g_pTexture);

    D3D->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
    D3D->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
    D3D->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);

    // Beleuchtung und Culling ausschalten, Dithering aktivieren

    D3D->SetRenderState(D3DRS_LIGHTING, FALSE);
    D3D->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
    D3D->SetRenderState(D3DRS_DITHERENABLE, TRUE);

    // Das Bildseitenverhältnis berechnen

    float fAspect =   (float)(D3D.GetScreenSize().x)/ (float)(D3D.GetScreenSize().y);

    // Die Projektionsmatrix erzeugen

    tbMatrix mProjection = tbMatrixProjection(TB_DEG_TO_RAD(90.0f), // Sichtfeld: 90° (180°)

                                     fAspect,               // Bildseitenverhältnis

                                     0.1f,                  // Nahe Clipping-Ebene

                                     100.0f);               // Ferne Clipping-Ebene


    // Projektionsmatrix einsetzen

    D3D->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*)(&mProjection));

    return TB_OK;

}



tbResult RunGame(void)
{

    if(tbDoMessageLoop(&Move, &Render))
    {
        TB_ERROR("Fehler in der Nachrichtenschleife.", TB_ERROR);
    }   

    return TB_OK;

}


tbResult ExitGame(void)
{
    tbTextureManager::Instance().Exit();

    tbDirect3D::Instance().Exit();

    if(tbExit())
    {
        TB_ERROR("Fehler beim beenden der TriBase Engine.", TB_ERROR);
    }   

    return TB_OK;

}



int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char* pcCpmmandLine, int iShowCommand)
{
    InitGame("Test");

    RunGame();

    ExitGame();

    return 0;

}
Tutorials zu OpenGL, Ubuntu und Programmieren allgemein: www.tomprogs.at

Forum und Wiki zum Programmieren lernen: proggen.org/forum.proggen.org

Chris14465

Frischling

  • »Chris14465« ist der Autor dieses Themas

Beiträge: 44

Wohnort: Nähe Siegburg bei Köln

Beruf: Schüler

  • Private Nachricht senden

12

26.07.2007, 21:40

Ok vielen Dank! Dein Code gilt jetzt als Vorlage für andere Sachen zum eben mal testen :P

13

27.07.2007, 08:17

Gern geschehen. Ich hoffe der Code funktioniert auch bei dir. Aber eigentlich sollte es keine Probleme geben. 8)
Tutorials zu OpenGL, Ubuntu und Programmieren allgemein: www.tomprogs.at

Forum und Wiki zum Programmieren lernen: proggen.org/forum.proggen.org

Werbeanzeige