Tuesday, 9 February 2021

Chapter 19 // Exercise 8, 9 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and a modified version of the std_lib_facilities header found here.

Chapter 19 // Exercise 8

Implement an allocator (section 19.3.7) using the basic allocation functions malloc() and free() (appendix B.11.4). Get vector as defined by the end of section 19.4 to work for a few simple test cases. Hint: Look up "placement new" and "explicit call of destructor" in a complete C++ reference.

Exercise 9

Re-implement vector::operator=() (section 19.2.5) using an allocator (section 19.3.7) for memory managment. (I ended up doing this one automatically as part of 8).

Github: 

I honestly had no clue on this one. It took me a couple of hours of just staring at chapter 19.

I looked up those terms in Bjarne's "The C++ Programming Language 4th ed." and he says that 'explicit calls of destructors should be avoided except in the implementation of resource management classes....However, it would be hard to implement an efficient general container along the lines of the standard-library vector without using explicit destructor calls.'

One very interesting thing I learnt during this exercise is how to detect memory leaks in visual studio when using malloc and free. After I had finished writing my class, I wanted to be absolutely positive that the allocated space was being freed properly so followed this guide to do so:

I then pushed back a few values and exited the program to be greeted with this in the output window:

Interestingly; the destructor for MyVector is not called on exiting main but it is called when MyVector is local to a function. I tested this with a std::vector and it to did not have it's destructor called at the end of main. Now I know for a fact that std::vector will clean up after itself properly so it must mean that this _CrtDumpMemoryLeaks() function dumps the output before MyVector has been destroyed. I'm happy that the class is properly allocating and freeing up memory though when it's local to other functions and I can see it being destroyed.

Then when testing I had a problem with values being destroyed when trying to print. I spent a few hours staring at my screen and eventually went to bed angry. The next day I posted my code and asked for some help in our chat channel at work and my colleagues came to the rescue. Turns out I wasn't actually destroying anything in destroy(). They taught me how to use placement new and call the destructor explicitly.

This exercises took me far longer than I expected but I learnt a ton.

Saturday, 6 February 2021

BlenderGuru's Blender Doughnut Tutorial Review (so good I did it twice!)

If you want to become an indie game developer (or a 3d artist) and you have no money for Maya or 3ds Max then Blender is your saving grace. Thank you Mr Blender creator man for making it free. 

3D modelling is a job in itself but by learning a few shortcuts and basic tools you can create game ready assets in next to no time. In order to do that though you need to learn how to use Blender; enter Andrew Price. I first heard of his tutorial on twitter from one of our environment artists who was doing it during lockdown. It turns out, nearly everyone was making doughnuts during lockdown. I got round to first doing it in October 2020. It took me about 2 weeks but by the end of it I had a nice pink doughnut with sprinkles and a cup of coffee; I was very proud. I then took those skills and quickly modelled a duracell battery I needed for a simple game I was making. 

Andrew takes you through modelling, sculpting, texture painting, lighting, material creation and more with a laid back confidence that's hard to find in tutorials. He doesn't talk to fast, he repeats things, explains when necessary as not to overload you with information and he's also very funny. It kind of feels like a 1-on-1 session with your favourite tutor at uni.

Part 1 focuses on becoming comfortable with the viewport and basic commands like rotate, scale and grab before moving onto sculpting a plain torus into a doughnut shape and creating some icing. Here's a comparison of my attempts:
BlenderGuru's Blender Doughnut Tutorial Review

I was pretty proud of that pink and milky white doughnut back in October but I've definitely improved. I've been doing quite a few material courses and learnt about the importance of roughness, subsurface and other factors like clearcoat. I also spent more time searching for appropriate colours instead of using the colour picker and spent time making sure my lighting was more realistic. Even though there are 3 more levels to go, it feels like I could use that chocolate doughnut as a low-poly stylized game asset.

BlenderGuru's Blender Doughnut Tutorial Review

Level 2 is all about texture painting and adding displacement to a mesh to make it more realistic. I spent a stupid amount of time at the end of this level trying to get the displacement of the donut texture just right. It was one of the major things that bugged me about the 1st donut; it was just too textured. It looks like mid-2000s CGI bread and this is supposed to be a donut. After studying lots of donut pictures and buying a donut myself (that I couldn't even eat because I'm gluten intolerant) I eventually ended up blending two noise textures together to get this slightly bumpy glazed look. Doughnuts are much smoother than you would think given it's a baked treat. It's still not perfect but much more realistic; even if it looks like it should have more texture. I'd like to spend some more time with the pinch and crease sculpting tools though to add some proper detail around the middle as it tends to fold in right where it's been cooking.

I also found that Adobe has a great website called Adobe Colour which allows you to upload an image and create colour palettes from it. You can easily copy the hex values to any program. 

BlenderGuru's Blender Doughnut Tutorial Review


So the third level is the creation of a cup of coffee. I wanted to deviate from the one on the left and used my own reference image of a Bodum glass. They are my favourite glasses; I use them for everything as you don't get any condensation on the outside due to the fact they're double walled. Also, coffee looks beautiful in them as you can see the gradient and the liquid looks like it floating. At first, the glass was just reflecting the inside liquid material however, after messing around with the material settings of the glass I found that setting the alpha to 0.5 allowed you to see through it giving that double walled look. For the foam; instead of using the top part of the liquid mesh and applying a different texture, I duplicated the top face, extruded the edges downwards and then closed them to create a new mesh. This allowed me to deform the foam exactly how I wanted with the sculpting tools without ruining the rest of the liquid (like I did last time, you'll see later). I then stuck a noise texture on top to try and simulate bubbles.

In the first attempt, the liquid was created using volume absorption but I wanted that gradient look. I could've used the nodes in the material editor to procedurally generate it but I opted for UV unwrapping the liquid and then creating a gradient in PhotoShop and texturing it with that.

BlenderGuru's Blender Doughnut Tutorial Review

Level 4 is the final part where Andrew teaches you about depth of field, adding the background and creating an animated scene. I skipped animating it this time, mainly because last time it took my poor gtx 980 six hours to render 20 seconds. I'm incredibly happy with the progress I made this time round. I even added a simple gold plant pot. I didn't make the plant (that was from Quixel) but I did make the tiled print for the plate. I have plates just like these that I bought from Sainsbury's a while ago. I took a photo of them, created a 1k tiled print from it and applied it to the plate.

BlenderGuru's Blender Doughnut Tutorial Review

So there you have it; my donut attempts. I'll probably end up doing this series once more but without Andrew's videos. It's time to take the training wheels off and make a scene myself. 

Sunday, 31 January 2021

Chapter 19 // Exercise 7 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and a modified version of the std_lib_facilities header found here.

Chapter 19 // Exercise 7

Try your solution to exercise 2 with some Numbers


This one got me until I managed to find this post:

I then understood that I was trying to return a variable that was a common type between the two given template types. The compiler doesn't know that Number<int> and Number<double> are common types but Number does have ways to add themselves together and return a common type.

There is a way to define a common_type for user defined classes:

I used this to create common types for the combinations given in the exercise. It was working...in odd ways. I did some more testing and found my Number class broke spectacularly when doing Number<char> operations with any other type of number. 

It took me a while to realise it was because when assigning, it created a copy of the Number<type> with whatever type was the first operand. So for example:

When using ints and chars, it always produces 970 no matter the order. So I'm guessing that the expected behaviour of int * char is always to treat the char as an int. My Number type was not doing this due to default constructing and returning the wrong type.

I eventually found a fix for this by setting the return type of the operator overload to a common type between the two of them:

It looks hideous. I could get rid of some of the ugliness with a typedef but I left it this way for "clarity". The only problem with this, is that all combinations of common types must be declared for the class.

Thursday, 28 January 2021

Chapter 19 // Exercise 6 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and a modified version of the std_lib_facilities header found here.

Chapter 19 // Exercise 6

Repeat the previous exercise, but with a class Number<T> where T can be any numeric type. Try adding % to Number and see what happens when you try to use % for Number<double> and Number<int>.


During this exercise I realised that Concepts were supposed to have become a part of the standard for C++11 and C++14 but were dropped and only now are they being introduced in C++20 (they're still not here yet). Concepts would have been perfect for this exercise as it meant we could restrict Number class to only being numeric types.

I eventually ended up finding this post:

which led me to a function in <type_traits>

So you can stick a static_assert(is_arithmetic_v<T>, "that's not a numeric type") in your class. It will check at compile time if T is a numeric type and if it's not it will print out the error message in the output log instead of some horrible template error.

Very handy. Probably not as flexible as concepts however there are other useful checks you can do with functions in type_traits:

I then wanted to be able to add ints to doubles etc., like you can do with numeric types. You will get a compiler warning due to truncation but it works. As the template already restricts it to common numerical types I didn't have to bother using common_type and templated all the operator overloads to take in a different type. If it's not a numerical type it won't even compile. If you do operations on floats and ints though you get a horrible looking warning from the compiler but it runs and rounds down as expected.

As for modulo, I can't believe it's only now that I realised you can only use it for ints; it doesn't work for floating point values. You have to use fmod(). I tried to allow for it by checking if the type was floating point but the compiler doesn't really seem to care. I thought about it and even the standard double doesn't allow it so why should I?

Wednesday, 20 January 2021

Chapter 19 // Exercise 5 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and a modified version of the std_lib_facilities header found here.

Chapter 19 // Exercise 5

Define a class Int having a single member of class int. Define constructors, assignment, and operators +, -, *, / for it. Test it, and improve its design as needed (e.g., define operators << and >> for convenient I/O).


I think I went a bit overboard with the operator overloading on this one. I tried searching but I couldn't find if there is a way to reduce the amount of overloading as I only want two types allowed; int and Int. I thought about using type_traits and templating the class but then that would mean Int would need a type when declared which isn't in the brief.