Skip to content

Commit

Permalink
Add proxy package for building different GL versions
Browse files Browse the repository at this point in the history
  • Loading branch information
gdm85 committed Nov 2, 2019
1 parent 2a9ed0a commit d8c64aa
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 11 deletions.
14 changes: 12 additions & 2 deletions Makefile
@@ -1,13 +1,23 @@

ifeq ($(shell uname),Darwin)
GL_VER:=v4.1-core
else
GL_VER:=v2.1
endif

all: wolfengo test

wolfengo:
wolfengo: gl
go build -o bin/wolfengo ./src

gl:
go run src/gl/generate/generate.go $(GL_VER) > src/gl/gl.go
gofmt -w src/gl/gl.go

errcheck:
errcheck ./src

test:
go test ./src

.PHONY: all wolfengo test errcheck
.PHONY: all wolfengo test errcheck gl
133 changes: 133 additions & 0 deletions src/gl/generate/generate.go
@@ -0,0 +1,133 @@
// +build generate
/*
WolfenGo - https://github.com/gdm85/wolfengo
Copyright (C) 2016~2019 gdm85
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

package main

import (
"fmt"
"os"
"strings"
)

func main() {
glVer := os.Args[1]

parts := strings.Split(glVer, ".")
major, minor := parts[0][1:], parts[1]

// remove '-core'
minor = strings.Replace(minor, "-core", "", 1)

fmt.Printf(`// Code generated by src/gl/generate/generate.go DO NOT EDIT
package gl
import "github.com/go-gl/gl/%s/gl"
const (
RequestedContextVersionMajor = %s
RequestedContextVersionMinor = %s
)
const (
VERTEX_SHADER = gl.VERTEX_SHADER
FRAGMENT_SHADER = gl.FRAGMENT_SHADER
DEBUG_SEVERITY_HIGH = gl.DEBUG_SEVERITY_HIGH
VERSION = gl.VERSION
DEBUG_OUTPUT = gl.DEBUG_OUTPUT
CW = gl.CW
BACK = gl.BACK
CULL_FACE = gl.CULL_FACE
DEPTH_TEST = gl.DEPTH_TEST
BLEND = gl.BLEND
SRC_ALPHA = gl.SRC_ALPHA
ONE_MINUS_SRC_ALPHA = gl.ONE_MINUS_SRC_ALPHA
DEPTH_CLAMP = gl.DEPTH_CLAMP
TEXTURE_2D = gl.TEXTURE_2D
COLOR_BUFFER_BIT = gl.COLOR_BUFFER_BIT
DEPTH_BUFFER_BIT = gl.DEPTH_BUFFER_BIT
ARRAY_BUFFER = gl.ARRAY_BUFFER
ELEMENT_ARRAY_BUFFER = gl.ELEMENT_ARRAY_BUFFER
STATIC_DRAW = gl.STATIC_DRAW
FLOAT = gl.FLOAT
TRIANGLES = gl.TRIANGLES
UNSIGNED_INT = gl.UNSIGNED_INT
INFO_LOG_LENGTH = gl.INFO_LOG_LENGTH
LINK_STATUS = gl.LINK_STATUS
FALSE = gl.FALSE
VALIDATE_STATUS = gl.VALIDATE_STATUS
COMPILE_STATUS = gl.COMPILE_STATUS
TEXTURE_MIN_FILTER = gl.TEXTURE_MIN_FILTER
TEXTURE_WRAP_T = gl.TEXTURE_WRAP_T
REPEAT = gl.REPEAT
TEXTURE_WRAP_S = gl.TEXTURE_WRAP_S
NEAREST = gl.NEAREST
TEXTURE_MAG_FILTER = gl.TEXTURE_MAG_FILTER
RGBA8 = gl.RGBA8
RGBA = gl.RGBA
UNSIGNED_BYTE = gl.UNSIGNED_BYTE
)
var (
DrawElements = gl.DrawElements
Init = gl.Init
GoStr = gl.GoStr
GetString = gl.GetString
DebugMessageCallback = gl.DebugMessageCallback
Enable = gl.Enable
ClearColor = gl.ClearColor
FrontFace = gl.FrontFace
CullFace = gl.CullFace
BlendFunc = gl.BlendFunc
Clear = gl.Clear
GenBuffers = gl.GenBuffers
BindBuffer = gl.BindBuffer
BufferData = gl.BufferData
Ptr = gl.Ptr
EnableVertexAttribArray = gl.EnableVertexAttribArray
VertexAttribPointer = gl.VertexAttribPointer
PtrOffset = gl.PtrOffset
DisableVertexAttribArray = gl.DisableVertexAttribArray
CreateProgram = gl.CreateProgram
UseProgram = gl.UseProgram
Strs = gl.Strs
GetUniformLocation = gl.GetUniformLocation
GetProgramiv = gl.GetProgramiv
GetProgramInfoLog = gl.GetProgramInfoLog
GetShaderiv = gl.GetShaderiv
GetShaderInfoLog = gl.GetShaderInfoLog
Str = gl.Str
LinkProgram = gl.LinkProgram
ValidateProgram = gl.ValidateProgram
CreateShader = gl.CreateShader
ShaderSource = gl.ShaderSource
CompileShader = gl.CompileShader
AttachShader = gl.AttachShader
Uniform3f = gl.Uniform3f
UniformMatrix4fv = gl.UniformMatrix4fv
BindTexture = gl.BindTexture
GenTextures = gl.GenTextures
TexParameteri = gl.TexParameteri
TexParameterf = gl.TexParameterf
TexImage2D = gl.TexImage2D
GenVertexArrays = gl.GenVertexArrays
BindVertexArray = gl.BindVertexArray
)
`, glVer, major, minor)
}
94 changes: 94 additions & 0 deletions src/gl/gl.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/level.go
Expand Up @@ -22,7 +22,7 @@ import (
"fmt"
"math"

"github.com/go-gl/gl/v2.1/gl"
"github.com/gdm85/wolfengo/src/gl"
)

const (
Expand Down Expand Up @@ -71,11 +71,11 @@ func getBasicShader() (*Shader, error) {
return nil, err
}

err = _basicShader.addProgramFromFile("basicVertex120.vs", gl.VERTEX_SHADER)
err = _basicShader.addProgramFromFile("basicVertex"+shaderVersion+".vs", gl.VERTEX_SHADER)
if err != nil {
return nil, err
}
err = _basicShader.addProgramFromFile("basicFragment120.fs", gl.FRAGMENT_SHADER)
err = _basicShader.addProgramFromFile("basicFragment"+shaderVersion+".fs", gl.FRAGMENT_SHADER)
if err != nil {
return nil, err
}
Expand Down
18 changes: 15 additions & 3 deletions src/main.go
Expand Up @@ -26,7 +26,7 @@ import (
"time"
"unsafe"

"github.com/go-gl/gl/v2.1/gl"
"github.com/gdm85/wolfengo/src/gl"
"github.com/go-gl/glfw/v3.1/glfw"
)

Expand Down Expand Up @@ -72,6 +72,10 @@ func debugCb(
fmt.Fprintln(os.Stderr, msg)
}

// needed for some older hardware e.g. Intel HD graphics
// notice that this suffix only changes a couple shaders, while the rest uses the 330 version
var shaderVersion = "120"

func main() {
fmt.Printf(`WolfenGo v%s, Copyright (C) 2016~2019 gdm85
https://github.com/gdm85/wolfengo
Expand All @@ -85,8 +89,16 @@ under GNU/GPLv2 license.`+"\n", version)
defer glfw.Terminate()

// create a context and activate it
glfw.WindowHint(glfw.ContextVersionMajor, 2)
glfw.WindowHint(glfw.ContextVersionMinor, 1)
glfw.WindowHint(glfw.ContextVersionMajor, gl.RequestedContextVersionMajor)
glfw.WindowHint(glfw.ContextVersionMinor, gl.RequestedContextVersionMinor)
if gl.RequestedContextVersionMajor >= 3 {
// switch to version 330
shaderVersion = ""
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
if gl.RequestedContextVersionMajor >= 2 {
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
}
}
glfw.WindowHint(glfw.Resizable, glfw.False)
glfw.WindowHint(glfw.DoubleBuffer, glfw.True)
var err error
Expand Down
2 changes: 1 addition & 1 deletion src/mesh.go
Expand Up @@ -18,7 +18,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/
package main

import "github.com/go-gl/gl/v2.1/gl"
import "github.com/gdm85/wolfengo/src/gl"

type Mesh struct {
vbo, ibo uint32
Expand Down
6 changes: 5 additions & 1 deletion src/shader.go
Expand Up @@ -24,7 +24,7 @@ import (
"io/ioutil"
"strings"

"github.com/go-gl/gl/v2.1/gl"
"github.com/gdm85/wolfengo/src/gl"
)

type Shader struct {
Expand Down Expand Up @@ -84,6 +84,10 @@ func (s *Shader) getShaderInfoLog(shader uint32, context string) error {
}

func (s *Shader) compile() error {
var vao uint32
gl.GenVertexArrays(1, &vao)
gl.BindVertexArray(vao)

gl.LinkProgram(s.program)
var result int32
gl.GetProgramiv(s.program, gl.LINK_STATUS, &result)
Expand Down
2 changes: 1 addition & 1 deletion src/texture.go
Expand Up @@ -25,7 +25,7 @@ import (
_ "image/png"
"os"

"github.com/go-gl/gl/v2.1/gl"
"github.com/gdm85/wolfengo/src/gl"
)

type Texture struct {
Expand Down

0 comments on commit d8c64aa

Please sign in to comment.