# HG changeset patch # User John Tsiombikas # Date 1436295397 -10800 # Node ID c2a2069a49ec75ef4b5e530807b6802eb39b340e # Parent c3fbf9616dbdebeaff015b6eab7beeddbff77f1b slot highlighting, TODO blur diff -r c3fbf9616dbd -r c2a2069a49ec src/board.cc --- a/src/board.cc Thu Jul 02 00:01:39 2015 +0300 +++ b/src/board.cc Tue Jul 07 21:56:37 2015 +0300 @@ -85,6 +85,7 @@ Board::Board() { piece_obj = 0; + slot_sel = -1; clear(); for(int i=0; i= NUM_SLOTS ? -1 : idx; +} + +int Board::get_selected_slot() const +{ + return slot_sel; +} + void Board::draw() const { bool use_shadows = opt.shadows && sdr_shadow; @@ -232,38 +243,50 @@ piece_obj->draw(); } - // draw the slot bounds - /* - static const float pal[][3] = { + /*static const float pal[][3] = { {1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {1, 1, 0}, {0, 1, 1}, {1, 0, 1} - }; - int idx = dbg_int % NUM_SLOTS; + };*/ + int idx = slot_sel % NUM_SLOTS; if(idx >= 0) { glUseProgram(0); glPushAttrib(GL_ENABLE_BIT); glDisable(GL_LIGHTING); glDisable(GL_CULL_FACE); - glDisable(GL_DEPTH_TEST); + glEnable(GL_DEPTH_TEST); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, img_highlight.texture()); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE); + + glDepthMask(0); glBegin(GL_TRIANGLES); - glColor3fv(pal[idx % (sizeof pal / sizeof *pal)]); + //glColor3fv(pal[idx % (sizeof pal / sizeof *pal)]); + glColor4f(1, 1, 1, 0.4); + glTexCoord2f(0, 0); glVertex3f(slotbb[idx].tri0.v[0].x, slotbb[idx].tri0.v[0].y, slotbb[idx].tri0.v[0].z); + glTexCoord2f(1, 0); glVertex3f(slotbb[idx].tri0.v[1].x, slotbb[idx].tri0.v[1].y, slotbb[idx].tri0.v[1].z); + glTexCoord2f(1, 1); glVertex3f(slotbb[idx].tri0.v[2].x, slotbb[idx].tri0.v[2].y, slotbb[idx].tri0.v[2].z); + glTexCoord2f(0, 0); glVertex3f(slotbb[idx].tri1.v[0].x, slotbb[idx].tri1.v[0].y, slotbb[idx].tri1.v[0].z); + glTexCoord2f(1, 1); glVertex3f(slotbb[idx].tri1.v[1].x, slotbb[idx].tri1.v[1].y, slotbb[idx].tri1.v[1].z); + glTexCoord2f(0, 1); glVertex3f(slotbb[idx].tri1.v[2].x, slotbb[idx].tri1.v[2].y, slotbb[idx].tri1.v[2].z); glEnd(); + glDepthMask(1); + glPopAttrib(); } - */ // TODO slot highlighting } @@ -477,6 +500,21 @@ return sqrt(x * x + y * y) < rad; } +static bool field_pattern(float x, float y) +{ + y = y < 0.5 ? y * 2.0 : 2.0 - y * 2.0; + bool inside = false; + + inside |= (spike(x, y + 0.33333) && !spike(x, y + 0.4)) || + (spike(x, y + 0.5) && !spike(x, y + 0.68)); + inside |= (circle(x, y, 0.12) && !circle(x, y, 0.1)) || circle(x, y, 0.06); + inside |= (diamond(x, y) && !diamond(x, y - 0.015)) || + (diamond(x, y - 0.023) && !diamond(x, y - 0.028)); + inside |= center_circle(x, y, 0.03); + + return inside; +} + bool Board::generate_textures() { // ---- board field texture ---- @@ -492,39 +530,49 @@ for(int j=0; j 255 ? 255 : r; pptr[1] = g > 255 ? 255 : g; pptr[2] = b > 255 ? 255 : b; - pptr += 3; + pptr[3] = 255; + pptr += 4; } } img_field.texture(); + + // ---- slot highlighting texture ---- + img_highlight.create(128, 256); + pptr = img_highlight.pixels; + for(int i=0; i 255 ? 255 : r; pptr[1] = g > 255 ? 255 : g; pptr[2] = b > 255 ? 255 : b; - pptr += 3; + pptr[3] = 255; + pptr += 4; } } img_wood.texture(); @@ -576,8 +625,9 @@ pptr[0] = r > 255 ? 255 : (r < 0 ? 0 : r); pptr[1] = g > 255 ? 255 : (g < 0 ? 0 : g); pptr[2] = b > 255 ? 255 : (b < 0 ? 0 : b); + pptr[3] = 255; - pptr += 3; + pptr += 4; } } img_hinge.texture(); diff -r c3fbf9616dbd -r c2a2069a49ec src/board.h --- a/src/board.h Thu Jul 02 00:01:39 2015 +0300 +++ b/src/board.h Tue Jul 07 21:56:37 2015 +0300 @@ -41,11 +41,12 @@ Piece pieces[MAX_PIECES]; int hist[NUM_SLOTS + 1]; Quad slotbb[NUM_SLOTS]; + int slot_sel; // current slot selection std::vector obj; Object *piece_obj; - Image img_wood, img_field, img_hinge; + Image img_wood, img_field, img_hinge, img_highlight; bool generate(); bool generate_textures(); @@ -66,6 +67,8 @@ Vector3 piece_pos(int slot, int level = 0) const; int slot_hit(const Ray &ray) const; + void select_slot(int idx); + int get_selected_slot() const; void draw() const; }; diff -r c3fbf9616dbd -r c2a2069a49ec src/game.cc --- a/src/game.cc Thu Jul 02 00:01:39 2015 +0300 +++ b/src/game.cc Tue Jul 07 21:56:37 2015 +0300 @@ -19,6 +19,8 @@ bool wireframe; int dbg_int; +Ray pick_ray; + static Board board; static float cam_theta, cam_phi = 45, cam_dist = 3; @@ -289,13 +291,29 @@ if(cam_phi < -90) cam_phi = -90; if(cam_phi > 90) cam_phi = 90; - - redisplay(); } if(bnstate[2]) { cam_dist += dy * 0.1; if(cam_dist < 0.0) cam_dist = 0.0; + } - redisplay(); - } + // in all cases construct the global pick ray + double viewmat[16], projmat[16]; + int vp[4], ysz; + double px, py, pz; + + glGetIntegerv(GL_VIEWPORT, vp); + glGetDoublev(GL_MODELVIEW_MATRIX, viewmat); + glGetDoublev(GL_PROJECTION_MATRIX, projmat); + + ysz = vp[3] - vp[1]; + + gluUnProject(x, ysz - y, 0, viewmat, projmat, vp, &px, &py, &pz); + pick_ray.origin = Vector3(px, py, pz); + gluUnProject(x, ysz - y, 1, viewmat, projmat, vp, &px, &py, &pz); + pick_ray.dir = Vector3(px, py, pz) - pick_ray.origin; + + board.select_slot(board.slot_hit(pick_ray)); + + redisplay(); } diff -r c3fbf9616dbd -r c2a2069a49ec src/game.h --- a/src/game.h Thu Jul 02 00:01:39 2015 +0300 +++ b/src/game.h Tue Jul 07 21:56:37 2015 +0300 @@ -1,12 +1,16 @@ #ifndef GAME_H_ #define GAME_H_ +#include "vmath/vmath.h" + extern int win_width, win_height; extern unsigned int sdr_phong, sdr_phong_notex; extern unsigned int sdr_shadow, sdr_shadow_notex; extern unsigned int sdr_unlit; extern unsigned long cur_time; +extern Ray pick_ray; + extern bool wireframe; extern int dbg_int; diff -r c3fbf9616dbd -r c2a2069a49ec src/image.cc --- a/src/image.cc Thu Jul 02 00:01:39 2015 +0300 +++ b/src/image.cc Tue Jul 07 21:56:37 2015 +0300 @@ -38,7 +38,7 @@ destroy(); try { - unsigned char *tmp = new unsigned char[width * height * 3]; + unsigned char *tmp = new unsigned char[width * height * 4]; this->pixels = tmp; this->width = width; this->height = height; @@ -48,7 +48,7 @@ } if(pixels) { - memcpy(this->pixels, pixels, width * height * 3); + memcpy(this->pixels, pixels, width * height * 4); } return true; } @@ -69,7 +69,7 @@ bool Image::load(const char *fname) { int xsz, ysz; - unsigned char *pix = (unsigned char*)img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGB24); + unsigned char *pix = (unsigned char*)img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32); if(!pix) { return false; } @@ -96,13 +96,13 @@ if(GLEW_SGIS_generate_mipmap) { glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex_width, tex_height, 0, GL_RGB, GL_UNSIGNED_BYTE, + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_width, tex_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, width == tex_width && height == tex_height ? pixels : 0); if(width != tex_width || height != tex_height) { - glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels); + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); } } else { - gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, tex_width, tex_height, GL_RGB, GL_UNSIGNED_BYTE, pixels); + gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, tex_width, tex_height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); } if(GLEW_EXT_texture_filter_anisotropic) { @@ -138,28 +138,45 @@ void clear_image(Image *img) { - clear_image(img, 0, 0, 0); + clear_image(img, 0, 0, 0, 255); } -void clear_image(Image *img, float r, float g, float b) +void clear_image(Image *img, float r, float g, float b, float a) { if(!img->pixels) { return; } - unsigned char col[3]; + unsigned char col[4]; unsigned char *ptr = img->pixels; int npix = img->width * img->height; col[0] = (int)(r * 255.0); col[1] = (int)(g * 255.0); col[2] = (int)(b * 255.0); + col[3] = (int)(a * 255.0); for(int i=0; ipixels) { + return; + } + + unsigned char alpha = (int)(a * 255.0); + unsigned char *ptr = img->pixels; + int npix = img->width * img->height; + + for(int i=0; iwidth; int ysz = dest->height; int npixels = xsz * ysz; - int nbytes = npixels * 3; + int nbytes = npixels * 4; int tint = (int)(t * 255); if(aimg->width != xsz || bimg->width != xsz || aimg->height != ysz || bimg->height != ysz) { diff -r c3fbf9616dbd -r c2a2069a49ec src/image.h --- a/src/image.h Thu Jul 02 00:01:39 2015 +0300 +++ b/src/image.h Tue Jul 07 21:56:37 2015 +0300 @@ -30,7 +30,8 @@ }; void clear_image(Image *img); -void clear_image(Image *img, float r, float g, float b); +void clear_image(Image *img, float r, float g, float b, float a = 255); +void clear_image_alpha(Image *img, float a); enum ImgCombine { IMG_OP_ADD, diff -r c3fbf9616dbd -r c2a2069a49ec src/scenery.cc --- a/src/scenery.cc Thu Jul 02 00:01:39 2015 +0300 +++ b/src/scenery.cc Tue Jul 07 21:56:37 2015 +0300 @@ -129,7 +129,8 @@ pptr[0] = r > 255 ? 255 : (r < 0 ? 0 : r); pptr[1] = g > 255 ? 255 : (g < 0 ? 0 : g); pptr[2] = b > 255 ? 255 : (b < 0 ? 0 : b); - pptr += 3; + pptr[3] = 255; + pptr += 4; } } img_marble.texture();