Created
August 3, 2023 01:44
-
-
Save jamesu/56ae068c06ae84a27d3bcabbc6735259 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Simple script - Print out pseudo-code for glTexGen* states | |
# | |
# Author: James Urquhart, [email protected] | |
# | |
# Put your code in the doCode at the end of the file. | |
# | |
require 'json' | |
GL_TEXTURE_2D = 1 | |
GL_TEXTURE_ENV = :texture_env | |
GL_TEXTURE_ENV_MODE = :texture_env_mode | |
GL_COMBINE_RGB = :combine_rgb | |
GL_SOURCE0_RGB = :source0_rgb | |
GL_OPERAND0_RGB = :operand0_rgb | |
GL_COMBINE_ALPHA = :combine_alpha | |
GL_SOURCE0_ALPHA = :source0_alpha | |
GL_OPERAND0_ALPHA = :operand0_alpha | |
GL_TEXTURE_ENV_COLOR = :texture_env_color | |
GL_SOURCE1_ALPHA = :source1_alpha | |
GL_OPERAND1_ALPHA = :operand1_alpha | |
GL_SOURCE2_ALPHA = :source2_alpha | |
GL_OPERAND2_ALPHA = :operand2_alpha | |
GL_SOURCE1_RGB = :source1_rgb | |
GL_OPERAND1_RGB = :operand1_rgb | |
GL_SOURCE2_RGB = :source2_rgb | |
GL_OPERAND2_RGB = :operand2_rgb | |
GL_COMBINE = :combine | |
GL_REPLACE = :replace | |
GL_PREVIOUS = :previous | |
GL_SRC_COLOR = :src_color | |
GL_INTERPOLATE = :interpolate | |
GL_CONSTANT = :constant | |
GL_SRC_ALPHA = :src_alpha | |
GL_TEXTURE = :texture | |
GL_RGB_SCALE = :rgb_scale | |
GL_ALPHA_SCALE = :alpha_scale | |
GL_MODULATE = :modulate | |
GL_DECAL = :decal | |
GL_ADD = :add | |
GL_PRIMARY_COLOR = :primary_color | |
$units = [] | |
$active_unit = nil | |
def glActiveTextureARB(unit) | |
if unit >= $units.count | |
diff = ((unit+1) - $units.count) | |
diff.times { $units << { | |
GL_OPERAND0_RGB => GL_SRC_COLOR, | |
GL_OPERAND1_RGB => GL_SRC_COLOR, | |
GL_OPERAND2_RGB => GL_SRC_COLOR, | |
GL_OPERAND0_ALPHA => GL_SRC_ALPHA, | |
GL_OPERAND1_ALPHA => GL_SRC_ALPHA, | |
GL_OPERAND2_ALPHA => GL_SRC_ALPHA, | |
} } | |
end | |
$active_unit = $units[unit] | |
end | |
def glTexEnvi(mode, param, value) | |
$active_unit[param] = value | |
end | |
def glTexEnvf(mode, param, value) | |
$active_unit[param] = value | |
end | |
def glTexEnvfv(mode, param, values) | |
$active_unit[param] = values | |
end | |
def glEnable(mode) | |
if mode == GL_TEXTURE_2D | |
$active_unit[:textured] = true | |
end | |
end | |
def glDisable(mode) | |
if mode == GL_TEXTURE_2D | |
$active_unit[:textured] = false | |
end | |
end | |
def operandRemap(source, operand) | |
case operand | |
when GL_SRC_COLOR | |
"#{source}.rgb" | |
when GL_SRC_ALPHA | |
"#{source}.a" | |
else | |
raise Exception.new("Unhandled operand #{operand}") | |
end | |
end | |
def wrapUpAndPrint | |
puts "// JSON Info" | |
puts JSON.pretty_generate($units) | |
puts "// Pseudo-code" | |
$units.each_with_index do |unit, index| | |
puts "// Unit #{index}" | |
arg0 = unit[:textured] ? "sampler#{index}.sample(tex#{index}, in.uv#{index})": 'float1(1,1,1,1)' | |
arg1 = "vertCol" | |
if unit[GL_TEXTURE_ENV_MODE] == GL_COMBINE | |
rgbCombine = unit[GL_COMBINE_RGB] | |
alphaCombine = unit[GL_COMBINE_ALPHA] | |
if unit[GL_SOURCE0_RGB] == GL_TEXTURE || | |
unit[GL_SOURCE1_RGB] == GL_TEXTURE || | |
unit[GL_SOURCE1_RGB] == GL_TEXTURE || | |
unit[GL_SOURCE0_ALPHA] == GL_TEXTURE || | |
unit[GL_SOURCE1_ALPHA] == GL_TEXTURE || | |
unit[GL_SOURCE2_ALPHA] == GL_TEXTURE | |
puts "texture = #{arg0};" | |
end | |
arg0 = operandRemap(unit[GL_SOURCE0_RGB], unit[GL_OPERAND0_RGB]) | |
arg1 = operandRemap(unit[GL_SOURCE1_RGB], unit[GL_OPERAND1_RGB]) | |
arg2 = operandRemap(unit[GL_SOURCE2_RGB], unit[GL_OPERAND2_RGB]) | |
case rgbCombine | |
when GL_REPLACE | |
puts "combine.rgb = #{arg0}; // replace" | |
when GL_MODULATE | |
puts "combine.rgb = #{arg0} * #{arg1}; // modulate" | |
when GL_INTERPOLATE | |
puts "combine.rgb = (#{arg0} * #{arg2}) + (#{arg1} * (1.0 - #{arg2})); // interpolate" | |
end | |
arg0 = operandRemap(unit[GL_SOURCE0_ALPHA], unit[GL_OPERAND0_ALPHA]) | |
arg1 = operandRemap(unit[GL_SOURCE1_ALPHA], unit[GL_OPERAND1_ALPHA]) | |
arg2 = operandRemap(unit[GL_SOURCE2_ALPHA], unit[GL_OPERAND2_ALPHA]) | |
case alphaCombine | |
when GL_REPLACE | |
puts "combine.a = #{arg0}; // replace" | |
when GL_MODULATE | |
puts "combine.a = #{arg0} * #{arg2}; // modulate" | |
when GL_INTERPOLATE | |
puts "combine.a = (#{arg0} * #{arg2}) + (#{arg1} * (1.0 - #{arg2})); // interpolate" | |
end | |
if !unit[GL_RGB_SCALE].nil? && (unit[GL_RGB_SCALE] != 1.0) | |
puts "previous.rgb = saturate(combine.rgb * #{unit[GL_RGB_SCALE]});" | |
else | |
puts "previous.rgb = combine.rgb;" | |
end | |
if !unit[GL_ALPHA_SCALE].nil? && (unit[GL_ALPHA_SCALE] != 1.0) | |
puts "previous.a = saturate(combine.a * #{unit[GL_ALPHA_SCALE]});" | |
else | |
puts "previous.a = combine.a;" | |
end | |
elsif unit[GL_TEXTURE_ENV_MODE] == GL_REPLACE | |
puts "previous = #{arg0}" | |
if !unit[GL_RGB_SCALE].nil? && (unit[GL_RGB_SCALE] != 1.0) | |
puts "previous.rgb = saturate(previous.rgb * #{unit[GL_RGB_SCALE]});" | |
end | |
if !unit[GL_ALPHA_SCALE].nil? && (unit[GL_ALPHA_SCALE] != 1.0) | |
puts "previous.a = saturate(previous.a * #{unit[GL_ALPHA_SCALE]});" | |
end | |
elsif unit[GL_TEXTURE_ENV_MODE] == GL_MODULATE | |
puts "modulate = #{arg0} * #{arg1};" | |
if !unit[GL_RGB_SCALE].nil? && (unit[GL_RGB_SCALE] != 1.0) | |
puts "previous.rgb = saturate(modulate.rgb * #{unit[GL_RGB_SCALE]});" | |
else | |
puts "previous.rgb = modulate.rgb;" | |
end | |
if !unit[GL_ALPHA_SCALE].nil? && (unit[GL_ALPHA_SCALE] != 1.0) | |
puts "previous.a = saturate(modulate.a * #{unit[GL_ALPHA_SCALE]});" | |
else | |
puts "previous.a = modulate.a;" | |
end | |
elsif unit[GL_TEXTURE_ENV_MODE] == GL_DECAL | |
arg1 = 'previous' | |
if !unit[GL_RGB_SCALE].nil? && (unit[GL_RGB_SCALE] != 1.0) | |
puts "previous = saturate(float4((#{arg0}.rgb * #{arg0}.a) + (#{arg1}.rgb * (1.0f - #{arg0}.a)), #{arg1}.a) * unit[GL_RGB_SCALE]);" | |
else | |
puts "previous = float4((#{arg0}.rgb * #{arg0}.a) + (#{arg1}.rgb * (1.0f - #{arg0}.a)), #{arg1}.a);" | |
end | |
else | |
raise Exception.new("Unhandled #{unit[GL_TEXTURE_ENV_MODE]}") | |
end | |
end | |
end | |
def doCode | |
glActiveTextureARB(0) | |
glEnable(GL_TEXTURE_2D) | |
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_COMBINE); | |
glTexEnvi(GL_TEXTURE_ENV,GL_COMBINE_RGB,GL_REPLACE); | |
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE0_RGB,GL_PREVIOUS); | |
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND0_RGB,GL_SRC_COLOR); | |
glTexEnvi(GL_TEXTURE_ENV,GL_COMBINE_ALPHA,GL_INTERPOLATE); | |
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE0_ALPHA,GL_CONSTANT); | |
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND0_ALPHA,GL_SRC_ALPHA); | |
glTexEnvfv(GL_TEXTURE_ENV,GL_TEXTURE_ENV_COLOR,'float4(' + [1.0,1.0,1.0,1.0].join(',') + ')'); | |
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE1_ALPHA,GL_TEXTURE); | |
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND1_ALPHA,GL_SRC_ALPHA); | |
glTexEnvi(GL_TEXTURE_ENV,GL_SOURCE2_ALPHA,GL_PREVIOUS); | |
glTexEnvi(GL_TEXTURE_ENV,GL_OPERAND2_ALPHA,GL_SRC_ALPHA) | |
end | |
doCode | |
wrapUpAndPrint | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment