Attached is a running copy that uses Brian’s tetrahedron with the original cube 
and now they are both side by side. Spinning around. 

I expand vbo to handle this with one buffer name that is a list of glGenBuffers 
locations. I print out what 
I receive back from the buffer call. This shows what is coming back from the 
openGL DLL call back to J. It also reminded me what the {:: is doing (it’s an 
unbox select-index operator).

It is important that the call to glDrawArrays:
Cube: glDrawArrays GL_TRIANGLES; 0; 36
And
 Tetrahedron:  glDrawArrays GL_TRIANGLES; 0; 12

Correctly total the number of face index combinations.

Interestingly each time I run the program in a Jqt session I get ever 
increasing pointers from the glGenBuffers call.
a_g_close is being called so the delete call for vbo is happening. Minor issue 
but interesting how openGL works.

Tom McGuire

cocurrent 'demoshader'

mp=: +/ . *
sprog=: 0
GLSL=: 0
STOP=: 1

A=: 0 : 0
pc a;
minwh 800 800;cc g opengl flush;
rem form end;
)

a_run=: 3 : 0
NB. if. -. checkrequire_qtdemo_ 'gles';'api/gles' do. return. end.
require 'gl2'
coinsert 'jgl2'
require 'gles'
coinsert 'jgles'
require '~addons/ide/qt/opengl.ijs'
coinsert 'qtopengl'
STEPS=: 100
R=: 20 30 0
EYE=: 0 0 1
LR=: UD=: IO=: 0
UP=: 0 1 0
wd A
HD=: ".wd'qhwndc g'
wd 'ptimer 100'
wd 'pshow'
)

a_g_initialize=: 3 : 0
if. p=. >@{. glGetString GL_VERSION do. smoutput 'GL_VERSION: ', memr 0 _1 2,~ 
p end.
if. 0=p do. smoutput 'cannot retrieve GL_VERSION' return. end.
if. p=. >@{. glGetString GL_VENDOR do. smoutput 'GL_VENDOR: ', memr 0 _1 2,~ p 
end.
if. p=. >@{. glGetString GL_RENDERER do. smoutput 'GL_RENDERER: ', memr 0 _1 
2,~ p end.
if. p=. >@{. glGetString GL_SHADING_LANGUAGE_VERSION do. smoutput 
'GL_SHADING_LANGUAGE_VERSION: ', memr 0 _1 2,~ p end.
GLSL=: wglGLSL''

wglPROC''
sprog=: 0
if. GLSL>120 do.
  vsrc=. vsrc2
  fsrc=. fsrc2
else.
  vsrc=. vsrc1
  fsrc=. fsrc1
  if. 0=GLES_VERSION_jgles_ do.
    vsrc=. vsrc,~ '#define lowp', LF, '#define mediump', LF, '#define highp', LF
    fsrc=. fsrc,~ '#define lowp', LF, '#define mediump', LF, '#define highp', LF
  end.
end.
vsrc=. '#version ',(":GLSL),((GLSL>:300)#(*GLES_VERSION){::' core';' 
es'),LF,vsrc
fsrc=. '#version ',(":GLSL),((GLSL>:300)#(*GLES_VERSION){::' core';' 
es'),LF,fsrc
if.(GLSL>:300)*.0~:GLES_VERSION_jgles_ do.
  fsrc=. ('void main';'out vec4 gl_FragColor;',LF,'void main') stringreplace 
fsrc
end.
smoutput vsrc
smoutput fsrc
'err program'=. gl_makeprogram vsrc;fsrc
if. #err do. smoutput err return. end.

NB. this associates the kernel shader code 
NB. original cube data and the model-view-projection matrix
vertexAttr=: >@{. glGetAttribLocation program;'vertex'
assert. _1~: vertexAttr
colorAttr=: >@{. glGetAttribLocation program;'color'
assert. _1~: colorAttr
mvpUni=: >@{. glGetUniformLocation program;'mvp'
assert. _1~: mvpUni


NB. Here is where my shape is defined. this provides the hooks
NB. between J data and openGL
NB. Here I increase the buffer vbo to 4 locations:
NB. vbo =: 2 {:: glGenBuffers 4;4#_1
NB. I break up the above statement to print out the intermediate and
NB. final J-usable values. The boxed structure on the intermediate
NB. is how things are returned from a DLL call so the 2 {:: 
NB. accesses the 2-index boxed element data (select index unbox) 
vbo_rawreturn =: glGenBuffers 4;4#_1
smoutput vbo_rawreturn
vbo=: 2 {:: vbo_rawreturn
smoutput vbo

NB. now access into the buffer can use typical J index selection:
glBindBuffer GL_ARRAY_BUFFER; 0{vbo
glBufferData GL_ARRAY_BUFFER; (#vertexData); (symdat <'vertexData'); 
GL_STATIC_DRAW
glBindBuffer GL_ARRAY_BUFFER; 1{vbo
glBufferData GL_ARRAY_BUFFER; (#colorData); (symdat <'colorData'); 
GL_STATIC_DRAW
glBindBuffer GL_ARRAY_BUFFER; 2{vbo
glBufferData GL_ARRAY_BUFFER; (#vertexData1); (symdat <'vertexData1'); 
GL_STATIC_DRAW
glBindBuffer GL_ARRAY_BUFFER; 3{vbo
glBufferData GL_ARRAY_BUFFER; (#colorData1); (symdat <'colorData1'); 
GL_STATIC_DRAW
glBindBuffer GL_ARRAY_BUFFER; 0

sprog=: program

glClearColor 0; 0; 1; 0
)

a_g_char=: 3 : 0
if. 0=#sysdata do. return. end.
R=: 360 | R + 2 * 'xyz' = 0 { sysdata
k=. 0{sysdata
STEPS=: 200 <. STEPS + 's' = k
STEPS=: 3 >. STEPS - 'a' = k
LR=: LR - 0.01*'l'=k
LR=: LR + 0.01*'r'=k
gl_paint''
)

a_timer=: 3 : 0
if. 0=STOP do. return. end.
R=: 360 | R + 2 * 1 1 1
gl_sel HD
gl_paint''
)

a_g_paint=: 3 : 0
if. 0=sprog do. return. end.

wh=. gl_qwh''
glClearColor 0 0 0 0
glClear GL_COLOR_BUFFER_BIT + GL_DEPTH_BUFFER_BIT

glUseProgram sprog
glEnable GL_DEPTH_TEST
glEnable GL_CULL_FACE

NB. matrix convention: current matrix on the left
NB. note pre-multiplication

NB. model-view
NB. I created a new model: mvp1 so it will translate the tetrahedron
NB. to the left
NB. so there will be a cubes and a tetrahedron side by side
mvp=: (gl_Rotate (0{R), 1 0 0 ) mp (gl_Rotate (1{R), 0 1 0) mp (gl_Rotate 
(2{R), 0 0 1) mp (gl_Scale STEPS%100) mp (gl_Translate 0.6 0 _8) mp glu_LookAt 
EYE,LR,UD,IO,UP
mvp1=: (gl_Rotate (0{R), 1 0 0 ) mp (gl_Rotate (1{R), 0 1 0) mp (gl_Rotate 
(2{R), 0 0 1) mp (gl_Scale STEPS%100) mp (gl_Translate _1.2 0 _8) mp glu_LookAt 
EYE,LR,UD,IO,UP

NB. projection
mvp=: mvp mp gl_Perspective 30, (%/wh),1 10
mvp1=: mvp1 mp gl_Perspective 30, (%/wh),1 10

NB. note GL_FALSE, no transpose  
NB. Draw Object 1
glUniformMatrix4fv mvpUni; 1; GL_FALSE; mvp

glBindBuffer GL_ARRAY_BUFFER; 0{vbo
glEnableVertexAttribArray vertexAttr
glVertexAttribPointer vertexAttr; 3; GL_FLOAT; 0; 0; 0

glBindBuffer GL_ARRAY_BUFFER; 1{vbo
glEnableVertexAttribArray colorAttr
glVertexAttribPointer colorAttr; 3; GL_FLOAT; 0; 0; 0

glDrawArrays GL_TRIANGLES; 0; 36

NB. Draw object 2
glUniformMatrix4fv mvpUni; 1; GL_FALSE; mvp1

glBindBuffer GL_ARRAY_BUFFER; 2{vbo
glEnableVertexAttribArray vertexAttr
glVertexAttribPointer vertexAttr; 3; GL_FLOAT; 0; 0; 0

glBindBuffer GL_ARRAY_BUFFER; 3{vbo
glEnableVertexAttribArray colorAttr
glVertexAttribPointer colorAttr; 3; GL_FLOAT; 0; 0; 0

glDrawArrays GL_TRIANGLES; 0; 12

NB. The draw arrays need to draw the correct number of
NB. face indexes for each shape:
NB. cube: 36
NB. tetrahedron: 12

NB. Objects drawn perform glDisableVertexAttribArray 
NB. what ever that is for
glBindBuffer GL_ARRAY_BUFFER; 0
glDisableVertexAttribArray colorAttr
glDisableVertexAttribArray vertexAttr

glDisable GL_DEPTH_TEST
glDisable GL_CULL_FACE

glUseProgram 0

gl_clear ''
gl_rgb 0 255 0
gl_textcolor ''
gl_textxy 10 30
gl_text 'keys: x y z a s l r F10'
gl_textxy 10 50
gl_text 'scale: ',":STEPS%100
gl_textxy 10 70
gl_text 'angle: ',":R
gl_textxy 10 90
if. 0=sprog do. return. end.
gl_text 'matrix:'
for_i. i.4 do.
  gl_textxy 10, 105+i*15
  gl_text 6j2": i{mvp
end.
)

a_f10_fkey=: 3 : 0
STOP=: -.STOP
)

a_cancel=: a_close

a_close=: 3 : 0
STOP=: 0
wd 'ptimer 0'
glDeleteBuffers ::0: 4; vbo
glDeleteProgram ::0: sprog
wd 'pclose'
smoutput 'Exiting program'
)

vertexData=: 1&fc , 0 1 2 2 1 3&{"2 ] 6 4 3$ , 0&".@(-.&',') ;._2 [ 0 : 0
_1 _1  1
 1 _1  1
_1  1  1
 1  1  1 ,
 1 _1  1
 1 _1 _1
 1  1  1
 1  1 _1 ,
 1 _1 _1
_1 _1 _1
 1  1 _1
_1  1 _1 ,
_1 _1 _1
_1 _1  1
_1  1 _1
_1  1  1 ,
_1 _1 _1
 1 _1 _1
_1 _1  1
 1 _1  1 ,
_1  1  1
 1  1  1
_1  1 _1
 1  1 _1 ,
)

NB. rgb for each vertex
colorData=: 1&fc , 0 1 2 2 1 3&{"2 ] 6 4 3$ , 0&".@(-.&',') ;._2 [ 0 : 0
0 0 1
1 0 1
0 1 1
1 1 1
1 0 1
1 0 0
1 1 1
1 1 0
1 0 0
0 0 0
1 1 0
0 1 0
0 0 0
0 0 1
0 1 0
0 1 1
0 0 0
1 0 0
0 0 1
1 0 1
0 1 1
1 1 1
0 1 0
1 1 0
)

NB. =========================================================
NB. Tetrahedron data
vertexData1=: 1&fc ,1 2 0 0 2 3 3 2 1 1 0 3{4 3$, %&10, 0&". ;._2 [ 0 : 0
_3 _9 _4
3 _9 _4
0  0  2
0  8 _4
)

colorData1=: 1&fc ,1,"1~_3]\3#"2] 0&". ;._2 [ 0 : 0
1 0 0
0 1 0
1 1 1
0 1 1
)


NB. =========================================================
vsrc1=: 0 : 0
attribute highp vec3 vertex;
attribute lowp vec3 color;
varying lowp vec4 v_color;
uniform mat4 mvp;
void main(void)
{
  gl_Position = mvp * vec4(vertex,1.0);
  v_color = vec4(color,1.0);
}
)

fsrc1=: 0 : 0
varying lowp vec4 v_color;
void main(void)
{
  gl_FragColor = v_color;
}
)

NB. =========================================================
vsrc2=: 0 : 0
in vec3 vertex;
in vec3 color;
out vec4 v_color;
uniform mat4 mvp;
void main(void)
{
  gl_Position = mvp * vec4(vertex,1.0);
  v_color = vec4(color,1.0);
}
)

fsrc2=: 0 : 0
in vec4 v_color;
void main(void)
{
  gl_FragColor = v_color;
}
)

NB. =========================================================
a_run''

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to