Lua_OpenGL_Api - beyond-all-reason/springrts_engine_wiki_mirror GitHub Wiki

Development < Lua Scripting <

Spring

or

or

or

or

or

Matrices & Viewport

To render a 2D billboarded quad in the gameworld at world position x,y,z, you do the following (at a minimum; I am skipping blending / depthtesting) here:
  gl.PushMatrix()
  gl.Texture(read what you need here)
  gl.Translate(x,y,z)
  gl.Billboard()
  gl.TexRect(x1,y1,x2,y2)
  gl.PopMatrix()

Clear

or

or

ResetState/Matrices

  ShadeModel = GL.SMOOTH
  Scissor    = false
  Texture    = false
  Lighting    = false,
  ColorMask  = true, true, true, true
  DepthMask  = false
  DepthTest  = false  (GL.LEQUAL)
  Culling    = false  (GL.BACK)
  LogicOp    = false  (GL.INVERT)
  AlphaTest  = false  (GL.GREATER, 0.5f)
  Blending   = true (GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA)
  Resets GL.TEXTURE matrix stack
  Resets GL.MODELVIEW matrix stack
  Resets GL.PROJECTION matrix stack

Draw Basics

or

or

or

   elements = {
     [1] = { v | vertex   = { number x, number y, number z },           <font color="#55ee99">required</font>
             n | normal   = { number x, number y, number z },           <font color="#55ee99">optional</font>
             t | texcoord = { number x, number y },                     <font color="#55ee99">optional</font>
             c | color    = { number r, number g, number b, number a }  <font color="#55ee99">optional</font>
     },
     etc ...
   }

Display Lists

Display Lists save given vertex attributes in a static buffer, so the GPU can directly read them (without the CPU). Display Lists are a lot faster (10-60x) than standard drawing with glShape/glBeginEnd, so try to use them when ever you can. If you want to learn more about them, g00gle is your friend ;)

Vertex Array Objects

Only available in "develop" branch of spring (the future 105.0+). This is conceptually similar and serve as modern OpenGL replacement to deprecated Display Lists.

See what elements and indices are here: http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-9-vbo-indexing/
numElements and numIndices define the size of for elements and indices respectively. if indices are not going to be used, then set numIndices to 0
Persistent buffers (persistentBuffer = true) are more efficient for dynamically/often updated geometry.

{elementPos, indexPos} define starting element in {element, index} VBO corresponding to 1st element of the supplied vertex data
4 elements table example:
  local verts = {
    p  = {0,0,0,1,  4,0,0,1,  4,4,0,1,  0,4,0,1},
    n  = {0,1,0  ,  0,1,0  ,  0,1,0  ,  0,1,0  },
    uv = {0,0,0,0,  1,0,0,0,  1,1,0,0,  0,1,0,0},
    c0 = {1,0,0,1,  0,1,0,1,  0,0,1,1,  1,1,1,0},
    i = {0, 1, 2, 2, 1, 3},            optional, use in case of indexed VBO
    c1 = {...} }

Alternatively Lua func can use gl.Vertex/gl.Color/gl.Normal/gl.TexCoord to define vertices, similarly how it used to be done in gl.BeginEnd.Note there is no function yet to define indices.

primType can be GL.POINTS, GL.LINE_STRIP, GL.LINE_LOOP, GL.LINES, GL.LINE_STRIP_ADJACENCY, GL.LINES_ADJACENCY, GL.TRIANGLE_STRIP, GL.TRIANGLE_FAN, GL.TRIANGLES, GL.TRIANGLE_STRIP_ADJACENCY, GL.TRIANGLES_ADJACENCY and GL.PATCHES
firstIndex specifies the starting index in the enabled arrays.
count specifies the number of indices to be rendered.

Text

  options are:
    horizontal alignment:
       'c' = center
       'r' = right
    vertical alignment:
       'a' = ascender
       't' = top
       'v' = vertical center
       'x' = baseline
       'b' = bottom
       'd' = descender
    decorations:
       'o' = black outline
       'O' = white outline
       's' = shadow
    other:
       'n' = don't round vertex coords to nearest integer (font may get blurry)

Unit/Feature Draw Functions

States (Blending,AlphaTest...)

or

"add"
 = GL.ONE, GL.ONE

 "alpha_add"
 = GL.SRC_ALPHA, GL.ONE

 "alpha"
 = GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA

 "reset"
 = GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA

 "color"
 = GL.SRC_COLOR, GL.ONE_MINUS_SRC_ALPHA

 "modulate"
 = GL.DST_COLOR, GL.ZERO

 "disable"
 = GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA

or

or

or

or

Stencil

LineWidth & PointSize

Textures

name may be:

  1. 12

= unitDef 12 buildpic

%34:1 = unitDef 34 s3o tex2 (:0->tex1,:1->tex2)

%-102:0 = featureDef 102 s3o tex1

!56

lua generated texture 56 (

return value of gl.CreateTexture)

$units = 3do textureatlas tex1

$units1 = 3do textureatlas tex1

$units2 = 3do textureatlas tex2

$shadow = shadowmap

$specular = specular cube map (removed in the latest develop)

$reflection

reflection cube map (can be mipmaped starting from 104.0.1-714-ga29177f. Set CubeTexGenerateMipMaps

1 in settings)

$shading = ground shading texture

$grass = grass shading texture

$heightmap

heightmap, the height in elmos is in the R channel (needs HeightMapTexture

1 set in spring config)

$minimap = minimap texture

$map_reflection = same as "$reflection"

$sky_reflection = defined for SSMF maps that have a skyReflectModTex

$detail = detail texture

$normals = auto generated normals texture, X normal component is in r,g and b channels, Z normal component is in the A channel. If you need Y you'll need to reconstruct it form X and Z.

$ssmf_normals = SSMF normals texture

$ssmf_specular = SSMF specular texture

$ssmf_splat_distr = SSMF splat distribution texture

$ssmf_splat_detail = SSMF splat detail texture

$ssmf_splat_normals = SSMF splat normals texture , used as "$ssmf_splat_normals" or "$ssmf_splat_normals:X", 'X' can be a value between 0 and 3, defaults to 0 if ":X" is omitted

$ssmf_emission = SSMF emission texture

$ssmf_parallax = SSMF parallax texture

$info = currently active info texture (los, airlos, radar, jammer) Note that textures contain no data until user switches to their respective modes and non-active textures are not updated in the background, so will go stale

$info_losmap = los texture (L-view)

$info_mtlmap = metal map texture (F4-view)

$info_hgtmap = height map texture (F1-view)

$info_blkmap = blocking map texture (F2-view)

$map_gbuffer_normtex = contains the smoothed normals buffer of the map in view in world space coordinates (note that to get true normal vectors from it, you must multiply the vector by 2 and subtract 1)

$map_gbuffer_difftex = contains the diffuse texture buffer of the map in view

$map_gbuffer_spectex = contains the specular textures of the map in view

$map_gbuffer_emittex = for emissive materials (bloom would be the canonical use)

$map_gbuffer_misctex = for arbitrary shader data

$map_gbuffer_zvaltex = contains the depth values (z-buffer) of the map in view. To translate this to world position coordinates, use:

 No`` ``clip`` ``control`` ``/`` ``clipspace`` ``for`` ``depth`` ``is`` ``[-1,1]  vec4 worldpos= viewProjectionInv * vec4(vec3(gl_TexCoord[0].st,   texture2D( map_gbuffer_zvaltex,gl_TexCoord[0].st ).x) * 2.0 - 1.0, 1.0);

 Default`` ``if`` ``clip`` ``control`` ``is`` ``supported`` ``/`` ``clipspace`` ``for`` ``depth`` ``is`` ``[0,1].  vec4 worldpos= viewProjectionInv * vec4(gl_TexCoord[0].st * 2.0 - 1.0,   texture2D( map_gbuffer_zvaltex,gl_TexCoord[0].st ).x, 1.0);

 worldpos.xyz = worldpos.xyz / worldpos.w; 

$model_gbuffer_normtex = contains the smoothed normals buffer of the models in view in world space coordinates (note that to get true normal vectors from it, you must multiply the vector by 2 and subtract 1)

$model_gbuffer_difftex = contains the diffuse texture buffer of the models in view

$model_gbuffer_spectex = contains the specular textures of the models in view

$model_gbuffer_emittex = for emissive materials (bloom would be the canonical use)

$model_gbuffer_misctex = for arbitrary shader data

$model_gbuffer_zvaltex = contains the depth values (z-buffer) of the models in view.

${opt}:/LuaUI/images/image.png = load a texture from the VFS

  possible ${opt}'s are:      'n' = nearest      'l' = linear      'a' = aniso      'i' = invert      'g' = greyed      'c' = clamped      'b' = border      't%r,%g,%b' = tint      'r%width,%height' = resize   The above ${opt}'s are NOT available for .DDS textures! .dds will load faster than other image formats.

   example: ":iac:/LuaUI/images/image.png"    example: ":cr16,16:/LuaUI/images/image.png"    example2: ":t1.0,0.5,0:/LuaUI/images/image.png"  (orange tint)

texProps = {
    [target     = number,](like GL.TEXTURE_1D,GL.TEXTURE_2D,...)(Added support for GL_TEXTURE_2D_MULTISAMPLE in 104.0.1-1015-g8e36ef2 maintenance)
    [format     = number,]
    [min_filter = [[Lua_ConstGL#Texture_Filtering|number]],]
    [mag_filter = [[Lua_ConstGL#Texture_Filtering|number]],]
    [wrap_s     = [[Lua_ConstGL#Texture_Clamping|number]],]
    [wrap_t     = [[Lua_ConstGL#Texture_Clamping|number]],]
    [wrap_r     = [[Lua_ConstGL#Texture_Clamping|number]],]
    [aniso      = number,]
    [border     = boolean,]
    [fbo        = boolean,] (needs GLEW_EXT_framebuffer_object!)
    [fboDepth   = boolean,] (needs GLEW_EXT_framebuffer_object!)
    [samples    = number,] specify any number here and target = GL_TEXTURE_2D_MULTISAMPLE to create multisample texture (Added in 104.0.1-1015-g8e36ef2 maintenance)
  }
 texInfo = {
      xsize = number,
      ysize = number,
      alpha = boolean, //not used yet
      type  = number,  //not used yet
    }

The format of the texture string is the same as in gl.Texture().

or

or

or

or

or

or

The following are primarily intended to be used in conjunction with
     gl.{Unit,Feature}(objID, ..., raw=true)
     gl.{Unit,Feature}Shape(defID, ..., raw=true)
and apply a model's default textures; easier than gl.Texture("$%...")

PixelReadback

{rbracket}}{rbracket}} readPixels = { { { 1 = r, 2 = g, 3 = b, 4 = a}, ... }, ... } |info = This reads back pixels from the final rendered screen. E.g. you can make a screenshot by grabbing the whole view. }}

Lights

or

or

material = {
    ambient   = {number r, number g, number b[, number a]},
    diffuse   = {number r, number g, number b[, number a]},
    ambidiff  = {number r, number g, number b[, number a]},
    emission  = {number r, number g, number b[, number a]},
    specular  = {number r, number g, number b[, number a]},
    shininess = number specularExponent
  }

Hardware relevant

DrawGroundPrimitives

Queries

Extended

or

or

or

or

or

or

Shaders (GLSL)

 Lua_GLSL_Api

category: Lua

⚠️ **GitHub.com Fallback** ⚠️