Jump to content

Drawing a section of a 2D texture (Setting UV coords)

Recommended Posts

Hi, I recently upgrading from LE2 to LE3. I'd like to draw a section of a texture directly onto the screen for my GUI. My GUI texture contains lots of elements that I need to draw individually. I achieved this in LE2 using an OpenGL extension where I could set vertices and their texture coordinates, but I can't access OpenGL with LE3 can I? I'm using Context->DrawImage(...) but it has no parameters for texture UV.. So how do I do this? With a shader? I've never used shaders before so I'm completely baffled by them :| Please help! Thank you

Share this post

Link to post

Shadmar can do miracles with shaders and I think he helped someone in the last few months with them so I'd search the forums. But the other option is to copy texture pixels yourself.


The examples are down at the moment and oddly this is one of the functions that can really use it. Hope they come back soon.

Share this post

Link to post

If you are using c++, I believe you can still use opengl commands. Or you can do it via a shader.


Unzip and place this shader in your project's 'Shaders/Drawing' folder:



To Use:

  • shader = Shader:Load("Shaders/Drawing/drawimageparts.shader")
  • shader:SetVec2("uvcoords", Vec2(#,#))
  • shader:SetVec2("zoom", Vec2(#,#))
  • oldshader = context:GetShader()
  • context:SetShader(shader)
  • context:DrawImage(LoadedImageToDraw, x , y, w, h)
  • context:SetShader(oldshader)



Example code showing usage:

function App:Start()

self.window = Window:Create("Part of Image Example",0,0,800,600)

self.context = Context:Create(self.window)

self.world = World:Create()

self.camera = Camera:Create()

self.texture = Texture:Load("Materials/Developer/bluegrid.tex")

self.shader = Shader:Load("Shaders/Drawing/drawimageparts.shader")--load shader

uvcoords = 1.0

zoom = 2.0

return true



function App:Loop()

if (self.window:Closed() or self.window:KeyDown(Key.Escape)) then return false end


uvcoords = uvcoords + ((self.window:KeyHit(Key.Up) and 1 or 0) - (self.window:KeyHit(Key.Down) and 1 or 0)) * 0.1

zoom = zoom + ((self.window:KeyHit(Key.Right) and 1 or 0) - (self.window:KeyHit(Key.Left) and 1 or 0)) * 0.1

self.shader:SetVec2("uvcoords", Vec2(uvcoords))--set uvcoords in shader

self.shader:SetVec2("zoom", Vec2(zoom))--set zoom in shader






self.oldshader = self.context:GetShader()--get current shader


self.context:SetShader(self.shader)--set new shader

self.context:DrawImage(self.texture,405,150,300,300)--clipped/scaled image

self.context:SetShader(self.oldshader)--set back to previous shader


self.context:DrawImage(self.texture,100,150,300,300)--normal drawimage

self.context:DrawText("Press Up/Down to change uvcoords", 0, 2)

self.context:DrawText("UVCoords: "..uvcoords..", "..uvcoords, 0, 22)

self.context:DrawText("Press Right/Left to change zoom", 0, 42)

self.context:DrawText("Zoom: "..zoom..", "..zoom, 0, 62)



return true




Share this post

Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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.

  • Create New...