นี่คือกราฟิกเทียบเท่ากับ "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 ที่มีสีแดง, เขียว, สีน้ำเงิน, ค่าอัลฟ่าตั้งแต่ 0.0 ถึง 1.0 แต่โดยทั่วไปจะระบุสีเป็น RGBA (0-255 สำหรับ RGB, A จาก 0.0 ถึง 1.0)
void WindowClear()
WindowClear ล้างหน้าต่างเป็นสีพื้นหลังที่ตั้งไว้ก่อนหน้านี้
void AreaClear(unsigned int x, unsigned int y, unsigned int w, unsigned int h)
AreaClear ล้างสี่เหลี่ยมที่กำหนดในพิกัดหน้าต่าง
void WindowOpacity(unsigned int a)
WindowOpacity ตั้งค่าความทึบของหน้าต่าง
void WindowPosition(int x, int y)
Windowposition ย้ายหน้าต่างไปยังตำแหน่งที่กำหนด
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)
ล้างหน้าจอเป็นสีพื้นหลังด้วยอัลฟ่า
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)
ตั้งค่าการเติมให้เป็นเรเดียลการไล่ระดับสีที่อยู่ตรงกลางที่ (CX, CY) ด้วยรัศมี R และจุดโฟกัสที่ (FX, RY) โดยใช้ออฟเซ็ตและสีที่ระบุในจำนวน n ของการหยุด
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) ด้วยรัศมี (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)
วาดเส้นโค้ง bezier กำลังสองเริ่มต้นที่ (SX, SY) โดยใช้จุดควบคุมที่ (CX, CY) สิ้นสุดที่ (Ex, EY)
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)
วาดเส้นโค้ง bezier ลูกบาศก์เริ่มต้นที่ (sx, sy) โดยใช้จุดควบคุมที่ (cx, cy) และ (px, py), สิ้นสุดที่ (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)
วาดข้อความ srtring (s) ที่ตำแหน่ง (x, y) โดยใช้คะแนน
void TextMid(VGfloat x, VGfloat y, char* s, Fontinfo f, int pointsize)
วาดข้อความ srtring (s) ที่อยู่ตรงกลางที่ตำแหน่ง (x, y) โดยใช้คะแนน
void TextEnd(VGfloat x, VGfloat y, char* s, Fontinfo f, int pointsize)
วาดข้อความ srtring (s) ที่ด้วยการให้ยืมสอดคล้องกับตำแหน่ง (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)
วางภาพ JPEG ที่มีขนาด (w, h) ที่ (x, y)
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)
จำกัด การวาดพื้นที่วาดภาพไปยังสี่เหลี่ยมผืนผ้าที่ระบุจบลงด้วยคลิปบรรต ()
void ClipEnd()
สิ้นสุดพื้นที่ตัด
รวมถึงโปรแกรม Font2OPENVG ซึ่งเปลี่ยนข้อมูลตัวอักษรเป็นแหล่ง C ที่คุณสามารถฝังในโปรแกรมของคุณ MakeFile สร้างรหัสแบบอักษรจากไฟล์ที่พบใน/usr/share/fonts/truetype/ttf-dejavu/ หากคุณต้องการใช้แบบอักษรอื่น ๆ ให้ปรับ makeFile ตามนั้นหรือสร้างรหัสตัวอักษรด้วยตัวเองเมื่อโปรแกรม Font2Openvg ถูกสร้างขึ้น
Font2openvg ใช้สามอาร์กิวเมนต์: ไฟล์ Font 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);
โปรดทราบว่าตำแหน่งของไฟล์ตัวอักษรอาจแตกต่างกัน (ตำแหน่งปัจจุบันสำหรับ Jessie คือ/usr/share/fonts/truetype/ttf-dejavu) ใช้ตัวแปร fontlib makefile เพื่อปรับตำแหน่งนี้
โปรดทราบว่าคุณจะต้องใช้ GPU RAM อย่างน้อย 64 mbytes: คุณจะต้องใช้ฟอนต์ 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" ออกกำลังกาย API ระดับสูงที่สร้างขึ้นบน OpenVG ที่พบใน libshapes.c
./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
ตอนนี้ห้องสมุด OpenVG Shapes สามารถใช้ในรหัส C ได้โดยรวมถึง shapes.h และ fontinfo.h และเชื่อมโยงกับ libshapes.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
wrapper ภาษาการเขียนโปรแกรม GO สำหรับห้องสมุดพบได้ใน openvg.go กลุ่มตัวอย่างอยู่ในไดเรกทอรี go-client API ติดตาม C API อย่างใกล้ชิด; นี่คือโปรแกรม "Hello, World" ใน GO:
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
}
ในการสร้าง wrapper: (ตรวจสอบให้แน่ใจว่ามีการตั้งค่า 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