Форумчанин
Регистрация: 16.02.2013
Сообщений: 153
|
Исправил ошибку , рисует пустой экран.Не пойму , почему не рисуется куб.
Код:
#define _USE_MATH_DEFINES
#define _USE_MATH_DEFINES
#include<math.h>
#include<stdio.h>
#include<Windows.h>
#include<glut.h>
#define res 1
#define SW 160*res
#define SH 120*res
#define SW2 (SW/2)
#define SH2 (SH/2)
#define pixelScale 4/res
#define GLSW (SW*pixelScale)
#define GLSH (SH*pixelScale)
int numSect = 0;
int numWall = 0;
typedef struct
{
int fr1, fr2;
}time; time T;
typedef struct
{
float cos[360];
float sin[360];
}math; math M;
typedef struct
{
int w, s, a, d;
int sl, sr;
int m;
}keys; keys K;
typedef struct
{
int x, y, z;
int a;
int l;
}player; player P;
typedef struct
{
int x1, y1;
int x2, y2;
int wt,u,v;
int shade;
}walls; walls W[256];
typedef struct
{
int ws, we;
int z1, z2;
int d;
int st, ss;
int surf[SW];
}sectors; sectors S[128];
typedef struct
{
int w, h;
const unsigned char* name;
}TextureMaps; TextureMaps Textures[64];
void load()
{
FILE* fp;
errno_t err= fopen_s(&fp,"level.h","r");
if (err!= 0) { printf("Error opening level.h"); return; }
int s, w;
fscanf_s(fp, "%i", &numSect);
for (s = 0; s < numSect; s++)
{
fscanf_s(fp, "%i", &S[s].ws);
fscanf_s(fp, "%i", &S[s].we);
fscanf_s(fp, "%i", &S[s].z1);
fscanf_s(fp, "%i", &S[s].z2);
fscanf_s(fp, "%i", &S[s].st);
fscanf_s(fp, "%i", &S[s].ss);
}
fscanf_s(fp, "%i", &numWall);
for (s = 0; s < numWall; s++)
{
fscanf_s(fp, "%i", &W[s].x1);
fscanf_s(fp, "%i", &W[s].y1);
fscanf_s(fp, "%i", &W[s].x2);
fscanf_s(fp, "%i", &W[s].y2);
fscanf_s(fp, "%i", &W[s].wt);
fscanf_s(fp, "%i", &W[s].u);
fscanf_s(fp, "%i", &W[s].v);
fscanf_s(fp, "%i", &W[s].shade);
}
fscanf_s(fp, "%i %i %i %i %i", &P.x, &P.y, &P.z, &P.a, &P.l);
fclose(fp);
}
void pixel(int x, int y, int c)
{
int rgb[3];
if (c == 0) { rgb[0] = 255; rgb[1] = 255; rgb[2] = 0; }
if (c == 1) { rgb[0] = 160; rgb[1] = 160; rgb[2] = 0; }
if (c == 2) { rgb[0] = 0; rgb[1] = 255; rgb[2] = 0; }
if (c == 3) { rgb[0] = 0; rgb[1] = 160; rgb[2] = 0; }
if (c == 4) { rgb[0] = 0; rgb[1] = 255; rgb[2] = 255; }
if (c == 5) { rgb[0] = 0; rgb[1] = 160; rgb[2] = 160; }
if (c == 6) { rgb[0] = 160; rgb[1] = 100; rgb[2] = 0; }
if (c == 7) { rgb[0] = 110; rgb[1] = 50; rgb[2] = 0; }
if (c == 8) { rgb[0] = 0; rgb[1] = 60; rgb[2] = 130; }
glColor3ub(rgb[0], rgb[1], rgb[2]);
glBegin(GL_POINTS);
glVertex2i(x * pixelScale + 2, y * pixelScale + 2);
glEnd();
}
void movePlayer()
{
if (K.a == 1 && K.m == 0) { P.a -= 4; if (P.a < 0) { P.a += 360; } }
if (K.d == 1 && K.m == 0) { P.a += 4; if (P.a > 359) { P.a -= 360; } }
int dx = M.sin[P.a] * 10.0;
int dy = M.cos[P.a] * 10.0;
if (K.w == 1 && K.m == 0) { P.x += dx; P.y += dy; }
if (K.s == 1 && K.m == 0) { P.x -= dx; P.y -= dy; }
if (K.sr == 1) { P.x += dy; P.y -= dx; }
if (K.sl == 1) { P.x -= dy; P.y += dx; }
if (K.a == 1 && K.m == 1) { P.l -= 1; }
if (K.d == 1 && K.m == 1) { P.l += 1; }
if (K.w == 1 && K.m == 1) { P.z -= 4; }
if (K.s == 1 && K.m == 1) { P.z += 4; }
}
void clearBackground()
{
int x, y;
for (y = 0; y < SH; y++)
{
for (x = 0; x < SW; x++) { pixel(x, y, 8); }
}
}
void clipBehindPlayer(int* x1, int* y1, int* z1, int x2, int y2, int z2)
{
float da = *y1;
float db = y2;
float d = da - db; if (d == 0) { d = 1; }
float s = da / (da - db);
*x1 = *x1 + s * (x2 - (*x1));
*y1 = *y1 + s * (y2 - (*y1)); if (*y1 == 0) { *y1 = 1; }
*z1 = *z1 + s * (z2 - (*z1));
}
void drawWall(int x1, int x2, int b1, int b2, int t1, int t2, int s)
{
int x, y;
int dyb = b2 - b1;
int dyt = t2 - t1;
int dx = x2 - x1;
if (dx == 0) dx = 1;
int xs = x1;
if (x1 < 1) { x1 = 1; }
if (x2 < 1) { x2 = 1; }
if (x1 > SW - 1) { x1 = SW - 1; }
if (x2 > SW - 1) { x2 = SW - 1; }
for (x = x1; x < x2; x++)
{
int y1 = dyb * (x - xs + 0.5) / dx + b1;
int y2 = dyt * (x - xs + 0.5) / dx + t1;
if (y1 < 1) { y1 = 1; }
if (y2 < 1) { y2 = 1; }
if (y1 > SH - 1) { y1 = SH - 1; }
if (y2 > SH - 1) { y2 = SH - 1; }
for (y = y1; y < y2; y++)
{
pixel(x, y, 5);
}
}
}
int dist(int x1, int y1, int x2, int y2)
{
double distance = sqrt(double((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)));
return (int)distance;
}
void draw3D()
{
int loop, s, w, wx[4], wy[4], wz[4];
float CS = M.cos[P.a], SN = M.sin[P.a];
for (s = 0; s < numSect - 1; s++)
{
for (w = 0; w < numSect - s - 1; w++)
{
if (S[w].d < S[w + 1].d)
{
sectors st = S[w];
S[w] = S[w + 1];
S[w + 1] = st;
}
}
}
for (s = 0; s < numSect; s++)
{
S[s].d = 0;
for (loop = 0; loop < 2; loop++)
{
for (w = S[s].ws; w < S[s].we; w++)
{
int x1 = W[w].x1 - P.x;
int x2 = W[w].x2 - P.x;
int y1 = W[w].y1 - P.y;
int y2 = W[w].y2 - P.y;
if (loop == 0) { int swp = x1; x1 = x2; x2 = swp; swp = y1; y1 = y2; y2 = swp; }
wx[0] = x1 * CS - y1 * SN;
wx[1] = x2 * CS - y2 * SN;;
wx[2] = wx[0];
wx[3] = wx[1];
wy[0] = y1 * CS + x1 * SN;
wy[1] = y2 * CS + x2 * SN;
wy[2] = wy[0];
wy[3] = wy[1];
S[s].d += dist(0, 0, (wx[0] + wx[1]) / 2, (wy[0] + wy[1]) / 2);
wz[0] = S[s].z1 - P.z + ((P.l * wy[0]) / 32.0);
wz[1] = S[s].z1 - P.z + ((P.l * wy[1]) / 32.0);
wz[2] = wz[0] + S[s].z2;
wz[3] = wz[1] + S[s].z2;
if (wy[0] < 1 && wy[1] < 1) { continue; }
if (wy[0] < 1)
{
clipBehindPlayer(&wx[0], &wy[0], &wz[0], wx[1], wy[1], wz[1]);
clipBehindPlayer(&wx[2], &wy[2], &wz[2], wx[3], wy[3], wz[3]);
}
if (wy[1] < 1)
{
clipBehindPlayer(&wx[1], &wy[1], &wz[1], wx[0], wy[0], wz[0]);
clipBehindPlayer(&wx[3], &wy[3], &wz[3], wx[2], wy[2], wz[2]);
}
wx[0] = wx[0] * 200 / wy[0] + SW2;
wx[1] = wx[1] * 200 / wy[1] + SW2;
wx[2] = wx[2] * 200 / wy[2] + SW2;
wx[3] = wx[3] * 200 / wy[3] + SW2;
wy[0] = wz[0] * 200 / wy[0] + SH2;
wy[1] = wz[1] * 200 / wy[1] + SH2;
wy[2] = wz[2] * 200 / wy[2] + SH2;
wy[3] = wz[3] * 200 / wy[3] + SH2;
drawWall(wx[0], wx[1], wy[0], wy[1], wy[2], wy[3], s);
}
S[s].d /= (S[s].we - S[s].ws);
}
}
}
void display()
{
int x, y;
if (T.fr1 - T.fr2 >= 50)
{
clearBackground();
movePlayer();
draw3D();
T.fr2 = T.fr1;
glutSwapBuffers();
glutReshapeWindow(GLSW, GLSH);
}
T.fr1 = glutGet(GLUT_ELAPSED_TIME);
printf(" %i\n", glutGet(GLUT_ELAPSED_TIME) / 1000);
glutPostRedisplay();
}
void KeysDown(unsigned char key, int x, int y)
{
if (key == 'w' == 1) { K.w = 1; }
if (key == 's' == 1) { K.s = 1; }
if (key == 'a' == 1) { K.a = 1; }
if (key == 'd' == 1) { K.d = 1; }
if (key == 'm' == 1) { K.m = 1; }
if (key == ',' == 1) { K.sr = 1; }
if (key == '.' == 1) { K.sl = 1; }
if (key == 13) { load(); }
}
void KeysUp(unsigned char key, int x, int y)
{
if (key == 'w' == 1) { K.w = 0; }
if (key == 's' == 1) { K.s = 0; }
if (key == 'a' == 1) { K.a = 0; }
if (key == 'd' == 1) { K.d = 0; }
if (key == 'm' == 1) { K.m = 0; }
if (key == ',' == 1) { K.sr = 0; }
if (key == '.' == 1) { K.sl = 0; }
}
void init()
{
int x;
for (x = 0; x < 360; x++)
{
M.cos[x] = cos(x / 180.0 * M_PI);
M.sin[x] = sin(x / 180.0 * M_PI);
}
/*P.x = 70;
P.y = -110;
P.z = 20;
P.a = 0;
P.l = 0;*/
}
int main(int argc, char* argv[])
{
/**/
}
Последний раз редактировалось Timurkh77; Сегодня в 14:24.
|