All pastes #1701942 Raw Edit

Unnamed

public lua v1 · immutable
#1701942 ·published 2009-12-04 15:46 UTC
rendered paste body
--------------------------------------------------------------------------------local texfile = arg[1] or 'test.png'--------------------------------------------------------------------------------require 'sfml'require 'gl3'gl = gl3GL = GL3local gl = gl3local GL = GL3win = sfml.window.Create({  title = 'texture test',  sizex = 800, sizey = 600,  titlebar = true,  resizebutton = true,  closebutton = true})win:Show(true)win:SetActive(true)win:Display()---------------------------------------------------------------------------------- GL basicsgl.ClearColor(0, 0, 0, 1)gl.Clear(GL.COLOR_BUFFER_BIT)gl.Enable(GL.BLEND)gl.BlendFunc(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA)gl.Disable(GL.CULL_FACE)gl.Disable(GL.DEPTH_TEST)gl.Disable(GL.STENCIL_TEST)gl.Disable(GL.SCISSOR_TEST)--------------------------------------------------------------------------------local function ClearError()  while (gl.GetError() ~= GL.NO_ERROR) do    -- nothing  endendlocal GetErrorStringdo  local errStrs = {}  for k, v in pairs(GL) do    errStrs[v] = k  end  GetErrorString = function(code)    local str = errStrs[code]    return str and 'GL_'..str or '???'  endendlocal function PrintError(s)  local err = gl.GetError()  if (err ~= GL.NO_ERROR) then    print('GLerror(' .. s .. ') ' .. err .. ' ' .. GetErrorString(err))  endendlocal function ErrorWrap()  for k, v in pairs(gl) do    if (k ~= 'GetError') then      gl[k] = function(...)        ClearError()        local results = { pcall(v, ...) }        if (results[1] == false) then          error(results[2])        end        PrintError(k)        return unpack(results, 2)      end    end  endendErrorWrap()---------------------------------------------------------------------------------- image datalocal f, err = io.open(texfile, 'r')if (not f) then print(err) os.exit(1) endlocal data = f:read('*a')f:close()print('#data', #data)local img = sfml.image.Create(data)local imgX, imgY = img:GetSize()print('img', img, imgX, imgY)img:FlipY()---------------------------------------------------------------------------------- texturegl.ActiveTexture(GL.TEXTURE0)local tex = gl.GenTextures(1)gl.BindTexture(GL.TEXTURE_2D, tex)gl.TexImage2D(  GL.TEXTURE_2D,    -- target  0,                -- level  GL.RGBA,          -- format  imgX,             -- width  imgY,             -- height  0,                -- border  GL.RGBA,          -- dataformat  GL.UNSIGNED_BYTE, -- datatype  img:ToString())gl.GenerateMipmap(GL.TEXTURE_2D)gl.TexParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.LINEAR_MIPMAP_LINEAR)gl.TexParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.LINEAR)print('sizex = ' .. gl.GetTexLevelParameteriv(GL.TEXTURE_2D, 0, GL.TEXTURE_WIDTH))print('sizey = ' .. gl.GetTexLevelParameteriv(GL.TEXTURE_2D, 0, GL.TEXTURE_HEIGHT))print('sizez = ' .. gl.GetTexLevelParameteriv(GL.TEXTURE_2D, 0, GL.TEXTURE_DEPTH))gl.Enable(GL.TEXTURE_2D)---------------------------------------------------------------------------------- shaderlocal function BuildShader(type, source)  local shader = gl.CreateShader(type)  gl.ShaderSource(shader, source)  gl.CompileShader(shader)  local status = gl.GetShaderiv(shader, GL.COMPILE_STATUS)  local infoLog = gl.GetShaderInfoLog(shader)  if (status ~= GL.TRUE) then    print(type, infoLog)    return nil, infoLog  end  return shader, infoLogendlocal vert = BuildShader(GL.VERTEX_SHADER, [[  #version 150 core  uniform mat4 xform;  in vec4 pos;  in vec2 txcd;  in vec4 color;  out vec4 fpos;  out vec2 ftxcd;  out vec4 fcolor;  void main() {    fpos = pos;    ftxcd = txcd;    fcolor = color;    gl_Position = xform * pos;  }]])local frag = BuildShader(GL.FRAGMENT_SHADER, [[  #version 150 core  uniform sampler2D tex;  uniform float fade;  in vec4 fpos;  in vec2 ftxcd;  in vec4 fcolor;  out vec4 gl_FragColor;  void main() {    gl_FragColor = texture2D(tex, ftxcd.xy) * fcolor * fade;    gl_FragDepth = 0.0;  }]])local prog = gl.CreateProgram()gl.AttachShader(prog, vert)gl.AttachShader(prog, frag)gl.LinkProgram(prog)local status = gl.GetProgramiv(prog, GL.LINK_STATUS)if (status ~= GL.TRUE) then  print('LINK ERROR: ' .. gl.GetProgramInfoLog(prog))endgl.UseProgram(prog)local uniforms = gl.GetProgramiv(prog, GL.ACTIVE_UNIFORMS)print('uniforms', uniforms)for i = 0, uniforms - 1 do  print(i, gl.GetActiveUniform(prog, i))endlocal attribs = gl.GetProgramiv(prog, GL.ACTIVE_ATTRIBUTES)print('attribs', attribs)for i = 0, attribs - 1 do  print(i, gl.GetActiveAttrib(prog, i))end-- set the texture unitlocal texLoc  = gl.GetUniformLocation(prog, 'tex')local fadeLoc = gl.GetUniformLocation(prog, 'fade')gl.Uniform1i(texLoc, 0) -- GL.TEXTURE0gl.ValidateProgram(prog)print('prog validate status = '      ..tostring(gl.GetProgramiv(prog, GL.VALIDATE_STATUS)))local matLoc = gl.GetUniformLocation(prog, 'xform')local identity = sfml.PackFloat(1, 0, 0, 0,                                0, 1, 0, 0,                                0, 0, 1, 0,                                0, 0, 0, 1)print('id type', type(identity))gl.UniformMatrix4fv(matLoc, 1, false, identity)---------------------------------------------------------------------------------- vertex attrib locationslocal posLoc   = gl.GetAttribLocation(prog, 'pos')local txcdLoc  = gl.GetAttribLocation(prog, 'txcd')local colorLoc = gl.GetAttribLocation(prog, 'color')print('posLoc = '   .. posLoc)print('txcdLoc = '  .. txcdLoc)print('colorLoc = ' .. colorLoc)---------------------------------------------------------------------------------- vertex array  (separate)-- make a bufferlocal s = 1local verts = sfml.PackFloat(-s, -s, 0,                              s, -s, 0,                             -s,  s, 0,                              s,  s, 0)local txcds = sfml.PackFloat(0, 0,                             1, 0,                             0, 1,                             1, 1)local colors = sfml.PackFloat(0, 1, 1, 1,                              0, 1, 1, 0,                              1, 0, 0, 1,                              1, 0, 0, 0)local posBuf = gl.GenBuffers(1)gl.BindBuffer(GL.ARRAY_BUFFER, posBuf)gl.BufferData(GL.ARRAY_BUFFER, verts, GL.STATIC_DRAW)gl.BindBuffer(GL.ARRAY_BUFFER, 0)local txcdBuf = gl.GenBuffers(1)gl.BindBuffer(GL.ARRAY_BUFFER, txcdBuf)gl.BufferData(GL.ARRAY_BUFFER, txcds, GL.STATIC_DRAW)gl.BindBuffer(GL.ARRAY_BUFFER, 0)local colorBuf = gl.GenBuffers(1)gl.BindBuffer(GL.ARRAY_BUFFER, colorBuf)gl.BufferData(GL.ARRAY_BUFFER, colors, GL.STATIC_DRAW)gl.BindBuffer(GL.ARRAY_BUFFER, 0)-- make a vertex arraylocal va = gl.GenVertexArrays(1)gl.BindVertexArray(va)  gl.BindBuffer(GL.ARRAY_BUFFER, posBuf)  gl.VertexAttribPointer(posLoc,   3, GL.FLOAT, false, 0, nil)  gl.EnableVertexAttribArray(posLoc)  gl.BindBuffer(GL.ARRAY_BUFFER, txcdBuf)  gl.VertexAttribPointer(txcdLoc,  2, GL.FLOAT, false, 0, nil)  gl.EnableVertexAttribArray(txcdLoc)  gl.BindBuffer(GL.ARRAY_BUFFER, colorBuf)  gl.VertexAttribPointer(colorLoc, 4, GL.FLOAT, false, 0, nil)  gl.EnableVertexAttribArray(colorLoc)  gl.BindBuffer(GL.ARRAY_BUFFER, 0)gl.BindVertexArray(0)---------------------------------------------------------------------------------- vertex array  (interleaved)if (-1 > 0) then  -- make a buffer  local s = 1  -- vert(3) / txcd(2) / color(4)  local data = sfml.PackFloat(-s, -s, 0,   0, 0,   0, 1, 1, 1,                               s, -s, 0,   1, 0,   0, 1, 1, 0,                              -s,  s, 0,   0, 1,   1, 0, 0, 1,                               s,  s, 0,   1, 1,   1, 0, 0, 0)  local stride = (4 * 9)  print('data', #data)  local buf = gl.GenBuffers(1)  gl.BindBuffer(GL.ARRAY_BUFFER, buf)  gl.BufferData(GL.ARRAY_BUFFER, data, GL.STATIC_DRAW)  gl.BindBuffer(GL.ARRAY_BUFFER, 0)  -- make a vertex array  va = gl.GenVertexArrays(1)  gl.BindVertexArray(va)    gl.EnableVertexAttribArray(posLoc)    gl.EnableVertexAttribArray(txcdLoc)    gl.EnableVertexAttribArray(colorLoc)    gl.BindBuffer(GL.ARRAY_BUFFER, buf)      gl.VertexAttribPointer(posLoc,   3, GL.FLOAT, false, stride, 4 * 0)      gl.VertexAttribPointer(txcdLoc,  2, GL.FLOAT, false, stride, 4 * 3)      gl.VertexAttribPointer(colorLoc, 4, GL.FLOAT, false, stride, 4 * 5)    gl.BindBuffer(GL.ARRAY_BUFFER, 0)  gl.BindVertexArray(0)end--------------------------------------------------------------------------------local function Draw()  gl.ClearColor(1, 0, 1, 1)  gl.ClearColor(0, 0, 0, 1)  gl.Clear(GL.COLOR_BUFFER_BIT)  gl.BindVertexArray(va)  gl.DrawArrays(GL.TRIANGLE_STRIP, 0, 4)  gl.BindVertexArray(0)endlocal function HandleEvents()  local ev = win:GetEvent()  while (ev) do    if (ev.type == 'close') then      return true    end    if ((ev.type == 'key_pressed') and        (ev.code == sfml.keycodes.Escape)) then      return true    end    print(ev.type)    ev = win:GetEvent()  end  return falseendwin:UseVerticalSync(1)local clock = sfml.CreateClock()while (true) do  gl.Uniform1f(fadeLoc, 0.5 + math.abs(0.5 - (clock.time % 1)))  Draw()  win:Display()  if (HandleEvents()) then    break  endend--------------------------------------------------------------------------------local img = gl.GetTexImage(GL.TEXTURE_2D, 0, GL.RGBA, GL.FLOAT)print('#img', #img)--------------------------------------------------------------------------------print('prog = ' .. tostring(prog))gl.DetachShader(prog, vert)gl.DetachShader(prog, frag)print('isprog', gl.IsProgram(prog))gl.DeleteProgram(prog)print('isprog', gl.IsProgram(prog))print('isshader-v', gl.IsShader(vert))gl.DeleteShader(vert)print('isshader-v', gl.IsShader(vert))print('isshader-f', gl.IsShader(frag))gl.DeleteShader(frag)print('isshader-f', gl.IsShader(frag))print('istex', gl.IsTexture(tex))gl.DeleteTextures(tex)print('istex', gl.IsTexture(tex))print('isbuf', gl.IsBuffer(posBuf))gl.DeleteBuffers(posBuf)print('isbuf', gl.IsBuffer(posBuf))print('isbuf', gl.IsBuffer(txcdBuf))gl.DeleteBuffers(txcdBuf)print('isbuf', gl.IsBuffer(txcdBuf))print('isbuf', gl.IsBuffer(colorBuf))gl.DeleteBuffers(colorBuf)print('isbuf', gl.IsBuffer(colorBuf))--------------------------------------------------------------------------------win:Show(false)win = nil--------------------------------------------------------------------------------