Draw a rotating cube: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: added syntax colouring, made p2js compatible) |
(J) |
||
Line 1,221: | Line 1,221: | ||
Link to live demo: https://dc25.github.io/drawRotatingCubeHaskell/ |
Link to live demo: https://dc25.github.io/drawRotatingCubeHaskell/ |
||
=={{header|J}}== |
|||
Derived from J's [https://github.com/jsoftware/demos_qtdemo/blob/master/shader.ijs qt shader demo]: |
|||
<lang J>require'gl2 gles ide/qt/opengl' |
|||
coinsert'jgl2 jgles qtopengl' |
|||
rotcube=: {{ |
|||
if.0=nc<'sprog'do.return.end. |
|||
fixosx=. 'opengl';'opengl',('DARWIN'-:UNAME)#' version 4.1' |
|||
wd 'pc rot; minwh 300 300; cc cube opengl flush' rplc fixosx |
|||
HD=: ".wd 'qhwndc cube' |
|||
wd 'ptimer 17; pshow' |
|||
}} |
|||
rot_close=: {{ |
|||
wd 'ptimer 0' |
|||
glDeleteBuffers ::0: 2; vbo |
|||
glDeleteProgram ::0: sprog |
|||
erase 'sprog' |
|||
wd 'pclose' |
|||
}} |
|||
cstr=: {{if.y do.memr y,0 _1 2 else.EMPTY end.}} |
|||
gstr=: {{cstr>{.glGetString y}} |
|||
diag=: {{p[echo y,': ',p=.gstr".y}} |
|||
blitf=: {{ |
|||
dat=. 1 fc,y |
|||
glBindBuffer GL_ARRAY_BUFFER; x{vbo |
|||
glBufferData GL_ARRAY_BUFFER; (#dat); (symdat<'dat'); GL_STATIC_DRAW |
|||
}} |
|||
rot_cube_initialize=: {{ |
|||
erase'sprog' |
|||
if.0=#diag 'GL_VERSION' do.echo 'cannot retrieve GL_VERSION' return.end. |
|||
diag'GL_VENDOR' |
|||
diag'GL_RENDERER' |
|||
diag'GL_SHADING_LANGUAGE_VERSION' |
|||
GLSL=:wglGLSL'' |
|||
wglPROC'' |
|||
'err program'=. gl_makeprogram VSRC ;&fixversion FSRC |
|||
if.#err do. echo 'err: ', err return.end. |
|||
if. GLSL>120 do.vao=: >{:glGenVertexArrays 1;,_1 end. |
|||
assert _1~:vertexAttr=: >{.glGetAttribLocation program;'vertex' |
|||
assert _1~:colorAttr=: >{.glGetAttribLocation program;'color' |
|||
assert _1~:mvpUni=: >{.glGetUniformLocation program;'mvp' |
|||
vbo=: >{:glGenBuffers 2;2#_1 |
|||
0 blitf vertexDat |
|||
1 blitf colorDat |
|||
sprog=: program |
|||
}} |
|||
fixversion=: {{ |
|||
NB. cope with host shader language version |
|||
r=. '$version';GLSL,&":;(GLSL>:300)#(*GLES_VERSION){' core';' es' |
|||
r=.r, '$v_in';(GLSL>120){'attribute';'in' |
|||
r=.r, '$v_out';(GLSL>120){'varying';'out' |
|||
r=.r, '$f_in';(GLSL>120){'varying';'in' |
|||
r=.r, '$highp ';(GLSL<:120)#(*GLES_VERSION)#'highp' |
|||
r=.r, '$lowp ';(GLSL<:120)#(*GLES_VERSION)#'lowp' |
|||
r=.r, '$fragColor';((330<:GLSL)+.(300<:GLSL)**GLES_VERSION)#'out vec4 fragColor' |
|||
y rplc r |
|||
}} |
|||
VSRC=: {{)n |
|||
#version $version |
|||
$v_in $highp vec3 vertex; |
|||
$v_in $lowp vec3 color; |
|||
$v_out $lowp vec4 v_color; |
|||
uniform mat4 mvp; |
|||
void main(void) { |
|||
gl_Position= mvp * vec4(vertex,1.0); |
|||
v_color= vec4(color,1.0); |
|||
} |
|||
}} |
|||
FSRC=: {{)n |
|||
#version $version |
|||
$f_in $lowp vec4 v_color; |
|||
$fragColor; |
|||
void main(void) { |
|||
gl_FragColor= v_color; |
|||
} |
|||
}} |
|||
A=:0 |
|||
rot_timer=: {{ |
|||
try. |
|||
A=:360|A+1 |
|||
gl_sel HD |
|||
gl_paint'' |
|||
catch. |
|||
echo 'error in rot_timer',LF,13!:12'' |
|||
wd'ptimer 0' |
|||
end. |
|||
}} |
|||
zeroVAttr=: {{ |
|||
glEnableVertexAttribArray y |
|||
glBindBuffer GL_ARRAY_BUFFER; x{vbo |
|||
glVertexAttribPointer y; 3; GL_FLOAT; 0; 0; 0 |
|||
}} |
|||
mp=: +/ .* |
|||
ref=: (gl_Translate 0 0 _10) mp glu_LookAt 0 0 1,0 0 0,1 0 0 |
|||
rot_cube_paint=: {{ |
|||
try. |
|||
if.nc<'sprog' do.return.end. |
|||
wh=. gl_qwh'' |
|||
glClear GL_COLOR_BUFFER_BIT+GL_DEPTH_BUFFER_BIT [glClearColor 0 0 0 0+0.2 |
|||
glUseProgram sprog |
|||
glEnable each GL_DEPTH_TEST, GL_CULL_FACE, GL_BLEND |
|||
glBlendFunc GL_SRC_ALPHA; GL_ONE_MINUS_SRC_ALPHA |
|||
mvp=. (gl_Rotate A,1 0 0)mp ref mp gl_Perspective 30, (%/wh),1 20 |
|||
glUniformMatrix4fv mvpUni; 1; GL_FALSE; mvp |
|||
if. GLSL>120 do. glBindVertexArray {.vao end. |
|||
0 zeroVAttr vertexAttr |
|||
1 zeroVAttr colorAttr |
|||
glDrawArrays GL_TRIANGLES; 0; 36 |
|||
glUseProgram 0 |
|||
catch. |
|||
echo 'error in rot_cube_paint',LF,13!:12'' |
|||
wd'ptimer 0' |
|||
end. |
|||
}} |
|||
NB. oriented triangle representation of unit cube |
|||
unitCube=: #:(0 1 2, 2 1 3)&{@".;._2 {{)n |
|||
6 2 4 0 NB. unit cube corner indices |
|||
2 3 0 1 NB. 0: origin |
|||
3 7 1 5 NB. 1, 2, 4: unit distance along each axis |
|||
7 6 5 4 NB. 3, 5, 6, 7: combinations of axes |
|||
7 3 6 2 |
|||
4 0 5 1 |
|||
}} |
|||
NB. orientation for a cube such that diagonal is along first axis |
|||
daxis=: (_1^5 6 e.~i.3 3)*%:6%~2 0 4,2 3 1,:2 3 1 |
|||
NB. (short float) triangle representation of cube with center at origin |
|||
vertexData=: 1 fc,vertexDat=:(_1^unitCube)mp daxis |
|||
colorData=: 1 fc,colorDat=: unitCube NB. corresponding colors |
|||
rotcube'' |
|||
</lang> |
|||
A variation which did not use opengl would probably be much more concise. |
|||
=={{header|Java}}== |
=={{header|Java}}== |