From adfa666208bbf720e0dd33602661384b146e5268 Mon Sep 17 00:00:00 2001 From: Wolfgang Draxinger Date: Sun, 19 Feb 2012 17:46:50 +0100 Subject: FBO example added --- samples/OpenGL/x11argb_opengl/x11argb_opengl.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'samples/OpenGL/x11argb_opengl') diff --git a/samples/OpenGL/x11argb_opengl/x11argb_opengl.c b/samples/OpenGL/x11argb_opengl/x11argb_opengl.c index 8eaddf8..7a073fb 100644 --- a/samples/OpenGL/x11argb_opengl/x11argb_opengl.c +++ b/samples/OpenGL/x11argb_opengl/x11argb_opengl.c @@ -423,8 +423,10 @@ static void redrawTheWindow() glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); +#if 0 glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); +#endif glLightfv(GL_LIGHT0, GL_POSITION, light0_dir); glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_color); -- cgit v1.3.1 From 2442c23c78fbe205cbebbe2f26d8a20eecbb1347 Mon Sep 17 00:00:00 2001 From: Wolfgang Draxinger Date: Sun, 12 May 2013 02:05:08 +0200 Subject: commit of new sample 'frustum' --- samples/OpenGL/frustum/Makefile | 5 + samples/OpenGL/frustum/frustum.c | 326 +++++++++++++++++++++ samples/OpenGL/strand_illumination/.gitignore | 1 + samples/OpenGL/strand_illumination/CMakeLists.txt | 0 samples/OpenGL/strand_illumination/strand.fs.glsl | 0 samples/OpenGL/strand_illumination/strand.vs.glsl | 0 .../strand_illumination/strand_illumination.c | 56 ++++ samples/OpenGL/x11argb_opengl/x11argb_opengl.c | 2 + samples/OpenGL/x11argb_opengl_glsl/Makefile | 2 +- 9 files changed, 391 insertions(+), 1 deletion(-) create mode 100644 samples/OpenGL/frustum/Makefile create mode 100644 samples/OpenGL/frustum/frustum.c create mode 100644 samples/OpenGL/strand_illumination/.gitignore create mode 100644 samples/OpenGL/strand_illumination/CMakeLists.txt create mode 100644 samples/OpenGL/strand_illumination/strand.fs.glsl create mode 100644 samples/OpenGL/strand_illumination/strand.vs.glsl create mode 100644 samples/OpenGL/strand_illumination/strand_illumination.c (limited to 'samples/OpenGL/x11argb_opengl') diff --git a/samples/OpenGL/frustum/Makefile b/samples/OpenGL/frustum/Makefile new file mode 100644 index 0000000..24968ec --- /dev/null +++ b/samples/OpenGL/frustum/Makefile @@ -0,0 +1,5 @@ +CFLAGS = -std=c99 + +frustum: frustum.o + $(CC) -o frustum frustum.o -lGL -lGLU -lglut -lm + diff --git a/samples/OpenGL/frustum/frustum.c b/samples/OpenGL/frustum/frustum.c new file mode 100644 index 0000000..dd9c42c --- /dev/null +++ b/samples/OpenGL/frustum/frustum.c @@ -0,0 +1,326 @@ +#include +#include +#include + +#include + +#include +#include +#include +#include + +/* == basic Q^3 vector math functions == */ + +void crossproduct( + double ax, double ay, double az, + double bx, double by, double bz, + double *rx, double *ry, double *rz ) +{ + *rx = ay*bz - az*by; + *ry = az*bx - ax*bz; + *rz = ax*by - ay*bx; +} + +void crossproduct_v( + double const * const a, + double const * const b, + double * const c ) +{ + crossproduct( + a[0], a[1], a[2], + b[0], b[1], b[2], + c, c+1, c+2 ); +} + +double scalarproduct( + double ax, double ay, double az, + double bx, double by, double bz ) +{ + return ax*bx + ay*by + az*bz; +} + +double scalarproduct_v( + double const * const a, + double const * const b ) +{ + return scalarproduct( + a[0], a[1], a[2], + b[0], b[1], b[2] ); +} + +double length( + double ax, double ay, double az ) +{ + return sqrt( + scalarproduct( + ax, ay, az, + ax, ay, az ) ); +} + +double length_v( double const * const a ) +{ + return sqrt( scalarproduct_v(a, a) ); +} + +double normalize( + double *x, double *y, double *z) +{ + double const k = 1./length(*x, *y, *z); + + *x *= k; + *y *= k; + *z *= k; +} + +double normalize_v( double *a ) +{ + double const k = 1./length_v(a); + a[0] *= k; + a[1] *= k; + a[2] *= k; +} + +/* == annotation drawing functions == */ + +void draw_strokestring(void *font, float const size, char const *string) +{ + glPushMatrix(); + float const scale = size * 0.01; /* GLUT character base size is 100 units */ + glScalef(scale, scale, scale); + + char const *c = string; + for(; c && *c; c++) { + glutStrokeCharacter(font, *c); + } + glPopMatrix(); +} + +void draw_arrow( + float ax, float ay, float az, + float bx, float by, float bz, + float ah, float bh, + char const * const annotation, + float annot_size ) +{ + int i; + + GLdouble mv[16]; + glGetDoublev(GL_MODELVIEW_MATRIX, mv); + + /* We're assuming the modelview RS part is (isotropically scaled) + * orthonormal, so the inverse is the transpose. + * The local view direction vector is the 3rd column of the matrix; + * assuming the view direction to be the normal on the arrows tangent + * space taking the cross product of this with the arrow direction + * yields the binormal to be used as the orthonormal base to the + * arrow direction to be used for drawing the arrowheads */ + + double d[3] = { + bx - ax, + by - ay, + bz - az + }; + normalize_v(d); + + double n[3] = { mv[2], mv[6], mv[10] }; + { + double const s = scalarproduct_v(d,n); + for(int i = 0; i < 3; i++) + n[i] -= d[i]*s; + } + normalize_v(n); + + double b[3]; + crossproduct_v(n, d, b); + + GLfloat const pos[][3] = { + {ax, ay, az}, + {bx, by, bz}, + { ax + (0.866*d[0] + 0.5*b[0])*ah, + ay + (0.866*d[1] + 0.5*b[1])*ah, + az + (0.866*d[2] + 0.5*b[2])*ah }, + { ax + (0.866*d[0] - 0.5*b[0])*ah, + ay + (0.866*d[1] - 0.5*b[1])*ah, + az + (0.866*d[2] - 0.5*b[2])*ah }, + { bx + (-0.866*d[0] + 0.5*b[0])*bh, + by + (-0.866*d[1] + 0.5*b[1])*bh, + bz + (-0.866*d[2] + 0.5*b[2])*bh }, + { bx + (-0.866*d[0] - 0.5*b[0])*bh, + by + (-0.866*d[1] - 0.5*b[1])*bh, + bz + (-0.866*d[2] - 0.5*b[2])*bh } + }; + GLushort const idx[][2] = { + {0, 1}, + {0, 2}, {0, 3}, + {1, 4}, {1, 5} + }; + glDisableClientState(GL_COLOR_ARRAY); + glDisableClientState(GL_NORMAL_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(3, GL_FLOAT, 0, pos); + + glDrawElements(GL_LINES, 2*5, GL_UNSIGNED_SHORT, idx); + glDisableClientState(GL_VERTEX_ARRAY); + + if(annotation) { + float w = 0; + for(char const *c = annotation; *c; c++) + w += glutStrokeWidth(GLUT_STROKE_ROMAN, *c); + w *= annot_size / 100.; + + float tx = (ax + bx - w*d[0])/2.; + float ty = (ay + by - w*d[1])/2.; + float tz = (az + bz - w*d[2])/2.; + + GLdouble r[16] = { + d[0], d[1], d[2], 0, + b[0], b[1], b[2], 0, + n[0], n[1], n[2], 0, + 0, 0, 0, 1 + }; + glPushMatrix(); + glTranslatef(tx, ty, tz); + glMultMatrixd(r); + glTranslatef(0, annot_size*0.1, 0); + draw_strokestring(GLUT_STROKE_ROMAN, annot_size, annotation); + glPopMatrix(); + } +} + +void draw_frustum( + float l, float r, float b, float t, + float n, float f ) +{ + GLfloat const kf = f/n; + GLfloat const pos[][3] = { + {0,0,0}, + {l, b, -n}, + {r, b, -n}, + {r, t, -n}, + {l, t, -n}, + {kf*l, kf*b, -f}, + {kf*r, kf*b, -f}, + {kf*r, kf*t, -f}, + {kf*l, kf*t, -f} + }; + GLushort const idx_tip[][2] = { + {0, 1}, + {0, 2}, + {0, 3}, + {0, 4} + }; + GLushort const idx_vol[][2] = { + {1, 5}, {2, 6}, {3, 7}, {4, 8}, + {1, 2}, {2, 3}, {3, 4}, {4, 1}, + {5, 6}, {6, 7}, {7, 8}, {8, 5} + }; + + glDisableClientState(GL_COLOR_ARRAY); + glDisableClientState(GL_NORMAL_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(3, GL_FLOAT, 0, pos); + + glLineWidth(1); + glLineStipple(2, 0xf3cf); + glEnable(GL_LINE_STIPPLE); + glDrawElements(GL_LINES, 2*4, GL_UNSIGNED_SHORT, idx_tip); + + glLineWidth(2); + glLineStipple(1, 0xffff); + glDisable(GL_LINE_STIPPLE); + glDrawElements(GL_LINES, 2*4*3, GL_UNSIGNED_SHORT, idx_vol); + + glLineWidth(1); + glDisableClientState(GL_VERTEX_ARRAY); +} + +/* == scene drawing code == */ + +void display_observer(void) +{ + static float alpha = 0; + + int const win_width = glutGet(GLUT_WINDOW_WIDTH); + int const win_height = glutGet(GLUT_WINDOW_HEIGHT); + float const win_aspect = (float)win_width / (float)win_height; + + glViewport(0, 0, win_width, win_height); + glClearColor(1., 1., 1., 1.); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); +#ifdef USE_ORTHO + glOrtho(-10*win_aspect, 10*win_aspect, -10, 10, 0, 100); +#else + gluPerspective(35, win_aspect, 1, 50); +#endif + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + if(1) { + glTranslatef(0, 0, -5); + glRotatef(30, 1, 0, 0); + glRotatef(alpha, 0, 1, 0); + glTranslatef(0, 0, 2.5); + } else { + gluLookAt(3, 1, -5, 0, 0, -2.5, 0, 1, 0); + } + + float const l = -0.5, + r = 0.5, + b = -0.5, + t = 0.5, + n = 1, + f = 4; + + glEnable(GL_MULTISAMPLE); + + glColor3f(0.,0.,0.); + draw_frustum(l, r, b, t, n, f); + + glLineWidth(1); + draw_arrow(0, 0, 0, 0, 0, -n, 0.1, 0.1, "near", 0.075); + + draw_arrow(l, 0, -n, 0, 0, -n, 0.1, 0, "left", 0.075); + draw_arrow(0, 0, -n, r, 0, -n, 0, 0.1, "right", 0.075); + draw_arrow(0, b, -n, 0, 0, -n, 0.1, 0, "bottom", 0.075); + draw_arrow(0, 0, -n, 0, t, -n, 0, 0.1, "top", 0.075); + + glutSwapBuffers(); + + alpha = fmodf(alpha + 0.1, 360); + glutPostRedisplay(); +} + +void display_frustum_view(void) +{ + int const win_width = glutGet(GLUT_WINDOW_WIDTH); + int const win_height = glutGet(GLUT_WINDOW_HEIGHT); + float const win_aspect = (float)win_width / (float)win_height; + + glViewport(0, 0, win_width, win_height); + glClearColor(0.3, 0.3, 0.6, 1.); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glutSwapBuffers(); +} + +int main(int argc, char *argv[]) +{ + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE); + + glutCreateWindow("Observer"); + glutDisplayFunc(display_observer); + + glutCreateWindow("Frustum View"); + glutDisplayFunc(display_frustum_view); + + glutMainLoop(); + return 0; +} diff --git a/samples/OpenGL/strand_illumination/.gitignore b/samples/OpenGL/strand_illumination/.gitignore new file mode 100644 index 0000000..a007fea --- /dev/null +++ b/samples/OpenGL/strand_illumination/.gitignore @@ -0,0 +1 @@ +build/* diff --git a/samples/OpenGL/strand_illumination/CMakeLists.txt b/samples/OpenGL/strand_illumination/CMakeLists.txt new file mode 100644 index 0000000..e69de29 diff --git a/samples/OpenGL/strand_illumination/strand.fs.glsl b/samples/OpenGL/strand_illumination/strand.fs.glsl new file mode 100644 index 0000000..e69de29 diff --git a/samples/OpenGL/strand_illumination/strand.vs.glsl b/samples/OpenGL/strand_illumination/strand.vs.glsl new file mode 100644 index 0000000..e69de29 diff --git a/samples/OpenGL/strand_illumination/strand_illumination.c b/samples/OpenGL/strand_illumination/strand_illumination.c new file mode 100644 index 0000000..08ca7fa --- /dev/null +++ b/samples/OpenGL/strand_illumination/strand_illumination.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include + +typedef enum { + si_NoError = 0, + si_ResourceNotFound = 1, +} si_Error_t; + +struct { + GLuint prog; + GLuint vs; + GLuint fs; + + GLuint a_position; + GLuint a_direction; + + GLuint u_mv; + GLuint u_normal: + GLuint u_proj; + + GLuint u_lightpos; +} strandshader; + +si_Error_t loadStrandShader(void) +{ +} + +si_Error_t loadGLresources(void) +{ + loadStrandShader(); +} + +void display(void) +{ +} + +int main(int argc, char argv[]) +{ + si_Error_t err = si_NoError; + + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE); + glutCreateWindow("Illuminated Strands"); + glutDisplayFunc(display); + + if( (err = loadGLResources()) != si_NoError ) { + return -err; + } + + glutMainLoop(); + + return 0; +} + diff --git a/samples/OpenGL/x11argb_opengl/x11argb_opengl.c b/samples/OpenGL/x11argb_opengl/x11argb_opengl.c index 7a073fb..1c1e2e0 100644 --- a/samples/OpenGL/x11argb_opengl/x11argb_opengl.c +++ b/samples/OpenGL/x11argb_opengl/x11argb_opengl.c @@ -470,7 +470,9 @@ int main(int argc, char *argv[]) createTheRenderContext(); while (updateTheMessageQueue()) { + #if 0 redrawTheWindow(); + #endif } return 0; diff --git a/samples/OpenGL/x11argb_opengl_glsl/Makefile b/samples/OpenGL/x11argb_opengl_glsl/Makefile index 6c234cb..b59feea 100644 --- a/samples/OpenGL/x11argb_opengl_glsl/Makefile +++ b/samples/OpenGL/x11argb_opengl_glsl/Makefile @@ -1,3 +1,3 @@ x11argb_opengl_glsl: x11argb_opengl_glsl.c Makefile - $(CC) -std=c99 -g3 -o x11argb_opengl_glsl x11argb_opengl_glsl.c -lX11 -lXrender -lGLEW -lm + clang -std=c99 -g3 -o x11argb_opengl_glsl x11argb_opengl_glsl.c -lX11 -lXrender -lGLEW -lm -- cgit v1.3.1 From 987f70c8e9b1bab87647d4ed20df79900d6c0c9e Mon Sep 17 00:00:00 2001 From: Wolfgang Draxinger Date: Sat, 14 Sep 2013 15:59:51 +0200 Subject: X11 clobberable window added --- samples/OpenGL/x11argb_opengl/x11argb_opengl.c | 10 ++ .../x11argb_opengl_glsl/x11argb_opengl_glsl.c | 14 ++ samples/X11/clobberme/Makefile | 3 + samples/X11/clobberme/clobberme.c | 171 +++++++++++++++++++++ 4 files changed, 198 insertions(+) create mode 100644 samples/X11/clobberme/Makefile create mode 100644 samples/X11/clobberme/clobberme.c (limited to 'samples/OpenGL/x11argb_opengl') diff --git a/samples/OpenGL/x11argb_opengl/x11argb_opengl.c b/samples/OpenGL/x11argb_opengl/x11argb_opengl.c index 1c1e2e0..723c6ae 100644 --- a/samples/OpenGL/x11argb_opengl/x11argb_opengl.c +++ b/samples/OpenGL/x11argb_opengl/x11argb_opengl.c @@ -23,12 +23,16 @@ \_____/ FTB. ------------------------------------------------------------------------*/ +#define _GNU_SOURCE #include #include #include #include +#include +#include + #include #include #include @@ -461,7 +465,13 @@ static void redrawTheWindow() b = fmod(b+0.5, 360.); c = fmod(c+0.25, 360.); + struct timespec Ta, Tb; + + clock_gettime(CLOCK_MONOTONIC_RAW, &Ta); glXSwapBuffers(Xdisplay, glX_window_handle); + clock_gettime(CLOCK_MONOTONIC_RAW, &Tb); + + fprintf(stderr, "glXSwapBuffers returned after %f ms\n", 1e3*((double)Tb.tv_sec + 1e-6*(double)Tb.tv_nsec) - 1e3*((double)Ta.tv_sec + 1e-6*(double)Ta.tv_nsec)); } int main(int argc, char *argv[]) diff --git a/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c b/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c index b658f5c..0852cbc 100644 --- a/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c +++ b/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c @@ -24,12 +24,17 @@ ------------------------------------------------------------------------*/ +#define _GNU_SOURCE + #include #include #include #include #include +#include +#include + #include #include #include @@ -571,7 +576,16 @@ static void redrawTheWindow(double T) draw_cube(); popModelview(); + struct timespec Ta, Tb; + + clock_gettime(CLOCK_MONOTONIC_RAW, &Ta); glXSwapBuffers(Xdisplay, glX_window_handle); + glXWaitGL(); + clock_gettime(CLOCK_MONOTONIC_RAW, &Tb); + + fprintf(stderr, "glXSwapBuffers + glXWaitGL returned after %f ms\n", + 1e3*( (double)Tb.tv_sec + 1e-9*(double)Tb.tv_nsec ) - + 1e3*( (double)Ta.tv_sec + 1e-9*(double)Ta.tv_nsec ) ); } static double getftime(void) { diff --git a/samples/X11/clobberme/Makefile b/samples/X11/clobberme/Makefile new file mode 100644 index 0000000..52dfca8 --- /dev/null +++ b/samples/X11/clobberme/Makefile @@ -0,0 +1,3 @@ +clobberme: clobberme.c Makefile + $(CC) -o clobberme -lX11 clobberme.c + diff --git a/samples/X11/clobberme/clobberme.c b/samples/X11/clobberme/clobberme.c new file mode 100644 index 0000000..ac58dc5 --- /dev/null +++ b/samples/X11/clobberme/clobberme.c @@ -0,0 +1,171 @@ +/*------------------------------------------------------------------------ + * A demonstration of a clobberable X11 window, i.e. a window that will + * assume the contents of whatever was visible above it, without clearing + * or tidying up itself. + * + * (c) 2013 by Wolfgang 'datenwolf' Draxinger + + * License agreement: This source code is provided "as is". You + * can use this source code however you want for your own personal + * use. If you give this source code to anybody else then you must + * leave this message in it. +------------------------------------------------------------------------*/ +#define _GNU_SOURCE + +#include +#include +#include +#include + +#include +#include + +#include +#include + +static void fatalError(const char *why) +{ + fprintf(stderr, "%s", why); + exit(0x666); +} + +static int Xscreen; +static Atom del_atom; +static Colormap cmap; +static Display *Xdisplay; +static Visual *Xvisual; +static Window Xroot, window_handle; +static int width, height; + +static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg) +{ + return d && e && arg && (e->type == MapNotify) && (e->xmap.window == *(Window*)arg); +} + +static void createTheWindow() +{ + XEvent event; + int x,y, attr_mask; + XSizeHints hints; + XWMHints *startup_state; + XTextProperty textprop; + XSetWindowAttributes attr = {0,}; + static char *title = "FTB's little OpenGL example - ARGB extension by WXD"; + + Xdisplay = XOpenDisplay(NULL); + if (!Xdisplay) { + fatalError("Couldn't connect to X server\n"); + } + Xscreen = DefaultScreen(Xdisplay); + Xroot = RootWindow(Xdisplay, Xscreen); + Xvisual = DefaultVisual(Xdisplay, Xscreen); + + /* Create a colormap - only needed on some X clients, eg. IRIX */ + cmap = XCreateColormap(Xdisplay, Xroot, Xvisual, AllocNone); + + attr.colormap = cmap; + attr.background_pixmap = None; + attr.border_pixmap = None; + attr.border_pixel = 0; + attr.event_mask = + StructureNotifyMask | + EnterWindowMask | + LeaveWindowMask | + ExposureMask | + ButtonPressMask | + ButtonReleaseMask | + OwnerGrabButtonMask | + KeyPressMask | + KeyReleaseMask; + + attr_mask = 0 + // | CWBackPixmap + | CWColormap + // | CWBorderPixel + | CWEventMask ; + + width = DisplayWidth(Xdisplay, DefaultScreen(Xdisplay))/2; + height = DisplayHeight(Xdisplay, DefaultScreen(Xdisplay))/2; + x=width/2, y=height/2; + + window_handle = XCreateWindow( Xdisplay, + Xroot, + x, y, width, height, + 0, + CopyFromParent, + InputOutput, + Xvisual, + attr_mask, &attr); + + if( !window_handle ) { + fatalError("Couldn't create the window\n"); + } + + textprop.value = (unsigned char*)title; + textprop.encoding = XA_STRING; + textprop.format = 8; + textprop.nitems = strlen(title); + + hints.x = x; + hints.y = y; + hints.width = width; + hints.height = height; + hints.flags = USPosition|USSize; + + startup_state = XAllocWMHints(); + startup_state->initial_state = NormalState; + startup_state->flags = StateHint; + + XSetWMProperties(Xdisplay, window_handle,&textprop, &textprop, + NULL, 0, + &hints, + startup_state, + NULL); + + XFree(startup_state); + + XMapWindow(Xdisplay, window_handle); + XIfEvent(Xdisplay, &event, WaitForMapNotify, (char*)&window_handle); + + if ((del_atom = XInternAtom(Xdisplay, "WM_DELETE_WINDOW", 0)) != None) { + XSetWMProtocols(Xdisplay, window_handle, &del_atom, 1); + } +} + +static int updateTheMessageQueue() +{ + XEvent event; + XConfigureEvent *xc; + + while (XPending(Xdisplay)) + { + XNextEvent(Xdisplay, &event); + switch (event.type) + { + case ClientMessage: + if (event.xclient.data.l[0] == del_atom) + { + return 0; + } + break; + + case ConfigureNotify: + xc = &(event.xconfigure); + width = xc->width; + height = xc->height; + break; + } + } + return 1; +} + +int main(int argc, char *argv[]) +{ + createTheWindow(); + + while (updateTheMessageQueue()) { + } + + return 0; +} + -- cgit v1.3.1 From dbfdab658eea6830a3ca5be205ed766aaa8ac6db Mon Sep 17 00:00:00 2001 From: Wolfgang Draxinger Date: Tue, 17 Sep 2013 12:42:48 +0200 Subject: added minimalvbo example --- samples/OpenGL/minimalvbo/.minimalvbo.c.swp | Bin 0 -> 20480 bytes samples/OpenGL/minimalvbo/Makefile | 5 + samples/OpenGL/minimalvbo/minimalvbo.c | 154 +++++++++++++++++++++++++ samples/OpenGL/x11argb_opengl/x11argb_opengl.c | 4 +- 4 files changed, 160 insertions(+), 3 deletions(-) create mode 100644 samples/OpenGL/minimalvbo/.minimalvbo.c.swp create mode 100644 samples/OpenGL/minimalvbo/Makefile create mode 100644 samples/OpenGL/minimalvbo/minimalvbo.c (limited to 'samples/OpenGL/x11argb_opengl') diff --git a/samples/OpenGL/minimalvbo/.minimalvbo.c.swp b/samples/OpenGL/minimalvbo/.minimalvbo.c.swp new file mode 100644 index 0000000..9ec9f9c Binary files /dev/null and b/samples/OpenGL/minimalvbo/.minimalvbo.c.swp differ diff --git a/samples/OpenGL/minimalvbo/Makefile b/samples/OpenGL/minimalvbo/Makefile new file mode 100644 index 0000000..0e6b724 --- /dev/null +++ b/samples/OpenGL/minimalvbo/Makefile @@ -0,0 +1,5 @@ +OBJS = minimalvbo.o + +minimalvbo: $(OBJS) + $(CC) -o minimalvbo $(OBJS) -lm -lGL -lGLU -lGLEW -lglut + diff --git a/samples/OpenGL/minimalvbo/minimalvbo.c b/samples/OpenGL/minimalvbo/minimalvbo.c new file mode 100644 index 0000000..3440c88 --- /dev/null +++ b/samples/OpenGL/minimalvbo/minimalvbo.c @@ -0,0 +1,154 @@ +#include +#include + +#include +#include +#include +#include + +void init(); +void display(); + +/* With the introduction of Buffer Objects, a somewhat questionable decision + * regarding the reuse of existing API was made. Specifically the reuse of the + * gl...Pointer functions to supply integers through their `data` parameter + * which is a pointer can lead to serious complications, should a compiler + * decide to implement augmented or tagged pointers. + * + * In many OpenGL tutorials, and even the reference documentation one can + * find that an iteger gets cast into a pointer. From the point of view of + * the C language standard and the interpretation of integers cast to pointer + * this practice should be strongly discouraged. The far better method + * is not to cast the offset integer value to a pointer, but to cast the + * gl...Pointer functions to a type signature that defines the `data` parameter + * to be an integer. To keep the size of the `data` parameter consistent + * use the C standard type `uintptr_t` which is guaranted to be large enough + * to hold the integer representation of a pointer. + * + * The same also goes for the glDraw...Elements functions. + * + * Also see http://stackoverflow.com/a/8284829/524368 + */ +void (*glfixVertexOffset)(GLint, GLenum, GLsizei, uintptr_t const) = (void*)glVertexPointer; +void (*glfixTexCoordOffset)(GLint, GLenum, GLsizei, uintptr_t const) = (void*)glTexCoordPointer; +void (*glfixDrawElementsOffset)(GLenum, GLsizei, GLenum, uintptr_t const) = (void*)glDrawElements; +/* Those three functions are part of OpenGL-1.1, hence in the OpenGL ABIs of + * all operating systems with OpenGL support and thus available as symbols + * of the interface library, without additional initialization. + * + * Don't use static const initialization for function pointer obtained + * through the OpenGL extension mechanism! + */ + +int main(int argc, char *argv[]) +{ + glutInit(&argc, argv); + glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH ); + + glutCreateWindow("Minimal VBO example"); + glutDisplayFunc(display); + glutIdleFunc(glutPostRedisplay); + + glewInit(); + init(); + + glutMainLoop(); + + return 0; +} + +GLuint vertexbufId; +GLuint elementbufId; + +void init() +{ + glGenBuffers(1, &vertexbufId); + glBindBuffer(GL_ARRAY_BUFFER, vertexbufId); + + float const cube[5*8]= + { /* X Y Z U V */ + -1, -1, -1, 0, 0, + 1, -1, -1, 1, 0, + 1, 1, -1, 1, 1, + -1, 1, -1, 0, 1, + + -1, -1, 1, -1, 0, + 1, -1, 1, 0, 0, + 1, 1, 1, 0, 1, + -1, 1, 1, -1, 1, + }; + + glBufferData(GL_ARRAY_BUFFER, 5*8*sizeof(float), cube, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + + glGenBuffers(1, &elementbufId); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbufId); + + unsigned int faces[4*6]= + { + 0, 1, 2, 3, + 1, 5, 6, 2, + 5, 4, 7, 6, + 4, 0, 3, 7, + 3, 2, 6, 7, + 4, 5, 1, 0 + }; + + glBufferData(GL_ELEMENT_ARRAY_BUFFER, 4*6*sizeof(float), faces, GL_STATIC_DRAW); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); +} + +static float a = 0., b = 0., c = 0.; + +void display() +{ + int const win_width = glutGet(GLUT_WINDOW_WIDTH); + int const win_height = glutGet(GLUT_WINDOW_HEIGHT); + float const win_aspect = (float)win_width / (float) win_height; + + glClearColor(0.5, 0.5, 1., 1.); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glViewport(0, 0, win_width, win_height); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glFrustum(-win_aspect, win_aspect, -1, 1, 1, 10); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0, 0, -3); + glRotatef(a += 1.00, 1, 0, 0); + glRotatef(b += 0.66, 0, 1, 0); + glRotatef(c += 0.33, 0, 0, 1); + + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + + glBindBuffer(GL_ARRAY_BUFFER, vertexbufId); + glfixVertexOffset( 3, GL_FLOAT, 5*sizeof(float), 0); + glfixTexCoordOffset(2, GL_FLOAT, 5*sizeof(float), 3); + /* After the data source of a Vertex Array has been set + * this setting stays persistent regardless of any other + * state changes. So we can unbind the VBO bufferId here + * without loosing the connection. We must not delete + * the VBO though. + */ + glBindBuffer(GL_ARRAY_BUFFER, 0), + + glColor4f(0., 0., 0., 1.), + + /* Just drawing a wireframe */ + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbufId); + glfixDrawElementsOffset(GL_QUADS, 24, GL_UNSIGNED_INT, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + + glutSwapBuffers(); +} + diff --git a/samples/OpenGL/x11argb_opengl/x11argb_opengl.c b/samples/OpenGL/x11argb_opengl/x11argb_opengl.c index 723c6ae..092c202 100644 --- a/samples/OpenGL/x11argb_opengl/x11argb_opengl.c +++ b/samples/OpenGL/x11argb_opengl/x11argb_opengl.c @@ -187,7 +187,7 @@ static void createTheWindow() KeyReleaseMask; attr_mask = - CWBackPixmap| + // CWBackPixmap| CWColormap| CWBorderPixel| CWEventMask; @@ -480,9 +480,7 @@ int main(int argc, char *argv[]) createTheRenderContext(); while (updateTheMessageQueue()) { - #if 0 redrawTheWindow(); - #endif } return 0; -- cgit v1.3.1