-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example #16
Open
Konstantin8105
wants to merge
3
commits into
gopherjs:master
Choose a base branch
from
Konstantin8105:Example
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Example #16
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.js | ||
*.js.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Examples | ||
|
||
1. `Simple2D` in according to [www.tutorialspoint.com](https://www.tutorialspoint.com/webgl/webgl_sample_application.htm) | ||
|
||
![Simple2D](./Simple2D/simple2d.png) | ||
|
||
# Links | ||
|
||
* [The WebGL API: 2D and 3D graphics for the web](https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/gopherjs/gopherjs/js" | ||
"github.com/gopherjs/webgl" | ||
) | ||
|
||
func main() { | ||
// Step1: Prepare the canvas and get WebGL context | ||
|
||
document := js.Global.Get("document") | ||
canvas := document.Call("getElementById", "my_Canvas") | ||
|
||
attrs := webgl.DefaultAttributes() | ||
attrs.Alpha = false | ||
|
||
gl, err := webgl.NewContext(canvas, attrs) | ||
if err != nil { | ||
js.Global.Call("alert", "Error: "+err.Error()) | ||
} | ||
|
||
// Step2: Define the geometry and store it in buffer objects | ||
|
||
vertices := [6]float32{-0.5, 0.5, -0.5, -0.5, 0.0, -0.5} | ||
|
||
// Create a new buffer object | ||
vertex_buffer := gl.CreateBuffer() | ||
|
||
// Bind an empty array buffer to it | ||
gl.BindBuffer(gl.ARRAY_BUFFER, vertex_buffer) | ||
|
||
// Pass the vertices data to the buffer | ||
gl.BufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW) | ||
|
||
// Unbind the buffer | ||
gl.BindBuffer(gl.ARRAY_BUFFER, nil) | ||
|
||
/* Step3: Create and compile Shader programs */ | ||
|
||
// Vertex shader source code | ||
vertCode := `attribute vec2 coordinates;` + | ||
`void main(void) {` + ` gl_Position = vec4(coordinates,0.0, 1.0);` + `}` | ||
|
||
//Create a vertex shader object | ||
vertShader := gl.CreateShader(gl.VERTEX_SHADER) | ||
|
||
//Attach vertex shader source code | ||
gl.ShaderSource(vertShader, vertCode) | ||
|
||
//Compile the vertex shader | ||
gl.CompileShader(vertShader) | ||
|
||
//Fragment shader source code | ||
fragCode := `void main(void) {` + `gl_FragColor = vec4(0.0, 0.0, 0.0, 0.1);` + `}` | ||
|
||
// Create fragment shader object | ||
fragShader := gl.CreateShader(gl.FRAGMENT_SHADER) | ||
|
||
// Attach fragment shader source code | ||
gl.ShaderSource(fragShader, fragCode) | ||
|
||
// Compile the fragment shader | ||
gl.CompileShader(fragShader) | ||
|
||
// Create a shader program object to store combined shader program | ||
shaderProgram := gl.CreateProgram() | ||
|
||
// Attach a vertex shader | ||
gl.AttachShader(shaderProgram, vertShader) | ||
|
||
// Attach a fragment shader | ||
gl.AttachShader(shaderProgram, fragShader) | ||
|
||
// Link both programs | ||
gl.LinkProgram(shaderProgram) | ||
|
||
// Use the combined shader program object | ||
gl.UseProgram(shaderProgram) | ||
|
||
/* Step 4: Associate the shader programs to buffer objects */ | ||
|
||
//Bind vertex buffer object | ||
gl.BindBuffer(gl.ARRAY_BUFFER, vertex_buffer) | ||
|
||
//Get the attribute location | ||
coord := gl.GetAttribLocation(shaderProgram, "coordinates") | ||
|
||
//point an attribute to the currently bound VBO | ||
gl.VertexAttribPointer(coord, 2, gl.FLOAT, false, 0, 0) | ||
|
||
//Enable the attribute | ||
gl.EnableVertexAttribArray(coord) | ||
|
||
/* Step5: Drawing the required object (triangle) */ | ||
|
||
// Clear the canvas | ||
gl.ClearColor(0.5, 0.5, 0.5, 0.9) | ||
|
||
// Enable the depth test | ||
gl.Enable(gl.DEPTH_TEST) | ||
|
||
// Clear the color buffer bit | ||
gl.Clear(gl.COLOR_BUFFER_BIT) | ||
|
||
// Set the view port | ||
gl.Viewport(0, 0, 300, 300) | ||
|
||
// Draw the triangle | ||
gl.DrawArrays(gl.TRIANGLES, 0, 3) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>WebGL Demo 2D</title> | ||
</head> | ||
<body> | ||
<canvas width = "300" height = "300" id = "my_Canvas"></canvas> | ||
<script src="webgl.js"></script> | ||
</body> | ||
</html> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comments should be consistent and use a space after
//
, like above.Here, and below.
Reference: https://dmitri.shuralyov.com/idiomatic-go#comments-for-humans-always-have-a-single-space-after-the-slashes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your comment. I will wait @ajhager comment.