void draw_text(DPlane &img, char *str, int row, int col, int value) { static DPlane characters(height, width); if(first) { char *data = header_data; for(int i=0; i<height; i++) for(int j=0; j<width; j++) { char pixel[4]; HEADER_PIXEL(data, pixel); characters[i][j] = pixel[0]; } first=0; } DPoint pt(row, col); for(int i=0; i<strlen(str); i++) { int char_no = str[i] - 33; img.set_submatrix(pt, characters.extract(DRect(0, char_no*chr_cols, chr_rows-1, (char_no+1)*chr_cols-1))); pt.col(pt.col() + chr_cols); } }
void _nds_copy_header_pixels(char *src, unsigned char *buf, unsigned char black, unsigned char white) { while (*src) { union { int rgba[4]; long value; } pixel; HEADER_PIXEL(src, pixel.rgba); *buf = (pixel.value != 0) ? white : black; buf++; } }
// Helper function to load the horizon image from the HorizonImage.h file. RGBA32BitMap loadHorizonImage(const char* data){ RGBA32BitMap bmp; bmp.imgHeight = horiz_height; bmp.imgWidth = horiz_width; bmp.memHeight = bmp.imgHeight; bmp.memWidth = bmp.imgWidth; bmp.data.reserve(bmp.memHeight*bmp.memWidth); for (unsigned int i = 0; i <= horiz_width*horiz_height; i++){ char pix[3]; HEADER_PIXEL(data,pix); unsigned int col = 0xff000000; col |= (pix[0] & 0xff) ; col |= (pix[1] & 0xff) << 8; col |= (pix[2] & 0xff) << 16; bmp.data.push_back(col); } return bmp; }
void InitX( void ) { int i; char *pSkinPixelData; char *pSkinPixelCur; char *pBadgeSkinImageData; int status; unsigned long black,white; socklen_t len; struct sockaddr_in peer; char ipstr[256]; int port; // Get peername for stdin FD len = sizeof(peer); if ( getpeername( 0, &peer, &len) == -1 ) { printf( "Couldn't get peername!\n" ); exit(-1); } // deal with both IPv4 and IPv6: #if DEBUG_PRINT_HELPER printf( "Peer IP Address: %s\n", inet_ntoa(peer.sin_addr) ); printf( "Peer Port: %d\n", ntohs(peer.sin_port) ); #endif /* if (addr.ss_family == AF_INET) { struct sockaddr_in *s = (struct sockaddr_in *)&addr; port = ntohs(s->sin_port); inet_ntop(AF_INET, &s->sin_addr, ipstr, sizeof ipstr); } else { // AF_INET6 struct sockaddr_in6 *s = (struct sockaddr_in6 *)&addr; port = ntohs(s->sin6_port); inet_ntop(AF_INET6, &s->sin6_addr, ipstr, sizeof ipstr); } */ char szDisplayHost[512]; sprintf( szDisplayHost, "%s:0", inet_ntoa(peer.sin_addr) ); g_badgeDisplay = XOpenDisplay( szDisplayHost ); if ( g_badgeDisplay == NULL ) { printf( "Couldn't open X11 display (%s)!\n", szDisplayHost ); exit(-1); } g_screen = DefaultScreen( g_badgeDisplay ); black = BlackPixel( g_badgeDisplay, g_screen ); white = WhitePixel( g_badgeDisplay, g_screen ); g_badgeWindow = XCreateSimpleWindow( g_badgeDisplay, RootWindow( g_badgeDisplay, g_screen ),0,0, g_badgeskinwidth, g_badgeskinheight, 1, black, black); XSetStandardProperties( g_badgeDisplay, g_badgeWindow, "Badger Service", "Badger",None,NULL,0,NULL); XSelectInput( g_badgeDisplay, g_badgeWindow, ExposureMask|ButtonPressMask|KeyPressMask|KeyReleaseMask ); g_GC = DefaultGC( g_badgeDisplay, g_screen );// XCreateGC( g_badgeDisplay, g_badgeWindow, 0, 0 ); /* XSetBackground( g_badgeDisplay, g_GC, white ); XSetForeground( g_badgeDisplay, g_GC, black); */ //XClearWindow( g_badgeDisplay, g_badgeWindow ); //XMapRaised( g_badgeDisplay, g_badgeWindow ); XMapWindow( g_badgeDisplay, g_badgeWindow ); // Get image data pSkinPixelData = malloc( g_badgeskinwidth*g_badgeskinheight * 4 ); i = (g_badgeskinwidth * g_badgeskinheight); pSkinPixelCur = pSkinPixelData; pBadgeSkinImageData = g_badgeskinimage; while ( i-- > 0 ) { char temp; HEADER_PIXEL( pBadgeSkinImageData, pSkinPixelCur ) temp = pSkinPixelCur[0]; pSkinPixelCur[0] = pSkinPixelCur[2]; pSkinPixelCur[2] = temp; pSkinPixelCur[3] = 0; pSkinPixelCur += 4; } g_pBadgeSkinImage = XCreateImage( g_badgeDisplay, XDefaultVisual( g_badgeDisplay, g_screen ), XDefaultDepth( g_badgeDisplay, g_screen ), ZPixmap, 0, pSkinPixelData, g_badgeskinwidth, g_badgeskinheight, 32, 0 ); if ( !g_pBadgeSkinImage ) { printf( "Failed to XCreateImage from pixel data!\n" ); exit(1); } }
test_window(): my_window(this, "Example 2 - Texture", 1024, 768, true), my_buffer(&my_window), my_shader(&my_window), my_compute(&my_window), my_camera_pos("camera_pos"), my_texture(&my_window), my_texture_pos("texture"), my_rendertraget(&my_window), my_rendertarget_texture(&my_window), run(true) { const float vertex[] = { //FRONT: -0.5, -0.5, -0.5, //0 -0.5, 0.5, -0.5, //1 0.5, -0.5, -0.5, //3 0.5, 0.5, -0.5, //2 //BACK: 0.5, -0.5, 0.5, //4 0.5, 0.5, 0.5, //5 -0.5, -0.5, 0.5, //6 -0.5, 0.5, 0.5, //7 //RIGHT: 0.5, -0.5, -0.5, //8 0.5, 0.5, -0.5, //9 0.5, -0.5, 0.5, //10 0.5, 0.5, 0.5, //11 //LEFT: -0.5, -0.5, 0.5, //12 -0.5, 0.5, 0.5, //13 -0.5, -0.5, -0.5, //14 -0.5, 0.5, -0.5, //15 //TOP: 0.5, 0.5, 0.5, //16 0.5, 0.5, -0.5, //17 -0.5, 0.5, 0.5, //18 -0.5, 0.5, -0.5, //19 //BOTTOM: 0.5, -0.5, -0.5, //20 0.5, -0.5, 0.5, //21 -0.5, -0.5, -0.5, //22 -0.5, -0.5, 0.5, //23 }; const float camera_matrix[] = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 }; //uv coordinates for texture: const float uv[] = { //FRONT: 0, 0, 0, 1, 1, 0, 1, 1, //BACK: 0, 0, 0, 1, 1, 0, 1, 1, //RIGHT: 0, 0, 0, 1, 1, 0, 1, 1, //LEFT: 0, 0, 0, 1, 1, 0, 1, 1, //TOP: 0, 0, 0, 1, 1, 0, 1, 1, //BOTTOM: 0, 0, 0, 1, 1, 0, 1, 1 }; const unsigned short index[] = { //FRONT: 0, 1, 2, 3, //BACK: 4, 5, 6, 7, //RIGHT: 8, 9,10,11, //LEFT: 12,13,14,15, //TOP: 16,17,18,19, //BOTTOM: 20,21,22,23 }; //Upload data to buffer my_buffer.upload(false, vertex, 24*3, 3); //layout(location = 0) my_buffer.upload(false, camera_matrix, 4*4, 1); //layout(binding = 1) my_buffer.upload(false, uv, 24*2, 2); //layout(location = 2) my_buffer.upload(true, index, 24*1, 1); //Simple vertex-shader: my_shader.uploadVertex("#version 400\n" "layout(location = 0) in vec3 Position;\n" "layout(location = 2) in vec2 UV;\n" "out vec3 Position_tc;\n" "out vec2 UV_tc;\n" "void main()\n" "{\n" " Position_tc = Position;\n" " UV_tc = UV;\n" "}\n"); //Simple tessellation-control-shader: my_shader.uploadTessellationControl("#version 400\n" "layout(vertices = 4) out;" "in vec3 Position_tc[];\n" "in vec2 UV_tc[];\n" "out vec3 Position_te[];\n" "out vec2 UV_te[];\n" "#define ID gl_InvocationID\n" "void main()\n" "{\n" " Position_te[ID] = Position_tc[ID];\n" " UV_te[ID] = UV_tc[ID];\n" " if (ID != 0)\n" " {\n" " gl_TessLevelInner[0] = 2;\n" //tes-level 1 == nothing " gl_TessLevelInner[1] = gl_TessLevelInner[0];\n" " gl_TessLevelOuter[0] = gl_TessLevelInner[0];\n" " gl_TessLevelOuter[1] = gl_TessLevelInner[0];\n" " gl_TessLevelOuter[2] = gl_TessLevelInner[0];\n" " gl_TessLevelOuter[3] = gl_TessLevelInner[0];\n" " }\n" "}\n"); //Simple tessellation-eval-shader: my_shader.uploadTessellationEval("#version 400\n" "layout(quads, equal_spacing) in;\n" "in vec3 Position_te[];\n" "in vec2 UV_te[];\n" "out vec3 Position_geo;\n" "out vec2 UV_geo;\n" "void main()\n" "{\n" " float u = gl_TessCoord.x, v = gl_TessCoord.y;\n" " vec3 a = mix(Position_te[0], Position_te[1], u);\n" " vec3 b = mix(Position_te[2], Position_te[3], u);\n" " Position_geo = mix(a, b, v);\n" " vec2 au = mix(UV_te[0], UV_te[1], u);\n" " vec2 bu = mix(UV_te[2], UV_te[3], u);\n" " UV_geo = mix(au, bu, v);\n" "}\n"); //Simple geometry-shader: my_shader.uploadGeometry("#version 430\n" "layout(triangles) in;\n" "layout(triangle_strip, max_vertices=3) out;\n" "in vec3 Position_geo[];\n" "in vec2 UV_geo[];\n" "out vec2 UV_fragment;\n" "out vec3 Distance_fragment;\n" "layout (binding=1) uniform result { mat4 CameraMatrix[]; }; \n" "void main()\n" "{\n" " for(int i = 0; i < 3; i++)\n" " {\n" " gl_Position = CameraMatrix[0] * vec4(Position_geo[i], 1);\n" " UV_fragment = UV_geo[i];\n" //For wireframe-mode: " if (i == 0) Distance_fragment = vec3(1,0,0);\n" " else if (i == 1) Distance_fragment = vec3(0,1,0);\n" " else Distance_fragment = vec3(0,0,1);\n" " EmitVertex();\n" " }\n" " EndPrimitive();\n" "}\n"); my_shader.uploadFragment("#version 400\n" "layout(location = 0) out vec4 FragColor;\n" "in vec2 UV_fragment;\n" "in vec3 Distance_fragment;\n" "uniform sampler2D texture;\n" "void main()\n" "{\n" " float D = clamp(min(min(Distance_fragment.x, Distance_fragment.y), Distance_fragment.z), 0.0, 1.0);\n" " if (D < 0.05) D = 0.6; else D = 1;\n" " FragColor = texture2D(texture, UV_fragment) * vec4(D,D,D,1);" "}\n"); my_shader.compile(); //Simple compute-shader: (Calculated the camera-matrix) my_compute.upload("#version 430\n" "uniform vec3 camera_pos;\n" "uniform vec3 camera_look_at_pos;\n" "layout (binding=1) buffer result { mat4 matrix[]; }; \n" "layout (local_size_x = 1) in;\n" "void main()\n" "{\n" //calculate look up at gpu " vec3 from = camera_pos;\n" " vec3 to = camera_look_at_pos;\n" " vec3 dir = normalize(to - from);\n" " vec3 up = vec3(0,1,0);\n" " vec3 right= cross(dir, up);\n" " up = cross(dir, right);\n" " mat4 look_at_matrix = mat4(vec4(right, 0),\n" " vec4(up , 0),\n" " vec4(dir , 0),\n" " vec4(-from, 1));\n" " matrix[gl_GlobalInvocationID.x] = inverse(look_at_matrix);\n" /* PROJECTION */ " float h = 1.0/tan(45*0.0087266);\n" //FOV " float zNear = 0.1;\n" " float zFar = 1000.0; \n" " float frustrumLength = zFar - zNear;\n" " mat4 ProjectionMatrix = mat4(vec4(h, 0, 0, 0),\n" " vec4(0, h, 0, 0),\n" " vec4(0, 0, -(zFar + zNear)/frustrumLength, -1),\n" " vec4(0, 0, -2.0*(zNear * zFar)/frustrumLength, 0));\n" /* PROJECTION END */ "matrix[gl_GlobalInvocationID.x] = ProjectionMatrix * matrix[gl_GlobalInvocationID.x];" "}\n"); my_compute.compile(); //Upload texture: //1. decode image (texture.h generated by gimp) char* image = new char[sample_texture::width*sample_texture::height*3]; char* data = sample_texture::header_data; for(int i = 0; i < sample_texture::width*sample_texture::height; ++i) { HEADER_PIXEL(data, (image+i*3)); } //2. upload image my_texture.upload((unsigned char*)image, sample_texture::width, sample_texture::height, 3); //Empty texture for rendertarget my_rendertarget_texture.upload((unsigned char*)0, 256, 256, 4); }
static void pinit(ModeInfo * mi) { boxedstruct *gp = &boxed[MI_SCREEN(mi)]; int wire = MI_IS_WIREFRAME (mi); ballman *bman; int i,texpixels; char *texpixeldata; char *texpixeltarget; glShadeModel(GL_SMOOTH); glClearDepth(1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* Load configuration */ setdefaultconfig(&gp->config); /* give the decay parameter a better curve */ if (gp->config.decay <= 0.8182) { gp->config.decay = gp->config.decay / 3; } else { gp->config.decay = (gp->config.decay - 0.75) * 4; } bman = &gp->bman; bman->balls = (ball *)malloc(gp->config.numballs * sizeof(ball)); bman->num_balls = gp->config.numballs; bman->ballsize = gp->config.ballsize; bman->explosion = gp->config.explosion; gp->tman = (triman *)malloc(bman->num_balls * sizeof(triman)); memset(gp->tman,0,bman->num_balls * sizeof(triman)); for(i=0;i<bman->num_balls;i++) { gp->tman[i].explosion = (float) (((int)gp->config.explosion) / 15.0f ); gp->tman[i].decay = gp->config.decay; gp->tman[i].momentum = gp->config.momentum; gp->tman[i].vertices = NULL; gp->tman[i].normals = NULL; gp->tman[i].tris = NULL; createball(&bman->balls[i]); bman->balls[i].loc.y *= rnd(); } generatesphere(gp); if (!wire) { glEnable(GL_CULL_FACE); glEnable(GL_LIGHTING); } /* define cam path */ gp->cam_x_speed = 1.0f/((float)gp->config.camspeed/50.0 + rnd()*((float)gp->config.camspeed/50.0)); gp->cam_z_speed = 1.0f/((float)gp->config.camspeed/50.0 + rnd()*((float)gp->config.camspeed/50.0)); gp->cam_y_speed = 1.0f/((float)gp->config.camspeed/250.0 + rnd()*((float)gp->config.camspeed/250.0)); if (rnd() < 0.5f) gp->cam_x_speed = -gp->cam_x_speed; if (rnd() < 0.5f) gp->cam_z_speed = -gp->cam_z_speed; /* define initial cam position */ gp->tic = gp->camtic = rnd() * 100.0f; /* define tex1 (bottom plate) */ gp->tex1 = (char *)malloc(3*width*height*sizeof(GLuint)); texpixels = 256*256; /*width*height;*/ texpixeldata = header_data; texpixeltarget = gp->tex1; for (i=0; i < texpixels; i++) { HEADER_PIXEL(texpixeldata,texpixeltarget); texpixeltarget += 3; } glPixelStorei(GL_UNPACK_ALIGNMENT, 1); clear_gl_error(); #if 0 i = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 256, 256, GL_RGB, GL_UNSIGNED_BYTE, gp->tex1); if (i) { const char *s = (char *) gluErrorString (i); fprintf (stderr, "%s: error mipmapping texture: %s\n", progname, (s ? s : "(unknown)")); exit (1); } check_gl_error("mipmapping"); #else glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, gp->tex1); check_gl_error("texture"); #endif glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); }