OpenGL pixel shader: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|JavaScript}} (WebGL): Library doesn't go in header) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 11: | Line 11: | ||
{{libheader|GLUT}} |
{{libheader|GLUT}} |
||
Getting a true (pseudo) random number is surprisingly tricky. The following code makes something noisy, but not at all random:[[image:pixel_shader_C.png|right]] |
Getting a true (pseudo) random number is surprisingly tricky. The following code makes something noisy, but not at all random:[[image:pixel_shader_C.png|right]] |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <GL/glew.h> |
#include <GL/glew.h> |
||
Line 86: | Line 86: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
Line 94: | Line 94: | ||
<br> |
<br> |
||
The following uses 'cgo' to bind to the above C libraries. As C macro functions cannot be invoked directly from Go code, it has been necessary to wrap them first in 'normal' C functions and then invoke those. |
The following uses 'cgo' to bind to the above C libraries. As C macro functions cannot be invoked directly from Go code, it has been necessary to wrap them first in 'normal' C functions and then invoke those. |
||
< |
<syntaxhighlight lang="go">package main |
||
/* |
/* |
||
Line 227: | Line 227: | ||
setShader() |
setShader() |
||
C.glutMainLoop() |
C.glutMainLoop() |
||
}</ |
}</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
Line 233: | Line 233: | ||
===(WebGL)=== |
===(WebGL)=== |
||
< |
<syntaxhighlight lang="javascript"><html style="margin: 0;"> |
||
<head> |
<head> |
||
<title>Fragment Shader WebGL Example</title> |
<title>Fragment Shader WebGL Example</title> |
||
Line 339: | Line 339: | ||
</script> |
</script> |
||
</body> |
</body> |
||
</html></ |
</html></syntaxhighlight> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
Line 354: | Line 354: | ||
</pre> |
</pre> |
||
You then need to compile the following Kotlin program, linking against opengl2.klib, and run the resulting .kexe file to view the rotating triangle. |
You then need to compile the following Kotlin program, linking against opengl2.klib, and run the resulting .kexe file to view the rotating triangle. |
||
< |
<syntaxhighlight lang="scala">// Kotlin Native v0.6 |
||
import kotlinx.cinterop.* |
import kotlinx.cinterop.* |
||
Line 438: | Line 438: | ||
setShader() |
setShader() |
||
glutMainLoop() |
glutMainLoop() |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Ol}}== |
=={{header|Ol}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
#!/usr/bin/ol |
#!/usr/bin/ol |
||
(import (lib gl2)) |
(import (lib gl2)) |
||
Line 479: | Line 479: | ||
(glVertex2f -0.0 +0.7) |
(glVertex2f -0.0 +0.7) |
||
(glEnd))) |
(glEnd))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 486: | Line 486: | ||
{{libheader|Phix/online}} |
{{libheader|Phix/online}} |
||
You can run this online [http://phix.x10.mx/p2js/OpenGLShader.htm here]. |
You can run this online [http://phix.x10.mx/p2js/OpenGLShader.htm here]. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #000080;font-style:italic;">-- |
<span style="color: #000080;font-style:italic;">-- |
||
-- demo\rosetta\OpenGLShader.exw |
-- demo\rosetta\OpenGLShader.exw |
||
Line 613: | Line 613: | ||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span> |
||
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span> |
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">#lang racket/gui |
||
(require typed/opengl) |
(require typed/opengl) |
||
Line 692: | Line 692: | ||
(define gl (new my-canvas% [parent win])) |
(define gl (new my-canvas% [parent win])) |
||
(send win show #t)</ |
(send win show #t)</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
Line 700: | Line 700: | ||
Using the [http://www.tcl3d.org Tcl3d] library and liberally borrowing from [http://wiki.tcl.tk/41477 this pixel shader demo on the wiki], here is a brute translation of the C implementation. |
Using the [http://www.tcl3d.org Tcl3d] library and liberally borrowing from [http://wiki.tcl.tk/41477 this pixel shader demo on the wiki], here is a brute translation of the C implementation. |
||
<syntaxhighlight lang="tcl"> |
|||
<lang Tcl> |
|||
package require tcl3d |
package require tcl3d |
||
Line 763: | Line 763: | ||
set_shader |
set_shader |
||
render |
render |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
Line 771: | Line 771: | ||
<br> |
<br> |
||
As it's not currently possible for Wren-cli to access OpenGL directly, we embed a Wren script in a C application to complete this task. See the [[OpenGL#Wren]] task for some of the issues involved here. |
As it's not currently possible for Wren-cli to access OpenGL directly, we embed a Wren script in a C application to complete this task. See the [[OpenGL#Wren]] task for some of the issues involved here. |
||
< |
<syntaxhighlight lang="ecmascript">/* pixel_shader.wren */ |
||
import "random" for Random |
import "random" for Random |
||
Line 905: | Line 905: | ||
} |
} |
||
setShader.call() |
setShader.call() |
||
Glut.setOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS)</ |
Glut.setOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS)</syntaxhighlight> |
||
<br> |
<br> |
||
We now embed this Wren script in the following C program, compile and run it. |
We now embed this Wren script in the following C program, compile and run it. |
||
< |
<syntaxhighlight lang="c">#include <stdlib.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 1,174: | Line 1,174: | ||
free(script); |
free(script); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |