Add License, Attribution, and Readme files.

Disable lighting and shading when drawing wireframe mode.
main
Sean Hickey 2021-09-05 17:56:04 -07:00
parent 0098f283f2
commit 747d25ccfb
10 changed files with 112 additions and 26 deletions

30
Attribution.md Normal file
View File

@ -0,0 +1,30 @@
# Attributions
While Go does list out the dependencies in the `go.mod` file, I would
like to list them here as well for completeness. I'd also like to
attribute other projects that may have influenced the code, even if
they weren't actual dependencies.
This project uses the following actual dependencies:
* [go-sdl2][4] the Golang wrappers for SDL. (BSD 3-clause License)
* [go-gl][3] the Golang wrappers for OpenGL, specifically `v3.1/gles2`. (MIT License)
There are some [Go SDL Examples][5] and [Go OpenGL Examples][6] that I
used to get my project started, both maintained by the same groups
that maintain the wrapper libraries.
[Open.gl][1] got me up to speed on how OpenGL 3 works with the shader
pipelines.
[LearnOpenGL.com][2] was very helpful in learning how to add lighting
to my fragment shader. I had learned the Phong shading model at
university, but this showed me how to actually apply it with OpenGL
shaders.
1: https://open.gl/introduction
2: https://learnopengl.com/Lighting/Basic-Lighting
3: https://github.com/go-gl/gl
4: https://github.com/veandco/go-sdl2
5: https://github.com/veandco/go-sdl2-examples
6: https://github.com/go-gl/example

17
License.md Normal file
View File

@ -0,0 +1,17 @@
# ISC License (ISC)
Copyright 2021 Sean Hickey (Wisellama)
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all
copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.

View File

@ -5,3 +5,6 @@ dependencies:
build:
go build
build-release:
go build -ldflags "-s -w"

20
Readme.md Normal file
View File

@ -0,0 +1,20 @@
# Carpy Breakout
## Why "Carpy"?
Because it's a funny misspelling of "Crappy". This was originally
going to be a bad, crappy game for me to learn Golang and re-learn SDL
and OpenGL stuff. It ended up being a bit nicer than I expected.
## License/Copyright
This project is licensed under the [ISC License][1], a permissive open
source license. Essentially you can use this code for any purpose so
long as you acknowledge me for creating it. You can find the full text
in `License.md`.
Another file called `Attribution.md` contains links to the other
projects used to build this one.
[1]: https://opensource.org/licenses/ISC

View File

@ -38,7 +38,12 @@ func main() {
}
func run() {
runtime.LockOSThread() // TODO can rework stuff to not need this.
// TODO can potentially rework stuff to not need LockOSThread()
// here. SDL has the built-in sdl.Do(), but I would need to create
// my own similar thing for OpenGL. Essentially they both need to
// be locked to the main thread, but anything else can be in a
// goroutine.
runtime.LockOSThread()
err := sdl.Init(sdl.INIT_EVERYTHING)
if err != nil {
@ -66,7 +71,7 @@ func run() {
}
defer gameWindow.Destroy()
sunLight := gl_objects.NewDirLight(mgl32.Vec3{1, -1, -1})
sunLight := gl_objects.NewDirectionalLight(mgl32.Vec3{1, -1, -1})
sunLight.GLInit(gameWindow.GLProgram)
camera := gl_objects.NewCamera(gameWindow.GLProgram)

View File

@ -113,6 +113,7 @@ func (w *GameWindow) SetCamera(camera *gl_objects.Camera) {
}
func (w *GameWindow) ToggleWireframe() {
// TODO figure out concurrency
// var wg sync.WaitGroup
// for _, o := range w.GLObjects {
// wg.Add(1)
@ -138,7 +139,6 @@ func (w *GameWindow) Draw() {
func (w *GameWindow) Update() {
// TODO figure out concurrency
// TODO add lighting eventually
// var wg sync.WaitGroup
// for _, o := range w.GLObjects {
// wg.Add(1)

View File

@ -10,14 +10,14 @@ in vec2 fragTexCoord;
out vec4 outputColor;
struct Material {
sampler2D diffuse;
sampler2D specular;
sampler2D textureDiffuse;
sampler2D textureSpecular;
float shininess;
vec3 color;
int textureOn; // if textureOn == 0, then just color is used
};
struct DirLight {
struct DirectionalLight {
vec3 direction;
vec3 ambient;
@ -25,27 +25,33 @@ struct DirLight {
vec3 specular;
};
uniform DirLight dirLight;
uniform DirectionalLight dirLight;
uniform Material material;
uniform vec3 cameraPos;
uniform int lightsOn;
vec3 GetTextureDiffuse();
vec3 GetTextureSpecular();
vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir);
vec3 DirectionalLightColor(DirectionalLight light, vec3 normal, vec3 viewDir);
void main() {
vec3 normal = normalize(fragNormal);
vec3 viewDir = normalize(fragPos - cameraPos);
vec3 result = CalcDirLight(dirLight, normal, viewDir);
vec3 directionalLightColor;
if (lightsOn != 0) {
directionalLightColor = DirectionalLightColor(dirLight, normal, viewDir);
} else {
directionalLightColor = material.color;
}
outputColor = vec4(result, 1);
outputColor = vec4(directionalLightColor, 1);
}
vec3 GetTextureDiffuse() {
vec3 t = material.color;
if (material.textureOn != 0) {
t = t * vec3(texture(material.diffuse, fragTexCoord));
t = t * vec3(texture(material.textureDiffuse, fragTexCoord));
}
return t;
}
@ -53,28 +59,30 @@ vec3 GetTextureDiffuse() {
vec3 GetTextureSpecular() {
vec3 t = material.color;
if (material.textureOn != 0) {
t = t * vec3(texture(material.specular, fragTexCoord));
t = t * vec3(texture(material.textureSpecular, fragTexCoord));
}
return t;
}
vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)
vec3 DirectionalLightColor(DirectionalLight light, vec3 normal, vec3 viewDir)
{
vec3 lightDir = normalize(light.direction);
// Diffuse
float diffuse = max(dot(normal, -lightDir), 0.0);
float diffuseAmount = max(dot(normal, -lightDir), 0.0);
// Specular
vec3 reflectDir = reflect(-lightDir, normal);
float specular = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
float specularAmount = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
// Textures
vec3 tDiffuse = GetTextureDiffuse();
vec3 tSpecular = GetTextureSpecular();
// Multiply to get each value, then add to get the final result
vec3 a = light.ambient * tDiffuse;
vec3 d = light.diffuse * diffuse * tDiffuse;
vec3 s = light.specular * specular * tSpecular;
vec3 d = light.diffuse * diffuseAmount * tDiffuse;
vec3 s = light.specular * specularAmount * tSpecular;
return (a + d + s);
}

View File

@ -104,6 +104,8 @@ func (b *Box) Draw() {
}
func (b *Box) drawTriangles() {
gl_helpers.SetUniformInt(b.GLProgram, "lightsOn", 1)
gl.BindVertexArray(b.GLVertexArrayId)
gl.BindBuffer(gl.ARRAY_BUFFER, b.GLVertexBufferId)
@ -117,6 +119,7 @@ func (b *Box) drawTriangles() {
}
func (b *Box) drawOutline() {
gl_helpers.SetUniformInt(b.GLProgram, "lightsOn", 0)
for _, face := range b.Faces {
face.DrawLineLoop(b.GLProgram)
}

View File

@ -7,15 +7,15 @@ import (
)
// Directional Light (e.g. the Sun)
type DirLight struct {
type DirectionalLight struct {
Direction mgl32.Vec3 // the direction the light is travelling from the sun
AmbientColor mgl32.Vec3
DiffuseColor mgl32.Vec3
SpecularColor mgl32.Vec3
}
func NewDirLight(direction mgl32.Vec3) *DirLight {
d := DirLight{
func NewDirectionalLight(direction mgl32.Vec3) *DirectionalLight {
d := DirectionalLight{
Direction: direction,
AmbientColor: mgl32.Vec3{0.2, 0.2, 0.2},
DiffuseColor: mgl32.Vec3{1, 1, 1},
@ -24,7 +24,7 @@ func NewDirLight(direction mgl32.Vec3) *DirLight {
return &d
}
func (l *DirLight) GLInit(glProgram uint32) {
func (l *DirectionalLight) GLInit(glProgram uint32) {
gl_helpers.SetUniformVec3f(glProgram, "dirLight.ambient", l.AmbientColor)
gl_helpers.SetUniformVec3f(glProgram, "dirLight.diffuse", l.DiffuseColor)
gl_helpers.SetUniformVec3f(glProgram, "dirLight.specular", l.SpecularColor)

View File

@ -7,8 +7,8 @@ import (
)
type Material struct {
DiffuseTexture int32
SpecularTexture int32
TextureDiffuse int32
TextureSpecular int32
Shininess float32
Color mgl32.Vec3
TextureOn bool
@ -17,7 +17,7 @@ type Material struct {
func NewMaterial() *Material {
m := Material{
Shininess: 32.0,
Shininess: 64.0,
Color: mgl32.Vec3{1, 1, 1},
TextureOn: false,
}
@ -26,8 +26,8 @@ func NewMaterial() *Material {
}
func (m *Material) Draw(glProgram uint32) {
gl_helpers.SetUniformInt(glProgram, "material.diffuse", m.DiffuseTexture)
gl_helpers.SetUniformInt(glProgram, "material.specular", m.SpecularTexture)
gl_helpers.SetUniformInt(glProgram, "material.textureDiffuse", m.TextureDiffuse)
gl_helpers.SetUniformInt(glProgram, "material.textureSpecular", m.TextureSpecular)
gl_helpers.SetUniformFloat(glProgram, "material.shininess", m.Shininess)
gl_helpers.SetUniformVec3f(glProgram, "material.color", m.Color)