这是“ 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