這是“ Hello,World”的圖形
// first OpenVG program
// Anthony Starks ([email protected])
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "VG/openvg.h"
#include "VG/vgu.h"
#include "fontinfo.h"
#include "shapes.h"
int main() {
int width, height;
char s[3];
init(&width, &height); // Graphics initialization
Start(width, height); // Start the picture
Background(0, 0, 0); // Black background
Fill(44, 77, 232, 1); // Big blue marble
Circle(width / 2, 0, width); // The "world"
Fill(255, 255, 255, 1); // White text
TextMid(width / 2, height / 2, "hello, world", SerifTypeface, width / 10); // Greetings
End(); // End the picture
fgets(s, 2, stdin); // look at the pic, end with [RETURN]
finish(); // Graphics cleanup
exit(0);
}
坐標是vgfloat值,其原點位於左下方,x向右增加,y增加。 OpenVG將顏色指定為vgfloat陣列,其中包含紅色,綠色,藍色,alpha值範圍為0.0至1.0,但通常將顏色指定為RGBA(RGB的0-255,a為0-255,a 0.0至1.0))
void WindowClear()
窗簾清除窗口以設置背景顏色
void AreaClear(unsigned int x, unsigned int y, unsigned int w, unsigned int h)
區域清除在窗口坐標中清除給定的矩形
void WindowOpacity(unsigned int a)
窗口台設置窗口不透明度
void WindowPosition(int x, int y)
窗口位置將窗口移至給定的位置
void init(int *w, int *h)
初始化圖形:返回畫布的寬度和高度。這應該開始每個程序。
void initWindowSize(int x, int y, unsigned int w, unsigned int h)
用特定維度初始化
void finish()
關閉圖形。這應該結束每個程序。
void Start(int width, int height)
開始圖片,用默認的白色清除屏幕,將筆觸設置為黑色。
void End()
結束圖片,渲染到屏幕。
void SaveEnd(char *filename)
結束圖片,渲染到屏幕,將柵格保存到命名文件為4字節RGBA單詞,寬度為寬度*4字節。 RAW2PNG程序將“ RAW”柵格轉換為PNG。
void saveterm(), restoreterm(), rawterm()
終端設置,保存當前設置,還原設置,將終端設置為原始模式。
void setfill(float color[4])
設置填充顏色
void Background(unsigned int r, unsigned int g, unsigned int b)
用RGB值定義的背景顏色填充屏幕。
void BackgroundRGB(unsigned int r, unsigned int g, unsigned int b, VGfloat a)
用alpha將屏幕清除為背景顏色
void StrokeWidth(float width)
設置中風寬度。
void RGBA(unsigned int r, unsigned int g, unsigned int b, VGfloat a, VGfloat color[4])
從RGBA值填充顏色向量。
void RGB(unsigned int r, unsigned int g, unsigned int b, VGfloat color[4])
從RGB值中填充顏色向量。
void Stroke(unsigned int r, unsigned int g, unsigned int b, VGfloat a)
使用RGBA值設置衝程顏色。
void Fill(unsigned int r, unsigned int g, unsigned int b, VGfloat a)
使用RGBA值設置填充顏色。
void FillLinearGradient(VGfloat x1, VGfloat y1, VGfloat x2, VGfloat y2, VGfloat *stops, int n)
將填充物設置為一個線性梯度,該梯度由(x1,y1)和(x2,y2)界定。使用n個停止數中指定的偏移和顏色
void FillRadialGradient(VGfloat cx, VGfloat cy, VGfloat fx VGfloat fy, VGfloat r, VGfloat *stops, int n)
將填充物設置為徑向梯度,以radius r為中心(CX,CY),並使用n個停止數中指定的偏移量和顏色(fx,ry)處於(fx,ry)處的焦點
void Line(VGfloat x1, VGfloat y1, VGfloat x2, VGfloat y2)
在(x1,y1)和(x2,y2)之間畫一條線。
void Rect(VGfloat x, VGfloat y, VGfloat w, VGfloat h)
在(x,y)處繪製其原點(左下)的矩形,大小為(寬度,高度)。
void RectOutline(VGfloat x, VGfloat y, VGfloat w, VGfloat h)
概述版本
void Roundrect(VGfloat x, VGfloat y, VGfloat w, VGfloat h, VGfloat rw, VGfloat rh)
在(x,y)上繪製一個圓形矩形(左下),大小為(寬度,高度)。
拐角的寬度和高度用(RW,RH)指定。
void RoundrectOutline(VGfloat x, VGfloat y, VGfloat w, VGfloat h, VGfloat rw, VGfloat rh)
概述版本
void Polygon(VGfloat *x, VGfloat *y, VGint n)
使用x和y指向的陣列中的坐標繪製多邊形。坐標的數量為n。
void Polyline(VGfloat *x, VGfloat *y, VGint n)
使用指向x和y指向的陣列中的坐標繪製多線線。坐標的數量為n。
void Circle(VGfloat x, VGfloat y, VGfloat d)
在(x,y)以直徑為中心繪製一個圓圈d。
void CircleOutline(VGfloat x, VGfloat y, VGfloat r)
概述版本
void Ellipse(VGfloat x, VGfloat y, VGfloat w, VGfloat h)
畫一個用半徑(x,y)以(x,y)為中心的橢圓形(W,h)。
void EllipseOutline(VGfloat x, VGfloat y, VGfloat w, VGfloat h)
概述版本
void Qbezier(VGfloat sx, VGfloat sy, VGfloat cx, VGfloat cy, VGfloat ex, VGfloat ey)
使用(cx,cy)的控制點(ex,ey)繪製(sx,sy)的二次曲線曲線。
void QbezierOutline(VGfloat sx, VGfloat sy, VGfloat cx, VGfloat cy, VGfloat ex, VGfloat ey)
概述版本
void Cbezier(VGfloat sx, VGfloat sy, VGfloat cx, VGfloat cy, VGfloat px, VGfloat py, VGfloat ex, VGfloat ey)
使用(cx,cy)和(px,py)的控制點從(sx,sy)開始,繪製一個立方體bezer曲線,結束於(ex,ey)。
void CbezierOutline(VGfloat sx, VGfloat sy, VGfloat cx, VGfloat cy, VGfloat px, VGfloat py, VGfloat ex, VGfloat ey)
概述版本
void Arc(VGfloat x, VGfloat y, VGfloat w, VGfloat h, VGfloat sa, VGfloat aext)
在(x,y)中以寬度和高度為中心,在(w,h)上繪製一個橢圓形弧。啟動角(度)為SA,角度範圍為aext。
void ArcOutline(VGfloat x, VGfloat y, VGfloat w, VGfloat h, VGfloat sa, VGfloat aext)
概述版本
void Text(VGfloat x, VGfloat y, char* s, Fontinfo f, int pointsize)
使用點尺寸在位置(x,y)處繪製a文本srtring(s)。
void TextMid(VGfloat x, VGfloat y, char* s, Fontinfo f, int pointsize)
使用點尺寸,在位置(x,y)的中心繪製a文本srtring(s)。
void TextEnd(VGfloat x, VGfloat y, char* s, Fontinfo f, int pointsize)
繪製a文本srtring(s),其借貸與位置(x,y)對齊(x,y),使用指數
VGfloat TextWidth(char *s, Fontinfo f, int pointsize)
返回文字的寬度
VGfloat TextHeight(Fontinfo f, int pointsize)
返回字體的高度
TextDepth(Fontinfo f, int pointsize)
將字體的距離返回基線之外。
void Image(VGfloat x, VGfloat y, int w, int h, char * filename)
在(x,y)上放置帶有尺寸(W,h)的JPEG圖像。
void Translate(VGfloat x, VGfloat y)
將坐標系轉換為(x,y)。
void Rotate(VGfloat r)
將坐標系圍繞角度R(度)旋轉。
void Scale(VGfloat x, VGfloat y)
x,y比例。
void Shear(VGfloat x, VGfloat y)
剪切角x,y。
void ClipRect(VGint x, VGint y, VGint w, VGint h)
將圖紙區域限制為指定的矩形,以clipend()結尾
void ClipEnd()
結束剪裁區域
還包括FONT2OPENVG程序,該程序將字體信息轉換為您可以嵌入程序中的C來源。 MakeFile從/usr/share/fonts/trueType/ttf-dejavu/中找到的文件中製作字體代碼。如果您想使用其他字體,請相應地調整MakeFile,或在構建Font2Openvg程序後自行生成字體代碼。
font2openvg採用三個參數:TrueType字體文件,要包括的輸出文件和標識符的前綴。例如,使用dejavu sans字體:
./font2openvg /usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf DejaVuSans.inc DejaVuSans
並在您的程序中包括生成的代碼:
#include "DejaVuSans.inc"
Fontinfo DejaFont
LOADFONT函數從字體數據創建OpenVG路徑:
loadfont(DejaVuSans_glyphPoints,
DejaVuSans_glyphPointIndices,
DejaVuSans_glyphInstructions,
DejaVuSans_glyphInstructionIndices,
DejaVuSans_glyphInstructionCounts,
DejaVuSans_glyphAdvances,
DejaVuSans_characterMap,
DejaVuSans_glyphCount);
UnloadFont函數發布路徑信息:
unloadfont(DejaFont.Glyphs, DejaFont.Count);
請注意,字體文件的位置可能有所不同。 (傑西的當前位置是/usr/share/fonts/trueType/ttf-dejavu)使用fontlib makefile變量來調整此位置。
請注意,您至少需要64個gpu ram:。您還需要Dejavu字體,以及JPEG和Freetype庫。縮進工具也可用於代碼格式。通過:
pi@raspberrypi ~ $ sudo apt-get install libjpeg8-dev indent libfreetype6-dev ttf-dejavu-core
接下來,構建庫並測試:
pi@raspberrypi ~ $ git clone git://github.com/ajstarks/openvg
pi@raspberrypi ~ $ cd openvg
pi@raspberrypi ~/openvg $ make
g++ -I/usr/include/freetype2 fontutil/font2openvg.cpp -o font2openvg -lfreetype
./font2openvg /usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf DejaVuSans.inc DejaVuSans
224 glyphs written
./font2openvg /usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf DejaVuSansMono.inc DejaVuSansMono
224 glyphs written
./font2openvg /usr/share/fonts/truetype/ttf-dejavu/DejaVuSerif.ttf DejaVuSerif.inc DejaVuSerif
224 glyphs written
gcc -O2 -Wall -I/opt/vc/include -I/opt/vc/include/interface/vmcs_host/linux -I/opt/vc/include/interface/vcos/pthreads -c libshapes.c
gcc -O2 -Wall -I/opt/vc/include -I/opt/vc/include/interface/vmcs_host/linux -I/opt/vc/include/interface/vcos/pthreads -c oglinit.c
pi@raspberrypi ~/openvg/client $ cd client
pi@raspberrypi ~/openvg/client $ make test
cc -Wall -I/opt/vc/include -I/opt/vc/include/interface/vcos/pthreads -o shapedemo shapedemo.c ../libshapes.o ../oglinit.o -L/opt/vc/lib -lGLESv2 -ljpeg
./shapedemo demo 5
該程序“ shapedemo”練習了在Libphapes.c中發現的OpenVG上建立的高級API。
./shapedemo # show a reference card
./shapedemo raspi # show a self-portrait
./shapedemo image # show four test images
./shapedemo astro # the sun and the earth, to scale
./shapedemo text # show blocks of text in serif, sans, and mono fonts
./shapedemo rand 10 # show 10 random shapes
./shapedemo rotate 10 a # rotated and faded "a"
./shapedemo test "hello, world" # show a test pattern, with "hello, world" at mid-display in sans, serif, and mono.
./shapedemo fontsize # show a range of font sizes (per <https://speakerdeck.com/u/idangazit/p/better-products-through-typography>)
./shapedemo demo 10 # run through the demo, pausing 10 seconds between each one; contemplate the awesome.
將形狀庫作為一個系統範圍的共享庫安裝
pi@raspberrypi ~/openvg $ make library
pi@raspberrypi ~/openvg $ sudo make install
現在,可以通過包括shapes.h和fontinfo.h並與libshapes.so:so:::
#include <shapes.h>
#include <fontinfo.h>
pi@raspberrypi ~ $ gcc -I/opt/vc/include -I/opt/vc/include/interface/vmcs_host/linux -I/opt/vc/include/interface/vcos/pthreads anysource.c -o anysource -lshapes
pi@raspberrypi ~ $ ./anysource
在OpenVG.Go中找到了圖書館的GO編程語言包裝器。示例客戶端位於目錄中。 API緊隨C API;這是GO中的“ Hello,World”程序:
GO API
package main
import (
"bufio"
"github.com/ajstarks/openvg"
"os"
)
func main() {
width, height := openvg.Init() // OpenGL, etc initialization
w2 := openvg.VGfloat(width / 2)
h2 := openvg.VGfloat(height / 2)
w := openvg.VGfloat(width)
openvg.Start(width, height) // Start the picture
openvg.BackgroundColor("black") // Black background
openvg.FillRGB(44, 77, 232, 1) // Big blue marble
openvg.Circle(w2, 0, w) // The "world"
openvg.FillColor("white") // White text
openvg.TextMid(w2, h2, "hello, world", "serif", width/10) // Greetings
openvg.End() // End the picture
bufio.NewReader(os.Stdin).ReadBytes('n') // Pause until [RETURN]
openvg.Finish() // Graphics cleanup
}
構建包裝器:(確保設置了Gopath)
pi@raspberrypi ~/openvg $ go install .
pi@raspberrypi ~/openvg $ cd go-client/hellovg
pi@raspberrypi ~/openvg/go-client/hellovg $ go build .
pi@raspberrypi ~/openvg/go-client/hellovg $ ./hellovg