Parcourir la source

Add compressed texture support

Added compressed texture methods to Texture2D and updated render setup to determine which OpenGL method to use. Also added C wrapper func in gls.
Adam Leyland il y a 6 ans
Parent
commit
b6dc5ed49c
2 fichiers modifiés avec 63 ajouts et 10 suppressions
  1. 13 0
      gls/gls-desktop.go
  2. 50 10
      texture/texture2D.go

+ 13 - 0
gls/gls-desktop.go

@@ -601,6 +601,19 @@ func (gs *GLS) TexImage2D(target uint32, level int32, iformat int32, width int32
 		ptr(data))
 }
 
+// CompressedTexImage2D specified a two-dimensional compressed texture image.
+func (gs *GLS) CompressedTexImage2D(target uint32, level uint32, iformat uint32, width int32, height int32, size int32, data interface{}) {
+
+	C.glCompressedTexImage2D(C.GLenum(target),
+		C.GLint(level),
+		C.GLenum(iformat),
+		C.GLsizei(width),
+		C.GLsizei(height),
+		C.GLint(0),
+		C.GLsizei(size),
+		ptr(data))
+}
+
 // TexParameteri sets the specified texture parameter on the specified texture.
 func (gs *GLS) TexParameteri(target uint32, pname uint32, param int32) {
 

+ 50 - 10
texture/texture2D.go

@@ -38,6 +38,8 @@ type Texture2D struct {
 	updateData   bool        // texture data needs to be sent
 	updateParams bool        // texture parameters needs to be sent
 	genMipmap    bool        // generate mipmaps flag
+	compressed   bool        // whether the texture is compressed
+	size         int32       // the size of the texture data in bytes
 	data         interface{} // array with texture data
 	uniUnit      gls.Uniform // Texture unit uniform location cache
 	uniInfo      gls.Uniform // Texture info uniform location cache
@@ -107,6 +109,13 @@ func NewTexture2DFromData(width, height int, format int, formatType, iformat int
 	return t
 }
 
+func NewTexture2DFromCompressedData(width, height int, iformat int32, size int32, data interface{}) *Texture2D {
+
+	t := newTexture2D()
+	t.SetDataCompressed(width, height, iformat, size, data)
+	return t
+}
+
 // Incref increments the reference count for this texture
 // and returns a pointer to the geometry.
 // It should be used when this texture is shared by another
@@ -178,6 +187,19 @@ func (t *Texture2D) SetData(width, height int, format int, formatType, iformat i
 	t.format = uint32(format)
 	t.formatType = uint32(formatType)
 	t.iformat = int32(iformat)
+	t.compressed = false
+	t.data = data
+	t.updateData = true
+}
+
+// SetDataCompressed sets the compressed texture data
+func (t *Texture2D) SetDataCompressed(width, height int, iformat int32, size int32, data interface{}) {
+
+	t.width = int32(width)
+	t.height = int32(height)
+	t.iformat = iformat
+	t.compressed = true
+	t.size = size
 	t.data = data
 	t.updateData = true
 }
@@ -282,6 +304,12 @@ func (t *Texture2D) Height() int {
 	return int(t.height)
 }
 
+// Compressed returns whether this texture is compressed
+func (t *Texture2D) Compressed() bool {
+
+	return t.compressed
+}
+
 // DecodeImage reads and decodes the specified image file into RGBA8.
 // The supported image files are PNG, JPEG and GIF.
 func DecodeImage(imgfile string) (*image.RGBA, error) {
@@ -323,16 +351,28 @@ func (t *Texture2D) RenderSetup(gs *gls.GLS, slotIdx, uniIdx int) { // Could hav
 
 	// Transfer texture data to OpenGL if necessary
 	if t.updateData {
-		gs.TexImage2D(
-			gls.TEXTURE_2D, // texture type
-			0,              // level of detail
-			t.iformat,      // internal format
-			t.width,        // width in texels
-			t.height,       // height in texels
-			t.format,       // format of supplied texture data
-			t.formatType,   // type of external format color component
-			t.data,         // image data
-		)
+		if t.compressed {
+			gs.CompressedTexImage2D(
+				gls.TEXTURE_2D,
+				0,
+				uint32(t.iformat),
+				t.width,
+				t.height,
+				t.size,
+				t.data,
+			)
+		} else {
+			gs.TexImage2D(
+				gls.TEXTURE_2D, // texture type
+				0,              // level of detail
+				t.iformat,      // internal format
+				t.width,        // width in texels
+				t.height,       // height in texels
+				t.format,       // format of supplied texture data
+				t.formatType,   // type of external format color component
+				t.data,         // image data
+			)
+		}
 		// Generates mipmaps if requested
 		if t.genMipmap {
 			gs.GenerateMipmap(gls.TEXTURE_2D)