Skip to content

Commit dff36a7

Browse files
committed
Add UVMappog in config
1 parent 1f98e1b commit dff36a7

File tree

5 files changed

+57
-36
lines changed

5 files changed

+57
-36
lines changed

config.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,36 +75,31 @@
7575
"y": -0.5,
7676
"z": 2
7777
},
78-
"color": {
79-
"r": 255,
80-
"g": 0,
81-
"b": 0
82-
}
78+
"texCoord": [
79+
1, 1
80+
]
8381
},
8482
"vb": {
8583
"position": {
8684
"x": -2,
8785
"y": 3,
8886
"z": 3.5
8987
},
90-
"color": {
91-
"r": 0,
92-
"g": 255,
93-
"b": 0
94-
}
88+
"texCoord": [
89+
0, 1
90+
]
9591
},
9692
"vc": {
9793
"position": {
9894
"x": 0.5,
9995
"y": -0.5,
10096
"z": 4
10197
},
102-
"color": {
103-
"r": 0,
104-
"g": 0,
105-
"b": 255
106-
}
107-
}
98+
"texCoord": [
99+
0.5, 0
100+
]
101+
},
102+
"textIdx": 0
108103
}
109104
}
110105
],
@@ -172,5 +167,10 @@
172167
"intensity": 0.3
173168
}
174169
}
170+
],
171+
"assets": [
172+
{
173+
"imageName": "megu.png"
174+
}
175175
]
176176
}

src/Config.zig

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const Transformation = @import("Transformation.zig").Transformation;
1313
const Translation = @import("Translation.zig").Translation;
1414
const Rotation = @import("Rotation.zig").Rotation;
1515
const Vertex = @import("Vertex.zig").Vertex;
16+
const Image = Scene.Image;
1617
const rl = @cImport({
1718
@cInclude("raylib.h");
1819
});
@@ -45,6 +46,7 @@ const ObjectProxy = struct {
4546
va: Vertex,
4647
vb: Vertex,
4748
vc: Vertex,
49+
textIdx: usize,
4850
} = null,
4951
};
5052

@@ -53,11 +55,16 @@ const LightProxy = struct {
5355
point: ?PointLight = null,
5456
};
5557

58+
const AssetProxy = struct {
59+
imageName: [:0]const u8,
60+
};
61+
5662
const ConfigProxy = struct {
5763
camera: Camera,
5864
materials: []Material,
5965
objects: []ObjectProxy,
6066
lights: []LightProxy,
67+
assets: []AssetProxy,
6168
};
6269

6370
fn transform_proxy_to_transform(transform: ?TransformationProxy) ?Transformation {
@@ -76,6 +83,11 @@ fn transform_proxy_to_transform(transform: ?TransformationProxy) ?Transformation
7683
pub const Config = struct {
7784
const Self = @This();
7885

86+
camera: Camera,
87+
objects: []Object,
88+
lights: []Light,
89+
assets: []Image,
90+
7991
pub fn fromFilePath(path: []const u8, allocator: std.mem.Allocator) !Self {
8092
// TODO: Check if there is a better way to do that
8193
// Cause right now it looks a little bit silly :3
@@ -93,7 +105,15 @@ pub const Config = struct {
93105
.camera = proxy.camera,
94106
.objects = try allocator.alloc(Object, proxy.objects.len),
95107
.lights = try allocator.alloc(Light, proxy.lights.len),
108+
.assets = try allocator.alloc(Image, proxy.assets.len),
96109
};
110+
for (proxy.assets, 0..) |obj, i| {
111+
const image = rl.LoadImage(obj.imageName);
112+
conf.assets[i] = Image{
113+
.rlImage = image,
114+
.rlColors = rl.LoadImageColors(image),
115+
};
116+
}
97117
for (proxy.objects, 0..) |obj, i| {
98118
if (obj.sphere) |item| {
99119
conf.objects[i] = Object{ .sphere = .{
@@ -117,13 +137,11 @@ pub const Config = struct {
117137
.transform = transform_proxy_to_transform(item.transform),
118138
} };
119139
} else if (obj.triangle) |item| {
120-
const image = rl.LoadImage("megu.png");
121140
conf.objects[i] = Object{ .triangle = .{
122141
.va = item.va,
123142
.vb = item.vb,
124143
.vc = item.vc,
125-
.image = image,
126-
.imageColor = rl.LoadImageColors(image),
144+
.text = &conf.assets[item.textIdx],
127145
} };
128146
} else {
129147
unreachable;
@@ -140,8 +158,4 @@ pub const Config = struct {
140158
}
141159
return conf;
142160
}
143-
144-
camera: Camera,
145-
objects: []Object,
146-
lights: []Light,
147161
};

src/Scene.zig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ const Plane = @import("Plane.zig").Plane;
77
const Transformation = @import("Transformation.zig").Transformation;
88
const std = @import("std");
99
const Triangle = @import("Triangle.zig").Triangle;
10+
const rl = @cImport({
11+
@cInclude("raylib.h");
12+
});
1013

1114
pub const SceneObject = union(enum) {
1215
sphere: Sphere,
@@ -20,6 +23,15 @@ pub const SceneLight = union(enum) {
2023
ambient_light: AmbientLight,
2124
};
2225

26+
pub const Image = struct {
27+
rlImage: rl.Image,
28+
rlColors: [*]rl.Color,
29+
};
30+
31+
pub const Asset = union(enum) {
32+
image: Image,
33+
};
34+
2335
pub const Scene = struct {
2436
const Self = @This();
2537

src/Triangle.zig

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const HitRecord = @import("HitRecord.zig").HitRecord;
44
const Vec3 = @import("Vec3.zig").Vec3;
55
const Material = @import("Material.zig").Material;
66
const std = @import("std");
7+
const Image = @import("Scene.zig").Image;
78
const rl = @cImport({
89
@cInclude("raylib.h");
910
});
@@ -14,8 +15,7 @@ pub const Triangle = struct {
1415
va: Vertex,
1516
vb: Vertex,
1617
vc: Vertex,
17-
imageColor: [*]rl.Color,
18-
image: rl.Image,
18+
text: *const Image,
1919

2020
pub fn hits(self: *const Self, ray: Ray) HitRecord {
2121
// TODO: add bvh + compute this only one time
@@ -45,15 +45,12 @@ pub const Triangle = struct {
4545
return HitRecord.nil();
4646
}
4747

48-
const texCordA: @Vector(2, f32) = .{ 1, 1 };
49-
const texCordB: @Vector(2, f32) = .{ 0, 1 };
50-
const texCordC: @Vector(2, f32) = .{ 0.5, 0 };
5148
const posInImage: @Vector(2, usize) = .{
52-
@as(usize, @intFromFloat((u * texCordA[0] + v * texCordB[0] + w * texCordC[0]) / (u + v + w) * @as(f32, @floatFromInt(self.image.width)))),
53-
@as(usize, @intFromFloat((u * texCordA[1] + v * texCordB[1] + w * texCordC[1]) / (u + v + w) * @as(f32, @floatFromInt(self.image.height)))),
49+
@as(usize, @intFromFloat((u * self.va.texCoord[0] + v * self.vb.texCoord[0] + w * self.vc.texCoord[0]) / (u + v + w) * @as(f32, @floatFromInt(self.text.rlImage.width)))),
50+
@as(usize, @intFromFloat((u * self.va.texCoord[1] + v * self.vb.texCoord[1] + w * self.vc.texCoord[1]) / (u + v + w) * @as(f32, @floatFromInt(self.text.rlImage.height)))),
5451
};
55-
const cInt_to_usize = @as(usize, @intCast(self.image.width));
56-
const color: rl.Color = self.imageColor[posInImage[1] * cInt_to_usize + posInImage[0]];
52+
const cInt_to_usize = @as(usize, @intCast(self.text.rlImage.width));
53+
const color: rl.Color = self.text.rlColors[posInImage[1] * cInt_to_usize + posInImage[0]];
5754
const colorRGB = .{
5855
.r = @as(f32, @floatFromInt(color.r)),
5956
.g = @as(f32, @floatFromInt(color.g)),
@@ -62,7 +59,7 @@ pub const Triangle = struct {
6259

6360
const material: Material = .{
6461
.color = colorRGB,
65-
.reflective = 0.9,
62+
.reflective = 0.75,
6663
.specular = 0,
6764
};
6865
return HitRecord{

src/Vertex.zig

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,5 @@ pub const Vertex = struct {
66
const Self = @This();
77

88
position: Pt3,
9-
color: ColorRGB,
10-
// normal: Vec3,
11-
// texCoord: Vec2, // TODO: have Vec2 (or VecX (useful for quaternion))
9+
texCoord: @Vector(2, f32),
1210
};

0 commit comments

Comments
 (0)