remove gl00
This commit is contained in:
parent
c2a69d7cd0
commit
1ec4f9d281
65
embed.go
65
embed.go
@ -1,65 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
)
|
||||
|
||||
//go:embed shader.frag
|
||||
var shaderFrag string
|
||||
|
||||
//go:embed shader.vert
|
||||
var shaderVert string
|
||||
|
||||
//go:embed square.png
|
||||
var textureData []byte
|
||||
|
||||
var cubeVertices = []float32{
|
||||
// X, Y, Z, U, V
|
||||
// Bottom
|
||||
-1.0, -1.0, -1.0, 0.0, 0.0,
|
||||
1.0, -1.0, -1.0, 1.0, 0.0,
|
||||
-1.0, -1.0, 1.0, 0.0, 1.0,
|
||||
1.0, -1.0, -1.0, 1.0, 0.0,
|
||||
1.0, -1.0, 1.0, 1.0, 1.0,
|
||||
-1.0, -1.0, 1.0, 0.0, 1.0,
|
||||
|
||||
// Top
|
||||
-1.0, 1.0, -1.0, 0.0, 0.0,
|
||||
-1.0, 1.0, 1.0, 0.0, 1.0,
|
||||
1.0, 1.0, -1.0, 1.0, 0.0,
|
||||
1.0, 1.0, -1.0, 1.0, 0.0,
|
||||
-1.0, 1.0, 1.0, 0.0, 1.0,
|
||||
1.0, 1.0, 1.0, 1.0, 1.0,
|
||||
|
||||
// Front
|
||||
-1.0, -1.0, 1.0, 1.0, 0.0,
|
||||
1.0, -1.0, 1.0, 0.0, 0.0,
|
||||
-1.0, 1.0, 1.0, 1.0, 1.0,
|
||||
1.0, -1.0, 1.0, 0.0, 0.0,
|
||||
1.0, 1.0, 1.0, 0.0, 1.0,
|
||||
-1.0, 1.0, 1.0, 1.0, 1.0,
|
||||
|
||||
// Back
|
||||
-1.0, -1.0, -1.0, 0.0, 0.0,
|
||||
-1.0, 1.0, -1.0, 0.0, 1.0,
|
||||
1.0, -1.0, -1.0, 1.0, 0.0,
|
||||
1.0, -1.0, -1.0, 1.0, 0.0,
|
||||
-1.0, 1.0, -1.0, 0.0, 1.0,
|
||||
1.0, 1.0, -1.0, 1.0, 1.0,
|
||||
|
||||
// Left
|
||||
-1.0, -1.0, 1.0, 0.0, 1.0,
|
||||
-1.0, 1.0, -1.0, 1.0, 0.0,
|
||||
-1.0, -1.0, -1.0, 0.0, 0.0,
|
||||
-1.0, -1.0, 1.0, 0.0, 1.0,
|
||||
-1.0, 1.0, 1.0, 1.0, 1.0,
|
||||
-1.0, 1.0, -1.0, 1.0, 0.0,
|
||||
|
||||
// Right
|
||||
1.0, -1.0, 1.0, 1.0, 1.0,
|
||||
1.0, -1.0, -1.0, 1.0, 0.0,
|
||||
1.0, 1.0, -1.0, 0.0, 0.0,
|
||||
1.0, -1.0, 1.0, 1.0, 1.0,
|
||||
1.0, 1.0, -1.0, 0.0, 0.0,
|
||||
1.0, 1.0, 1.0, 0.0, 1.0,
|
||||
}
|
130
main.go
130
main.go
@ -1,130 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/go-gl/gl/v4.1-core/gl"
|
||||
"github.com/go-gl/glfw/v3.3/glfw"
|
||||
"github.com/go-gl/mathgl/mgl32"
|
||||
)
|
||||
|
||||
const (
|
||||
windowWidth = 800
|
||||
windowHeight = 600
|
||||
)
|
||||
|
||||
func init() {
|
||||
runtime.LockOSThread()
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
if err := glfw.Init(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer glfw.Terminate()
|
||||
|
||||
glfw.WindowHint(glfw.Resizable, 0)
|
||||
glfw.WindowHint(glfw.ContextVersionMajor, 4)
|
||||
glfw.WindowHint(glfw.ContextVersionMinor, 1)
|
||||
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
|
||||
glfw.WindowHint(glfw.OpenGLForwardCompatible, 1)
|
||||
win, err := glfw.CreateWindow(windowWidth, windowHeight, "Cube", nil, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
win.MakeContextCurrent()
|
||||
glfw.SwapInterval(1)
|
||||
|
||||
err = gl.Init()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("OpenGL Version: ", gl.GoStr(gl.GetString(gl.VERSION)))
|
||||
|
||||
program, err := newProgram(shaderVert, shaderFrag)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
gl.UseProgram(program)
|
||||
|
||||
model := mgl32.Ident4()
|
||||
modelUniform := gl.GetUniformLocation(program, gl.Str("model\x00"))
|
||||
gl.UniformMatrix4fv(modelUniform, 1, false, &model[0])
|
||||
|
||||
view := mgl32.LookAtV(mgl32.Vec3{3, 3, 3}, mgl32.Vec3{0, 0, 0}, mgl32.Vec3{0, 1, 0})
|
||||
viewUniform := gl.GetUniformLocation(program, gl.Str("view\x00"))
|
||||
gl.UniformMatrix4fv(viewUniform, 1, false, &view[0])
|
||||
|
||||
projection := mgl32.Perspective(mgl32.DegToRad(45), float32(windowWidth)/float32(windowHeight), 0.1, 10)
|
||||
projectionUniform := gl.GetUniformLocation(program, gl.Str("projection\x00"))
|
||||
gl.UniformMatrix4fv(projectionUniform, 1, false, &projection[0])
|
||||
|
||||
// Texture
|
||||
textureUniform := gl.GetUniformLocation(program, gl.Str("tex\x00"))
|
||||
gl.Uniform1i(textureUniform, 0)
|
||||
|
||||
texture, err := newTexture(bytes.NewReader(textureData))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
gl.BindFragDataLocation(program, 0, gl.Str("outputColor\x00"))
|
||||
|
||||
// Vertex Array
|
||||
var vao uint32
|
||||
gl.GenVertexArrays(1, &vao)
|
||||
gl.BindVertexArray(vao)
|
||||
|
||||
// Vertex Buffer
|
||||
var vbo uint32
|
||||
gl.GenBuffers(1, &vbo)
|
||||
gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
|
||||
gl.BufferData(gl.ARRAY_BUFFER, len(cubeVertices)*4, gl.Ptr(cubeVertices), gl.STATIC_DRAW)
|
||||
|
||||
vertAttrib := uint32(gl.GetAttribLocation(program, gl.Str("vert\x00")))
|
||||
gl.EnableVertexAttribArray(vertAttrib)
|
||||
gl.VertexAttribPointer(vertAttrib, 3, gl.FLOAT, false, 5*4, gl.PtrOffset(0))
|
||||
|
||||
texCoordAttrib := uint32(gl.GetAttribLocation(program, gl.Str("vertTexCoord\x00")))
|
||||
gl.EnableVertexAttribArray(texCoordAttrib)
|
||||
gl.VertexAttribPointer(texCoordAttrib, 2, gl.FLOAT, false, 5*4, gl.PtrOffset(3*4))
|
||||
|
||||
gl.Enable(gl.CULL_FACE)
|
||||
gl.Enable(gl.DEPTH_TEST)
|
||||
gl.DepthFunc(gl.LESS)
|
||||
gl.ClearColor(1, 1, 1, 1)
|
||||
|
||||
angle := 0.0
|
||||
prevTime := time.Now()
|
||||
|
||||
for !win.ShouldClose() {
|
||||
|
||||
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
|
||||
|
||||
// Angle update
|
||||
angle += time.Since(prevTime).Seconds()
|
||||
prevTime = time.Now()
|
||||
|
||||
model = mgl32.HomogRotate3D(float32(angle), mgl32.Vec3{0, 1, 0})
|
||||
|
||||
// Render
|
||||
// Shader variable
|
||||
gl.UseProgram(program)
|
||||
gl.UniformMatrix4fv(modelUniform, 1, false, &model[0])
|
||||
|
||||
gl.BindVertexArray(vao)
|
||||
|
||||
gl.ActiveTexture(gl.TEXTURE0)
|
||||
gl.BindTexture(gl.TEXTURE_2D, texture)
|
||||
|
||||
gl.DrawArrays(gl.TRIANGLES, 0, 6*2*3)
|
||||
|
||||
win.SwapBuffers()
|
||||
glfw.PollEvents()
|
||||
}
|
||||
}
|
11
shader.frag
11
shader.frag
@ -1,11 +0,0 @@
|
||||
#version 330
|
||||
|
||||
uniform sampler2D tex;
|
||||
|
||||
in vec2 fragTexCoord;
|
||||
|
||||
out vec4 outputColor;
|
||||
|
||||
void main() {
|
||||
outputColor = texture(tex, fragTexCoord);
|
||||
}
|
81
shader.go
81
shader.go
@ -1,81 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/go-gl/gl/v4.1-core/gl"
|
||||
)
|
||||
|
||||
func compileShader(src string, stype uint32) (prog uint32, err error) {
|
||||
prog = gl.CreateShader(stype)
|
||||
|
||||
strs, free := gl.Strs(src, "\x00")
|
||||
gl.ShaderSource(prog, 1, strs, nil)
|
||||
free()
|
||||
gl.CompileShader(prog)
|
||||
|
||||
var status int32
|
||||
gl.GetShaderiv(prog, gl.COMPILE_STATUS, &status)
|
||||
if status == gl.FALSE {
|
||||
var len int32
|
||||
gl.GetShaderiv(prog, gl.INFO_LOG_LENGTH, &len)
|
||||
|
||||
log := strings.Repeat("\x00", int(len+1))
|
||||
gl.GetShaderInfoLog(prog, len, nil, gl.Str(log))
|
||||
|
||||
gl.DeleteShader(prog)
|
||||
|
||||
switch stype {
|
||||
case gl.VERTEX_SHADER:
|
||||
return 0, fmt.Errorf("failed to compile Vertex Shader: %s", log)
|
||||
case gl.FRAGMENT_SHADER:
|
||||
return 0, fmt.Errorf("failed to compile Fragment Shader: %s", log)
|
||||
default:
|
||||
return 0, fmt.Errorf("failed to compile Unknown(%d) Shader: %s", stype, log)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func newProgram(vert, frag string) (prog uint32, err error) {
|
||||
|
||||
vertid, err := compileShader(vert, gl.VERTEX_SHADER)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
fragid, err := compileShader(frag, gl.FRAGMENT_SHADER)
|
||||
if err != nil {
|
||||
gl.DeleteShader(vertid)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
prog = gl.CreateProgram()
|
||||
|
||||
gl.AttachShader(prog, vertid)
|
||||
gl.AttachShader(prog, fragid)
|
||||
gl.LinkProgram(prog)
|
||||
|
||||
var status int32
|
||||
gl.GetProgramiv(prog, gl.LINK_STATUS, &status)
|
||||
if status == gl.FALSE {
|
||||
var len int32
|
||||
gl.GetProgramiv(prog, gl.INFO_LOG_LENGTH, &len)
|
||||
|
||||
log := strings.Repeat("\x00", int(len+1))
|
||||
gl.GetProgramInfoLog(prog, len, nil, gl.Str(log))
|
||||
|
||||
gl.DeleteProgram(prog)
|
||||
gl.DeleteShader(vertid)
|
||||
gl.DeleteShader(fragid)
|
||||
|
||||
return 0, fmt.Errorf("failed to link Program: %s", log)
|
||||
}
|
||||
|
||||
gl.DeleteShader(vertid)
|
||||
gl.DeleteShader(fragid)
|
||||
|
||||
return
|
||||
}
|
16
shader.vert
16
shader.vert
@ -1,16 +0,0 @@
|
||||
#version 330
|
||||
|
||||
uniform mat4 projection;
|
||||
uniform mat4 view;
|
||||
uniform mat4 model;
|
||||
|
||||
in vec3 vert;
|
||||
in vec2 vertTexCoord;
|
||||
|
||||
out vec2 fragTexCoord;
|
||||
|
||||
void main() {
|
||||
fragTexCoord = vertTexCoord;
|
||||
gl_Position = projection * view * model * vec4(vert, 1);
|
||||
}
|
||||
|
BIN
square.png
BIN
square.png
Binary file not shown.
Before Width: | Height: | Size: 352 KiB |
46
texture.go
46
texture.go
@ -1,46 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"image"
|
||||
"image/draw"
|
||||
_ "image/png"
|
||||
"io"
|
||||
|
||||
"github.com/go-gl/gl/v4.1-core/gl"
|
||||
)
|
||||
|
||||
func newTexture(file io.Reader) (tex uint32, err error) {
|
||||
img, _, err := image.Decode(file)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
rgba := image.NewRGBA(img.Bounds())
|
||||
if rgba.Stride != rgba.Rect.Size().X*4 {
|
||||
return 0, errors.New("unsupported stride")
|
||||
}
|
||||
draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, draw.Src)
|
||||
|
||||
gl.GenTextures(1, &tex)
|
||||
gl.ActiveTexture(gl.TEXTURE0)
|
||||
gl.BindTexture(gl.TEXTURE_2D, tex)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
|
||||
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
|
||||
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)
|
||||
|
||||
gl.TexImage2D(
|
||||
gl.TEXTURE_2D,
|
||||
0,
|
||||
gl.RGBA,
|
||||
int32(rgba.Rect.Size().X),
|
||||
int32(rgba.Rect.Size().Y),
|
||||
0,
|
||||
gl.RGBA,
|
||||
gl.UNSIGNED_BYTE,
|
||||
gl.Ptr(rgba.Pix),
|
||||
)
|
||||
|
||||
return
|
||||
}
|
Loading…
Reference in New Issue
Block a user