Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
qmuntal committed Oct 29, 2020
1 parent 378e760 commit f41a409
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 142 deletions.
282 changes: 141 additions & 141 deletions math.go
Original file line number Diff line number Diff line change
@@ -1,141 +1,141 @@
package gltf

import "math"

// NormalizeByte normalize a float32 into a int8
func NormalizeByte(v float32) int8 {
return int8(math.Round(float64(v) * 127))
}

// NormalizeByte denormalize a int8 into a float32
func DenormalizeByte(v int8) float32 {
return float32(math.Max(float64(v)/127, -1))
}

// NormalizeUbyte normalize a float32 into a uint8
func NormalizeUbyte(v float32) uint8 {
return uint8(math.Round(float64(v) * 255))
}

// DenormalizeUbyte denormalize a uint8 into a float32
func DenormalizeUbyte(v uint8) float32 {
return float32(v) / 255
}

// NormalizeShort normalize a float32 into a int16
func NormalizeShort(v float32) int16 {
return int16(math.Round(float64(v) * 32767))
}

// DenormalizeShort denormalize a int16 into a float32
func DenormalizeShort(v int16) float32 {
return float32(math.Max(float64(v)/32767, -1))
}

// NormalizeUshort normalize a float32 into a uint16
func NormalizeUshort(v float32) uint16 {
return uint16(math.Round(float64(v) * 65535))
}

// DenormalizeuShort denormalize a uint16 into a float32
func DenormalizeUshort(v uint16) float32 {
return float32(v) / 65535
}

// NormalizeRGB transform a RGB float32 color (from 0 to 1)
// to its uint8 represtation (from 0 to 255).
func NormalizeRGB(v [3]float32) [3]uint8 {
return [3]uint8{
uint8(deliniarize(v[0]) * 255),
uint8(deliniarize(v[1]) * 255),
uint8(deliniarize(v[2]) * 255),
}
}

// DenormalizeRGB transform a RGB uint8 color (from 0 to 255)
// to its float represtation (from 0 to 1).
func DenormalizeRGB(v [3]uint8) [3]float32 {
return [3]float32{
linearize(float64(v[0]) / 255),
linearize(float64(v[1]) / 255),
linearize(float64(v[2]) / 255),
}
}

// NormalizeRGB transform a RGBA float32 color (from 0 to 1)
// to its uint8 represtation (from 0 to 255).
func NormalizeRGBA(v [4]float32) [4]uint8 {
return [4]uint8{
uint8(deliniarize(v[0]) * 255),
uint8(deliniarize(v[1]) * 255),
uint8(deliniarize(v[2]) * 255),
uint8(v[3] * 255),
}
}

// DenormalizeRGBA transform a RGBA uint8 color (from 0 to 255)
// to its float represtation (from 0 to 1).
func DenormalizeRGBA(v [4]uint8) [4]float32 {
return [4]float32{
linearize(float64(v[0]) / 255),
linearize(float64(v[1]) / 255),
linearize(float64(v[2]) / 255),
float32(v[3]) / 255,
}
}

// NormalizeRGB64 transform a RGB float32 color (from 0 to 1)
// to its uint16 represtation (from 0 to 65535).
func NormalizeRGB64(v [3]float32) [3]uint16 {
return [3]uint16{
uint16(deliniarize(v[0]) * 65535),
uint16(deliniarize(v[1]) * 65535),
uint16(deliniarize(v[2]) * 65535),
}
}

// DenormalizeRGB64 transform a RGB uint16 color (from 0 to 65535)
// to its float represtation (from 0 to 1).
func DenormalizeRGB64(v [3]uint16) [3]float32 {
return [3]float32{
linearize(float64(v[0]) / 65535),
linearize(float64(v[1]) / 65535),
linearize(float64(v[2]) / 65535),
}
}

// NormalizeRGBA64 transform a RGBA float32 color (from 0 to 1)
// to its uint16 represtation (from 0 to 65535).
func NormalizeRGBA64(v [4]float32) [4]uint16 {
return [4]uint16{
uint16(deliniarize(v[0]) * 65535),
uint16(deliniarize(v[1]) * 65535),
uint16(deliniarize(v[2]) * 65535),
uint16(v[3] * 65535),
}
}

// DenormalizeRGBA64 transform a RGBA uint16 color (from 0 to 65535)
// to its float represtation (from 0 to 1).
func DenormalizeRGBA64(v [4]uint16) [4]float32 {
return [4]float32{
linearize(float64(v[0]) / 65535),
linearize(float64(v[1]) / 65535),
linearize(float64(v[2]) / 65535),
float32(v[3]) / 65535,
}
}

func linearize(v float64) float32 {
if v <= 0.04045 {
return float32(v / 12.92)
}
return float32(math.Pow((v+0.055)/1.055, 2.4))
}

func deliniarize(v float32) float32 {
if v < 0.0031308 {
return v * 12.92
}
return float32(1.055*math.Pow(float64(v), 1.0/2.4) - 0.055)
}
package gltf

import "math"

// NormalizeByte normalize a float32 into a int8
func NormalizeByte(v float32) int8 {
return int8(math.Round(float64(v) * 127))
}

// NormalizeByte denormalize a int8 into a float32
func DenormalizeByte(v int8) float32 {
return float32(math.Max(float64(v)/127, -1))
}

// NormalizeUbyte normalize a float32 into a uint8
func NormalizeUbyte(v float32) uint8 {
return uint8(math.Round(float64(v) * 255))
}

// DenormalizeUbyte denormalize a uint8 into a float32
func DenormalizeUbyte(v uint8) float32 {
return float32(v) / 255
}

// NormalizeShort normalize a float32 into a int16
func NormalizeShort(v float32) int16 {
return int16(math.Round(float64(v) * 32767))
}

// DenormalizeShort denormalize a int16 into a float32
func DenormalizeShort(v int16) float32 {
return float32(math.Max(float64(v)/32767, -1))
}

// NormalizeUshort normalize a float32 into a uint16
func NormalizeUshort(v float32) uint16 {
return uint16(math.Round(float64(v) * 65535))
}

// DenormalizeuShort denormalize a uint16 into a float32
func DenormalizeUshort(v uint16) float32 {
return float32(v) / 65535
}

// NormalizeRGB transform a RGB float32 color (from 0 to 1)
// to its uint8 represtation (from 0 to 255).
func NormalizeRGB(v [3]float32) [3]uint8 {
return [3]uint8{
uint8(deliniarize(v[0]) * 255),
uint8(deliniarize(v[1]) * 255),
uint8(deliniarize(v[2]) * 255),
}
}

// DenormalizeRGB transform a RGB uint8 color (from 0 to 255)
// to its float represtation (from 0 to 1).
func DenormalizeRGB(v [3]uint8) [3]float32 {
return [3]float32{
linearize(float64(v[0]) / 255),
linearize(float64(v[1]) / 255),
linearize(float64(v[2]) / 255),
}
}

// NormalizeRGB transform a RGBA float32 color (from 0 to 1)
// to its uint8 represtation (from 0 to 255).
func NormalizeRGBA(v [4]float32) [4]uint8 {
return [4]uint8{
uint8(deliniarize(v[0]) * 255),
uint8(deliniarize(v[1]) * 255),
uint8(deliniarize(v[2]) * 255),
uint8(v[3] * 255),
}
}

// DenormalizeRGBA transform a RGBA uint8 color (from 0 to 255)
// to its float represtation (from 0 to 1).
func DenormalizeRGBA(v [4]uint8) [4]float32 {
return [4]float32{
linearize(float64(v[0]) / 255),
linearize(float64(v[1]) / 255),
linearize(float64(v[2]) / 255),
float32(v[3]) / 255,
}
}

// NormalizeRGB64 transform a RGB float32 color (from 0 to 1)
// to its uint16 represtation (from 0 to 65535).
func NormalizeRGB64(v [3]float32) [3]uint16 {
return [3]uint16{
uint16(deliniarize(v[0]) * 65535),
uint16(deliniarize(v[1]) * 65535),
uint16(deliniarize(v[2]) * 65535),
}
}

// DenormalizeRGB64 transform a RGB uint16 color (from 0 to 65535)
// to its float represtation (from 0 to 1).
func DenormalizeRGB64(v [3]uint16) [3]float32 {
return [3]float32{
linearize(float64(v[0]) / 65535),
linearize(float64(v[1]) / 65535),
linearize(float64(v[2]) / 65535),
}
}

// NormalizeRGBA64 transform a RGBA float32 color (from 0 to 1)
// to its uint16 represtation (from 0 to 65535).
func NormalizeRGBA64(v [4]float32) [4]uint16 {
return [4]uint16{
uint16(deliniarize(v[0]) * 65535),
uint16(deliniarize(v[1]) * 65535),
uint16(deliniarize(v[2]) * 65535),
uint16(v[3] * 65535),
}
}

// DenormalizeRGBA64 transform a RGBA uint16 color (from 0 to 65535)
// to its float represtation (from 0 to 1).
func DenormalizeRGBA64(v [4]uint16) [4]float32 {
return [4]float32{
linearize(float64(v[0]) / 65535),
linearize(float64(v[1]) / 65535),
linearize(float64(v[2]) / 65535),
float32(v[3]) / 65535,
}
}

func linearize(v float64) float32 {
if v <= 0.04045 {
return float32(v / 12.92)
}
return float32(math.Pow((v+0.055)/1.055, 2.4))
}

func deliniarize(v float32) float32 {
if v < 0.0031308 {
return v * 12.92
}
return float32(1.055*math.Pow(float64(v), 1.0/2.4) - 0.055)
}
2 changes: 1 addition & 1 deletion modeler/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestReadAccessor(t *testing.T) {
Buffer: 0, ByteLength: 6, ByteOffset: 3,
}}}, &gltf.Accessor{
BufferView: gltf.Index(1), ByteOffset: 3, ComponentType: gltf.ComponentUbyte, Type: gltf.AccessorScalar, Count: 3,
}}, []byte{7, 8, 9}, false},
}}, nil, true},
{"interleaved", args{&gltf.Document{
Buffers: []*gltf.Buffer{{ByteLength: 52, Data: []byte{
0, 0, 0, 0,
Expand Down

0 comments on commit f41a409

Please sign in to comment.