Jump to content

Drawing to texture


neseir
 Share

Recommended Posts

Hi

 

Is it possible to use draw commands like the context functions to create a texture runtime ? I think I saw a simular questions where he who asked it solved it by using OpenGL commands instead of functions in Leadwerks but it would be nice if this was supported in the engine.

 

The idea is to create textures with information drawn using functions like drawtext and drawimage and apply the created texture to an entity.

 

Regards

Eirik

Link to comment
Share on other sites

The Buffer API is not officially exposed in the docs yet, but it does work. See Source/Classes/Buffer.h for the commands. You can draw to a buffer, then use the buffer's color component as a texture in another material, and it works fine.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Hi,

 

this is the usage i found out to help our glsl god "shadmar" ;)

 


#include "App.h"
using namespace Leadwerks;
App::App() : window(NULL), context(NULL), world(NULL), camera(NULL), buffer(NULL) {}
App::~App() { delete world; delete window; }
Vec3 camerarotation;
#if defined (PLATFORM_WINDOWS) || defined (PLATFORM_MACOS)
bool freelookmode=true;
#else
bool freelookmode=false;
#endif
bool App::Start()
{
//Create a window
window = Window::Create("ShadmarTest");
//Create a context
context = Context::Create(window);
//Create a world
world = World::Create();
//Create a camera
camera = Camera::Create();
camera->Move(0,2,-5);
//Hide the mouse cursor
window->HideMouse();
std::string mapname = System::GetProperty("map","Maps/start.map");
Map::Load(mapname);
//Move the mouse to the center of the screen
window->SetMousePosition(context->GetWidth()/2,context->GetHeight()/2);
//Create Buffer & Texture
buffer = Buffer::Create(256,256,1,0,0);
buffer->SetColorTexture(Texture::Create(256,256));
return true;
}
bool App::Loop()
{
//Close the window to end the program
if (window->Closed()) return false;

//Press escape to end freelook mode
if (window->KeyHit(Key::Escape))
{
 if (!freelookmode) return false;
 freelookmode=false;
 window->ShowMouse();
}
if (freelookmode)
{
 //Keyboard movement
 float strafe = (window->KeyDown(Key:) - window->KeyDown(Key::A))*Time::GetSpeed() * 0.05;
 float move = (window->KeyDown(Key::W) - window->KeyDown(Key::S))*Time::GetSpeed() * 0.05;
 camera->Move(strafe,0,move);
 //Get the mouse movement
 float sx = context->GetWidth()/2;
 float sy = context->GetHeight()/2;
 Vec3 mouseposition = window->GetMousePosition();
 float dx = mouseposition.x - sx;
 float dy = mouseposition.y - sy;
 //Adjust and set the camera rotation
 camerarotation.x += dy / 10.0;
 camerarotation.y += dx / 10.0;
 camera->SetRotation(camerarotation);
 //Move the mouse to the center of the screen
 window->SetMousePosition(sx,sy);
}
//Update world and Time
Time::Update();
world->Update();
//Render to Buffer (CLS Color set to red)
camera->SetClearColor(1.0f,0.0f,0.0f);
buffer->Enable();
buffer->Clear();
world->Render();
buffer->Disable();
//Render to Backbuffer (normal Context) (CLS Color set to black)
camera->SetClearColor(0.0f,0.0f,0.0f);
Context::SetCurrent(context);
world->Render();
//Draw Buffer
context->DrawImage(buffer->GetColorTexture(),0,0,256,256);
context->Sync(false);
return true;
}

 

I hope this will help you.

  • Upvote 1
  • Intel® Core™ i7-8550U @ 1.80 Ghz 
  • 16GB RAM 
  • INTEL UHD Graphics 620
  • Windows 10 Pro 64-Bit-Version
Link to comment
Share on other sites

You can also use lua and make a prefab scipt (test water prefab script)

 

function Script:Start()
--Create a water material
self.mat = Material:Create()
self.shader = Shader:Load("Shaders/Dynamic/refract+specular.shader") -- Projection shader
self.mat:SetTexture(Texture:Load("Materials/Water/wavemap_Pure3D.tex"),1) -- normal map for refraction
self.mat:SetShader(self.shader)

--Paint me
self.entity:SetMaterial(self.mat);

--create the buffer for color and depth
self.buffer = Buffer:Create(App.window:GetWidth(),App.window:GetHeight(),1,1)
--And a texture for it.
self.buffer:SetColorTexture(Texture:Create(App.window:GetWidth(),App.window:GetHeight()))
end

function Script:Draw()
--Clear and enble the buffer
self.buffer:Clear()
self.buffer:Enable()

--Hide water enity
self.entity:Hide()
--Render to buffer
App.world:Render()
self.buffer:Disable()

--Apply buffer as material
self.mat:SetTexture(self.buffer:GetColorTexture(),0) --get buffer for diffuse
self.mat:SetTexture(self.buffer:GetDepthTexture(),3) --get scene depth

--Switch back to context and show water
Context:SetCurrent(App.context);
self.entity:Show()
end

  • Upvote 2

HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB

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...