Skip to content

Commit

Permalink
New changes hehe
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkauzh committed Sep 3, 2024
1 parent 4ddc4c3 commit 9f5483f
Show file tree
Hide file tree
Showing 16 changed files with 268 additions and 49 deletions.
32 changes: 31 additions & 1 deletion internal/gl/opengl.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ type Location struct {
UniformLocation int32
}

type Texture struct {
Texture uint32
}

var VERTEX_SHADER = &VertexShader{}
var FRAGMENT_SHADER = &FragmentShader{}

Expand Down Expand Up @@ -60,6 +64,8 @@ func (p *Program) Link() {}

func (p *Program) Use() {}

func (p *Program) Delete() {}

func (p *Program) UniformLocation(location string) *Location {
return &Location{}
}
Expand All @@ -76,8 +82,32 @@ func (b *Buffer) BindVA() {}

func (b *Buffer) BindVBO() {}

func (b *Buffer) UnBindVA() {}

func (b *Buffer) UnBindVBO() {}

func (b *Buffer) Data() {}

func (b *Buffer) Delete() {}

func GenTexture() *Texture {
return &Texture{}
}

func (t *Texture) Bind() {}

func (t *Texture) UnBind() {}

func InitVertexAttrib() {}

func DrawElements() {}
func DrawElements(corners int) {}

func Clear() {}

func Enable(args ...uint32) {}

func Viewport(width, height int32) {}

func Ortho() {}

func Texture2D() {}
84 changes: 68 additions & 16 deletions internal/gl/opengl/modern.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,34 +58,46 @@ func CompileShader(shader uint32) {
C.glCompileShader(C.uint(shader))
}

func CreateProgram() uint32 {
return uint32(C.glCreateProgram())
}

func AttachShader(program, shader uint32) {
C.glAttachShader(C.uint(program), C.uint(shader))
}

func LinkProgram(program uint32) {
C.glLinkProgram(C.uint(program))
}

func DeleteShader(shader uint32) {
C.glDeleteShader(C.uint(shader))
}

func CreateProgram() uint32 {
return uint32(C.glCreateProgram())
}

func LinkProgram(program uint32) {
C.glLinkProgram(C.uint(program))
}

func UseProgram(program uint32) {
C.glUseProgram(C.uint(program))
}

func DeleteProgram(program uint32) {
C.glDeleteProgram(C.uint(program))
}

func GenBuffers(n int32, buffers *uint32) {
C.glGenBuffers(C.int(n), (*C.uint)(unsafe.Pointer(buffers)))
}

func DeleteBuffers(n int32, buffers *uint32) {
C.glDeleteBuffers(C.int(n), (*C.uint)(unsafe.Pointer(buffers)))
}

func BindVertexArray(array uint32) {
C.glBindVertexArray(C.uint(array))
}

func DeleteVertexArrays(n int32, arrays *uint32) {
C.glDeleteVertexArrays(C.int(n), (*C.uint)(unsafe.Pointer(arrays)))
}

func BindBuffer(target, buffer uint32) {
C.glBindBuffer(C.uint(target), C.uint(buffer))
}
Expand All @@ -106,14 +118,6 @@ func ClearColor(red, green, blue, alpha float32) {
C.glClearColor(C.float(red), C.float(green), C.float(blue), C.float(alpha))
}

func Clear(mask uint32) {
C.glClear(C.uint(mask))
}

func Enable(cap uint32) {
C.glEnable(C.uint(cap))
}

func DrawArrays(mode uint32, first, count int32) {
C.glDrawArrays(C.uint(mode), C.int(first), C.int(count))
}
Expand All @@ -127,3 +131,51 @@ func GetUniformLocation(program uint32, name string) int32 {
func Uniform4f(location int32, v0, v1, v2, v3 float32) {
C.glUniform4f(C.int(location), C.float(v0), C.float(v1), C.float(v2), C.float(v3))
}

func Clear(mask uint32) {
C.glClear(C.uint(mask))
}

func Enable(cap uint32) {
C.glEnable(C.uint(cap))
}

func Viewport(x, y, width, height int32) {
C.glViewport(C.int(x), C.int(y), C.int(width), C.int(height))
}

func Ortho(left, right, bottom, top, near, far float64) {
C.glOrtho(C.GLdouble(left), C.GLdouble(right), C.GLdouble(bottom), C.GLdouble(top), C.GLdouble(near), C.GLdouble(far))
}

func BlendFunc(sfactor, dfactor uint32) {
C.glBlendFunc(C.uint(sfactor), C.uint(dfactor))
}

func DrawElements(mode uint32, count int32, typ uint32, indices unsafe.Pointer) {
C.glDrawElements(C.uint(mode), C.int(count), C.uint(typ), indices)
}

func GenTextures(n int32, textures *uint32) {
C.glGenTextures(C.int(n), (*C.uint)(unsafe.Pointer(textures)))
}

func DeleteTextures(n int32, textures *uint32) {
C.glDeleteTextures(C.int(n), (*C.uint)(unsafe.Pointer(textures)))
}

func BindTexture(target, texture uint32) {
C.glBindTexture(C.uint(target), C.uint(texture))
}

func TexParameteri(target, pname, param uint32) {
C.glTexParameteri(C.uint(target), C.uint(pname), C.int(param))
}

func TexImage2D(target, level, internalformat, width, height, border, format, typ uint32, pixels unsafe.Pointer) {
C.glTexImage2D(C.uint(target), C.int(level), C.int(internalformat), C.int(width), C.int(height), C.int(border), C.uint(format), C.uint(typ), pixels)
}

func GenerateMipmap(target uint32) {
C.glGenerateMipmap(C.uint(target))
}
85 changes: 85 additions & 0 deletions internal/gl/webgl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,88 @@
// +build js,wasm

package gl

import gl "vuelto.me/internal/gl/webgl"

type VertexShader struct{}
type FragmentShader struct{}

type Shader struct {
WebShader string
DesktopShader string

Type any
}

type Program struct {
Program uint32

VertexShader Shader
FragmentShader Shader
}

type Buffer struct {
Vao uint32
Vbo uint32

Vertices []float32
}

type Location struct {
UniformLocation int32
}

var VERTEX_SHADER = &VertexShader{}
var FRAGMENT_SHADER = &FragmentShader{}

func NewShader(shadertype any, webshader, desktopshader string) *Shader {
return &Shader{
Type: shadertype,

WebShader: webshader,
DesktopShader: desktopshader,
}
}

func (s *Shader) Compile() {}

func (s *Shader) Delete() {}

func NewProgram(vertexshader, fragmentshader Shader) *Program {
return &Program{
VertexShader: vertexshader,
FragmentShader: fragmentshader,
}
}

func (p *Program) Link() {}

func (p *Program) Use() {}

func (p *Program) UniformLocation(location string) *Location {
return &Location{}
}

func (l *Location) Set(arg ...float32) {}

func GenBuffers(vertices []float32) *Buffer {
return &Buffer{
Vertices: vertices,
}
}

func (b *Buffer) BindVA() {}

func (b *Buffer) BindVBO() {}

func (b *Buffer) Data() {}

func (b *Buffer) Delete() {}

func InitVertexAttrib() {}

func DrawElements() {}

func Clear() {}

func Enable(args ...uint32) {}
39 changes: 39 additions & 0 deletions internal/image/image_go.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package image

import (
"image"
"image/draw"
_ "image/jpeg"
_ "image/png"
"log"
"os"
)

type Image struct {
Texture []uint8

ImageWidth int
ImageHeight int
}

func Load(imagePath string) *Image {
file, err := os.Open(imagePath)
if err != nil {
log.Fatalln("Failed to open image: ", err)
}
defer file.Close()

img, _, err := image.Decode(file)
if err != nil {
log.Fatalln("Failed to decode image: ", err)
}

rgba := image.NewRGBA(img.Bounds())
draw.Draw(rgba, rgba.Bounds(), img, image.Point{}, draw.Over)

return &Image{
Texture: rgba.Pix,
ImageWidth: rgba.Rect.Size().X,
ImageHeight: rgba.Rect.Size().Y,
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
25 changes: 3 additions & 22 deletions pkg/image.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
package vuelto

import (
"image"
"image/draw"
_ "image/jpeg"
_ "image/png"
"log"

"os"

"vuelto.me/internal/gl/legacy"
"vuelto.me/internal/image"
)

type Image struct {
Expand All @@ -22,26 +15,14 @@ var ImageArray []uint32

// Loads a new image and returns a Image struct. Can be later drawn using the Draw() method
func (r *Renderer2D) LoadImage(imagePath string, x, y, width, height float32) *Image {
file, err := os.Open(imagePath)
if err != nil {
log.Fatalln("Failed to open image: ", err)
}
defer file.Close()

img, _, err := image.Decode(file)
if err != nil {
log.Fatalln("Failed to decode image: ", err)
}

rgba := image.NewRGBA(img.Bounds())
draw.Draw(rgba, rgba.Bounds(), img, image.Point{}, draw.Over)
file := image.Load(imagePath)

var textureID uint32
gl.GenTextures(1, &textureID)

gl.BindTexture(gl.TEXTURE_2D, textureID)

gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, rgba.Rect.Size().X, rgba.Rect.Size().Y, 0, gl.RGBA, gl.UNSIGNED_BYTE, rgba.Pix)
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, file.ImageWidth, file.ImageHeight, 0, gl.RGBA, gl.UNSIGNED_BYTE, file.Texture)

gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
Expand Down
2 changes: 1 addition & 1 deletion pkg/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/go-gl/glfw/v3.3/glfw"

"vuelto.me/internal/gl/legacy"
"vuelto.me/internal/windowing"
windowing "vuelto.me/internal/window"
)

type Window struct {
Expand Down
Loading

0 comments on commit 9f5483f

Please sign in to comment.