diff --git a/all.h b/all.h index 7e978f8887e3c813dd618680ab2075d3a830bf4e..e3163aaa1df68702225090798f51d65da4c3ed3a 100644 --- a/all.h +++ b/all.h @@ -152,7 +152,7 @@ Plane plane_from_points(Vector3 p1, Vector3 p2, Vector3 p3); void poly_free(Polygon p); Polygon poly_clip(Polygon poly, Plane plane); Polygon create_large_quad(Plane plane); -Vector2 get_uv(Vector3 p, MapPlane *mp, Plane plane); +Vector2 get_uv(Vector3 p, MapPlane *mp, Plane plane, int texWidth, int texHeight); // Interface void draw_crosshair(void); diff --git a/assets.c b/assets.c index 9a71cdd35824ddb861da1f3c88f44dd06c6e86c3..4c8e85cf2c330073dfca68eb52ce1639f67bfec3 100644 --- a/assets.c +++ b/assets.c @@ -49,8 +49,7 @@ Image img = GenImageChecked(64, 64, 8, 8, (Color){128, 128, 128, 255}, (Color){200, 200, 200, 255}); tex = LoadTextureFromImage(img); UnloadImage(img); } else { - GenTextureMipmaps(&tex); - SetTextureFilter(tex, TEXTURE_FILTER_BILINEAR); + SetTextureFilter(tex, TEXTURE_FILTER_POINT); SetTextureWrap(tex, TEXTURE_WRAP_REPEAT); } diff --git a/main.c b/main.c index d791a81393fb9e21b09c740acaf8699a336b5ee3..dc1f4a73b728709f0b6f383bef37f2ac5aee8780 100644 --- a/main.c +++ b/main.c @@ -7,7 +7,7 @@ int main(int argc, char *argv[]) { stringb map_path; sb_init(&map_path, 256); - sb_append_cstr(&map_path, "maps/demo3.map"); + sb_append_cstr(&map_path, "maps/demo4.map"); bool skip_title = false; int target_fps = -1; @@ -96,6 +96,13 @@ } } if (IsKeyPressed(KEY_THREE)) { if (load_map("maps/demo3.map")) { + game.mode = STATE_PLAYING; + game.cursor_captured = true; + DisableCursor(); + } + } + if (IsKeyPressed(KEY_FOUR)) { + if (load_map("maps/demo4.map")) { game.mode = STATE_PLAYING; game.cursor_captured = true; DisableCursor(); diff --git a/map.c b/map.c index 4f9a1319c94340dd1f4269cb27c215d4047f42e4..145c182c126e96ec2985cb075412f117b22a64b7 100644 --- a/map.c +++ b/map.c @@ -235,16 +235,55 @@ return poly; } -Vector2 get_uv(Vector3 p, MapPlane *mp, Plane plane) { +Vector2 get_uv(Vector3 p, MapPlane *mp, Plane plane, int texWidth, int texHeight) { + // Map coordinate system: X right, Y forward, Z up + // Raylib coordinate system: X right, Y up, Z back + // Conversion: Raylib.x = Map.x, Raylib.y = Map.z, Raylib.z = -Map.y + + // Map coordinates from Raylib coordinates: + float mx = p.x; + float my = -p.z; + float mz = p.y; + + // Map normal from Raylib normal: Vector3 n = plane.normal; - Vector3 up = (fabsf(n.y) < 0.999f) ? (Vector3){ 0, 1, 0 } : (Vector3){ 1, 0, 0 }; - Vector3 right = Vector3Normalize(Vector3CrossProduct(up, n)); - up = Vector3CrossProduct(n, right); + float nx = n.x; + float ny = -n.z; + float nz = n.y; + + float u = 0, v = 0; + + // Quake Standard axis projection + if (fabsf(nz) >= fabsf(nx) && fabsf(nz) >= fabsf(ny)) { + u = mx; + v = -my; + } else if (fabsf(nx) >= fabsf(ny) && fabsf(nx) >= fabsf(nz)) { + u = my; + v = -mz; + } else { // ny is dominant + u = mx; + v = -mz; + } - float u = Vector3DotProduct(p, right) * (1.0f / (mp->scale[0] ? mp->scale[0] : 1.0f)) + mp->shift[0]; - float v = Vector3DotProduct(p, up) * (1.0f / (mp->scale[1] ? mp->scale[1] : 1.0f)) + mp->shift[1]; + // Apply rotation + if (mp->rotate != 0.0f) { + float angle = mp->rotate * (PI / 180.0f); + float sinA = sinf(angle); + float cosA = cosf(angle); + float ru = u * cosA - v * sinA; + float rv = u * sinA + v * cosA; + u = ru; + v = rv; + } - return (Vector2){ u / 64.0f, v / 64.0f }; + // Apply scale, shift + float scaleU = mp->scale[0] ? mp->scale[0] : 1.0f; + float scaleV = mp->scale[1] ? mp->scale[1] : 1.0f; + + u = u / scaleU + mp->shift[0]; + v = v / scaleV + mp->shift[1]; + + return (Vector2){ u / (float)texWidth, v / (float)texHeight }; } void unload_map(void) { @@ -355,9 +394,10 @@ Vector3 v2 = poly.verts[v]; array_push(group->vertices, v0.x); array_push(group->vertices, v0.y); array_push(group->vertices, v0.z); array_push(group->vertices, v1.x); array_push(group->vertices, v1.y); array_push(group->vertices, v1.z); array_push(group->vertices, v2.x); array_push(group->vertices, v2.y); array_push(group->vertices, v2.z); - Vector2 uv0 = get_uv(v0, mp, plane); - Vector2 uv1 = get_uv(v1, mp, plane); - Vector2 uv2 = get_uv(v2, mp, plane); + Texture2D tex = get_texture(mp->texture); + Vector2 uv0 = get_uv(v0, mp, plane, tex.width, tex.height); + Vector2 uv1 = get_uv(v1, mp, plane, tex.width, tex.height); + Vector2 uv2 = get_uv(v2, mp, plane, tex.width, tex.height); array_push(group->texcoords, uv0.x); array_push(group->texcoords, uv0.y); array_push(group->texcoords, uv1.x); array_push(group->texcoords, uv1.y); array_push(group->texcoords, uv2.x); array_push(group->texcoords, uv2.y);