إليكم الرسومات المكافئة لـ "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()
Windowsclear يمسح النافذة لتعيين لون الخلفية مسبقًا
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)
تنقل النافذة النافذة إلى الموضع المعطى
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)
قم بإنهاء الصورة ، مما يجعل على الشاشة ، احفظ النقطية إلى الملف المسماة ككلمات RGBA 4 بايت ، مع خطوة من العرض*4 بايت. يحول البرنامج RAW2PNG النقطية "الخام" إلى 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). باستخدام الإزاحة والألوان المحددة في عدد التوقفات
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) مع قطر د.
void CircleOutline(VGfloat x, VGfloat y, VGfloat r)
نسخة محددة
void Ellipse(VGfloat x, VGfloat y, VGfloat w, VGfloat h)
ارسم القطع الناقص المتمركز في (x ، y) مع نصف القطر (w ، ح).
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) ، باستخدام cointize.
void TextMid(VGfloat x, VGfloat y, char* s, Fontinfo f, int pointsize)
ارسم النص (s) srtring (s) في الموقع في الموقع (x ، y) ، باستخدام cointize.
void TextEnd(VGfloat x, VGfloat y, char* s, Fontinfo f, int pointsize)
ارسم النص srtring (S)
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 ، ذ.
void Shear(VGfloat x, VGfloat y)
القص بواسطة الزوايا x ، ذ.
void ClipRect(VGint x, VGint y, VGint w, VGint h)
الحد من رسم منطقة الرسم إلى المستطيل المحدد ، نهاية مع المقياس ()
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);
لاحظ أن موقع ملفات الخط قد يختلف. (الموقع الحالي لـ Jessie IS/usr/share/fonts/truetype/ttf-dejavu) استخدم متغير makefile fontlib لضبط هذا الموقع.
لاحظ أنك ستحتاج إلى 64 mbytes على الأقل من RAM GPU : . ستحتاج أيضًا إلى خطوط 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" واجهة برمجة تطبيقات عالية المستوى مبنية على 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 في رمز C من خلال تضمين الأشكال. 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
تم العثور على غلاف لغة البرمجة GO للمكتبة في OpenVG.GO. عينة من العملاء في الدليل go-client. API يتبع عن كثب واجهة برمجة تطبيقات C ؛ إليكم برنامج "Hello ، World" في GO:
واجهة برمجة تطبيقات GO
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