Struct problem

I'm using C++ and DirectX.
I want to make a tunnel in which the camera moves into the screen. It won't be a large tunnel, this is just for my game intro. This is the code I have written:


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
// global declarations
LPDIRECT3D9 d3d; // the pointer to our Direct3D interface
LPDIRECT3DDEVICE9 d3ddev; // the pointer to the device class
LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL; // the pointer to the vertex buffer

//texture declerations
LPDIRECT3DTEXTURE9 texture_1; // the wall texture

struct CUSTOMVERTEX
{
CUSTOMVERTEX(FLOAT fx, FLOAT fy, FLOAT fz, DWORD dwcolor, FLOAT fu, FLOAT fv) :
X(fx), Y(fy), Z(fz), COLOR(dwcolor), U(fu), V(fv) {}
FLOAT X, Y, Z;
DWORD COLOR;
FLOAT U, V;
};

#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)

// this is the function used to render a single frame
void render_frame(void)
{
static float movement = 0.0f;

d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

d3ddev->BeginScene();

// select which vertex format we are using
d3ddev->SetFVF(CUSTOMFVF);

// SET UP THE PIPELINE
D3DXMATRIX matView; // the view transform matrix

D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3 (0.0f, 0.0f, movement), // the camera position
&D3DXVECTOR3 (0.0f, 0.0f, movement+10.0f), // the look-at position
&D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction

movement+=0.1f;

d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView

D3DXMATRIX matProjection; // the projection transform matrix

D3DXMatrixPerspectiveFovLH(&matProjection,
D3DXToRadian(45), // the horizontal field of view
(FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, // aspect ratio
1.0f, // the near view-plane
100.0f); // the far view-plane

d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection

// select the vertex buffer to display
d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));

// set the texture
d3ddev->SetTexture(0, texture_1);

// copy the vertex buffer to the back buffer
int i;
for (i=0;i<40;i++)
{
d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, i*4, 2);
}


d3ddev->EndScene();

d3ddev->Present(NULL, NULL, NULL, NULL);

return;
}

// this is the function that puts the 3D models into video RAM
void init_graphics(void)
{
// load the texture we will use

D3DXCreateTextureFromFile(d3ddev,
L"wall.png",
&texture_1);

float depth = 10.0f;
struct CUSTOMVERTEX* vertices[160];

// create the vertices using the CUSTOMVERTEX struct
int i;
for (i=0;i < 145;i+=16)
{
//left wall
vertices[i] = new CUSTOMVERTEX(-3.0f, 3.0f, depth, D3DCOLOR_XRGB(255, 255, 255),1 , 0);

vertices[i+1] = new CUSTOMVERTEX(-3.0f, 3.0f, depth + 25.0f, D3DCOLOR_XRGB(255, 255, 255),0 , 0);
vertices[i+2] = new CUSTOMVERTEX(-3.0f, -3.0f, depth, D3DCOLOR_XRGB(255, 255, 255), 1, 1);
vertices[i+3] = new CUSTOMVERTEX(-3.0f, -3.0f, depth + 25.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 1);

//ceiling
vertices[i+4] = new CUSTOMVERTEX(-3.0f, 3.0f, depth, D3DCOLOR_XRGB(255, 255, 255), 1, 0);
vertices[i+5] = new CUSTOMVERTEX(3.0f, 3.0f, depth, D3DCOLOR_XRGB(255, 255, 255), 0 , 0);
vertices[i+6] = new CUSTOMVERTEX(-3.0f, 3.0f, depth + 25.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 1);
vertices[i+7] = new CUSTOMVERTEX(3.0f, 3.0f, depth + 25.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 1);

//right wall
vertices[i+8] = new CUSTOMVERTEX(3.0f, 3.0f, depth + 25.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 0);
vertices[i+9] = new CUSTOMVERTEX(3.0f, 3.0f, depth, D3DCOLOR_XRGB(255, 255, 255), 0, 0);
vertices[i+10] = new CUSTOMVERTEX(3.0f, -3.0f, depth + 25.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 1);
vertices[i+11] = new CUSTOMVERTEX(3.0f, -3.0f, depth, D3DCOLOR_XRGB(255, 255, 255), 0, 1);

//floor
vertices[i+12] = new CUSTOMVERTEX(-3.0f, -3.0f, depth + 25.0f, D3DCOLOR_XRGB(255, 255, 255), 1, 0);
vertices[i+13] = new CUSTOMVERTEX(3.0f, -3.0f, depth + 25.0f, D3DCOLOR_XRGB(255, 255, 255), 0, 0);
vertices[i+14] = new CUSTOMVERTEX(-3.0f, -3.0f, depth, D3DCOLOR_XRGB(255, 255, 255), 1, 1);
vertices[i+15] = new CUSTOMVERTEX(3.0f, -3.0f, depth, D3DCOLOR_XRGB(255, 255, 255), 0, 1);

depth +=25.0f;
}

// create a vertex buffer interface called v_buffer
d3ddev->CreateVertexBuffer(160*sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL);

VOID* pVoid; // a void pointer

// lock v_buffer and load the vertices into it
v_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vertices, sizeof(vertices));
v_buffer->Unlock();

return;
}



I actually want to make the tunnel a combination of small tunnels because otherwise the texture will be stretched.

At first I tried with a CUSTOMVERTEX format without a struct constructor and by initializing the vertices along with the vertices[] decleration, giving the vertices for just 2 tunnels. It worked fine but the texture was somewhat stretched. Note that I have left out the code that has to do with the window and the D3D initializations and I've only included the functions that I have made changes in.
The result is an empty window (black window with an arrow cursor I can move).

Anyone has any idea what the problem might be?
Last edited on
Ok, I found out myself.
Actually, I thought that texture coordinates could only be between 0 and 1 but I found out that if you give coordinates more than 1 the texture is tiled on the primitive. So no need for more than 1 tunnels.
Topic archived. No new replies allowed.