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_glsl/Makefile | 2 ++ samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) (limited to 'samples/OpenGL/x11argb_opengl_glsl') diff --git a/samples/OpenGL/x11argb_opengl_glsl/Makefile b/samples/OpenGL/x11argb_opengl_glsl/Makefile index 6c234cb..e32b597 100644 --- a/samples/OpenGL/x11argb_opengl_glsl/Makefile +++ b/samples/OpenGL/x11argb_opengl_glsl/Makefile @@ -1,3 +1,5 @@ +CC=pcc + x11argb_opengl_glsl: x11argb_opengl_glsl.c Makefile $(CC) -std=c99 -g3 -o x11argb_opengl_glsl x11argb_opengl_glsl.c -lX11 -lXrender -lGLEW -lm diff --git a/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c b/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c index 643c0ca..d918f0b 100644 --- a/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c +++ b/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c @@ -62,7 +62,7 @@ static const GLchar *fragment_shader_source = " vec2 mod_texcoord = gl_TexCoord[0].st*vec2(1., 2.) + vec2(0, -0.5 + 0.5*sin(T + 1.5*ts*pi));\n" " if( mod_texcoord.t < 0. || mod_texcoord.t > 1. ) { discard; }\n" " gl_FragColor = -texture2D(texCMYK, mod_texcoord) + texture2D(texRGB, gl_TexCoord[0].st);\n" -" gl_FragColor.a = -0.5;\n" +" gl_FragColor.a = 1.;\n" "}\n\0"; GLuint shaderFragment = 0; -- cgit v1.3.1 From 623fc8c73de35bef0785f09bae8a3f607f8ed087 Mon Sep 17 00:00:00 2001 From: Wolfgang Draxinger Date: Fri, 27 Apr 2012 20:43:08 +0200 Subject: qt_terr - a simple quadtree based terrain renderer with LOD --- samples/OpenGL/qt_terr/Makefile | 7 + samples/OpenGL/qt_terr/main.cpp | 582 ++++++++++++++++++++++++++++ samples/OpenGL/qt_terr/mountains.png | Bin 0 -> 9265856 bytes samples/OpenGL/qt_terr/mountains.ter | Bin 0 -> 526424 bytes samples/OpenGL/qt_terr/mountains.tgw | Bin 0 -> 3128 bytes samples/OpenGL/qt_terr/quad.cpp | 140 +++++++ samples/OpenGL/qt_terr/quad.h | 49 +++ samples/OpenGL/qt_terr/readme.txt | 14 + samples/OpenGL/qt_terr/terragen.cpp | 138 +++++++ samples/OpenGL/qt_terr/terragen.h | 6 + samples/OpenGL/qt_terr/terrain.cpp | 43 ++ samples/OpenGL/qt_terr/terrain.h | 26 ++ samples/OpenGL/qt_terr/vecmath.cpp | 39 ++ samples/OpenGL/qt_terr/vecmath.h | 16 + samples/OpenGL/x11argb_opengl_glsl/Makefile | 2 - 15 files changed, 1060 insertions(+), 2 deletions(-) create mode 100644 samples/OpenGL/qt_terr/Makefile create mode 100644 samples/OpenGL/qt_terr/main.cpp create mode 100644 samples/OpenGL/qt_terr/mountains.png create mode 100644 samples/OpenGL/qt_terr/mountains.ter create mode 100644 samples/OpenGL/qt_terr/mountains.tgw create mode 100644 samples/OpenGL/qt_terr/quad.cpp create mode 100644 samples/OpenGL/qt_terr/quad.h create mode 100644 samples/OpenGL/qt_terr/readme.txt create mode 100644 samples/OpenGL/qt_terr/terragen.cpp create mode 100644 samples/OpenGL/qt_terr/terragen.h create mode 100644 samples/OpenGL/qt_terr/terrain.cpp create mode 100644 samples/OpenGL/qt_terr/terrain.h create mode 100644 samples/OpenGL/qt_terr/vecmath.cpp create mode 100644 samples/OpenGL/qt_terr/vecmath.h (limited to 'samples/OpenGL/x11argb_opengl_glsl') diff --git a/samples/OpenGL/qt_terr/Makefile b/samples/OpenGL/qt_terr/Makefile new file mode 100644 index 0000000..1f9c52b --- /dev/null +++ b/samples/OpenGL/qt_terr/Makefile @@ -0,0 +1,7 @@ +CC = gcc +CPP = g++ +OBJS += main.o quad.o terragen.o terrain.o vecmath.o + +qt_terr: $(OBJS) + $(CPP) -o qt_terr $(OBJS) -lm -lGL -lGLU -lGLEW -lglut -lIL -lILU -lILUT + diff --git a/samples/OpenGL/qt_terr/main.cpp b/samples/OpenGL/qt_terr/main.cpp new file mode 100644 index 0000000..64c4327 --- /dev/null +++ b/samples/OpenGL/qt_terr/main.cpp @@ -0,0 +1,582 @@ +#include +#include +#include +#include +#include + +#include + +#define ILUT_USE_OPENGL 1 + +#include +#include +#include +#include +#include + +using namespace std; + +#include "vecmath.h" +#include "terragen.h" +#include "terrain.h" + +Terrain Q; + +double const water_level=-1.0; + +void reshape(int width, int height); +void display(); +void keyboard(unsigned char key, int x, int y); + +double frand() +{ + return (double)rand()/(double)RAND_MAX; +} + +struct RGB +{ + GLubyte r, g, b; +}; + +int width, height, levels; +int iMaxLOD; +int iMaxCull=7; +double t, k, l; +double scale[3]; +double scale_dim; +double fov=1.414/2; +//double fov=0.1; +double near_clip=1; +double far_clip=100000; + +void assign_elevations(Terrain *pT, double *buffer, int width, int height, double t, double k, + int left=-1, int bottom=-1, int right=-1, int top=-1); + +GLuint texID; + +void idle() +{ + glutPostRedisplay(); +} + +int main(int argc, char **argv) +{ + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE); + glutCreateWindow("Terrain"); + glutDisplayFunc(display); + glutReshapeFunc(reshape); + glutKeyboardFunc(keyboard); + glutIdleFunc(idle); + + glewInit(); + + if(argc<2) + return -1; + + std::string terrain_filename(argv[1]); + + double *buffer=NULL; + read_terrain(terrain_filename.c_str(), &buffer, &width, &height, scale); + levels = max(width, height); + levels = log(levels)/log(2); + scale_dim=sqrt(pow(scale[0]*width, 2)+pow(scale[1]*height, 2)+scale[2]*scale[2]); + + iMaxLOD=levels; + + ilInit(); + iluInit(); + ilutInit(); + ilutRenderer(ILUT_OPENGL); + + std::string img_filename( terrain_filename ); + img_filename.replace(img_filename.length()-5, 4, ".png"); + + texID=ilutGLLoadImage(img_filename.c_str()); + glBindTexture(GL_TEXTURE_2D, texID); + + int x, y; + double min_v = 0, max_v = 0; + for(y = 0; y < height; y++) + { + for( x = 0; x < width; x++) + { + min_v = min( buffer[ y*width + x ], min_v ); + max_v = max( buffer[ y*width + x ], max_v ); + } + } + +#if 0 + RGB *img_buffer = new RGB[width*height]; + t=min_v; + k=255.0/(double)(max_v-min_v); + l=1.0/(double)(max_v-min_v); + for(y=0; y=water_level) + { + rgb->r=rgb->g=rgb->b=(*value-t)*k; + } + else + { + rgb->r=rgb->g=0.5*(*value-t)*k+75; + rgb->b=(*value-t)*k+128; + } + + } + } + + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glBindTexture(GL_TEXTURE_RECTANGLE_NV, 1); + glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_buffer); + + delete[] img_buffer; +#endif + + assign_elevations(&Q, buffer, width, height, 0, 1); + + delete[] buffer; + + glEnable(GL_CULL_FACE); + glClearColor(0.7, 0.8, 1.0, 1.0); + + glutMainLoop(); + + return 0; +}; + +void assign_elevations(Terrain *pT, double *buffer, int width, int height, double t, double k, + int left, int bottom, int right, int top) +{ + if(left == -1) + left=0; + if(bottom == -1) + bottom=0; + if(right == -1) + right=width-1; + if(top == -1) + top=height-1; + +// WTF, a no-op macro, why?! +// BTW, I wrote that, whatever I was planning, I didn't finish it +// -- Wolfgang +#define PEL(d) (d) + + pT->z[0][0]=( buffer[ PEL(left+bottom*width) ] -t )*k; + pT->z[0][1]=( buffer[ PEL(right+bottom*width) ] -t )*k; + pT->z[1][1]=( buffer[ PEL(right+top*width) ] -t )*k; + pT->z[1][0]=( buffer[ PEL(left+top*width) ] -t )*k; + + pT->z_mean=( + pT->z[0][0]+ + pT->z[0][1]+ + pT->z[1][1]+ + pT->z[1][0])*0.25; + + if(pT->is_split()) + { + assign_elevations((Terrain*)pT->quads[0][0], buffer, width, height, t, k, + left, bottom, left+(right-left)/2, bottom+(top-bottom)/2); + + assign_elevations((Terrain*)pT->quads[0][1], buffer, width, height, t, k, + left+(right-left)/2, bottom, right, bottom+(top-bottom)/2); + + assign_elevations((Terrain*)pT->quads[1][1], buffer, width, height, t, k, + left+(right-left)/2, bottom+(top-bottom)/2, right, top); + + assign_elevations((Terrain*)pT->quads[1][0], buffer, width, height, t, k, + left, bottom+(top-bottom)/2, left+(right-left)/2, top); + } +} + +double view_dot; + +void reshape(int width, int height) +{ + glViewport(0,0,width, height); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + double aspect=(double)width/(double)height; + + double x=near_clip*fov; + double y=x/aspect; + + double RIGHT_UPPER[3]={x, y, near_clip}; + double cam_view[3]={0,0,1}; + normalize(RIGHT_UPPER, RIGHT_UPPER); + view_dot=DOT(cam_view, RIGHT_UPPER); + + glFrustum(-x, x, -y, y, near_clip, far_clip); + glMatrixMode(GL_MODELVIEW); +} + +bool bDrawQuads=true; +bool bFill=true; +bool bLOD=true; +bool bFrustumCulling=true; +bool bContinuous=true; +bool bAnimation=true; +bool bPaused=false; + +enum Camera{follow, bird_view}; +int eCam; + +void keyboard(unsigned char key, int x, int y) +{ + switch(key) + { + case ' ': + bAnimation=!bAnimation; + break; + + case 'Q': + case 'q': + exit(0); + break; + + case 'F': + case 'f': + bFill=!bFill; + break; + + case 'H': + case 'h': + bFrustumCulling=!bFrustumCulling; + break; + + case 'L': + case 'l': + bLOD=!bLOD; + break; + + case 'V': + case 'v': + eCam++; + if(eCam>bird_view) + eCam = 0; + break; + + case 'C': + case 'c': + bContinuous=!bContinuous; + break; + + case 'p': + case 'P': + bPaused=!bPaused; + break; + + case 's': + case 'S': + ilutGLScreenie(); + + case '+': + if(iMaxLOD0) + iMaxLOD--; + break; + } + glutPostRedisplay(); +} + +void draw_quad(Terrain *pQ, int LOD=0, double z1=0, double z2=0, double z3=0, double z4=0); + +GLdouble cam_pos[3];//={68.0f*scale[0], 44.0f*scale[1], 47.7*scale[2]*0.5}; +double cam_la[3]; +double cam_vec[3]; +double cam_vec_n0[3]; + +#define PI 3.141512 + +double modelview_matrix[16]; +double projection_matrix[16]; +GLint viewport[4]={0,0,1,1}; + +void display() +{ + static double alpha=0; + static double beta=0; + + if(bAnimation) + { + alpha+=0.0025*PI; + beta+=0.001*PI; + + if(alpha>2*PI) + alpha=0; + + if(beta>2*PI) + beta=0; + } + + double dx=(1+cos(alpha))*0.5; + double dy=(1+sin(alpha))*0.5; + + double dz=(1+sin(beta))*0.5; + + cam_pos[0]=dx*width*scale[0]; + cam_pos[1]=dy*height*scale[1]; + cam_pos[2]=max(dz*scale[2]*width*3, (20.0f+((Terrain*)Q.get_at(dx, dy))->z_mean)*scale[2]); + //cam_pos[2]=(20.0f+((Terrain*)Q.get_at(dx, dy))->z_mean)*scale[2]; + + cam_la[0]=width*scale[0]*0.5; + cam_la[1]=height*scale[1]*0.5; + cam_la[2]=9.6*scale[2]*0.5; + + cam_vec[0]=cam_la[0]-cam_pos[0]; + cam_vec[1]=cam_la[1]-cam_pos[1]; + cam_vec[2]=cam_la[2]-cam_pos[2]; + + normalize(cam_vec_n0, cam_vec); + + normalize(cam_vec, cam_vec); + + glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); + + glLoadIdentity(); + gluLookAt( cam_pos[0], cam_pos[1], cam_pos[2], + cam_la[0], cam_la[1], cam_la[2], + 0,0,1); + glScalef(1,1,0.5); + glGetDoublev(GL_MODELVIEW_MATRIX, modelview_matrix); + glGetDoublev(GL_PROJECTION_MATRIX, projection_matrix); + glGetIntegerv(GL_VIEWPORT, viewport); + + switch(eCam) + { + case follow: + break; + + case bird_view: + glLoadIdentity(); + gluLookAt( width*scale[0]*0.5, height*scale[1]*0.5, scale[2]*500, + cam_la[0], cam_la[1], cam_la[2], + 0,1,0); + //glScalef(1,1,0.5); + break; + } + //glTranslatef(-0.5, -0.5, -5); + + glEnable(GL_DEPTH_TEST); + /* + if(bDrawTerrain) + { + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + glEnable(GL_TEXTURE_RECTANGLE_NV); + glBegin(GL_QUADS); + glColor3f(1,1,1); + glTexCoord2i(0,0); + glVertex2f(0,0); + glTexCoord2i(width,0); + glVertex2f(1,0); + glTexCoord2i(width, height); + glVertex2f(1,1); + glTexCoord2i(0,height); + glVertex2f(0,1); + glEnd(); + }*/ + + glColor3f(1,0,0); + glPointSize(5); + glBegin(GL_POINTS); + glVertex3dv(cam_pos); + glEnd(); + + if(bDrawQuads) + { + glColor3f(1,1,1); + glDisable(GL_TEXTURE_RECTANGLE_NV); + glEnable(GL_TEXTURE_2D); + glPolygonMode(GL_FRONT_AND_BACK, bFill?GL_FILL:GL_LINE); + glBegin(GL_QUADS); + draw_quad(&Q); + glEnd(); + } + + glutSwapBuffers(); +} + +bool in_view(double a[3]) +{ + GLdouble winx, winy, winz; + + gluProject(a[0], a[1], a[2], modelview_matrix, projection_matrix, viewport, &winx, &winy, &winz); + if( winx>=viewport[0] && winx<=viewport[2] && + winy>=viewport[1] && winy<=viewport[3] ) + return true; + + return false; +} + +void draw_quad(Terrain *pT, int LOD, double z1, double z2, double z3, double z4) +{ + bool bRefine=true; + bool bInFront=true; + + double Q[3]={pT->x_mid*width*scale[0], pT->y_mid*height*scale[1], pT->z_mean*scale[2]}; + double Q1[3]={pT->x1*width*scale[0], pT->y1*height*scale[1], pT->z[0][0]*scale[2]}; + double Q2[3]={pT->x2*width*scale[0], pT->y1*height*scale[1], pT->z[0][1]*scale[2]}; + double Q3[3]={pT->x2*width*scale[0], pT->y2*height*scale[1], pT->z[1][1]*scale[2]}; + double Q4[3]={pT->x1*width*scale[0], pT->y2*height*scale[1], pT->z[1][0]*scale[2]}; + double Qt[3]; + + double dir[3]; + SUB(dir, Q, cam_pos); + double dot=DOT(dir, cam_vec); + + double l_n[4]; + double dir_n[3]; + + /* + SUB(dir_n, Q1, cam_pos); + l_n[0]=length(dir_n); + + SUB(dir_n, Q2, cam_pos); + l_n[1]=length(dir_n); + + SUB(dir_n, Q3, cam_pos); + l_n[2]=length(dir_n); + + SUB(dir_n, Q4, cam_pos); + l_n[3]=length(dir_n); + */ + + //if(l_n[0]0) + { + for(int i=0; i<4; i++) + { + lod_scale[i]=(d_lod-l_n[i])/(d_lod-d_lod_plus_1); + + lod_scale[i]=min(lod_scale[i], 1.0); + lod_scale[i]=max(lod_scale[i], 0.0); + + one_minus_lod_scale[i]=1.0-lod_scale[i]; + } + } + + if(bLOD) + { + bRefine = sz > k; + } + + if(bFrustumCulling&&LODis_split() && LODquads[0][0], LOD+1, + Q1[2], Z1, Q[2], Z4); + + draw_quad( pT->quads[0][1], LOD+1, + Z1, Q2[2], Z2, Q[2]); + + draw_quad( pT->quads[1][1], LOD+1, + Q[2], Z2, Q3[2], Z3); + + draw_quad( pT->quads[1][0], LOD+1, + Z4, Q[2], Z3, Q4[2]); + } + else// if(bInFront) + { + Q1[2]=Q1[2]*lod_scale[0] + z1*one_minus_lod_scale[0]; + Q2[2]=Q2[2]*lod_scale[1] + z2*one_minus_lod_scale[1]; + Q3[2]=Q3[2]*lod_scale[2] + z3*one_minus_lod_scale[2]; + Q4[2]=Q4[2]*lod_scale[3] + z4*one_minus_lod_scale[3]; + +#define LUM(v) glColor3f(v,v,v) + + if(bClipped) + glColor3f(1,0,0); + else + glColor3f(1,1,1); + //LUM(one_minus_lod_scale[0]); + glTexCoord2f(pT->x1, pT->y1); + glVertex3dv(Q1); + + //LUM(one_minus_lod_scale[1]); + glTexCoord2f(pT->x2, pT->y1); + glVertex3dv(Q2); + + //LUM(one_minus_lod_scale[2]); + glTexCoord2f(pT->x2, pT->y2); + glVertex3dv(Q3); + + //LUM(one_minus_lod_scale[3]); + glTexCoord2f(pT->x1, pT->y2); + glVertex3dv(Q4); + } + } +} diff --git a/samples/OpenGL/qt_terr/mountains.png b/samples/OpenGL/qt_terr/mountains.png new file mode 100644 index 0000000..77e87c5 Binary files /dev/null and b/samples/OpenGL/qt_terr/mountains.png differ diff --git a/samples/OpenGL/qt_terr/mountains.ter b/samples/OpenGL/qt_terr/mountains.ter new file mode 100644 index 0000000..5f48ff0 Binary files /dev/null and b/samples/OpenGL/qt_terr/mountains.ter differ diff --git a/samples/OpenGL/qt_terr/mountains.tgw b/samples/OpenGL/qt_terr/mountains.tgw new file mode 100644 index 0000000..5162265 Binary files /dev/null and b/samples/OpenGL/qt_terr/mountains.tgw differ diff --git a/samples/OpenGL/qt_terr/quad.cpp b/samples/OpenGL/qt_terr/quad.cpp new file mode 100644 index 0000000..e57bcfa --- /dev/null +++ b/samples/OpenGL/qt_terr/quad.cpp @@ -0,0 +1,140 @@ +#include +#include "quad.h" + +Quad::Quad() : + V(new std::vector) +{ + quads[0][0] = + quads[0][1] = + quads[1][0] = + quads[1][1] = 0; + + unsigned int V_old_size = V->size(); + unsigned int V_new_size = V_old_size + 9; + V->resize(V_new_size, 0.); + + v1_offset = V_new_size - 3*3; + v2_offset = V_new_size - 2*3; + v_mid_offset = V_new_size - 1*3; + + set_range(0., 1., 0., 1.); +} + +Quad::Quad( std::vector * const V_) : + V(V_) +{ + quads[0][0] = + quads[0][1] = + quads[1][0] = + quads[1][1] = 0; + + unsigned int V_old_size = V->size(); + unsigned int V_new_size = V_old_size + 9; + V->resize(V_new_size, 0.); + + v1_offset = V_new_size - 3*3; + v2_offset = V_new_size - 2*3; + v_mid_offset = V_new_size - 1*3; + + set_range(0., 1., 0., 1.); +} + +Quad::Quad(Quad &q) : + V(new std::vector) +{ + operator= (q); +} + +Quad::~Quad() +{ + if(is_split()) + { + delete quads[0][0]; + delete quads[0][1]; + delete quads[1][0]; + delete quads[1][1]; + } +} + +Quad& Quad::operator=(Quad &q) +{ + *x1() = *q.x1(); + *x2() = *q.x2(); + *y1() = *q.y1(); + *y2() = *q.y2(); + + if( q.is_split() ) + { + split(); + quads[0][0] = q.quads[0][0]; + quads[0][1] = q.quads[0][1]; + quads[1][0] = q.quads[1][0]; + quads[1][1] = q.quads[1][1]; + } + + return *this; +} + +void Quad::split() +{ + if(is_split()) + return; + + quads[0][0] = new Quad(V); + quads[0][1] = new Quad(V); + quads[1][0] = new Quad(V); + quads[1][1] = new Quad(V); + + quads[0][0]->set_range( *x1(), *x_mid(), *y1(), *y_mid() ); + quads[0][1]->set_range( *x_mid(), *x2(), *y1(), *y_mid() ); + quads[1][0]->set_range( *x1(), *x_mid(), *y_mid(), *y2() ); + quads[1][1]->set_range( *x_mid(), *x2(), *y_mid(), *y2() ); +} + +bool Quad::is_split() +{ + return quads[0][0] && + quads[0][1] && + quads[1][0] && + quads[1][1]; +} + +void Quad::set_range(double nx1, double nx2, double ny1, double ny2) +{ + *x1() = nx1; + *x2() = nx2; + *y1() = ny1; + *y2() = ny2; + *x_mid() = ( *x1() + *x2() )*0.5f; + *y_mid() = ( *y1() + *y2() )*0.5f; + +} + +void Quad::track_down(double x, double y, int levels) +{ + if(levels>0) + { + int a = ( x < *x_mid() ) ? 0 : 1; + int b = ( y < *y_mid() ) ? 0 : 1; + + if( !is_split() ) + split(); + + quads[b][a]->track_down(x, y, levels-1); + } +} + +Quad *Quad::get_at(double x, double y, int max_level, int level) +{ + if(!is_split()) + return this; + + /*if(max_level>-1 && level>=max_level) + return this;*/ + + int a = ( x < *x_mid() ) ? 0 : 1; + int b = ( y < *y_mid() ) ? 0 : 1; + + return quads[b][a]->get_at(x, y, max_level, level+1); +} + diff --git a/samples/OpenGL/qt_terr/quad.h b/samples/OpenGL/qt_terr/quad.h new file mode 100644 index 0000000..1c6549a --- /dev/null +++ b/samples/OpenGL/qt_terr/quad.h @@ -0,0 +1,49 @@ +#ifndef QUAD_H +#define QUAD_H + +#include + +class Quad +{ +public: +// double x1,x2, x_mid; +// double y1,y2, y_mid; + + unsigned int v1_offset; + unsigned int v2_offset; + unsigned int v_mid_offset; + + double *x1() { return &(*V)[v1_offset + 0]; } + double *y1() { return &(*V)[v1_offset + 1]; } + double *z1() { return &(*V)[v1_offset + 2]; } + + double *x2() { return &(*V)[v2_offset + 0]; } + double *y2() { return &(*V)[v2_offset + 1]; } + double *z2() { return &(*V)[v2_offset + 2]; } + + double *x_mid() { return &(*V)[v_mid_offset + 0]; } + double *y_mid() { return &(*V)[v_mid_offset + 1]; } + double *z_mid() { return &(*V)[v_mid_offset + 2]; } + + Quad *quads[2][2]; + +public: + Quad(); + Quad(Quad &q); + ~Quad(); + Quad& operator=(Quad &q); + + virtual void split(); + virtual bool is_split(); + + virtual void track_down(double x, double y, int levels); + virtual Quad *get_at(double x, double y, int max_level=0, int level=0); + +protected: + virtual void set_range(double nx1, double nx2, double ny1, double ny2); + + Quad(std::vector*); + std::vector * const V; +}; + +#endif//QUAD_H diff --git a/samples/OpenGL/qt_terr/readme.txt b/samples/OpenGL/qt_terr/readme.txt new file mode 100644 index 0000000..cb2b065 --- /dev/null +++ b/samples/OpenGL/qt_terr/readme.txt @@ -0,0 +1,14 @@ +Extract all into a single directory + +Execute with +# terrain .ter + +Key mapping: +[V] Change camera mode +[H] Frustum Culling on/off +[L] Level of Detail triangle reduction on/off +[F] Polygonfilling/Wireframe on/off +[+] increase max. Level of Detail +[-] decrease max. Level of Detail +[S] Screenshot + pause/restart animation \ No newline at end of file diff --git a/samples/OpenGL/qt_terr/terragen.cpp b/samples/OpenGL/qt_terr/terragen.cpp new file mode 100644 index 0000000..3b922a4 --- /dev/null +++ b/samples/OpenGL/qt_terr/terragen.cpp @@ -0,0 +1,138 @@ +#include +#include +#include +#include + +#include "terragen.h" + +// Some custom types and helper macros that +// allow for a portable file to number variable +// read procedure + +typedef uint8_t DUET[2]; +typedef uint8_t QUARTET[4]; +typedef uint8_t OCTET[8]; + +#define DUET_TO_NUMBER(duet) ((duet[0]|duet[1]<<8)) +#define QUARTET_TO_NUMBER(quartet) ((quartet[0])|(quartet[1]<<8)|(quartet[2]<<16)|(quartet[3]<<24)) + +int read_terrain(char *filename, double **pbuffer, int *width, int *height, double *scale) +// [in] filename: path to the file to be read in +// [out] pbuffer: address of new allocated data buffer +// [out] width: width of the terrain +// [out] heigt: height of the terrain +// [out] return value: 0 if read in successfully 1 otherwise +{ + FILE *file=fopen(filename, "rb"); + if(!file) + return -1; + + OCTET octet; + QUARTET quartet; + QUARTET quartet_marker; + DUET duet; + + fread(octet, 8, 1, file); + if(memcmp(octet, "TERRAGEN", 8)) + return -1; + + fread(octet, 8, 1, file); + if(memcmp(octet, "TERRAIN ", 8)) + return -1; + + fread(quartet, 4, 1, file); + if(memcmp(quartet, "SIZE", 4)) + return -1; + fread(duet, 2, 1, file); + fseek(file, 2, SEEK_CUR); + int size=DUET_TO_NUMBER(duet); + + int xpts=0, ypts=0; + do + { + fread(quartet_marker, 4, 1, file); + + if(memcmp(quartet_marker, "XPTS", 4)==0) + { + fread(duet, 2, 1, file); + fseek(file, 2, SEEK_CUR); + xpts=DUET_TO_NUMBER(duet); + continue; + } + + if(memcmp(quartet_marker, "YPTS", 4)==0) + { + fread(duet, 2, 1, file); + fseek(file, 2, SEEK_CUR); + ypts=DUET_TO_NUMBER(duet); + continue; + } + + // I'm going to ignore the other segments so long + // so we can leave the quartet marker test to the + // while condition + //if(strcmp(quartet_marker, "SIZE")) // Ignore SIZE + // fseek(file, 4, SEEK_CUR); + + if(memcmp(quartet_marker, "SCAL", 4)==0&&scale) // Ignore SCAL + { + float fscale[3]; + fread(&(fscale[0]), 4, 1, file); + fread(&(fscale[1]), 4, 1, file); + fread(&(fscale[2]), 4, 1, file); + scale[0]=fscale[0]; + scale[1]=fscale[1]; + scale[2]=fscale[2]; + } + + //if(strcmp(quartet_marker, "CRAD")) // Ignore CRAD + // fseek(file, 4, SEEK_CUR); + + //if(strcmp(quartet_marker, "CRVM")) // Ignore CRVM + // fseek(file, 4, SEEK_CUR); + + }while(memcmp(quartet_marker, "ALTW", 4)); + + int16_t height_scale, base_height; + fread(duet, 2, 1, file); + height_scale = DUET_TO_NUMBER(duet); + fread(duet, 2, 1, file); + base_height = DUET_TO_NUMBER(duet); + + if(xpts==0&&ypts==0) + { + xpts=size+1; + ypts=size+1; + } + + *width=xpts; + *height=ypts; + + // The caller of this function is responsible + // to free the memory consumed by buffer + DUET *buffer=new DUET[xpts*ypts]; + fread(buffer, 2, xpts*ypts, file); + + *pbuffer=new double[xpts*ypts]; + for(int y=0; y(base_height+elev*height_scale/65536); + } + } + delete[] buffer; + + // a last test, the next we read should be "EOF ", since currently the Terragen + // file format says, that "ALTW" must be the last segment of a file + // however it's no problem for _us_ if we've no "EOF " here, because it doesn't matter + fread(quartet, 4, 1, file); + if(memcmp(quartet, "EOF ", 4)) + printf("read terrain file with \"EOF \" at the end\n"); + + fclose(file); + return 0; +} diff --git a/samples/OpenGL/qt_terr/terragen.h b/samples/OpenGL/qt_terr/terragen.h new file mode 100644 index 0000000..5037a08 --- /dev/null +++ b/samples/OpenGL/qt_terr/terragen.h @@ -0,0 +1,6 @@ +#ifndef TERRAGEN_H +#define TERRAGEN_H + +int read_terrain(char *filename, double **pbuffer, int *width, int *height, double *scale); + +#endif/*TERRAGEN*/ \ No newline at end of file diff --git a/samples/OpenGL/qt_terr/terrain.cpp b/samples/OpenGL/qt_terr/terrain.cpp new file mode 100644 index 0000000..d08d345 --- /dev/null +++ b/samples/OpenGL/qt_terr/terrain.cpp @@ -0,0 +1,43 @@ +#include "terrain.h" + +Terrain::Terrain() +{ + z_mean=0.0; +} + +void Terrain::split() +{ + if(is_split()) + return; + + quads[0][0] = new Terrain(V); + quads[0][1] = new Terrain(V); + quads[1][0] = new Terrain(V); + quads[1][1] = new Terrain(V); + + quads[0][0]->set_range(x1, x_mid, y1, y_mid); + quads[0][1]->set_range(x_mid, x2, y1, y_mid); + quads[1][0]->set_range(x1, x_mid, y_mid, y2); + quads[1][1]->set_range(x_mid, x2, y_mid, y2); +} + +void Terrain::track_down(double x, double y, double nz, int levels) +{ + if(levels>0) + { + int a=(xtrack_down(x, y, nz, levels-1); + } + else + { + z[0][0]= + z[0][1]= + z[1][1]= + z[1][0]=z_mean=nz; + } +} diff --git a/samples/OpenGL/qt_terr/terrain.h b/samples/OpenGL/qt_terr/terrain.h new file mode 100644 index 0000000..0197380 --- /dev/null +++ b/samples/OpenGL/qt_terr/terrain.h @@ -0,0 +1,26 @@ +#ifndef TERRAIN_H +#define TERRAIN_H + +#include "quad.h" + +#include + +class Terrain : public Quad +{ +public: + double * const z_mean() { + + }; + double z[2][2]; + +public: + Terrain(); + + virtual void split(); + virtual void track_down(double x, double y, double nz, int levels); + +/*protected: + virtual void set_range(double nx1, double nx2, double ny1, double ny2);*/ +}; + +#endif/*TERRAIN_H*/ diff --git a/samples/OpenGL/qt_terr/vecmath.cpp b/samples/OpenGL/qt_terr/vecmath.cpp new file mode 100644 index 0000000..ff4230c --- /dev/null +++ b/samples/OpenGL/qt_terr/vecmath.cpp @@ -0,0 +1,39 @@ +#include +#include "vecmath.h" + +double DOT(double a[3], double b[3]) +{ + return a[0]*b[0]+a[1]*b[1]+a[2]*b[2]; +} + +void ADD(double out[3], double a[3], double b[3]) +{ + out[0]=a[0]+b[0]; + out[1]=a[1]+b[1]; + out[2]=a[2]+b[2]; +} + +void SUB(double out[3], double a[3], double b[3]) +{ + out[0]=a[0]-b[0]; + out[1]=a[1]-b[1]; + out[2]=a[2]-b[2]; +} + +void SCALE(double out[3], double v[3], double scalar) +{ + out[0]=v[0]*scalar; + out[1]=v[1]*scalar; + out[2]=v[2]*scalar; +} + +double length(double v[3]) +{ + return (double)sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]); +} + +void normalize(double out[3], double in[3]) +{ + double k=1.0f/length(in); + SCALE(out, in, k); +} \ No newline at end of file diff --git a/samples/OpenGL/qt_terr/vecmath.h b/samples/OpenGL/qt_terr/vecmath.h new file mode 100644 index 0000000..0c27eff --- /dev/null +++ b/samples/OpenGL/qt_terr/vecmath.h @@ -0,0 +1,16 @@ +#ifndef VECMATH_H +#define VECMATH_H + +double DOT(double a[3], double b[3]); + +void ADD(double out[3], double a[3], double b[3]); + +void SUB(double out[3], double a[3], double b[3]); + +void SCALE(double out[3], double v[3], double scalar); + +double length(double v[3]); + +void normalize(double out[3], double in[3]); + +#endif/*VECMATH_H*/ diff --git a/samples/OpenGL/x11argb_opengl_glsl/Makefile b/samples/OpenGL/x11argb_opengl_glsl/Makefile index e32b597..6c234cb 100644 --- a/samples/OpenGL/x11argb_opengl_glsl/Makefile +++ b/samples/OpenGL/x11argb_opengl_glsl/Makefile @@ -1,5 +1,3 @@ -CC=pcc - x11argb_opengl_glsl: x11argb_opengl_glsl.c Makefile $(CC) -std=c99 -g3 -o x11argb_opengl_glsl x11argb_opengl_glsl.c -lX11 -lXrender -lGLEW -lm -- 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_glsl') 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 4842cb28522a89c0a6d800139b934001484fadbe Mon Sep 17 00:00:00 2001 From: Wolfgang Draxinger Date: Fri, 26 Jul 2013 11:14:54 +0200 Subject: ... --- samples/OpenGL/frustum/frustum.c | 12 ++++++ samples/OpenGL/qt_terr/Makefile | 2 +- samples/OpenGL/qt_terr/main.cpp | 38 ++++++++---------- samples/OpenGL/qt_terr/quad.h | 2 +- samples/OpenGL/qt_terr/terragen.cpp | 2 +- samples/OpenGL/qt_terr/terragen.h | 4 +- samples/OpenGL/qt_terr/terrain.cpp | 29 ++++++-------- samples/OpenGL/qt_terr/terrain.h | 13 +++++-- .../x11argb_opengl_glsl/x11argb_opengl_glsl.c | 45 +++++++++++++++++++--- 9 files changed, 95 insertions(+), 52 deletions(-) (limited to 'samples/OpenGL/x11argb_opengl_glsl') diff --git a/samples/OpenGL/frustum/frustum.c b/samples/OpenGL/frustum/frustum.c index 51efb7e..c5732c8 100644 --- a/samples/OpenGL/frustum/frustum.c +++ b/samples/OpenGL/frustum/frustum.c @@ -11,6 +11,8 @@ #include #include +#include "linmath.h" + #if defined(GLUT_MULTISAMPLE) && defined(GL_MULTISAMPLE) #define OPTION_GLUT_MULTISAMPLE GLUT_MULTISAMPLE #define OPTION_MULTISAMPLE 1 @@ -208,6 +210,16 @@ void draw_arrow( } } +void draw_arc( + vec3 center, + vec3 a, vec3 b, + float ah, float bh, + char const * const annotation, + float annot_size ) +{ + a[0] = b[2]; +} + void draw_frustum( float l, float r, float b, float t, float n, float f ) diff --git a/samples/OpenGL/qt_terr/Makefile b/samples/OpenGL/qt_terr/Makefile index 1f9c52b..22ecad9 100644 --- a/samples/OpenGL/qt_terr/Makefile +++ b/samples/OpenGL/qt_terr/Makefile @@ -1,6 +1,6 @@ CC = gcc CPP = g++ -OBJS += main.o quad.o terragen.o terrain.o vecmath.o +OBJS += quad.o terragen.o terrain.o vecmath.o main.o qt_terr: $(OBJS) $(CPP) -o qt_terr $(OBJS) -lm -lGL -lGLU -lGLEW -lglut -lIL -lILU -lILUT diff --git a/samples/OpenGL/qt_terr/main.cpp b/samples/OpenGL/qt_terr/main.cpp index 64c4327..e8bf78e 100644 --- a/samples/OpenGL/qt_terr/main.cpp +++ b/samples/OpenGL/qt_terr/main.cpp @@ -20,7 +20,8 @@ using namespace std; #include "terragen.h" #include "terrain.h" -Terrain Q; +std::vector terraindata; +Terrain Q(&terraindata); double const water_level=-1.0; @@ -92,7 +93,7 @@ int main(int argc, char **argv) std::string img_filename( terrain_filename ); img_filename.replace(img_filename.length()-5, 4, ".png"); - texID=ilutGLLoadImage(img_filename.c_str()); + texID = ilutGLLoadImage((char*)img_filename.c_str()); glBindTexture(GL_TEXTURE_2D, texID); int x, y; @@ -174,12 +175,6 @@ void assign_elevations(Terrain *pT, double *buffer, int width, int height, doubl pT->z[1][1]=( buffer[ PEL(right+top*width) ] -t )*k; pT->z[1][0]=( buffer[ PEL(left+top*width) ] -t )*k; - pT->z_mean=( - pT->z[0][0]+ - pT->z[0][1]+ - pT->z[1][1]+ - pT->z[1][0])*0.25; - if(pT->is_split()) { assign_elevations((Terrain*)pT->quads[0][0], buffer, width, height, t, k, @@ -291,7 +286,7 @@ void keyboard(unsigned char key, int x, int y) glutPostRedisplay(); } -void draw_quad(Terrain *pQ, int LOD=0, double z1=0, double z2=0, double z3=0, double z4=0); +void draw_quad(Quad *pQ, int LOD=0, double z1=0, double z2=0, double z3=0, double z4=0); GLdouble cam_pos[3];//={68.0f*scale[0], 44.0f*scale[1], 47.7*scale[2]*0.5}; double cam_la[3]; @@ -328,8 +323,8 @@ void display() cam_pos[0]=dx*width*scale[0]; cam_pos[1]=dy*height*scale[1]; - cam_pos[2]=max(dz*scale[2]*width*3, (20.0f+((Terrain*)Q.get_at(dx, dy))->z_mean)*scale[2]); - //cam_pos[2]=(20.0f+((Terrain*)Q.get_at(dx, dy))->z_mean)*scale[2]; + cam_pos[2]=max(dz*scale[2]*width*3, (20.0f+((Terrain*)Q.get_at(dx, dy))->z_mean())*scale[2]); + //cam_pos[2]=(20.0f+((Terrain*)Q.get_at(dx, dy))->z_mean())*scale[2]; cam_la[0]=width*scale[0]*0.5; cam_la[1]=height*scale[1]*0.5; @@ -420,16 +415,17 @@ bool in_view(double a[3]) return false; } -void draw_quad(Terrain *pT, int LOD, double z1, double z2, double z3, double z4) +void draw_quad(Quad *pQ, int LOD, double z1, double z2, double z3, double z4) { + Terrain *pT = dynamic_cast(pQ); bool bRefine=true; bool bInFront=true; - double Q[3]={pT->x_mid*width*scale[0], pT->y_mid*height*scale[1], pT->z_mean*scale[2]}; - double Q1[3]={pT->x1*width*scale[0], pT->y1*height*scale[1], pT->z[0][0]*scale[2]}; - double Q2[3]={pT->x2*width*scale[0], pT->y1*height*scale[1], pT->z[0][1]*scale[2]}; - double Q3[3]={pT->x2*width*scale[0], pT->y2*height*scale[1], pT->z[1][1]*scale[2]}; - double Q4[3]={pT->x1*width*scale[0], pT->y2*height*scale[1], pT->z[1][0]*scale[2]}; + double Q[3] ={(*pT->x_mid())*width*scale[0], (*pT->y_mid())*height*scale[1], (pT->z_mean())*scale[2]}; + double Q1[3]={(*pT->x1())*width*scale[0], (*pT->y1())*height*scale[1], (pT->z[0][0])*scale[2]}; + double Q2[3]={(*pT->x2())*width*scale[0], (*pT->y1())*height*scale[1], (pT->z[0][1])*scale[2]}; + double Q3[3]={(*pT->x2())*width*scale[0], (*pT->y2())*height*scale[1], (pT->z[1][1])*scale[2]}; + double Q4[3]={(*pT->x1())*width*scale[0], (*pT->y2())*height*scale[1], (pT->z[1][0])*scale[2]}; double Qt[3]; double dir[3]; @@ -563,19 +559,19 @@ void draw_quad(Terrain *pT, int LOD, double z1, double z2, double z3, double z4) else glColor3f(1,1,1); //LUM(one_minus_lod_scale[0]); - glTexCoord2f(pT->x1, pT->y1); + glTexCoord2f(*pT->x1(), *pT->y1()); glVertex3dv(Q1); //LUM(one_minus_lod_scale[1]); - glTexCoord2f(pT->x2, pT->y1); + glTexCoord2f(*pT->x2(), *pT->y1()); glVertex3dv(Q2); //LUM(one_minus_lod_scale[2]); - glTexCoord2f(pT->x2, pT->y2); + glTexCoord2f(*pT->x2(), *pT->y2()); glVertex3dv(Q3); //LUM(one_minus_lod_scale[3]); - glTexCoord2f(pT->x1, pT->y2); + glTexCoord2f(*pT->x1(), *pT->y2()); glVertex3dv(Q4); } } diff --git a/samples/OpenGL/qt_terr/quad.h b/samples/OpenGL/qt_terr/quad.h index 1c6549a..36850e6 100644 --- a/samples/OpenGL/qt_terr/quad.h +++ b/samples/OpenGL/qt_terr/quad.h @@ -39,9 +39,9 @@ public: virtual void track_down(double x, double y, int levels); virtual Quad *get_at(double x, double y, int max_level=0, int level=0); -protected: virtual void set_range(double nx1, double nx2, double ny1, double ny2); +protected: Quad(std::vector*); std::vector * const V; }; diff --git a/samples/OpenGL/qt_terr/terragen.cpp b/samples/OpenGL/qt_terr/terragen.cpp index 3b922a4..73c6eaa 100644 --- a/samples/OpenGL/qt_terr/terragen.cpp +++ b/samples/OpenGL/qt_terr/terragen.cpp @@ -16,7 +16,7 @@ typedef uint8_t OCTET[8]; #define DUET_TO_NUMBER(duet) ((duet[0]|duet[1]<<8)) #define QUARTET_TO_NUMBER(quartet) ((quartet[0])|(quartet[1]<<8)|(quartet[2]<<16)|(quartet[3]<<24)) -int read_terrain(char *filename, double **pbuffer, int *width, int *height, double *scale) +int read_terrain(char const *filename, double **pbuffer, int *width, int *height, double *scale) // [in] filename: path to the file to be read in // [out] pbuffer: address of new allocated data buffer // [out] width: width of the terrain diff --git a/samples/OpenGL/qt_terr/terragen.h b/samples/OpenGL/qt_terr/terragen.h index 5037a08..a63bc34 100644 --- a/samples/OpenGL/qt_terr/terragen.h +++ b/samples/OpenGL/qt_terr/terragen.h @@ -1,6 +1,6 @@ #ifndef TERRAGEN_H #define TERRAGEN_H -int read_terrain(char *filename, double **pbuffer, int *width, int *height, double *scale); +int read_terrain(char const *filename, double **pbuffer, int *width, int *height, double *scale); -#endif/*TERRAGEN*/ \ No newline at end of file +#endif/*TERRAGEN*/ diff --git a/samples/OpenGL/qt_terr/terrain.cpp b/samples/OpenGL/qt_terr/terrain.cpp index d08d345..e18efe4 100644 --- a/samples/OpenGL/qt_terr/terrain.cpp +++ b/samples/OpenGL/qt_terr/terrain.cpp @@ -1,10 +1,5 @@ #include "terrain.h" -Terrain::Terrain() -{ - z_mean=0.0; -} - void Terrain::split() { if(is_split()) @@ -15,29 +10,27 @@ void Terrain::split() quads[1][0] = new Terrain(V); quads[1][1] = new Terrain(V); - quads[0][0]->set_range(x1, x_mid, y1, y_mid); - quads[0][1]->set_range(x_mid, x2, y1, y_mid); - quads[1][0]->set_range(x1, x_mid, y_mid, y2); - quads[1][1]->set_range(x_mid, x2, y_mid, y2); + quads[0][0]->set_range(*x1(), *x_mid(), *y1(), *y_mid()); + quads[0][1]->set_range(*x_mid(), *x2(), *y1(), *y_mid()); + quads[1][0]->set_range(*x1(), *x_mid(), *y_mid(), *y2()); + quads[1][1]->set_range(*x_mid(), *x2(), *y_mid(), *y2()); } void Terrain::track_down(double x, double y, double nz, int levels) { - if(levels>0) - { - int a=(x 0 ) { + int a = ( x < *x_mid() ) ? 0 : 1; + int b = ( y < *y_mid() ) ? 0 : 1; - if(!is_split()) + if( !is_split() ) split(); - quads[b][a]->track_down(x, y, nz, levels-1); + dynamic_cast(quads[b][a])->track_down(x, y, nz, levels-1); } - else - { + else { z[0][0]= z[0][1]= z[1][1]= - z[1][0]=z_mean=nz; + z[1][0] = z_mean_ = nz; } } diff --git a/samples/OpenGL/qt_terr/terrain.h b/samples/OpenGL/qt_terr/terrain.h index 0197380..3382a4c 100644 --- a/samples/OpenGL/qt_terr/terrain.h +++ b/samples/OpenGL/qt_terr/terrain.h @@ -4,17 +4,24 @@ #include "quad.h" #include +#include class Terrain : public Quad { public: - double * const z_mean() { - + double z_mean_; + double z_mean() { + if( isnan(z_mean_) ) { + return z_mean_ = + ( z[0][0] + z[0][1] + + z[1][0] + z[1][1] ) / 4.; + } + return z_mean_; }; double z[2][2]; public: - Terrain(); + Terrain(std::vector* V_) : Quad(V_) { z_mean_ = NAN; } virtual void split(); virtual void track_down(double x, double y, double nz, int levels); diff --git a/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c b/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c index d918f0b..3c5908b 100644 --- a/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c +++ b/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c @@ -311,16 +311,51 @@ static void createTheRenderContext() fatalError("OpenGL not supported by X server\n"); } - render_context = glXCreateNewContext(Xdisplay, fbconfig, GLX_RGBA_TYPE, 0, True); - if (!render_context) { - fatalError("Failed to create a GL context\n"); +#if USE_GLX_CREATE_CONTEXT_ATTRIB + #define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091 + #define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092 + render_context = NULL; + if( isExtensionSupported( glXQueryExtensionsString(Xdisplay, DefaultScreen(Xdisplay)), "GLX_ARB_create_context" ) ) { + typedef GLXContext (*glXCreateContextAttribsARBProc)(Display*, GLXFBConfig, GLXContext, Bool, const int*); + glXCreateContextAttribsARBProc glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc)glXGetProcAddressARB( (const GLubyte *) "glXCreateContextAttribsARB" ); + if( glXCreateContextAttribsARB ) { + int context_attribs[] = + { + GLX_CONTEXT_MAJOR_VERSION_ARB, 3, + GLX_CONTEXT_MINOR_VERSION_ARB, 0, + //GLX_CONTEXT_FLAGS_ARB , GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, + None + }; + + int (*oldHandler)(Display*, XErrorEvent*) = XSetErrorHandler(&ctxErrorHandler); + + render_context = glXCreateContextAttribsARB( Xdisplay, fbconfig, 0, True, context_attribs ); + + XSync( Xdisplay, False ); + XSetErrorHandler( oldHandler ); + + fputs("glXCreateContextAttribsARB failed", stderr); + } else { + fputs("glXCreateContextAttribsARB could not be retrieved", stderr); + } + } else { + fputs("glXCreateContextAttribsARB not supported", stderr); + } + + if(!render_context) + { +#else + { +#endif + render_context = glXCreateNewContext(Xdisplay, fbconfig, GLX_RGBA_TYPE, 0, True); + if (!render_context) { + fatalError("Failed to create a GL context\n"); + } } if (!glXMakeContextCurrent(Xdisplay, glX_window_handle, glX_window_handle, render_context)) { fatalError("glXMakeCurrent failed for window\n"); } - - glewInit(); } static int updateTheMessageQueue() -- cgit v1.3.1 From de232895ac390d09ed242deaadbccbe4812b15bd Mon Sep 17 00:00:00 2001 From: Wolfgang Draxinger Date: Fri, 26 Jul 2013 11:23:13 +0200 Subject: added glXCreateContextAttrib to x11argb_opengl_glsl example --- samples/OpenGL/x11argb_opengl_glsl/Makefile | 9 ++++- .../x11argb_opengl_glsl/x11argb_opengl_glsl.c | 39 ++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) (limited to 'samples/OpenGL/x11argb_opengl_glsl') diff --git a/samples/OpenGL/x11argb_opengl_glsl/Makefile b/samples/OpenGL/x11argb_opengl_glsl/Makefile index b59feea..62e3496 100644 --- a/samples/OpenGL/x11argb_opengl_glsl/Makefile +++ b/samples/OpenGL/x11argb_opengl_glsl/Makefile @@ -1,3 +1,10 @@ +.PHONY: all + +all: x11argb_opengl_glsl x11argb_opengl_glsl3 + x11argb_opengl_glsl: x11argb_opengl_glsl.c Makefile - clang -std=c99 -g3 -o x11argb_opengl_glsl x11argb_opengl_glsl.c -lX11 -lXrender -lGLEW -lm + cc -std=c99 -g3 -o x11argb_opengl_glsl x11argb_opengl_glsl.c -lX11 -lXrender -lGLEW -lm + +x11argb_opengl_glsl3: x11argb_opengl_glsl.c Makefile + cc -std=c99 -g3 -o x11argb_opengl_glsl3 -D USE_GLX_CREATE_CONTEXT_ATTRIB x11argb_opengl_glsl.c -lX11 -lXrender -lGLEW -lm diff --git a/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c b/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c index 3c5908b..b658f5c 100644 --- a/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c +++ b/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c @@ -130,6 +130,37 @@ static void fatalError(const char *why) exit(0x666); } +static int isExtensionSupported(const char *extList, const char *extension) +{ + + const char *start; + const char *where, *terminator; + + /* Extension names should not have spaces. */ + where = strchr(extension, ' '); + if ( where || *extension == '\0' ) + return 0; + + /* It takes a bit of care to be fool-proof about parsing the + OpenGL extensions string. Don't be fooled by sub-strings, + etc. */ + for ( start = extList; ; ) { + where = strstr( start, extension ); + + if ( !where ) + break; + + terminator = where + strlen( extension ); + + if ( where == start || *(where - 1) == ' ' ) + if ( *terminator == ' ' || *terminator == '\0' ) + return 1; + + start = terminator; + } + return 0; +} + static int Xscreen; static Atom del_atom; static Colormap cmap; @@ -304,6 +335,12 @@ static void createTheWindow() } } +static int ctxErrorHandler( Display *dpy, XErrorEvent *ev ) +{ + fputs("Error at context creation", stderr); + return 0; +} + static void createTheRenderContext() { int dummy; @@ -356,6 +393,8 @@ static void createTheRenderContext() if (!glXMakeContextCurrent(Xdisplay, glX_window_handle, glX_window_handle, render_context)) { fatalError("glXMakeCurrent failed for window\n"); } + + glewInit(); } static int updateTheMessageQueue() -- 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_glsl') 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 4542fdb44ae7525b2806233b1d499b1c0202fbf7 Mon Sep 17 00:00:00 2001 From: Wolfgang Draxinger Date: Sat, 26 Apr 2014 18:21:56 +0200 Subject: rotateable observer --- .gitmodules | 3 +++ extra/linmath.h | 1 + samples/OpenGL/frustum/frustum.c | 23 ++++++++++++++++++++-- samples/OpenGL/minimal_glsl/minimal_glsl.c | 2 +- .../x11argb_opengl_glsl/x11argb_opengl_glsl.c | 8 +------- 5 files changed, 27 insertions(+), 10 deletions(-) create mode 100644 .gitmodules create mode 160000 extra/linmath.h (limited to 'samples/OpenGL/x11argb_opengl_glsl') diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..d03f608 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "extra/linmath.h"] + path = extra/linmath.h + url = git@github.com:datenwolf/linmath.h.git diff --git a/extra/linmath.h b/extra/linmath.h new file mode 160000 index 0000000..f7418fa --- /dev/null +++ b/extra/linmath.h @@ -0,0 +1 @@ +Subproject commit f7418faa1bffa56a5754f98dcc628fcfbf8606c5 diff --git a/samples/OpenGL/frustum/frustum.c b/samples/OpenGL/frustum/frustum.c index c5732c8..2728cee 100644 --- a/samples/OpenGL/frustum/frustum.c +++ b/samples/OpenGL/frustum/frustum.c @@ -412,6 +412,23 @@ struct { float near, far; } frustum = {-1, 1, -1, 1, 1, 4}; +struct { + struct { + float phi, theta; + } rot; +} observer; + +void observer_motion(int x, int y) +{ + int const win_width = glutGet(GLUT_WINDOW_WIDTH); + int const win_height = glutGet(GLUT_WINDOW_HEIGHT); + + observer.rot.phi = -180.f + 360.f * (float)x / (float)win_width; + observer.rot.theta = -90.f + 180.f * (float)y / (float)win_height; + + glutPostRedisplay(); +} + void display_observer(float frustum_aspect) { int const win_width = glutGet(GLUT_WINDOW_WIDTH); @@ -434,8 +451,8 @@ void display_observer(float frustum_aspect) glLoadIdentity(); if(1) { glTranslatef(0, 0, -10); - glRotatef(15, 1, 0, 0); - glRotatef(-15, 0, 1, 0); + glRotatef(observer.rot.theta, 1, 0, 0); + glRotatef(observer.rot.phi, 0, 1, 0); glTranslatef(0, 0, 2.5); } else { gluLookAt(3, 1, -5, 0, 0, -2.5, 0, 1, 0); @@ -536,6 +553,8 @@ int main(int argc, char *argv[]) window_observer = glutCreateWindow("Observer"); glutDisplayFunc(display); + glutMotionFunc(observer_motion); + glutPassiveMotionFunc(observer_motion); window_view = glutCreateWindow("Frustum View"); glutDisplayFunc(display); diff --git a/samples/OpenGL/minimal_glsl/minimal_glsl.c b/samples/OpenGL/minimal_glsl/minimal_glsl.c index 2d1b0fa..72ba3ff 100644 --- a/samples/OpenGL/minimal_glsl/minimal_glsl.c +++ b/samples/OpenGL/minimal_glsl/minimal_glsl.c @@ -38,7 +38,7 @@ static const GLchar *fragment_shader_source = "uniform sampler2D texRGB;" "void main()" "{" -" gl_FragColor = -texture2D(texCMYK, gl_TexCoord[0].st) + texture2D(texRGB, gl_TexCoord[0].st);" +" gl_FragColor = -texture2D(texCMYK, gl_TexCoord[0].st) + texture2D(texRGB, gl_TexCoord[0].st); " "}\0"; GLuint shaderFragment = 0; diff --git a/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c b/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c index 0852cbc..7aae5a8 100644 --- a/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c +++ b/samples/OpenGL/x11argb_opengl_glsl/x11argb_opengl_glsl.c @@ -578,14 +578,8 @@ static void redrawTheWindow(double T) 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 ) ); + glXWaitGL(); } static double getftime(void) { -- cgit v1.3.1