Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Parse ASS file
- package.path = package.path .. ";E:\\PROJEKTE\\FLuaG\\examples\\advanced\\karaoke\\?.lua"
- require("assparser")("[Hatsune Miku] Monster universe [Original PV].ass")
- --Load textures
- local flying_tex = glGenTextures(3)
- local silhouette_tex = glGenTextures(5)
- local face_tex = glGenTextures(1)
- do
- local image
- for i=1, 3 do
- image = flLoadPNG("textures\\flying" .. i .. ".png")
- glBindTexture(GL_TEXTURE_2D, flying_tex[i])
- glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
- glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width, image.height, 0, GL_RGBA, image)
- end
- for i=1, 5 do
- image = flLoadPNG("textures\\silhouette" .. i .. ".png")
- glBindTexture(GL_TEXTURE_2D, silhouette_tex[i])
- glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
- glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width, image.height, 0, GL_RGBA, image)
- end
- image = flLoadPNG("textures\\face.png")
- glBindTexture(GL_TEXTURE_2D, face_tex[1])
- glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
- glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width, image.height, 0, GL_RGBA, image)
- end
- --Text drawing list
- local textlist = glGenLists(1)
- --Culling for performance
- glEnable(GL_CULL_FACE)
- glFrontFace(GL_CW)
- --Light
- glLightModel(GL_LIGHT_MODEL_AMBIENT, {0, 0, 0, 1})
- glLight(GL_LIGHT0, GL_AMBIENT, {0, 0, 0, 1})
- glLight(GL_LIGHT0, GL_DIFFUSE, {1, 1, 1, 1})
- glLight(GL_LIGHT0, GL_POSITION, {VIDEO_WIDTH/2, VIDEO_HEIGHT/2, 1500, 1})
- glLight(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.8)
- glEnable(GL_LIGHT0)
- glEnable(GL_COLOR_MATERIAL)
- glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
- --Clear value
- glClearDepth(1)
- glDepthFunc(GL_LEQUAL)
- --Help functions
- local function myView(width, height, near, far, zoom)
- glMultMatrix({
- 2/width, 0, 0, 0,
- 0, -2/height, 0, 0,
- 0, 0, -2/(far-near), -1/zoom,
- -1, 1, -(far+near)/(far-near), 1
- })
- end
- local function text_render(text, font, text_depth)
- local text_path = font:getpath(text)
- glNormal(0, 0, 1)
- glNewList(textlist, GL_COMPILE_AND_EXECUTE)
- gluDrawPath( text_path )
- glEndList()
- for i, p in ipairs(text_path) do
- if p.type == GLU_MOVE then glBegin(GL_TRIANGLE_STRIP) end
- local nx, ny, nz
- if i == #text_path then
- nx, ny, nz = math.calcnormal(p.x, p.y, 0, p.x, p.y, -text_depth, text_path[i-1].x, text_path[i-1].y, 0)
- else
- nx, ny, nz = math.calcnormal(p.x, p.y, 0, p.x, p.y, -text_depth, text_path[i+1].x, text_path[i+1].y, 0)
- end
- glNormal(nx, ny, nz)
- --Front
- glVertex(p.x, p.y, 0)
- --Back
- glVertex(p.x, p.y, -text_depth)
- if p.type == GLU_ENDLINE then glEnd() end
- end
- glTranslate(0, 0, -text_depth)
- glNormal(0, 0, -1)
- glFrontFace(GL_CCW)
- glCallList(textlist)
- glFrontFace(GL_CW)
- end
- --Karaoke rendering
- local function kara_render(line, fi, ms)
- --Infade
- if ms < line.start_time then
- local pct = 1 - (line.start_time - ms) / (line.infade/2)
- local x_offset = line.height*(875/580)
- glBindTexture(GL_TEXTURE_2D, silhouette_tex[ math.mod(fi, 5)+1 ])
- glEnable(GL_TEXTURE_2D)
- glColor(1, 1, 1)
- glEnable(GL_BLEND)
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
- if line.styleref.alignment > 6 then
- glPushMatrix()
- glTranslate(pct * (VIDEO_WIDTH + x_offset), 0, 0)
- glBegin(GL_QUADS)
- glTexCoord(1, 0); glVertex(-x_offset, line.top)
- glTexCoord(0, 0); glVertex(0, line.top)
- glTexCoord(0, 1); glVertex(0, line.bottom)
- glTexCoord(1, 1); glVertex(-x_offset, line.bottom)
- glEnd()
- glPopMatrix()
- elseif line.styleref.alignment < 4 then
- glPushMatrix()
- glTranslate((1-pct) * (VIDEO_WIDTH + x_offset), 0, 0)
- glBegin(GL_QUADS)
- glTexCoord(0, 0); glVertex(-x_offset, line.top)
- glTexCoord(1, 0); glVertex(0, line.top)
- glTexCoord(1, 1); glVertex(0, line.bottom)
- glTexCoord(0, 1); glVertex(-x_offset, line.bottom)
- glEnd()
- glPopMatrix()
- end
- glDisable(GL_BLEND)
- glDisable(GL_TEXTURE_2D)
- --Outfade
- elseif ms > line.end_time then
- local pct = 1 - (line.end_time + line.outfade/2 - ms) / (line.outfade/2)
- local x_offset = line.height*1.3
- glBindTexture(GL_TEXTURE_2D, flying_tex[ math.mod(fi, 3)+1 ])
- glEnable(GL_TEXTURE_2D)
- glColor(1, 1, 1)
- glEnable(GL_BLEND)
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
- if line.styleref.alignment > 6 then
- glPushMatrix()
- glTranslate(pct * (VIDEO_WIDTH + x_offset), -(line.top + line.height) * (1 - pct ^ 0.25), 0)
- glBegin(GL_QUADS)
- glTexCoord(0, 0); glVertex(-x_offset, line.top)
- glTexCoord(1, 0); glVertex(0, line.top)
- glTexCoord(1, 1); glVertex(0, line.bottom)
- glTexCoord(0, 1); glVertex(-x_offset, line.bottom)
- glEnd()
- glPopMatrix()
- elseif line.styleref.alignment < 4 then
- glPushMatrix()
- glTranslate((1-pct) * (VIDEO_WIDTH + x_offset), (VIDEO_HEIGHT-line.top) * (1 - pct ^ 0.25), 0)
- glBegin(GL_QUADS)
- glTexCoord(1, 0); glVertex(-x_offset, line.top)
- glTexCoord(0, 0); glVertex(0, line.top)
- glTexCoord(0, 1); glVertex(0, line.bottom)
- glTexCoord(1, 1); glVertex(-x_offset, line.bottom)
- glEnd()
- glPopMatrix()
- end
- glDisable(GL_BLEND)
- glDisable(GL_TEXTURE_2D)
- --Base
- else
- for si, syl in ipairs(line.syls) do
- --Text properties
- glEnable(GL_LIGHTING)
- glColor(1, 1, 1)
- glPushMatrix()
- glTranslate(syl.x, line.y, 0)
- --Effect
- if ms >= line.start_time + syl.start_time and ms < line.start_time + syl.end_time then
- local pct = (ms-syl.start_time-line.start_time)/syl.duration
- glColor(1, 0, 0)
- glTranslate(syl.width/2, syl.height/2, -10)
- glRotate(math.random(-5,5), 1, 0, 0)
- glRotate(math.random(-5,5), 0, 1, 0)
- glTranslate(-syl.width/2, -syl.height/2, 10)
- end
- --Text drawing
- text_render(syl.text, line.styleref.font, 20)
- glPopMatrix()
- glDisable(GL_LIGHTING)
- end
- end
- end
- --Karaoke initialization
- local function karaoke(fi, ms)
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- myView(VIDEO_WIDTH, VIDEO_HEIGHT, -1000, 1000, 2000)
- glMatrixMode(GL_MODELVIEW)
- glLoadIdentity()
- glClear(GL_DEPTH_BUFFER_BIT)
- glEnable(GL_DEPTH_TEST)
- for li, line in ipairs(lines) do
- if line.start_time - line.infade/2 <= ms and line.end_time + line.outfade/2 > ms then
- kara_render(line, fi, ms)
- end
- end
- glDisable(GL_DEPTH_TEST)
- end
- --Deletes original kanjis at top
- local o_red, o_green, o_blue, o_height = 88/255, 98/255, 76/255, 1-2*(130/1080)
- local function grey_overlay()
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- glMatrixMode(GL_MODELVIEW)
- glLoadIdentity()
- glColor(o_red, o_green, o_blue)
- glRect(-1, 1, 1, o_height)
- end
- --Main loop
- function GetFrame(frame_index, frame_ms)
- LoadFrameToContext()
- grey_overlay()
- karaoke(frame_index, frame_ms)
- SaveFrameFromContext()
- end
Add Comment
Please, Sign In to add comment