Fix shaders

This commit is contained in:
Milan Nikolic 2021-05-26 13:09:06 +02:00
parent 400097df9d
commit 620d205532
No known key found for this signature in database
GPG key ID: 9229D0EAA3AA4E75
8 changed files with 22317 additions and 54982 deletions

File diff suppressed because it is too large Load diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 363 KiB

File diff suppressed because it is too large Load diff

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 MiB

View file

@ -13,15 +13,16 @@ func main() {
rl.InitWindow(screenWidth, screenHeight, "raylib [shaders] example - custom uniform variable")
camera := rl.Camera{}
camera.Position = rl.NewVector3(3.0, 3.0, 3.0)
camera.Position = rl.NewVector3(8.0, 8.0, 8.0)
camera.Target = rl.NewVector3(0.0, 1.5, 0.0)
camera.Up = rl.NewVector3(0.0, 1.0, 0.0)
camera.Fovy = 45.0
camera.Projection = rl.CameraPerspective
dwarf := rl.LoadModel("dwarf.obj") // Load OBJ model
texture := rl.LoadTexture("dwarf_diffuse.png") // Load model texture
obj := rl.LoadModel("barracks.obj") // Load OBJ model
texture := rl.LoadTexture("barracks_diffuse.png") // Load model texture
rl.SetMaterialTexture(dwarf.Materials, rl.MapDiffuse, texture) // Set dwarf model diffuse texture
rl.SetMaterialTexture(obj.Materials, rl.MapDiffuse, texture) // Set obj model diffuse texture
position := rl.NewVector3(0.0, 0.0, 0.0) // Set model position
@ -52,7 +53,7 @@ func main() {
swirlCenter[1] = float32(screenHeight) - mousePosition.Y
// Send new value to the shader to be used on drawing
rl.SetShaderValue(shader, swirlCenterLoc, swirlCenter, 1)
rl.SetShaderValue(shader, swirlCenterLoc, swirlCenter, rl.ShaderUniformVec2)
rl.UpdateCamera(&camera) // Update camera
@ -62,9 +63,11 @@ func main() {
rl.BeginTextureMode(target) // Enable drawing to texture
rl.ClearBackground(rl.RayWhite)
rl.BeginMode3D(camera)
rl.DrawModel(dwarf, position, 2.0, rl.White) // Draw 3d model with texture
rl.DrawModel(obj, position, 0.5, rl.White) // Draw 3d model with texture
rl.DrawGrid(10, 1.0) // Draw a grid
@ -81,7 +84,7 @@ func main() {
rl.EndShaderMode()
rl.DrawText("(c) Dwarf 3D model by David Moreno", screenWidth-200, screenHeight-20, 10, rl.Gray)
rl.DrawText("(c) Barracks 3D model by Alberto Cano", screenWidth-200, screenHeight-20, 10, rl.Gray)
rl.DrawFPS(10, 10)
@ -90,7 +93,7 @@ func main() {
rl.UnloadShader(shader) // Unload shader
rl.UnloadTexture(texture) // Unload texture
rl.UnloadModel(dwarf) // Unload model
rl.UnloadModel(obj) // Unload model
rl.UnloadRenderTexture(target) // Unload render texture
rl.CloseWindow()

View file

@ -102,6 +102,8 @@ func main() {
rl.BeginTextureMode(target) // Enable drawing to texture
rl.ClearBackground(rl.RayWhite)
rl.BeginMode3D(camera)
rl.DrawModel(obj, position, 0.1, rl.White) // Draw 3d model with texture

View file

@ -144,6 +144,22 @@ const (
CameraOrthographic
)
// ShaderUniformDataType type
type ShaderUniformDataType int32
// ShaderUniformDataType enumeration
const (
ShaderUniformFloat ShaderUniformDataType = iota
ShaderUniformVec2
ShaderUniformVec3
ShaderUniformVec4
ShaderUniformInt
ShaderUniformIvec2
ShaderUniformIvec3
ShaderUniformIvec4
ShaderUniformSampler2d
)
// Some basic Defines
const (
Pi = 3.1415927

View file

@ -79,21 +79,50 @@ func GetShaderLocation(shader Shader, uniformName string) int32 {
return v
}
// SetShaderValue - Set shader uniform value (float)
func SetShaderValue(shader Shader, uniformLoc int32, value []float32, size int32) {
// GetShaderLocationAttrib - Get shader attribute location
func GetShaderLocationAttrib(shader Shader, attribName string) int32 {
cshader := shader.cptr()
cuniformLoc := (C.int)(uniformLoc)
cuniformName := C.CString(attribName)
defer C.free(unsafe.Pointer(cuniformName))
ret := C.GetShaderLocationAttrib(*cshader, cuniformName)
v := (int32)(ret)
return v
}
// SetShaderValue - Set shader uniform value (float)
func SetShaderValue(shader Shader, locIndex int32, value []float32, uniformType ShaderUniformDataType) {
cshader := shader.cptr()
clocIndex := (C.int)(locIndex)
cvalue := unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&value)).Data)
csize := (C.int)(size)
C.SetShaderValue(*cshader, cuniformLoc, cvalue, csize)
cuniformType := (C.int)(uniformType)
C.SetShaderValue(*cshader, clocIndex, cvalue, cuniformType)
}
// SetShaderValueV - Set shader uniform value (float)
func SetShaderValueV(shader Shader, locIndex int32, value []float32, uniformType ShaderUniformDataType, count int32) {
cshader := shader.cptr()
clocIndex := (C.int)(locIndex)
cvalue := unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&value)).Data)
cuniformType := (C.int)(uniformType)
ccount := (C.int)(count)
C.SetShaderValueV(*cshader, clocIndex, cvalue, cuniformType, ccount)
}
// SetShaderValueMatrix - Set shader uniform value (matrix 4x4)
func SetShaderValueMatrix(shader Shader, uniformLoc int32, mat Matrix) {
func SetShaderValueMatrix(shader Shader, locIndex int32, mat Matrix) {
cshader := shader.cptr()
cuniformLoc := (C.int)(uniformLoc)
clocIndex := (C.int)(locIndex)
cmat := mat.cptr()
C.SetShaderValueMatrix(*cshader, cuniformLoc, *cmat)
C.SetShaderValueMatrix(*cshader, clocIndex, *cmat)
}
// SetShaderValueTexture - Set shader uniform value for texture (sampler2d)
func SetShaderValueTexture(shader Shader, locIndex int32, texture Texture2D) {
cshader := shader.cptr()
clocIndex := (C.int)(locIndex)
ctexture := texture.cptr()
C.SetShaderValueTexture(*cshader, clocIndex, *ctexture)
}
// SetMatrixProjection - Set a custom projection matrix (replaces internal projection matrix)