OpenGL: Difference between revisions

1,518 bytes added ,  4 months ago
m
(→‎{{header|Phix}}: added p2js compatible version, archived prior to sub-page)
m (→‎{{header|Wren}}: Minor tidy)
 
(3 intermediate revisions by 2 users not shown)
Line 14:
 
opengl.adb:
<langsyntaxhighlight Adalang="ada">with Lumen.Window;
with Lumen.Events;
with Lumen.Events.Animate;
Line 134:
when Program_Exit =>
null; -- normal termination
end OpenGL;</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">hOpenGL32 := DllCall("LoadLibrary", "Str", "opengl32")
Gui, +LastFound +Resize
hDC := DllCall("GetDC", "uInt", WinExist())
Line 197:
DllCall("ReleaseDC", "uInt", hDC)
DllCall("FreeLibrary", "uInt", hOpenGL32)
ExitApp</langsyntaxhighlight>
 
=={{header|BaCon}}==
BaCon allows embedding C code. This is an example with GLUT.
<langsyntaxhighlight lang="qbasic">PRAGMA INCLUDE <GL/gl.h> <GL/freeglut.h>
PRAGMA LDFLAGS GL glut
 
Line 234:
 
glutDisplayFunc(Triangle)
glutMainLoop()</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> *FLOAT64
SYS "LoadLibrary", "OPENGL32.DLL" TO opengl%
Line 319:
ghRC% += 0 : IF ghRC% SYS `wglDeleteContext`, ghRC% : ghRC% = 0
ghDC% += 0 : IF ghDC% SYS "ReleaseDC", @hwnd%, ghDC% : ghDC% = 0
ENDPROC</langsyntaxhighlight>
 
=={{header|C}}==
Line 326:
In this example, we use [[:Category:GLUT|GLUT]] to create a window and handle the main loop in a portable way. Windowing systems like MS Windows and X11 have their own platform-specific ways of handling these things.
 
<langsyntaxhighlight lang="c">#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glut.h>
Line 373:
 
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
C# example using the [http://www.opentk.com/ OpenTK] library, which is multiplatform and provides C# OpenGL bindings for .Net and Mono. This code creates its own window and draws the triangle into it.
 
<langsyntaxhighlight lang="csharp">using OpenTK;
using OpenTK.Graphics;
namespace OpenGLTest
Line 421:
}
}
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
Line 427:
 
In this example, we use [http://github.com/ztellman/penumbra Penumbra], which is an idiomatic wrapper for OpenGL.
<langsyntaxhighlight lang="lisp">(use 'penumbra.opengl)
(require '[penumbra.app :as app])
 
Line 448:
(color 0 0 1) (vertex 0 30)))
 
(app/start {:display display, :reshape reshape, :init init} {})</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
Line 457:
{{libheader|Lispbuilder-SDL}}
 
<langsyntaxhighlight lang="lisp">(defun draw-triangle (x y &key (z 0) (type 'right))
(case type
(right
Line 523:
(setup-gl w h)
(setf (sdl:frame-rate) 2)
(main-loop)))</langsyntaxhighlight>
 
=={{header|D}}==
Line 532:
{{libheader|dglut}}
opengl_sample.d:
<langsyntaxhighlight lang="d">module opengl_sample; // file name + directory
import dglut.core, dglut.window, dglut.opengl;
 
Line 563:
}
loop;
}</langsyntaxhighlight>
=={{header|Delphi}}==
{{libheader| System.Classes}}
Line 570:
{{libheader| System.UITypes}}
Thanks Neslib for libraries FastMath [https://github.com/neslib/FastMath], DelphiStb [https://github.com/neslib/DelphiStb] and Samples codes [https://github.com/neslib/DelphiLearnOpenGL] to abstract openGl functions and window creation.
<syntaxhighlight lang="delphi">
<lang Delphi>
program OpenGLTriangle;
 
Line 642:
begin
RunApp(TTriangleApp, 640, 480, 'OpenGL Triangle');
end.</langsyntaxhighlight>
 
=={{header|eC}}==
{{libheader|Ecere}}
 
<langsyntaxhighlight Clang="c">#include <GL/gl.h>
import "ecere"
 
Line 681:
}
 
GLTriangle window {};</langsyntaxhighlight>
 
=={{header|Euphoria}}==
{{works with|OpenEuphoria < 4.0}}
Adapted from NeHe tutorial #3 nehe.gamedev.net
<langsyntaxhighlight lang="euphoria">
include get.e
include dll.e
Line 979:
 
WinMain()
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
Translated from C
<langsyntaxhighlight lang="factor">USING: kernel math math.rectangles opengl.gl sequences ui
ui.gadgets ui.render ;
IN: rosettacode.opengl
Line 1,016:
[ triangle-gadget new "Triangle" open-window ] with-ui ;
MAIN: triangle-window
</syntaxhighlight>
</lang>
 
=={{header|Forth}}==
Line 1,023:
{{libheader|Theseus}}
triangle.fs:
<langsyntaxhighlight lang="forth">import glconst import float
glconst also float also opengl also</langsyntaxhighlight>
 
triangle.m:
<langsyntaxhighlight lang="forth">#! xbigforth
\ automatic generated code
\ do not edit
Line 1,080:
$1 0 ?DO stop LOOP bye ;
script? [IF] main [THEN]
previous previous previous</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
This is adapted from example OpenGL code that's included with FreeBASIC distributions.
 
<langsyntaxhighlight FreeBASIClang="freebasic">#include "fbgfx.bi"
#include once "GL/gl.bi"
#include once "GL/glu.bi"
Line 1,122:
flip
 
loop while inkey = ""</langsyntaxhighlight>
 
=={{header|Go}}==
Line 1,130:
<br>
This program was also tested on Windows 10 but ''may'' only work if you comment out the marked line. This is because gl.Init() may produce an initialization error which is non-critical as far as this program is concerned. The reason for this error is unknown (see [[https://rosettacode.org/wiki/Talk:OpenGL Talk page]]).
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,220:
 
gl.Flush()
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Graphics.Rendering.OpenGL
import Graphics.UI.GLUT
 
Line 1,247:
corner r g b x y = do color (Color3 r g b :: Color3 GLfloat)
vertex (Vertex2 x y :: Vertex2 GLfloat)</langsyntaxhighlight>
 
=={{header|J}}==
Line 1,255:
Additionally, if you are using 64 bit windows, to get opengl working on J602 you will need to copy jzopengl_win.ijs to jzopengl_win_64.ijs in system/classes/opengl/.
 
<langsyntaxhighlight Jlang="j">coclass 'example'
(coinsert[require) 'jzopengl'
 
Line 1,285:
)
 
conew~'example'</langsyntaxhighlight>
 
Note: OpenGL's initial state is well defined by the OpenGL standard.
Line 1,291:
=={{header|Java}}==
This example uses [http://lwjgl.org/ LWJGL], a game library which has an OpenGL binding for Java
<langsyntaxhighlight Javalang="java">import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
Line 1,349:
}
 
</syntaxhighlight>
</lang>
 
=={{header|JavaScript}} (WebGL)==
 
===(WebGL)===
 
Unfortunately for comparison with the other examples on this page, WebGL provides only OpenGL ES, which removes the classic “fixed-function pipeline” and glVertex() in favor of requiring you to write ''vertex shaders'' and ''fragment shaders'', and use vertex arrays. It is not hard to write shader programs to emulate as much of the fixed-function pipeline as you need, but it does mean more verbosity as you have to explicitly define all of the data you're going to communicate to your shader.
Line 1,357 ⟶ 1,359:
In the interest of brevity and not depending on an external matrix library, this example ''omits matrix operations entirely'', as OpenGL ES requires you to add those features yourself if you want them. Examples which show how to implement the classic OpenGL matrix stack are available at [http://learningwebgl.com/blog/?page_id=1217 Learning WebGL] (which this code was derived from).
 
<langsyntaxhighlight lang="html"><html style="margin: 0;">
<head>
<title>Minimal WebGL Example</title>
Line 1,472 ⟶ 1,474:
</script>
</body>
</html></langsyntaxhighlight>
 
=={{header|Julia}}==
Julia's Makie plotting package uses OpenGL as its backend. This example is from the Makie documentation.
<langsyntaxhighlight lang="julia">using Makie
 
mesh([(0.0, 0.0), (0.5, 1.0), (1.0, 0.0)], color = [:red, :green, :blue], shading = false)
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
Line 1,485 ⟶ 1,487:
{{libheader|GLUT}}
{{Works with|Ubuntu 14.04}}
<langsyntaxhighlight lang="scala">// Kotlin Native version 0.3
 
import kotlinx.cinterop.*
Line 1,532 ⟶ 1,534:
glutMainLoop()
}</langsyntaxhighlight>
 
{{output}}
Line 1,540 ⟶ 1,542:
 
=={{header|Liberty BASIC}}==
<langsyntaxhighlight lang="lb">nomainwin
struct rect, x as long, y as long, x2 as long, y2 as long
struct PFD, Size as word, Version as word, Flags as long,_
Line 1,590 ⟶ 1,592:
close #glu
close #gl
end</langsyntaxhighlight>
 
=={{header|Lingo}}==
{{libheader|RavOpenGL xtra}}
<langsyntaxhighlight Lingolang="lingo">global gOpenGL -- RavOpenGL xtra instance
global GL -- OpenGL constants
 
Line 1,651 ⟶ 1,653:
-- Show the window
_movie.stage.visible = TRUE
end</langsyntaxhighlight>
 
=={{header|Lua}}==
Line 1,659 ⟶ 1,661:
Note that GL functions that take constants in LuaGL can take either the numbers representing those flags (ie. gl.XXX + gl.YYY) or a comma-separated string of those flags (ie. "XXX,YYY"). This example uses strings.
 
<langsyntaxhighlight lang="lua">local gl = require "luagl"
local iup = require "iuplua"
require "iupluagl"
Line 1,701 ⟶ 1,703:
 
iup.MainLoop()
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Non-Windows only:
<langsyntaxhighlight Mathematicalang="mathematica">Style[Graphics3D[{Polygon[{{-1, 0, 0}, {1, 0, 0}, {0, Sqrt[3], 0.5}},
VertexColors -> {Red, Green, Blue}]}, Boxed -> False],
RenderingOptions -> {"3DRenderingEngine" -> "OpenGL"}]</langsyntaxhighlight>
 
=={{header|MAXScript}}==
The choice of OpenGL or D3D in MAX is a user configuration setting. All MAXScript code is platform independent.
<langsyntaxhighlight lang="maxscript">newMesh = mesh numVerts:3 numFaces:1
setMesh newMesh vertices:#([-100, -100, 0], [100, -100, 0], [-100, 100, 0]) faces:#([1, 2, 3])
defaultVCFaces newMesh
Line 1,721 ⟶ 1,723:
viewport.setType #view_top
max tool maximize
viewport.SetRenderLevel #smoothhighlights</langsyntaxhighlight>
 
=={{header|Mercury}}==
Line 1,727 ⟶ 1,729:
{{libheader|mercury_opengl}}
Translated from C.
<syntaxhighlight lang="text">:- module opengl.
:- interface.
 
Line 1,772 ⟶ 1,774:
mogl.load_identity(!IO),
mogl.ortho(-30.0, 30.0, -30.0, 30.0, -30.0, 30.0, !IO),
mogl.matrix_mode(modelview, !IO).</langsyntaxhighlight>
 
=={{header|Nim}}==
{{libheader|OpenGL}}
{{libheader|Nim bindings for OpenGL}}
<langsyntaxhighlight lang="nim">import opengl, opengl/glut
 
proc paint() {.cdecl.} =
Line 1,815 ⟶ 1,817:
glutReshapeFunc(reshape)
 
glutMainLoop()</langsyntaxhighlight>
 
=={{header|OCaml}}==
{{libheader|glMLite}}
<langsyntaxhighlight lang="ocaml">open GL
open Glut
 
Line 1,860 ⟶ 1,862:
 
glutMainLoop();
;;</langsyntaxhighlight>
 
=={{header|Ol}}==
Line 1,866 ⟶ 1,868:
OpenGL window works in background (as coroutine) and allow user to call any functions, including OpenGL, in REPL simultaneously with window rendering process.
 
<langsyntaxhighlight lang="scheme">
(import (lib gl))
(gl:set-window-title "Rosettacode OpenGL example")
Line 1,894 ⟶ 1,896:
(glEnd)
))
</syntaxhighlight>
</lang>
 
=={{header|OxygenBasic}}==
<langsyntaxhighlight lang="oxygenbasic">
title="Rotating Triangle"
include "OpenglSceneFrame.inc"
Line 1,940 ⟶ 1,942:
end sub
 
</syntaxhighlight>
</lang>
 
=={{header|Pascal}}==
Line 1,948 ⟶ 1,950:
{{libheader|GLUT}}
Ported from the C example.
<langsyntaxhighlight lang="pascal">Program OpenGLDemo;
 
uses
Line 1,998 ⟶ 2,000:
end.
 
</syntaxhighlight>
</lang>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use OpenGL;
 
sub triangle {
Line 2,024 ⟶ 2,026:
glpFlush;
 
glpMainLoop;</langsyntaxhighlight>
 
==={{libheader|Perl/SDL}}===
<langsyntaxhighlight lang="perl">use SDL::App;
use SDL::Event;
use SDL::OpenGL;
Line 2,055 ⟶ 2,057:
$app->loop ({
SDL_QUIT() => sub { exit; },
});</langsyntaxhighlight>
 
=={{header|Phix}}==
Line 2,062 ⟶ 2,064:
{{trans|JavaScript}}
This can be run online [http://phix.x10.mx/p2js/OpenGL.htm here], also works on desktop/Phix.<br>
You can find older windows 32bit only and OpenGL 1.0 verions [[OpenGL/Phix |here]].
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\OpenGL.exw
Line 2,190 ⟶ 2,192:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
 
=={{header|PicoLisp}}==
This is for the 64-bit version.
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/openGl.l")
 
(glutInit)
Line 2,227 ⟶ 2,229:
(mouseFunc '((Btn State X Y) (bye)))
 
(glutMainLoop)</langsyntaxhighlight>
 
=={{header|Pike}}==
Uses GLUE to create the window. Rendering code is based on the C example.
<langsyntaxhighlight lang="pike">int main() {
GLUE.init(([
"fullscreen": 0,
Line 2,267 ⟶ 2,269:
sleep(0.01);
}
}</langsyntaxhighlight>
 
=={{header|PureBasic}}==
Line 2,273 ⟶ 2,275:
 
 
<syntaxhighlight lang="purebasic">
<lang Purebasic>
XIncludeFile "OpenGL.pbi"
pfd.PIXELFORMATDESCRIPTOR
Line 2,316 ⟶ 2,318:
Wend
 
</syntaxhighlight>
</lang>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">#!/usr/bin/env python
#-*- coding: utf-8 -*-
 
Line 2,360 ⟶ 2,362:
glutReshapeFunc(reshape)
 
glutMainLoop()</langsyntaxhighlight>
=={{header|QB64}}==
<syntaxhighlight lang="qb64">
<lang QB64>
'Task
'Display a smooth shaded triangle with OpenGL.
Line 2,388 ⟶ 2,390:
_glEnd
End Sub
</syntaxhighlight>
</lang>
=={{header|R}}==
{{libheader|rgl}}
<langsyntaxhighlight Rlang="r">library(rgl)
x <- c(-1, -1, 1)
y <- c(0, -1, -1)
Line 2,397 ⟶ 2,399:
M <- cbind(x,y,z)
rgl.bg(color="gray15")
triangles3d(M, col=rainbow(8))</langsyntaxhighlight>
 
=={{header|Racket}}==
Line 2,405 ⟶ 2,407:
If OpenGL context creation fails please consult Racket [http://docs.racket-lang.org/gui/libs.html documentation]
 
<langsyntaxhighlight lang="racket">
#lang racket/gui
(require sgl/gl)
Line 2,448 ⟶ 2,450:
 
(send win show #t)
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 2,457 ⟶ 2,459:
It's a little verbose since it is doing all of the setup and loading manually.
 
<syntaxhighlight lang="raku" perl6line>use NativeCall;
 
class Window is repr('CPointer') {}
Line 2,545 ⟶ 2,547:
 
glfwTerminate();
</syntaxhighlight>
</lang>
 
See screen cap: [https://github.com/thundergnat/rc/blob/master/img/OpenGL-Triangle-perl6.png OpenGL-Triangle-perl6.png] (Offsite PNG image.)
Line 2,552 ⟶ 2,554:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project: OpenGL
 
Line 2,575 ⟶ 2,577:
glEnd()
glutSwapBuffers()
</syntaxhighlight>
</lang>
Outputimage:
 
Line 2,583 ⟶ 2,585:
{{libheader|ruby-opengl}}
 
<langsyntaxhighlight lang="ruby">require 'rubygems'
require 'gl'
require 'glut'
Line 2,626 ⟶ 2,628:
glutReshapeFunc(reshape)
 
glutMainLoop</langsyntaxhighlight>
=={{header|Rust}}==
using glutin for window creation and glow for opengl/opengles functions. as such, the fixed function pipeline is not supported
<langsyntaxhighlight Rustlang="rust">use glow::*;
use glutin::event::*;
use glutin::event_loop::{ControlFlow, EventLoop};
Line 2,739 ⟶ 2,741:
_ => {}
});
}</langsyntaxhighlight>
=={{header|Scala}}==
{{libheader|Light Weight Java Game Library}}
<langsyntaxhighlight Scalalang="scala">import org.lwjgl.opengl.{ Display, DisplayMode }
import org.lwjgl.opengl.GL11._
 
Line 2,780 ⟶ 2,782:
Thread.sleep(1000)
}
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
{{libheader|Tk}}
{{libheader|tcl3d}}
<langsyntaxhighlight Tcllang="tcl">package require Tk
package require tcl3d
 
Line 2,814 ⟶ 2,816:
togl .surface -width 640 -height 480 -double true -depth true \
-displayproc paintShape -reshapeproc resizedWin
pack .surface -fill both -expand 1</langsyntaxhighlight>Most of this code should be very familiar to anyone looking at the C version above, or with normal [[Tk]] applications.
 
=={{header|Wren}}==
Line 2,824 ⟶ 2,826:
 
Notice that we can't pass Wren methods directly to the glutDisplayFunc and glutReshapeFunc functions for callback registration purposes because of re-entrancy problems so, instead, we pass sufficient information from Wren to enable the callbacks to be constructed from the C side.
<langsyntaxhighlight ecmascriptlang="wren">/* openglOpenGL.wren */
 
var GL_COLOR_BUFFER_BIT = 0x4000
Line 2,909 ⟶ 2,911:
Glut.displayFunc("GLCallbacks", "paint()")
Glut.reshapeFunc("GLCallbacks", "reshape(_,_)")
Glut.setOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS)</langsyntaxhighlight>
<br>
We now embed this Wren script in the following C program, compile and run it.
<langsyntaxhighlight lang="c">#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Line 3,121 ⟶ 3,123:
vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "openglOpenGL.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
Line 3,138 ⟶ 3,140:
free(script);
return 0;
}</langsyntaxhighlight>
 
{{out}}
9,476

edits