Last active
November 26, 2022 10:42
-
-
Save neuro-sys/4a6f96e3b14f626e7d7e7824777d43e9 to your computer and use it in GitHub Desktop.
gforth opengl example
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
decimal | |
s" utils.fs" included | |
also utils | |
vocabulary main-gl | |
also main-gl definitions | |
640 constant #width | |
480 constant #height | |
variable window | |
create vertexShaderSource | |
s""" | |
#version 330 core | |
layout (location = 0) in vec3 aPos; | |
void main() | |
{ | |
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); | |
} | |
"""s | |
vertexShaderSource length constant #vertexShaderSource | |
create fragmentShaderSource | |
s""" | |
#version 330 core | |
out vec4 FragColor; | |
void main() | |
{ | |
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f); | |
} | |
"""s | |
fragmentShaderSource length constant #fragmentShaderSource | |
create vertexShaderSources | |
vertexShaderSource , | |
create fragmentShaderSources | |
fragmentShaderSource , | |
variable shaderProgram | |
variable vertexShader | |
variable fragmentShader | |
variable success | |
create infoLog 512 allot 0 c, | |
: shader-check ( shader -- ) | |
dup GL_COMPILE_STATUS success glGetShaderiv | |
success @ 0= if | |
dup 512 0 infoLog glGetShaderInfoLog | |
cr ." Shader compilation failed: " infoLog ztype | |
then | |
drop | |
; | |
: make-shader | |
GL_VERTEX_SHADER glCreateShader vertexShader ! | |
vertexShader @ 1 vertexShaderSources 0 glShaderSource | |
vertexShader @ glCompileShader | |
vertexShader @ shader-check | |
GL_FRAGMENT_SHADER glCreateShader fragmentShader ! | |
fragmentShader @ 1 fragmentShaderSources 0 glShaderSource | |
fragmentShader @ glCompileShader | |
fragmentShader @ shader-check | |
glCreateProgram shaderProgram ! | |
shaderProgram @ vertexShader @ glAttachShader | |
shaderProgram @ fragmentShader @ glAttachShader | |
shaderProgram @ glLinkProgram | |
shaderProgram @ GL_LINK_STATUS success glGetProgramiv | |
success @ 0= if | |
shaderProgram @ 512 0 infoLog glGetProgramInfoLog | |
cr ." Shader linking failed: " infoLog 512 type | |
then | |
; | |
create vertices | |
-0.5e sf, -0.5e sf, 0.0e sf, | |
0.5e sf, -0.5e sf, 0.0e sf, | |
0.0e sf, 0.5e sf, 0.0e sf, | |
vertices length constant #vertices | |
variable vao | |
variable vbo | |
: make-triangle | |
1 vao glGenVertexArrays | |
vao @ glBindVertexArray | |
1 vbo glGenBuffers | |
GL_ARRAY_BUFFER vbo @ glBindBuffer | |
GL_ARRAY_BUFFER #vertices vertices GL_STATIC_DRAW glBufferData | |
GL_ARRAY_BUFFER vbo @ glBindBuffer | |
GL_ARRAY_BUFFER #vertices vertices GL_STATIC_DRAW glBufferData | |
0 3 GL_FLOAT GL_FALSE 3 4 * 0 glVertexAttribPointer | |
0 glEnableVertexAttribArray | |
; | |
fvariable i 0e i f! | |
: main | |
glfwInit 0= abort" glfw Init failed" | |
GLFW_CONTEXT_VERSION_MAJOR 3 glfwWindowHint | |
GLFW_CONTEXT_VERSION_MINOR 3 glfwWindowHint | |
GLFW_OPENGL_PROFILE GLFW_OPENGL_CORE_PROFILE glfwWindowHint | |
GLFW_OPENGL_FORWARD_COMPAT GL_TRUE glfwWindowHint | |
#width #height s" Forth" 0 0 glfwCreateWindow ?dup 0= | |
abort" glfw glfwCreateWindow failed" window ! | |
window @ glfwMakeContextCurrent | |
make-shader | |
make-triangle | |
begin | |
window @ glfwWindowShouldClose 0= | |
while | |
i f@ 0.3e f+ fcos 0.5e f* 0.5e f+ | |
i f@ 0.5e f+ fsin 0.5e f* 0.5e f+ | |
i f@ 0.7e f+ fcos 0.5e f* 0.5e f+ | |
1.0e glClearColor | |
GL_COLOR_BUFFER_BIT glClear | |
shaderProgram @ glUseProgram | |
vao @ glBindVertexArray | |
GL_TRIANGLES 0 3 glDrawArrays | |
window @ glfwSwapBuffers | |
glfwPollEvents | |
repeat | |
glfwTerminate | |
; | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment