ページ

ラベル OpenGL の投稿を表示しています。 すべての投稿を表示
ラベル OpenGL の投稿を表示しています。 すべての投稿を表示

2014年10月7日火曜日

Cent OSでOpenGL 9 - マテリアル -

材質の設定

またまたいつものURL
http://seesaawiki.jp/w/mikk_ni3_92/d/%BA%E0%BC%C1%A4%CE%C0%DF%C4%EA

どうやら材質特性によって反射の仕方が変わるらしい。
「glMaterial()関数」を使用。

例)金属のような質感





















↓ソースコード
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

GLfloat gold_amb[] = { 0.24725, 0.1995, 0.0745, 1.0 };
        GLfloat gold_diff[] = { 0.75164, 0.60648 , 0.22648, 1.0 };
        GLfloat gold_spe[] = { 0.628281, 0.555802,0.366065,1.0 };
        GLfloat gold_shin[] = { 51.2 };

        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, gold_amb);
        glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, gold_diff);
        glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, gold_spe);
        glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, gold_shin);

glBegin(GL_POLYGON);


↓材質のパラメータ
パラメータ名内容初期値
GL_AMBIENT環境光(0.2, 0.2, 0.2, 1)
GL_DIFFUSE拡散光GL_LIGHT=(0.8, 0.8, 0.8, 1)
GL_SPECULAR鏡面光(0,0,0,1)
GL_EMISSION放射輝度(0,0,0,1)
GL_SHININESS鏡面光の指数0。0~128までできる
GL_AMBIENT_AND_DIFFUSE「環境光」、「拡散光」を同時に指定する
GL_COLOR_INDEXES「環境光」、「拡散光」、「鏡面光」のカラー座標

※ GL_FRONT_AND_BACKは面の裏、表両方に適用させる時に指定する。
    GL_FROMT だと表面のみ、GL_BACKだと裏面のみに材質を設定できる。


2014年8月17日日曜日

Cent OSでOpenGL 8 - 光源 -

光源の設定

いつもの参考URL
http://seesaawiki.jp/w/mikk_ni3_92/d/%B8%F7%B8%BB%A4%CE%C0%DF%C4%EA

glLight関数を使用して光源を設定する。

・GL_POSITION
 同次座標系で設定するらしい。
    例) (x:45, y:30, z:90, w:3) => (45/3, 30/3, 90/3, 3/3) => (15, 10, 30, 1)と等しい。
          もしw=0の場合無限遠方となる

・設定の順番
    1. 位置などの数値データを用意
    2. 光源との関連づけ
    3. 有効化

サンプル
#include <GL/gl.h>
#include <GL/glut.h>

const GLfloat lightPos[] = { 3 , 0 , -2 , 0 };
const GLfloat lightCol[] = { 1 , 0 , 0 , 1 };

void disp( void ) {
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 glBegin(GL_POLYGON);
  glNormal3f(3 , 0 , -2);
  glVertex3f(0 , -0.9 , -2);
  glVertex3f(3 , -0.9 , -7);
  glVertex3f(0 , 0.9 , -2);

  glNormal3f(-3 , 0 , -2);
  glVertex3f(0 , -0.9 , -2);
  glVertex3f(-3 , -0.9 , -7);
  glVertex3f(0 , 0.9 , -2);
 glEnd();

 glFlush();
}

int main(int argc , char ** argv) {
 glutInit(&argc , argv);
 glutInitWindowPosition(100 , 50);
 glutInitWindowSize(400 , 300);
 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH);

 glutCreateWindow("Kitty on your lap");
 glutDisplayFunc(disp);

 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 glFrustum(1 , -1 , -1 , 1 , 2 , 10);

 glLightfv(GL_LIGHT0 , GL_POSITION , lightPos);
 glLightfv(GL_LIGHT0 , GL_DIFFUSE , lightCol);
 glEnable(GL_LIGHTING);
 glEnable(GL_LIGHT0);
 glEnable(GL_DEPTH_TEST);

 glutMainLoop();
 return 0;
}


実行結果


















OpenGL の照光モデルは、光を放射環境拡散鏡面、に分解し
各要素を独立した計算によって処理し、最終的に統合するという方法を採用しています
具体的な内部実装は、この場で知る必要はないでしょう
独自の照光モデルを採用したい時などに、ソフトウェアで独自にこうした処理を行います

参考URL
http://wisdom.sakura.ne.jp/system/opengl/gl16.html

2014年7月21日月曜日

Cent OSでOpenGL 7 - 3Dプログラミング -

いよいよ、OpenGLで3Dプログラミングを開始。


以下サイトを参考
http://seesaawiki.jp/w/mikk_ni3_92/d/%B4%F0%CB%DC%CA%D403

・3D描画の流れ

  1. ビューポートの設定
  2. 視体積設定
  3. 視点の設定
これらの設定をウィンドウズ変更時のイベントで行う。

void glutReshapeFunc(void (*func)( int width, int height))
機能
GLUTのウィンドウの大きさが変ったときや、移動したときに呼び出されるコールバック関数を登録する。
この関数を使用しない場合、funcにNULLを設定した場合は、デフォルトの動作として、glViewport(0, 0, width, height)を呼び出す。
引数
funcウィンドウのサイズが変更されたときに呼び出されるコールバック関数。ウィンドウの幅と高さを表す2つのint型の引数をとる。
戻り値
なし

1. ビューポートの設定

glViewport(GLint x, GLint y, GLsizei width, GLsizei height)
x, y ・・・左隅(左下)の座標
width・・・幅
height・・・高さ


2. 視体積(視野)の設定

どの範囲まで視野に入れるか?

glMatrixMode(GL_PROJECTION)
まずは視体積の設定を有効にする。

次に投影方法の設定
・透視投影
・並行投影


透視投影には「glFrustum」関数と「glPerspective」関数がある。
glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble znear, GLdoble zfar)
「left」、「right」:「左」、「右」の垂直座標を指定
「bottom」、「top」:「下」、「上」の水平座標を指定
「znear」、「zfar」:「手前」、「奥行き」を指定

gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar)
「fovy」:「y座標」、すなわち「縦方向の視野角」を指定
「sapect」:「視野角」に対する、「比率」で「水平方向の視野角」を決定
「zNear」、「zFar」:「手前」、「奥行き」を指定


並行投影
視点に影響されず、常に正しいサイズで表示される。2次元で画像表示等に使用される。
glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble zbear, GLdoble zfar)
「引数」については、「glFrustum()関数」と同じ

3. 視点の設定

視点の位置設定には「gluLookAt」関数を使う
gluLookAt(GLdouble ex,GLdouble ey,GLdouble ez,
     GLdouble cx,GLdouble cy,GLdouble cz,
     GLdouble ux,GLdouble uy,GLdouble uz,)


「(ex,ey,ez)」:「視点の位置」
「(cx,cy,cz)」:「見たいもの位置」
「(ux,uy,uz)」:画像のどこが「上」なのかを指定(ベクトル)

void reshape(int w, int h)
{
  glViewport(0, 0, w, h);

  glMatrixMode(GL_PROJECTION); // モードの切り替え
  glLoadIdentity();
  gluPerspective(45.0, (double)w / (double)h, 0.1, 100.0);

  glMatrixMode(GL_MODELVIEW); // モードを元に戻す
  glLoadIdentity();
  gluLookAt(0.5, 1.5, 2.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

以下サイトのサンプルコードを実行

コンパイルgcc -o sample sample.c -lglut -lGLU -lGL -lm



2014年7月13日日曜日

Cent OSでOpenGL 6 - 線と矩形の描画 -

線をひく&矩形描画


今まで、GLUTを使用しない場合いかに大変かを見てきたので、
今回からはOpenGLの内容を見て行く為GLUTを使用。


#include <GL/glut.h>

void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
  glColor3d(1.0, 0.0, 0.0); /* 赤色の線を指定 */

  glBegin(GL_LINES);
  glVertex2d(-0.9, -0.9);
  glVertex2d(0.9, 0.6);
  glEnd();

  glFlush();
}

void init(void)
{
  glClearColor(1.0, 1.0, 1.0, 1.0);
}

int main(int argc, char *argv[])
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGBA);
  glutCreateWindow(argv[0]);
  glutDisplayFunc(display);
  init();
  glutMainLoop();
  return 0;
}

実行結果



今回は分かり易い様に、背景を白色に設定。
また線の色をglColor3d関数で赤に設定。

次はglBegin関数に「GL_LINE_LOOP」を指定。
void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
  glColor3d(1.0, 0.0, 0.0); /* 赤色の線を指定 */

  glBegin(GL_LINE_LOOP);
  glVertex2d(-0.9, -0.9);
  glVertex2d(0.9, -0.9);
  glVertex2d(0.9, 0.9);
  glVertex2d(-0.9, 0.9);
  glEnd();

  glFlush();
}

実行結果



四角形を塗りつぶす為にglBeginに「GL_POLYGON」を指定
void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
  glColor3d(1.0, 0.0, 0.0); /* 赤色の線を指定 */

  glBegin(GL_POLYGON);
  glVertex2d(-0.9, -0.9);
  glVertex2d(0.9, -0.9);
  glVertex2d(0.9, 0.9);
  glVertex2d(-0.9, 0.9);
  glEnd();

  glFlush();
}

実行結果


2014年6月21日土曜日

Cent OSでOpenGL 5 - 描画処理 -

今回でやっとOpenGLのメイン部分を解読。
全体像は以下。

void DrawAQuad() {
 glClearColor(1.0, 1.0, 1.0, 1.0);
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 glOrtho(-1., 1., -1., 1., 1., 20.);

 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
 gluLookAt(0., 0., 10., 0., 0., 0., 0., 1., 0.);

 glBegin(GL_QUADS);
  glColor3f(1., 0., 0.); glVertex3f(-.75, -.75, 0.);
  glColor3f(0., 1., 0.); glVertex3f( .75, -.75, 0.);
  glColor3f(0., 0., 1.); glVertex3f( .75.75, 0.);
  glColor3f(1., 1., 0.); glVertex3f(-.75.75, 0.);
 glEnd();
}

上から順に・・・
void glClearColor(
 GLclampf red , GLclampf green ,
 GLclampf blue , GLclampf alpha
);
バッファを初期化する色情報を設定する。
float型で0.0〜1.0で指定する。
void glClear(GLbitfield mask);
実際のバッファを初期化。
以下定数の組み合わせで初期化対称を設定。
定数解説
GL_COLOR_BUFFER_BITカラー バッファ
GL_DEPTH_BUFFER_BITデプス バッファ
GL_ACCUM_BUFFER_BITアキュムレーション バッファ
GL_STENCIL_BUFFER_BITステンシル バッファ
※ステンシルバッファ
 ・・・ステンシルは型板という意味。塗料を塗る時に型板で塗らない箇所を作る
    ように、OpenGLでも点を描画するかしないかのマスクを設定する
※アキュムレーションバッファ
void glMatrixMode(GLenum mode);
OpenGLには3つの変換行列があり、操作対称の行列を指定
定数解説
GL_MODEVIEWモデルビュー変換行列
GL_PROJECTION投影変換行列
GL_TEXTUREテクスチャ行列
void glLoadIdentity(void);
行列の初期化。
void glOrtho(GLdouble left, GLdouble right, GLdouble bottom
             GLdouble top, GLdouble near, GLdouble far);
現在の行列と正射影行列の乗算を行う。
void gluLookAt(///);
以下サイト参照。
void glBegin(GLenum mode);
頂点データの指定開始。モードは以下定数を指定。
定数解説
GL_POINTS各頂点を単独の点として扱う 頂点 n は、点 n を意味し n この点が描画される
GL_LINES2つの頂点をペアとし、それぞれのペアを独立した線分として扱う
GL_LINE_STRIP最初の頂点から最後の頂点まで、線分を連結して描画する
GL_LINE_LOOPすべての頂点を線分で連結する
GL_TRIANGLES3つの頂点をペアとし、それぞれ独立した三角形として扱う
GL_TRIANGLE_STRIP連結した三角形のグループを描画する
GL_TRIANGLE_FAN最初の頂点を軸に、連結した三角形のグループを描画する
GL_QUADS4つの頂点をペアとし、それぞれ独立した四角形として扱う
GL_QUAD_STRIP連結した四角形のグループを描画する
GL_POLYGON単独の凸ポリゴンを描画する
参考URL

2014年6月12日木曜日

Cent OSでOpenGL 4 - XLib調査2 -

前回の続き・・

 cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);
これは一体何をしているのか?

概要:カラーマップと色構造体の生成
引数:Display *         ・・・    Xサーバの接続を指定
            Window           ・・・    カラーマップを作るスクリーンに属するウィンドウ
            Visual *           ・・・    スクリーンがサポートするVisual 
            int                    ・・・     割当られるカラーマップのエントリー

XSetWindowAttributes    swa;

 swa.colormap = cmap;
 swa.event_mask = ExposureMask | KeyPressMask;

 win = XCreateWindow(dpy, root, 0, 0, 600, 600, 0, vi->depth, InputOutput, vi->visual, CWColormap | CWEventMask, &swa);
XCreateWindow

概要:ウィンドウの生成

 XMapWindow(dpy, win);
 XStoreName(dpy, win, "VERY SIMPLE APPLICATION”);

XMapWindow
   Xサーバーとウィンドウとのマッピング

XStoreName
  Window名の設定

ここからOpenGLの初期化
 glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
 glXMakeCurrent(dpy, win, glc);


 glEnable(GL_DEPTH_TEST); 

glXCreateContext

概要:GLXレンダリングコンテキストの作成
引数:Display* Xサーバー
            XVisualInfo* コンテキストが利用可能なフレームバッファリソースの定義
            GLXContext 共有するコンテキスト
            Bool レンダリングをグラフィックシステムと直接接続で行う(GL_TRUE)か
                      サーバー経由で行うか(GL_FALSE)

glXMakeCurrent

概要:GLXコンテキストをウィンドウ又はGLXピックスマップ(Bitmap)に割り当てる
引数:Display* Xサーバー
            GLXDrawable XウィンドウIDかGLXピックスマップID
            GLXContext コンテキスト

glEnable
概要:有効な描画方法を設定
参考:http://www.westernvillage.co.jp/openglprogram.htm

 while(1) {
        XNextEvent(dpy, &xev);

        if(xev.type == Expose) {
                XGetWindowAttributes(dpy, win, &gwa);
                glViewport(0, 0, gwa.width, gwa.height);
                DrawAQuad();
                glXSwapBuffers(dpy, win);
        }

        else if(xev.type == KeyPress) {
                glXMakeCurrent(dpy, None, NULL);
                glXDestroyContext(dpy, glc);
                XDestroyWindow(dpy, win);
                XCloseDisplay(dpy);
                exit(0);
        }
    } /* this closes while(1) { */

} /* this is the } which closes int main(int argc, char *argv[]) { */

XNextEvent
概要:イベントを型によって選択
参考:http://xjman.dsl.gr.jp/man/man3/XNextEvent.3x.html

XGetWindowAttributes
概要:ウィンドウの現在の属性を取得
参考:http://xjman.dsl.gr.jp/man/man3/XGetWindowAttributes.3x.html

glViewport
概要:ビューポートの設定
参考:http://wisdom.sakura.ne.jp/system/opengl/gl13.html

glXSwapBuffers
概要:ブッファの入れ替え
参考:http://manpages.ubuntu.com/manpages/gutsy/ja/man3/glXSwapBuffers.3x.html

2014年6月2日月曜日

Cent OSでOpenGL 3 - XLib調査 -

前回のサンプルプログラムを紐解いてみる

まずはメイン関数で最初に呼び出される以下関数。
dpy = XOpenDisplay(NULL);

名前からしてXLibの関数だな。
詳細:Xサーバーへの接続。
引数:char *display_name
            ハードウェアのディスプレイ名を指定。
            NULLの場合は環境変数DISPLAYの値を使用。
戻り値:Display構造体(Xlib.hで定義)
typedef struct
#ifdef XLIB_ILLEGAL_ACCESS
_XDisplay
#endif
{
        XExtData *ext_data;     /* hook for extension to hang data */
        struct _XPrivate *private1;
        int fd;                 /* Network socket. */
        int private2;
        int proto_major_version;/* major version of server's X protocol */
        int proto_minor_version;/* minor version of servers X protocol */
        char *vendor;           /* vendor of the server hardware */
        XID private3;
        XID private4;
        XID private5;
        int private6;
        XID (*resource_alloc)(  /* allocator function */
                struct _XDisplay*
        );
        int byte_order;         /* screen byte order, LSBFirst, MSBFirst */
        int bitmap_unit;        /* padding and data requirements */
        int bitmap_pad;         /* padding requirements on bitmaps */
        int bitmap_bit_order;   /* LeastSignificant or MostSignificant */
        int nformats;           /* number of pixmap formats in list */
        ScreenFormat *pixmap_format;    /* pixmap format list */
        int private8;
        int release;            /* release of the server */
        struct _XPrivate *private9, *private10;
        int qlen;               /* Length of input event queue */
        unsigned long last_request_read; /* seq number of last event read */
        unsigned long request;  /* sequence number of last request. */
        XPointer private11;
        XPointer private12;
        XPointer private13;
        XPointer private14;
        unsigned max_request_size; /* maximum number 32 bit words in request*/
        struct _XrmHashBucketRec *db;
        int (*private15)(
                struct _XDisplay*
                );
        char *display_name;     /* "host:display" string used on this connect*/
        int default_screen;     /* default screen for operations */
        int nscreens;           /* number of screens on this server*/
        Screen *screens;        /* pointer to list of screens */
        unsigned long motion_buffer;    /* size of motion buffer */
        unsigned long private16;
        int min_keycode;        /* minimum defined keycode */
        int max_keycode;        /* maximum defined keycode */
        XPointer private17;
        XPointer private18;
        int private19;
        char *xdefaults;        /* contents of defaults from server */
        /* there is more to this structure, but it is private to Xlib */
}
#ifdef XLIB_ILLEGAL_ACCESS
Display,
#endif
*_XPrivDisplay;



次にXOpenDisplayで取得したDisplay構造体を使って何やら行っている模様。
 root = DefaultRootWindow(dpy);

Xlib.hに以下のマクロとして定義されていました。
#define DefaultRootWindow(dpy)  (ScreenOfDisplay(dpy,DefaultScreen(dpy))->root)
#define ScreenOfDisplay(dpy, scr)(&((_XPrivDisplay)dpy)->screens[scr])
#define DefaultScreen(dpy)      (((_XPrivDisplay)dpy)->default_screen)

Screen構造体定義
typedef struct {
        XExtData *ext_data;     /* hook for extension to hang data */
        struct _XDisplay *display;/* back pointer to display structure */
        Window root;            /* Root window id. */
        int width, height;      /* width and height of screen */
        int mwidth, mheight;    /* width and height of  in millimeters */
        int ndepths;            /* number of depths possible */
        Depth *depths;          /* list of allowable depths on the screen */
        int root_depth;         /* bits per pixel */
        Visual *root_visual;    /* root visual */
        GC default_gc;          /* GC for the root root visual */
        Colormap cmap;          /* default color map */
        unsigned long white_pixel;
        unsigned long black_pixel;      /* White and Black pixel values */
        int max_maps, min_maps; /* max and min color maps */
        int backing_store;      /* Never, WhenMapped, Always */
        Bool save_unders;
        long root_input_mask;   /* initial root input mask */
} Screen;


XサーバーのルートバックグラウンドのWindow(X.h内でXID(unsigned long)として定義されている)を返している。

次はGLっぽい。
 vi = glXChooseVisual(dpy, 0, att);
詳細:グラフィックに必要な能力を要求する。GLサーバーはこれによって必要な
             Visualを選定する。

以下が必要な要件。
GLint                   att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
結構指定できるものが多数あるので詳しくは以下サイトを参照
http://lmb.informatik.uni-freiburg.de/people/reisert/opengl/doc/glXChooseVisual.html

typedef struct {
  Visual *visual;
  VisualID visualid;
  int screen;
  int depth;
#if defined(__cplusplus) || defined(c_plusplus)
  int c_class;                                  /* C++ */
#else
  int class;
#endif
  unsigned long red_mask;
  unsigned long green_mask;
  unsigned long blue_mask;
  int colormap_size;
  int bits_per_rgb;
} XVisualInfo;


2014年6月1日日曜日

Cent OSでOpenGL 2 - GLX拡張 -

前回はとりあえずウィンドウを表示してみたが、今回は真面目にOpenGLについて勉強。


まずは公式サイトを見てみる。
OpenGLの公式サイトはこちら

公式サイトのGetting Started 〜を見てみる。
主要なOSにはOpenGLが既に入っているが、ドライバーは最新に〜という内容が書かれてあるっぽい。
以下それぞれのプラットフォーム毎の説明

・Linux
Linuxのグラフィックスはほぼ "X Window”だ。LinuxでOpenGLをサポートするという事は
X WindowのGLX拡張を使用する事になる。
Direct Rendering Infrastructure(DRI)は簡単にハードウェアアクセラレーションを使える
ドライバーのフレームワーク。
DRIはXFree86の4.0からのサポートされているが、現在はXFree86からXorg
主流となっている。
コンパイル時にはOpenGLのライブラリをリンクする為に「-lGL」を指定する必要がある。

・Windows
コンパイルする際は「OpenGL32.lib」をリンクする必要がある。

実際のプログラミング

1. 初期化
    各プラットフォールで異なった書き方で初期化する必要がある。
 初期化には2つのフェースがある。
    1)  OpenGL Context の作成
    2)  OpenGLを使用する為に必要な機能をロード

2. LinuxでのOpenGL Contextの作成


GLXContext glXCreateContext(Display *  dpy,
XVisualInfo *  vis,
GLXContext  shareList,
Bool  direct);

上記関数を使用しContextを作成。

以下簡単なウィンドウ表示サンプル
// -- Written in C -- //

#include<stdio.h>
#include<stdlib.h>
#include<X11/X.h>
#include<X11/Xlib.h>
#include<GL/gl.h>
#include<GL/glx.h>
#include<GL/glu.h>

Display                 *dpy;
Window                  root;
GLint                   att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
XVisualInfo             *vi;
Colormap                cmap;
XSetWindowAttributes    swa;
Window                  win;
GLXContext              glc;
XWindowAttributes       gwa;
XEvent                  xev;

void DrawAQuad() {
 glClearColor(1.0, 1.0, 1.0, 1.0);
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 glOrtho(-1., 1., -1., 1., 1., 20.);

 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
 gluLookAt(0., 0., 10., 0., 0., 0., 0., 1., 0.);

 glBegin(GL_QUADS);
  glColor3f(1., 0., 0.); glVertex3f(-.75, -.75, 0.);
  glColor3f(0., 1., 0.); glVertex3f( .75, -.75, 0.);
  glColor3f(0., 0., 1.); glVertex3f( .75,  .75, 0.);
  glColor3f(1., 1., 0.); glVertex3f(-.75,  .75, 0.);
 glEnd();
} 
 
int main(int argc, char *argv[]) {

 dpy = XOpenDisplay(NULL);
 
 if(dpy == NULL) {
        printf("\n\tcannot connect to X server\n\n");
        exit(0);
 }
        
 root = DefaultRootWindow(dpy);

 vi = glXChooseVisual(dpy, 0, att);

 if(vi == NULL) {
        printf("\n\tno appropriate visual found\n\n");
        exit(0);
 } 
 else {
        printf("\n\tvisual %p selected\n", (void *)vi->visualid); /* %p creates hexadecimal output like in glxinfo */
 }


 cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);

 swa.colormap = cmap;
 swa.event_mask = ExposureMask | KeyPressMask;
 
 win = XCreateWindow(dpy, root, 0, 0, 600, 600, 0, vi->depth, InputOutput, vi->visual, CWColormap | CWEventMask, &swa);

 XMapWindow(dpy, win);
 XStoreName(dpy, win, "VERY SIMPLE APPLICATION");
 
 glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
 glXMakeCurrent(dpy, win, glc);
 
 glEnable(GL_DEPTH_TEST); 
 
 while(1) {
        XNextEvent(dpy, &xev);
        
        if(xev.type == Expose) {
                XGetWindowAttributes(dpy, win, &gwa);
                glViewport(0, 0, gwa.width, gwa.height);
                DrawAQuad(); 
                glXSwapBuffers(dpy, win);
        }
                
        else if(xev.type == KeyPress) {
                glXMakeCurrent(dpy, None, NULL);
                glXDestroyContext(dpy, glc);
                XDestroyWindow(dpy, win);
                XCloseDisplay(dpy);
                exit(0);
        }
    } /* this closes while(1) { */
} /* this is the } which closes int main(int argc, char *argv[]) { */

コンパイル
gcc -o test test.c -lX11 -lGL -lGLU
実行すると以下のグラフィックが表示される。

2014年5月31日土曜日

Cent OSでOpenGL 1 - GLUTを触る -

簡単にOpenGLが使えるGLUTを使用して開発&勉強。

以下サイトを参考
http://www.wakayama-u.ac.jp/~tokoi/opengl/libglut.html#3

とりあえず今日はインストールと簡単なウィンドウ表示くらいで。

・インストール
[root@localhost include]# yum install freeglut
[root@localhost include]# yum install freeglut-devel


サイトに書かれてあるウィンドウ実行するサンプルアプリを実行
・ソース
#include <GL/glut.h>

void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
  glFlush();
}

void init(void)
{
  glClearColor(0.0, 0.0, 1.0, 1.0);
}

int main(int argc, char *argv[])
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGBA);
  glutCreateWindow(argv[0]);
  glutDisplayFunc(display);
  init();
  glutMainLoop();
  return 0;
}
~    

・実行結果