Jump to content

Skybox


Paul Thomas
 Share

Recommended Posts

A buddy asked me about the Skybox in my latest blog screenshots. As I had mentioned to him, credit is not mine, I found it perusing the API documents. I'm sure it's somewhere on the forums, but I'm pasting my C++ conversion here. At the time (apparently July 16th) I had shared the code on the "Leadwerks Community Wiki" but my ignorant self had a hard time finding the link again.

Please clean this up for your own purposes. This code is an exact copy conversion from the LUA code written by .. not sure .. probably Josh?

class Sky
{
private:
 
    Model* sky;
    Surface* surface;
    Material* material;
 
public:
 
    Sky(Camera* camera) : sky(nullptr), surface(nullptr), material(nullptr)
    {
        sky = Model::Create();
        surface = sky->AddSurface();
        surface->AddVertex(-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -1.0f);
        surface->AddVertex(0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -1.0f);
        surface->AddVertex(0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f);
        surface->AddVertex(-0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f);
        surface->AddTriangle(2, 1, 0);
        surface->AddTriangle(0, 3, 2);
        sky->SetPosition(camera->GetPosition());
        sky->SetRotation(camera->GetRotation());
        sky->Move(0.0f, 0.0f, camera->GetRange().y - 50.0f);
        sky->SetScale(camera->GetRange().y * 10.0f);
        sky->SetParent(camera);
        material = Material::Load("Materials/Sky/skybox_texture.mat");
        sky->SetMaterial(material);
    }
 
    virtual ~Sky()
    {
        delete material;
        delete surface;
        delete sky;
    }
 
    inline Model* GetSky() { return sky; }
    inline void SetSky(Model* mesh) { sky = mesh; }
    inline Surface* GetSurface() { return surface; }
    inline void SetSurface(Surface* face) { surface = face; }
    inline Material* GetMaterial() { return material; }
    inline void SetMaterial(Material* mat) { material = mat; }
 
};

A camera must be provided as seen in the original LUA code. Here is an example:

Camera* camera = Camera::Create();
Sky* sky = new Sky(camera);

Remember to delete the sky when the game loop is closed.

delete sky;

The "Materials/Sky/skybox_texture.mat" and texture "Materials/Sky/skybox_texture.tex" is already provided by Leadwerks.

This is not for Ultra Engine.

Sharing a snippet library doesn't sound like a bad idea.

Cheers

 

P.S. I was going to attach a .zip file with my personal "sky.h" file, but I decided to paste here directly (no need to download a .zip for a single file - no, I did NOT try uploading just the H file, I'm just pushing forward at this point):

// LEADWERKS 4 / STEAM LEADWERKS C++
// https://leadwerks.com
// https://ultraengine.com

#pragma once

#include "Leadwerks.h"
using namespace Leadwerks;

// SKY
class Sky
{
private:

	// skybox mesh
	Model* sky;
	// skybox surfaces
	Surface* surface;
	// skybox material - @edit: change line 38 to include your own material
	Material* material;
	// camera
	Camera* camera;

	// material path
	std::string materialPath;

public:

	// SKY
	//@notes: A camera is required
	//@params: CAMERA
	Sky(Camera* icamera) : sky(nullptr), surface(nullptr), material(nullptr), camera(icamera), materialPath("Materials/Sky/skybox_texture.mat") { Initialize(); }
	
	// SKY
	//@params: CAMERA, string MATERIAL PATH
	Sky(Camera* icamera, const std::string& ipath) : sky(nullptr), surface(nullptr), material(nullptr), camera(icamera), materialPath(ipath) { Initialize(); }

	// destructor
	virtual ~Sky()
	{
		delete material;
		delete surface;
		delete sky;
	}

	// get/set functions
	inline Model* GetSky() { return sky; }
	inline void SetSky(Model* mesh) { sky = mesh; }
	inline Surface* GetSurface() { return surface; }
	inline void SetSurface(Surface* face) { surface = face; }
	inline Material* GetMaterial() { return material; }
	inline void SetMaterial(Material* mat) { material = mat; }

private:

	// Initialize Sky
	void Initialize()
	{
		sky = Model::Create();
		surface = sky->AddSurface();
		surface->AddVertex(-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -1.0f);
		surface->AddVertex(0.5f, -0.5f, 0.0f, 0.0f, 0.0f, -1.0f);
		surface->AddVertex(0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f);
		surface->AddVertex(-0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f);
		surface->AddTriangle(2, 1, 0);
		surface->AddTriangle(0, 3, 2);

		sky->SetPosition(camera->GetPosition());
		sky->SetRotation(camera->GetRotation());
		sky->Move(0.0f, 0.0f, camera->GetRange().y - 50.0f);
		sky->SetScale(camera->GetRange().y * 10.0f);
		sky->SetParent(camera);

		material = Material::Load(materialPath);
		sky->SetMaterial(material);
	}

};

// Example:
// Camera* camera = Camera::Create();
// Sky* skybox = new Sky(camera);
// If you have a custom material use:
// std::string material_path = "path/to/skybox/material.mat";
// Sky* skybox = new Sky(camera, material_path);
//
// Do not forget to delete when the game loop ends with: delete sky;

 

Link to comment
Share on other sites

Why would you make a class for the sky? Just use world->SetSkybox().

Also, don't use delete with Leadwerks entities. You would use Release() but the world class does this for you and you'll run into crashes if you try to manage entities on your own.

  • Like 1

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

9 hours ago, reepblue said:

Why would you make a class for the sky? Just use world->SetSkybox().

Also, don't use delete with Leadwerks entities. You would use Release() but the world class does this for you and you'll run into crashes if you try to manage entities on your own.

Ah, thanks! I love learning new things every day. I didn't even know world->SetSkybox() exists either. It's not on here: https://www.leadwerks.com/learn?page=API-Reference_Object_World

I need to start relying on just reading the headers LOL

Also, for my purposes, I will soon be doing a lot more than just a skybox. Day/night cycles, weather, clouds, etc.. I want the sky to be completely responsible.

 

  • Like 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...