void HelpDisplay(GLint ww, GLint wh) { glDisable(GL_LIGHTING); glDisable(GL_TEXTURE_2D); glColor3f(1.0f, 1.0f, 1.0f); /* switch to projection mode */ glMatrixMode(GL_PROJECTION); /* save previous matrix which contains the */ /* settings for the perspective projection */ glPushMatrix(); /* reset matrix */ glLoadIdentity(); /* set a 2D orthographic projection */ gluOrtho2D(0, ww, 0, wh); /* invert the y axis, down is positive */ glScalef(1, -1, 1); /* mover the origin from the bottom left corner */ /* to the upper left corner */ glTranslatef(0, -wh, 0); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); linestart = 10; HelpRenderBitmapString(30, linestart += linespace, Help_Font, "Help Menu"); HelpRenderBitmapString(30, linestart += linespace, Help_Font, "---------"); HelpRenderBitmapString(30, linestart += linespace, Help_Font, "H/h = Toggle Help Menu"); if (!full_screen) HelpRenderBitmapString(30, linestart += linespace, Help_Font, "TAB = Activate Full Screen"); HelpRenderBitmapString(30, linestart += linespace, Help_Font, "Esc = Exits Program"); HelpRenderBitmapString(30, linestart += linespace, Help_Font, "R/r = Reset Position"); HelpRenderBitmapString(30, linestart += linespace, Help_Font, "C/c = Toggle Axis"); HelpRenderBitmapString(30, linestart += linespace, Help_Font, "W/w = Toggle Wireframe"); HelpRenderBitmapString(30, linestart += linespace, Help_Font, "D/d = Toggle Double Sided Polygons"); HelpRenderBitmapString(30, linestart += linespace, Help_Font, "S/s = Toggle Smooth Shading"); HelpRenderBitmapString(30, linestart += linespace, Help_Font, "M/m = Toggle Materials"); HelpRenderBitmapString(30, linestart += linespace, Help_Font, "T/t = Toggle Textures"); HelpRenderBitmapString(30, linestart += linespace, Help_Font, "B/b = Toggle Auto Rotate"); /* HelpRenderBitmapString(30,linestart+=linespace,(void *)Help_Font, "L/l = Toggle Line Smoothing"); */ #ifdef AVL HelpRenderBitmapString(30, linestart += linespace, Help_Font, "F/f = Flip Textures"); #endif if (stereo) { HelpRenderBitmapString(30, linestart += linespace, Help_Font, "Stereo Variables"); HelpRenderBitmapString(30, linestart += linespace, Help_Font, "----------------"); HelpRenderBitmapString(30, linestart += linespace, Help_Font, "</, = Decrease Eye Separation"); HelpRenderBitmapString(30, linestart += linespace, Help_Font, ">/. = Increase Eye Separation"); HelpRenderBitmapString(30, linestart += linespace, Help_Font, "+/- = Increase/Decrease Focus Distance"); } glPopMatrix(); /* set the current matrix to GL_PROJECTION */ glMatrixMode(GL_PROJECTION); /* restore previous settings */ glPopMatrix(); /* get back to GL_MODELVIEW matrix */ glMatrixMode(GL_MODELVIEW); if (lighting) glEnable(GL_LIGHTING); glEnable(GL_TEXTURE_2D); }
void TransIcelandicExpress::redraw( void ) { static tex_init = 0; if (!tex_init) { ilutRenderer( ILUT_OPENGL ); ilGenImages( 1, &ImageName ); ilBindImage( ImageName ); glEnable( GL_TEXTURE_2D ); glEnable( GL_BLEND ); glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); if (!ilLoadImage( "ludum48.png" )) { printf("Loading image failed\n"); } gl_tex_id = ilutGLBindTexImage(); ilutGLTexImage( 0 ); tex_init = 1; } glBindTexture (GL_TEXTURE_2D, gl_tex_id ); glClearColor( 0.3f, 0.4f, 0.6f, 1.0f ); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); //glEnable( GL_CULL_FACE ); glEnable( GL_DEPTH_TEST ); // 3d part glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 60.0, 800.0/600.0, 0.05, 100.0 ); gluLookAt( player->pos[0]+cameraPos[0], player->pos[1]+cameraPos[1]+c_PlayerHeight, player->pos[2]+cameraPos[2], player->pos[0], player->pos[1]+ c_PlayerHeight, player->pos[2], 0.0, 1.0, 0.0 ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); // apply some of the camera xform to the light just to make // it more shiny glPushMatrix(); float viewHead = atan2( cameraPos[2], cameraPos[0] ); glRotated( viewHead * SG_RADIANS_TO_DEGREES, 0.0f, 1.0f, 0.0f ); setupLights(); glPopMatrix(); draw3d(); // 2d part glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluOrtho2D( 0, 800, 0, 600 ) ; glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); glDisable( GL_LIGHTING ); draw2d(); SDL_GL_SwapBuffers(); }
// The constructor creates a texture of the track data, such that the track // layout can be displayed efficiently. Additional data gets initialized. cGrTrackMap::cGrTrackMap() { // Hardcoded initial view mode, available per instance. viewmode = TRACK_MAP_NORMAL_WITH_OPPONENTS; // For all views we need the same track texture, so create it just once. if (isinitalized) { return; } else { // Initialize colors for the various car "dots". initColors(); tTrack *track = grTrack; tTrackSeg* first = track->seg; tTrackSeg* seg = first; // Search the maximum/minimum x/y values of the track (axis aligned bounding box), // to compute later the texture parameters and to be able to place the cars (dots) // correct. The impementation is inefficient, but it's just executed one time per // race, so it doesn't matter. track_min_x = FLT_MAX; track_max_x = -FLT_MAX; track_min_y = FLT_MAX; track_max_y = -FLT_MAX; do { // We analyse the straight and turns different, because (read on) if (seg->type == TR_STR) { // Straights are trivial, because the corners are sufficient to create a // bounding box. // TODO: If CCW/CW is known, you just need to check one side (the outside). // TODO: More effiecient checking. checkAndSetMinimum(track_min_x, seg->vertex[TR_SL].x); checkAndSetMinimum(track_min_x, seg->vertex[TR_SR].x); checkAndSetMinimum(track_min_y, seg->vertex[TR_SL].y); checkAndSetMinimum(track_min_y, seg->vertex[TR_SR].y); checkAndSetMaximum(track_max_x, seg->vertex[TR_SL].x); checkAndSetMaximum(track_max_x, seg->vertex[TR_SR].x); checkAndSetMaximum(track_max_y, seg->vertex[TR_SL].y); checkAndSetMaximum(track_max_y, seg->vertex[TR_SR].y); } else { // Turns are not that easy, think of a definition of a circle or an arc. If you // just consider the corners the bounding box might be any order too small // or too big. // To avoid that we create intermediate steps, such that we get a defined // accuracy (e.g. 5 meters). float curseglen = 0.0; float dphi = RESOLUTION / seg->radius; double xc = seg->center.x; double yc = seg->center.y; dphi = (seg->type == TR_LFT) ? dphi : -dphi; float phi = 0.0; while (curseglen < seg->length) { float cs = cos(phi), ss = sin(phi); // TODO: If CCW/CW is known, you just need to check one side (the outside). float lx, ly, rx, ry; lx = seg->vertex[TR_SL].x * cs - seg->vertex[TR_SL].y * ss - xc * cs + yc * ss + xc; ly = seg->vertex[TR_SL].x * ss + seg->vertex[TR_SL].y * cs - xc * ss - yc * cs + yc; rx = seg->vertex[TR_SR].x * cs - seg->vertex[TR_SR].y * ss - xc * cs + yc * ss + xc; ry = seg->vertex[TR_SR].x * ss + seg->vertex[TR_SR].y * cs - xc * ss - yc * cs + yc; // TODO: More effiecient checking. checkAndSetMinimum(track_min_x, lx); checkAndSetMinimum(track_min_x, rx); checkAndSetMinimum(track_min_y, ly); checkAndSetMinimum(track_min_y, ry); checkAndSetMaximum(track_max_x, lx); checkAndSetMaximum(track_max_x, rx); checkAndSetMaximum(track_max_y, ly); checkAndSetMaximum(track_max_y, ry); curseglen += RESOLUTION; phi += dphi; } } seg = seg->next; } while (seg != first); // Compute the maximum possible texture size possible to create the track texture. // TODO: use pbuffer if available or subdivide and render/readback in multiple passes. int texturesize = 1; int maxtexturesize = MIN(grWinw, grWinh); while (texturesize <= maxtexturesize) { texturesize <<= 1; } texturesize >>= 1; // Get maximum OpenGL texture size and reduce texturesize if necessary. int maxOpenGLtexturesize; glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxOpenGLtexturesize); if (texturesize > maxOpenGLtexturesize) { texturesize = maxOpenGLtexturesize; } // Compute an estimate of the overall width and height of the track in [m]. track_width = track_max_x - track_min_x; track_height = track_max_y - track_min_y; // Compute the final line width to draw the track. float linewidth = MIN(MAXLINEWIDTH, MINLINEWIDTH*4000.0/MAX(track_width, track_height)); linewidth = linewidth*(texturesize/512.0); // Compute a first estimate of the pixel to distance ratio. ratio = texturesize/MAX(track_width, track_height); // Compute final minimum/maximum values. track_max_x = track_max_x + RESOLUTION + linewidth/ratio; track_max_y = track_max_y + RESOLUTION + linewidth/ratio; track_min_x = track_min_x - RESOLUTION - linewidth/ratio; track_min_y = track_min_y - RESOLUTION - linewidth/ratio; // Compute final ratio, width and height. track_width = track_max_x - track_min_x; track_height = track_max_y - track_min_y; ratio = texturesize/MAX(track_width, track_height); // Compute the ratios if (track_width >= track_height) { track_x_ratio = 1.0; track_y_ratio = track_height/track_width; } else { track_y_ratio = 1.0; track_x_ratio = track_width/track_height; } isinitalized = true; // Now we are ready to render the track into the framebuffer (backbuffer). // Clear the framebuffer (backbuffer), make it "transparent" (alpha = 0.0). glFinish(); glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); // Set transformations and projection such that one pixel is one unit. glViewport (grWinx, grWiny, grWinw, grWinh); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, grWinw, 0.0, grWinh); glMatrixMode(GL_MODELVIEW); // Now draw the track as quad strip. The reason for that is that drawing with // glEnable(GL_LINE_SMOOTH) and glHint(GL_LINE_SMOOTH_HINT, GL_NICEST) caused problems // with the alpha channel. float halflinewidth = linewidth/2.0; bool firstvert = true; float xf1, yf1 , xf2, yf2; xf1 = yf1 = xf2 = yf2 = 0.0; glBegin(GL_QUAD_STRIP); seg = first; do { if (seg->type == TR_STR) { // Draw a straight. // Compute global coordinate on track middle. float xm = (seg->vertex[TR_SL].x + seg->vertex[TR_SR].x) / 2.0; float ym = (seg->vertex[TR_SL].y + seg->vertex[TR_SR].y) / 2.0; xm = (xm - track_min_x)*ratio; ym = (ym - track_min_y)*ratio; // Compute normal to the track middle. float xn = seg->vertex[TR_SL].x - seg->vertex[TR_SR].x; float yn = seg->vertex[TR_SL].y - seg->vertex[TR_SR].y; float length = sqrt(xn*xn+yn*yn); xn /= length; yn /= length; // Compute the new points. float x1 = (xm - xn*halflinewidth); float y1 = (ym - yn*halflinewidth); float x2 = (xm + xn*halflinewidth); float y2 = (ym + yn*halflinewidth); if (firstvert) { firstvert = false; xf1 = x1; yf1 = y1; xf2 = x2; yf2 = y2; } glVertex2f(x1, y1); glVertex2f(x2, y2); } else { // To draw the turns correct we subdivide them again, like above. float curseglen = 0.0; float dphi = RESOLUTION / seg->radius; double xc = seg->center.x; double yc = seg->center.y; dphi = (seg->type == TR_LFT) ? dphi : -dphi; float mx = (seg->vertex[TR_SL].x + seg->vertex[TR_SR].x) / 2.0; float my = (seg->vertex[TR_SL].y + seg->vertex[TR_SR].y) / 2.0; float phi = 0.0; while (curseglen < seg->length) { float cs = cos(phi), ss = sin(phi); float x = mx * cs - my * ss - xc * cs + yc * ss + xc; float y = mx * ss + my * cs - xc * ss - yc * cs + yc; float xn = x - xc; float yn = y - yc; float length = sqrt(xn*xn+yn*yn); xn /= length; yn /= length; x = (x - track_min_x)*ratio; y = (y - track_min_y)*ratio; float x1 = (x + xn*halflinewidth); float y1 = (y + yn*halflinewidth); float x2 = (x - xn*halflinewidth); float y2 = (y - yn*halflinewidth); if (seg->type == TR_LFT) { glVertex2f(x1, y1); glVertex2f(x2, y2); if (firstvert) { firstvert = false; xf1 = x1; yf1 = y1; xf2 = x2; yf2 = y2; } } else { glVertex2f(x2, y2); glVertex2f(x1, y1); if (firstvert) { firstvert = false; xf1 = x2; yf1 = y2; xf2 = x1; yf2 = y1; } } curseglen += RESOLUTION; phi += dphi; } } seg = seg->next; } while (seg != first); if (!firstvert) { glVertex2f(xf1, yf1); glVertex2f(xf2, yf2); } glEnd(); // Read track picture into memory to be able to generate mipmaps. I read back an RGBA // image because I don't know yet what people want to add to the map, so RGBA is most // flexible to add things like start line, elevation coloring etc. // Do not use GL_ALPHA or GL_LUMINANCE to save memory, somehow this leads to a // performance penalty (at least on NVidia cards). GLuint *trackImage = (GLuint*) malloc(texturesize*texturesize*sizeof(GLuint)); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_PACK_ALIGNMENT, 1); glReadBuffer(GL_BACK); glReadPixels(0, 0, texturesize, texturesize, GL_RGBA, GL_BYTE, trackImage); // Check if the color buffer has alpha, if not fix the texture. Black gets // replaced by transparent black, so don't draw black in the texture, you // won't see anything. if (glutGet(GLUT_WINDOW_ALPHA_SIZE) == 0) { // There is no alpha, so we fix it manually. Because we added a little border // around the track map the first pixel should always contain the // clearcolor. GLuint clearcolor = trackImage[0]; int i; for (i = 0; i < texturesize*texturesize; i++) { if (trackImage[i] == clearcolor) { // Assumption: transparent black is 0, portable? trackImage[i] = 0; } } } // Finally copy the created image of the track from the framebuffer into the texture. glGenTextures (1, &mapTexture); glBindTexture(GL_TEXTURE_2D, mapTexture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); // If GL_ARB_texture_compression is available at runtime, (try to) compress the // texture. This is done with the specification of the internal format to // GL_COMPRESSED_RGBA_ARB. if (isCompressARBAvailable()) { // This texture contains mostly the clear color value and should therefore // compress well even with high quality. glHint(GL_TEXTURE_COMPRESSION_HINT_ARB, GL_NICEST); gluBuild2DMipmaps(GL_TEXTURE_2D, GL_COMPRESSED_RGBA_ARB, texturesize, texturesize, GL_RGBA, GL_BYTE, trackImage); // The following commented code is just for testing purposes. /*int compressed; glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_ARB, &compressed); if (compressed == GL_TRUE) { int csize; printf("compression succesful!\n"); glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB, &csize); printf("compression ratio: %d to %d\n", csize, texturesize*texturesize*sizeof(GLuint)); }*/ } else { // GL_ARB_texture_compression not available at runtime, fallback. gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, texturesize, texturesize, GL_RGBA, GL_BYTE, trackImage); } // Free the memory of the initial texture. free(trackImage); // Init the position and size of the map in the window. map_x = -10; map_y = -40; map_size = 170; // Restore some state. glPopMatrix(); // Compile the car "dot" display list. cardot = glGenLists(1); if (cardot != 0) { glNewList(cardot, GL_COMPILE); glBegin(GL_TRIANGLE_FAN); // The center. glVertex2f(0.0, 0.0); // The border. int i; const int borderpoints = 8; halflinewidth = halflinewidth*float(map_size)/float(texturesize); for (i = 0; i < borderpoints + 1; i++) { float phi = 2.0*PI*float(i)/float(borderpoints); glVertex2f(halflinewidth*cos(phi), halflinewidth*sin(phi)); } glEnd(); glEndList(); } // Clear the screen, that in case of a delay the track is not visible. glClear(GL_COLOR_BUFFER_BIT); } }
int Splat_Prepare(SDL_Window *userWindow, int userViewportWidth, int userViewportHeight) { int width, height; window = userWindow; viewportWidth = userViewportWidth; viewportHeight = userViewportHeight; SDL_GetWindowSize(userWindow, &width, &height); SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 4); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 4); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 4); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 4); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); window_glcontext = SDL_GL_CreateContext(window); if (!window_glcontext) { Splat_SetError("OpenGL context creation failed. Check glGetError() and/or SDL_GetError() for more information."); Splat_Finish(); return -1; } // Our shading model--Flat glShadeModel(GL_FLAT); // Default the clear color to black. glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Setup our viewport. glViewport(0, 0, viewportWidth, viewportHeight); // Change to the projection matrix and set up our ortho view glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, width, 0, height); // Set up modelview for 2D integer coordinates glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.375f, height + 0.375f, 0.0f); glScalef(1.0f, -1.0f, 0.001f); // Make the positive Z-axis point "out" from the view (e.g images at depth 4 will be higher than those at depth 0), and swap the Y axis /* Deactivate the system cursor */ SDL_ShowCursor(SDL_DISABLE); glDisable(GL_DITHER); /* Create the frame buffer for rendering to texture*/ glGenFramebuffers(1, &framebuffer); glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); /* Set up the texture to which we're going to render */ glGenTextures(1, &frameTexture); glBindTexture(GL_TEXTURE_2D, frameTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, viewportWidth, viewportHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); /* Configure the framebuffer texture */ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, frameTexture, 0); GLenum DrawBuffers[1] = { GL_COLOR_ATTACHMENT0 }; glDrawBuffers(1, DrawBuffers); GLenum err = glGetError(); if (err != GL_NO_ERROR) { Splat_SetError("OpenGL error occurred during initialization"); Splat_Finish(); return -1; } return 0; }
virtual void redraw( void ) { static bool first=true; static RenderNode rn; int i; double tbalance; GeoLoadManager::ResultT region; if (_internalRoot == NullFC) { initialize(); showAll(); } _cart->getSFMatrix()->setValue(_navigator.getMatrix()); updateHighlight(); _win->activate(); _win->frameInit(); if(first) { loadManager=new GeoLoadManager(useFaceDistribution); first=false; rn.determinePerformance(_win); } if(_win->getPort().size() < (serverCount+1)) { ViewportPtr vp,ovp=_win->getPort(0); addRefCP(ovp); addRefCP(ovp); TileCameraDecoratorPtr deco; for(int i=_win->getPort().size()-1;i<serverCount;i++) { cout << "Add new" << endl; loadManager->addRenderNode(rn,i); if(simulateRendering) { beginEditCP(_win); deco=TileCameraDecorator::create(); beginEditCP(deco); deco->setFullWidth ( _win->getWidth() ); deco->setFullHeight( _win->getHeight() ); deco->setDecoratee( ovp->getCamera() ); vp=Viewport::create(); beginEditCP(vp); vp->setRoot ( ovp->getRoot() ); /* SkyBackgroundPtr sky = SkyBackground::create(); beginEditCP(sky); sky->setSphereRes(16); sky->getMFSkyColor()->addValue(Color3f(0, 0, .2)); sky->getMFSkyAngle()->addValue(Pi / 2); sky->getMFSkyColor()->addValue(Color3f(.6, .6, 1)); sky->getMFGroundColor()->addValue(Color3f(0, .3, 1)); sky->getMFGroundAngle()->addValue(Pi / 2); sky->getMFGroundColor()->addValue(Color3f(1, .3, 0)); endEditCP(sky); vp->setBackground( sky ); */ vp->setBackground( ovp->getBackground() ); vp->getMFForegrounds()->setValues( ovp->getForegrounds() ); vp->setCamera(deco); _win->addPort(vp); endEditCP(_win); endEditCP(vp); endEditCP(deco); } } } tbalance = -getSystemTime(); loadManager->update(_win->getPort()[0]->getRoot()); loadManager->balance(_win->getPort()[0],false,region); tbalance += getSystemTime(); if(simulateRendering) { ViewportPtr vp; TileCameraDecoratorPtr deco; for(i=0;i<region.size();i+=4) { #if 1 cout << "Region: " << i << " "; cout << region[i+0] << " "; cout << region[i+1] << " "; cout << region[i+2] << " "; cout << region[i+3] << endl; if(region[i+0] >= region[i+2]) { cout << "!!!" << endl; region[i+2]++; } if(region[i+1] >= region[i+3]) { cout << "!!!" << endl; region[i+3]++; } #endif vp=_win->getPort()[i/4+1]; deco=TileCameraDecoratorPtr::dcast(vp->getCamera()); beginEditCP(deco); beginEditCP(vp); vp->setSize(region[i+0], region[i+1], region[i+2], region[i+3]); deco->setSize(region[i+0]/(float)_win->getWidth(), region[i+1]/(float)_win->getHeight(), region[i+2]/(float)_win->getWidth(), region[i+3]/(float)_win->getHeight()); endEditCP(deco); endEditCP(vp); } } Time t,tmin,tmax; for(i=0;i<_win->getPort().size();++i) { t=-getSystemTime(); _action->setWindow( _win.getCPtr() ); _win->getPort(i)->render( _action ); glFlush(); t+=getSystemTime(); if(i==0) continue; if(i==1) { tmin=tmax=t; } else { if(t<tmin) tmin=t; if(t>tmax) tmax=t; } } if(!cache) printf("speed %5d %10.6f %10.6f %10.6f\n",_win->getPort().size()-1, tmin, tmax, tbalance); glPushAttrib(GL_ALL_ATTRIB_BITS); glDisable(GL_SCISSOR_TEST); glViewport(0,0, _win->getWidth(), _win->getHeight()); if(viewVolume) loadManager->drawVolumes(_win); glPushMatrix(); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluOrtho2D(0,_win->getWidth(), 0,_win->getHeight()); glDisable(GL_DEPTH_TEST); glEnable(GL_COLOR_MATERIAL); for(i=0;i<region.size();i+=4) { #if 0 cout << "Region: "; cout << region[i+0] << " "; cout << region[i+1] << " "; cout << region[i+2] << " "; cout << region[i+3] << endl; #endif glBegin(GL_LINE_LOOP); glColor3f(1, 1, 0); glVertex3f(region[i+0],region[i+1],0); glVertex3f(region[i+2],region[i+1],0); glVertex3f(region[i+2],region[i+3],0); glVertex3f(region[i+0],region[i+3],0); glEnd(); } glDisable(GL_COLOR_MATERIAL); glEnable(GL_DEPTH_TEST); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); glPopAttrib(); if(doSave) { Int32 w,h; w=_win->getPort(0)->getPixelWidth(); h=_win->getPort(0)->getPixelHeight(); Image image(Image::OSG_RGB_PF, w,h,1, 1,1,0.0, NULL,true); ImageFileType *imgTransType=ImageFileHandler::the().getFileType("JPEG"); char filename[256]; if(imgTransType==NULL) { cerr << "Unknown image trans type" << endl; return; } sprintf(filename,"%s_%d.jpg",dumpImage,dumpImageNr++); // read buffer data into image glPixelStorei(GL_PACK_ALIGNMENT,1); glReadPixels(0,0,w,h, GL_RGB,GL_UNSIGNED_BYTE, image.getData()); imgTransType->write(image,filename); } _win->swap(); _win->frameExit(); }
void display( void ) { int x[7], y[7]; /* * Clear window - glClear clears all buffers whose bits are set in mask * formed by logical OR of values defined in gl.h. GL_COLOR_BUFFER_BIT * refers to color buffer. */ glClear( GL_COLOR_BUFFER_BIT ); /* * Set the viewWindow */ glMatrixMode( GL_PROJECTION ); glLoadIdentity( ); gluOrtho2D( 0, 300, 0, 300 ); /* * Set the viewport to match the viewWindow */ glViewport( 0, 0, 299, 299 ); /* * Use Student's drawPolygon */ x[0] = 10; y[0] = 10; x[1] = 20; y[1] = 10; x[2] = 20; y[2] = 20; x[3] = 10; y[3] = 20; /* * LL => Lower Left Start, CCW => Vertices entered in * counter clockwise progression */ drawPolygon( 4, x, y ); /* Square, LL, CCW */ x[0] = 40; y[0] = 30; x[1] = 40; y[1] = 50; x[2] = 30; y[2] = 50; x[3] = 30; y[3] = 30; drawPolygon( 4, x, y ); /* Rectangle, LR, CCW */ x[0] = 40; y[0] = 90; x[1] = 40; y[1] = 70; x[2] = 10; y[2] = 70; x[3] = 10; y[3] = 90; drawPolygon( 4, x, y ); /* Rectangle, UR, CW */ x[0] = 10; y[0] = 230; x[1] = 40; y[1] = 230; x[2] = 40; y[2] = 210; x[3] = 10; y[3] = 210; drawPolygon( 4, x, y ); /* Rectangle, UL, CW */ x[0] = 100; y[0] = 10; x[1] = 150; y[1] = 10; x[2] = 125; y[2] = 20; drawPolygon( 3, x, y ); /* Isosceles, flat bottom */ x[0] = 100; y[0] = 30; x[1] = 140; y[1] = 50; x[2] = 175; y[2] = 50; drawPolygon( 3, x, y ); /* flat top - tail to left */ x[0] = 120; y[0] = 40; x[1] = 80; y[1] = 60; x[2] = 45; y[2] = 60; drawPolygon( 3, x, y ); /* flat top - tail to right */ x[0] = 10; y[0] = 100; x[1] = 10; y[1] = 120; x[2] = 25; y[2] = 100; drawPolygon( 3, x, y ); /* Right */ x[0] = 10; y[0] = 130; x[1] = 20; y[1] = 130; x[2] = 20; y[2] = 140; drawPolygon( 3, x, y ); /* Right */ x[0] = 10; y[0] = 170; x[1] = 20; y[1] = 170; x[2] = 10; y[2] = 150; drawPolygon( 3, x, y ); /* Right */ x[0] = 100; y[0] = 70; x[1] = 150; y[1] = 70; x[2] = 75; y[2] = 90; drawPolygon( 3, x, y ); /* flat bottom - top left */ x[0] = 100; y[0] = 100; x[1] = 150; y[1] = 100; x[2] = 195; y[2] = 120; drawPolygon( 3, x, y ); /* flat bottom - top right */ x[0] = 100; y[0] = 170; x[1] = 150; y[1] = 150; x[2] = 175; y[2] = 130; drawPolygon( 3, x, y ); /* scalene */ x[0] = 200; y[0] = 50; x[1] = 225; y[1] = 90; x[2] = 250; y[2] = 50; x[3] = 225; y[3] = 10; drawPolygon ( 4, x, y ); /* diamond */ x[0] = 200; y[0] = 125; x[1] = 210; y[1] = 150; x[2] = 260; y[2] = 145; x[3] = 275; y[3] = 120; x[4] = 250; y[4] = 100; x[5] = 225; y[5] = 100; drawPolygon ( 6, x, y ); /* hexagon */ x[0] = 215; y[0] = 225; x[1] = 200; y[1] = 250; x[2] = 215; y[2] = 250; x[3] = 225; y[3] = 275; x[4] = 235; y[4] = 250; x[5] = 250; y[5] = 250; x[6] = 235; y[6] = 225; drawPolygon ( 7, x, y ); /* star top */ /* * Flushs OpenGL commands to display */ glFlush(); }
//<<<<<<<<<<<<<<<<<<<<<<< myInit >>>>>>>>>>>>>>>>>>>> void myInit(void) { img = cv::imread("1.png"); if(img.empty()) { printf("Error imread.\n"); return ; } //cv::cvtColor(img, img, CV_BGR2RGB); glGenTextures(2, texId); CheckGlErrors("check glGenTextures"); glBindTexture(GL_TEXTURE_2D, texId[0]); CheckGlErrors("check glBindTextures"); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.cols, img.rows, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); CheckGlErrors("check glTexImage2D"); #ifdef FBO img1 = cv::Mat(screenHeight, screenWidth, CV_8UC3); if(img1.empty()) { printf("Error create img1.\n"); return ; } glBindTexture(GL_TEXTURE_2D, texId[1]); CheckGlErrors("check glBindTextures"); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img1.cols, img1.rows, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); glGenFramebuffers(1, &fb); glBindFramebuffer(GL_FRAMEBUFFER, fb); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texId[1], 0); #endif GLint nModelViewMatrixCount = 0; GLint nProjectionMatrixCount = 0; glGetIntegerv(GL_MAX_MODELVIEW_STACK_DEPTH, &nModelViewMatrixCount);// 32 glGetIntegerv(GL_MAX_PROJECTION_STACK_DEPTH, &nProjectionMatrixCount); // 4 printf("before, modelview matrix count %d, projection matrix count %d\n", nModelViewMatrixCount, nProjectionMatrixCount); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 1, 0.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glViewport(0, 0, screenWidth, screenHeight); //glRotatef(90, 0, 0, -1); glGetIntegerv(GL_MAX_MODELVIEW_STACK_DEPTH, &nModelViewMatrixCount);// 32 glGetIntegerv(GL_MAX_PROJECTION_STACK_DEPTH, &nProjectionMatrixCount); // 4 printf("after, modelview matrix count %d, projection matrix count %d\n", nModelViewMatrixCount, nProjectionMatrixCount); float mv_matrix[16]; float projection_matrix[16]; glGetFloatv(GL_MODELVIEW_MATRIX, mv_matrix); glGetFloatv(GL_PROJECTION_MATRIX, projection_matrix); printf("model view matrix\n"); for(int i=0;i<16;++i) printf("%f ", mv_matrix[i]); printf("\n"); printf("projection matrix\n"); for(int i=0;i<16;++i) printf("%f ", projection_matrix[i]); printf("\n"); CheckGlErrors("myInit()"); }
void render(date renderDate) { assert(trailElements != NULL); Shader::Program::unUse(); Texture::Unit::unUse(); VBO::Element::unUse(); FBO::list[FBO::trails].use(); GLenum buffers[] = { GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT }; oglDrawBuffers(1, buffers); #if DEBUG_TRAILS glClearColor(0.1f, 0.1f, 0.1f, 1.f); #else glClearColor(0, 0, 0, 1.f); #endif // DEBUG_TRAILS glClear(GL_COLOR_BUFFER_BIT); glDisable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE); glDisable( GL_LIGHTING ); glDisable(GL_DEPTH_TEST); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0., 1., 0., 1.); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /* { int i = 0; while (i < trailElements->size) if ((*trailElements)[i].birth + 500 < renderDate) { trailElements->remove(i); } else { ++i; } } */ #if DEBUG_TRAILS // Fond pour debugger glColor4f(0.4f, 0.4f, 0.4f, 0.4f); glBegin(GL_QUADS); glVertex2f(0.05f, 0.05f); glVertex2f(0.05f, 0.95f); glVertex2f(0.95f, 0.95f); glVertex2f(0.95f, 0.05f); glEnd(); #endif // DEBUG_TRAILS glEnableClientState(GL_VERTEX_ARRAY); Array<trailDesc> & list = (*trailElements); for (int i = 0; i < list.size; ++i) { const trailDesc & element = list[i]; if (element.birth + 500 >= renderDate && element.birth <= renderDate) { renderElement(element, 0.002f * (renderDate - element.birth)); } } glDisableClientState(GL_VERTEX_ARRAY); }
void CMiniMap::DrawForReal(bool use_geo) { SCOPED_TIMER("MiniMap::DrawForReal"); //glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glPushAttrib(GL_DEPTH_BUFFER_BIT); glDisable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glDepthMask(GL_FALSE); glDisable(GL_TEXTURE_2D); glMatrixMode(GL_MODELVIEW); if (minimized) { if (!slaveDrawMode) { DrawMinimizedButton(); } glPopAttrib(); glEnable(GL_TEXTURE_2D); return; } // draw the frameborder if (!slaveDrawMode && !globalRendering->dualScreenMode && !maximized) { glEnable(GL_BLEND); DrawFrame(); glDisable(GL_BLEND); } bool resetTextureMatrix = false; if (use_geo) { glPushMatrix(); // switch to normalized minimap coords if (globalRendering->dualScreenMode) { glViewport(xpos, ypos, width, height); glScalef(width * globalRendering->pixelX, height * globalRendering->pixelY, 1.0f); } else { glTranslatef(xpos * globalRendering->pixelX, ypos * globalRendering->pixelY, 0.0f); glScalef(width * globalRendering->pixelX, height * globalRendering->pixelY, 1.0f); } /* FIXME: fix mouse handling too and make it fully customizable, so Lua can rotate the minimap to any angle CCameraController* camController = &camHandler->GetCurrentController(); COverheadController* taCam = dynamic_cast<COverheadController*>(camController); SmoothController* smCam = dynamic_cast<SmoothController*>(camController); if ((taCam && taCam->flipped) || (smCam && smCam->flipped)) { glTranslatef(1.0f, 1.0f, 0.0f); glScalef(-1.0f, -1.0f, 1.0f); glMatrixMode(GL_TEXTURE); glPushMatrix(); glTranslatef(1.0f, 1.0f, 0.0f); glScalef(-1.0f, -1.0f, 1.0f); glMatrixMode(GL_MODELVIEW); resetTextureMatrix = true; }*/ } setSurfaceCircleFunc(DrawSurfaceCircle); setSurfaceSquareFunc(DrawSurfaceSquare); cursorIcons.Enable(false); glColor4f(0.6f, 0.6f, 0.6f, 1.0f); // don't mirror the map texture with flipped cameras glMatrixMode(GL_TEXTURE); glPushMatrix(); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); // draw the map glDisable(GL_BLEND); readmap->DrawMinimap(); glEnable(GL_BLEND); glMatrixMode(GL_TEXTURE); glPopMatrix(); glMatrixMode(GL_MODELVIEW); // clip everything outside of the minimap box { const double plane0[4] = {0,-1,0,1}; const double plane1[4] = {0,1,0,0}; const double plane2[4] = {-1,0,0,1}; const double plane3[4] = {1,0,0,0}; glClipPlane(GL_CLIP_PLANE0, plane0); // clip bottom glClipPlane(GL_CLIP_PLANE1, plane1); // clip top glClipPlane(GL_CLIP_PLANE2, plane2); // clip right glClipPlane(GL_CLIP_PLANE3, plane3); // clip left glEnable(GL_CLIP_PLANE0); glEnable(GL_CLIP_PLANE1); glEnable(GL_CLIP_PLANE2); glEnable(GL_CLIP_PLANE3); } // switch to top-down map/world coords (z is twisted with y compared to the real map/world coords) glPushMatrix(); glTranslatef(0.0f, +1.0f, 0.0f); glScalef(+1.0f / (gs->mapx * SQUARE_SIZE), -1.0f / (gs->mapy * SQUARE_SIZE), 1.0f); glEnable(GL_TEXTURE_2D); glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER, 0.0f); { GML_RECMUTEX_LOCK(unit); // DrawForReal const std::set<CUnit*>& units = unitDrawer->GetUnsortedUnits(); for (std::set<CUnit*>::const_iterator it = units.begin(); it != units.end(); ++it) { DrawUnit(*it); } // highlight the selected unit CUnit* unit = GetSelectUnit(GetMapPosition(mouse->lastx, mouse->lasty)); if (unit != NULL) { DrawUnitHighlight(unit); } } glDisable(GL_ALPHA_TEST); glDisable(GL_TEXTURE_2D); glPushMatrix(); glRotatef(-90.0f, +1.0f, 0.0f, 0.0f); // real 'world' coordinates glScalef(1.0f, 0.0f, 1.0f); // skip the y-coord (Lua's DrawScreen is perspective and so any z-coord in it influence the x&y, too) // draw the projectiles if (drawProjectiles) { glPointSize(1.0f); WorkaroundATIPointSizeBug(); projectileDrawer->DrawProjectilesMiniMap(); } // draw the queued commands // // NOTE: this needlessly adds to the CursorIcons list, but at least // they are not drawn (because the input receivers are drawn // after the command queues) LuaUnsyncedCtrl::DrawUnitCommandQueues(); if ((drawCommands > 0) && guihandler->GetQueueKeystate()) { selectedUnits.DrawCommands(); } lineDrawer.DrawAll(); // draw the selection shape, and some ranges if (drawCommands > 0) { guihandler->DrawMapStuff(!!drawCommands); } { GML_RECMUTEX_LOCK(sel); // DrawForReal // draw unit ranges const float radarSquare = radarhandler->radarDiv; CUnitSet& selUnits = selectedUnits.selectedUnits; for(CUnitSet::iterator si = selUnits.begin(); si != selUnits.end(); ++si) { CUnit* unit = *si; if (unit->radarRadius && !unit->beingBuilt && unit->activated) { glColor3fv(cmdColors.rangeRadar); DrawCircle(unit->pos, (unit->radarRadius * radarSquare)); } if (unit->sonarRadius && !unit->beingBuilt && unit->activated) { glColor3fv(cmdColors.rangeSonar); DrawCircle(unit->pos, (unit->sonarRadius * radarSquare)); } if (unit->jammerRadius && !unit->beingBuilt && unit->activated) { glColor3fv(cmdColors.rangeJammer); DrawCircle(unit->pos, (unit->jammerRadius * radarSquare)); } // change if someone someday create a non stockpiled interceptor const CWeapon* w = unit->stockpileWeapon; if((w != NULL) && w->weaponDef->interceptor) { if (w->numStockpiled) { glColor3fv(cmdColors.rangeInterceptorOn); } else { glColor3fv(cmdColors.rangeInterceptorOff); } DrawCircle(unit->pos, w->weaponDef->coverageRange); } } } glPopMatrix(); // revert to the 2d xform if (!minimap->maximized) { // draw the camera frustum lines cam2->GetFrustumSides(0.0f, 0.0f, 1.0f, true); cam2->ClipFrustumLines(true, -10000.0f, 400096.0f); const std::vector<CCamera::FrustumLine>& negSides = cam2->negFrustumSides; // const std::vector<CCamera::FrustumLine>& posSides = cam2->posFrustumSides; std::vector<CCamera::FrustumLine>::const_iterator fli; CVertexArray* va = GetVertexArray(); va->Initialize(); va->EnlargeArrays(negSides.size() * 2, 0, VA_SIZE_2D0); for (fli = negSides.begin(); fli != negSides.end(); ++fli) { if (fli->minz < fli->maxz) { va->AddVertex2dQ0(fli->base + (fli->dir * fli->minz), fli->minz); va->AddVertex2dQ0(fli->base + (fli->dir * fli->maxz), fli->maxz); } } glLineWidth(2.5f); glColor4f(0, 0, 0, 0.5f); va->DrawArray2d0(GL_LINES); glLineWidth(1.5f); glColor4f(1, 1, 1, 0.75f); va->DrawArray2d0(GL_LINES); glLineWidth(1.0f); } // selection box glColor4f(1.0f, 1.0f, 1.0f, 1.0f); CMouseHandler::ButtonPressEvt& bp = mouse->buttons[SDL_BUTTON_LEFT]; if (selecting && fullProxy && (bp.movement > 4)) { const float3 oldPos = GetMapPosition(bp.x, bp.y); const float3 newPos = GetMapPosition(mouse->lastx, mouse->lasty); glColor4fv(cmdColors.mouseBox); glBlendFunc((GLenum)cmdColors.MouseBoxBlendSrc(), (GLenum)cmdColors.MouseBoxBlendDst()); glLineWidth(cmdColors.MouseBoxLineWidth()); float verts[] = { oldPos.x, oldPos.z, newPos.x, oldPos.z, newPos.x, newPos.z, oldPos.x, newPos.z, }; glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(2, GL_FLOAT, 0, verts); glDrawArrays(GL_LINE_LOOP, 0, 4); glDisableClientState(GL_VERTEX_ARRAY); glLineWidth(1.0f); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } DrawNotes(); // reset 1 if (resetTextureMatrix) { glMatrixMode(GL_TEXTURE_MATRIX); glPopMatrix(); } glMatrixMode(GL_MODELVIEW); if (use_geo) { glPopMatrix(); } // reset 2 glPopMatrix(); glPopAttrib(); glEnable(GL_TEXTURE_2D); { //! prepare ClipPlanes for Lua's DrawInMinimap Modelview matrix //! quote from glClipPlane spec: //! "When glClipPlane is called, equation is transformed by the inverse of the modelview matrix and stored in the resulting eye coordinates. //! Subsequent changes to the modelview matrix have no effect on the stored plane-equation components." //! -> we have to use the same modelview matrix when calling glClipPlane and later draw calls //! set the modelview matrix to the same as used in Lua's DrawInMinimap glPushMatrix(); glLoadIdentity(); glScalef(1.0f / width, 1.0f / height, 1.0f); const double plane0[4] = {0, -1, 0, double(height)}; const double plane1[4] = {0, 1, 0, 0}; const double plane2[4] = {-1, 0, 0, double(width)}; const double plane3[4] = {1, 0, 0, 0}; glClipPlane(GL_CLIP_PLANE0, plane0); // clip bottom glClipPlane(GL_CLIP_PLANE1, plane1); // clip top glClipPlane(GL_CLIP_PLANE2, plane2); // clip right glClipPlane(GL_CLIP_PLANE3, plane3); // clip left glPopMatrix(); } //! allow the LUA scripts to draw into the minimap eventHandler.DrawInMiniMap(); if (use_geo && globalRendering->dualScreenMode) glViewport(globalRendering->viewPosX,0,globalRendering->viewSizeX,globalRendering->viewSizeY); //FIXME: Lua modifies the matrices w/o reseting it! (quite complexe to fix because ClearMatrixStack() makes it impossible to use glPushMatrix) glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0,1,0,1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // disable ClipPlanes glDisable(GL_CLIP_PLANE0); glDisable(GL_CLIP_PLANE1); glDisable(GL_CLIP_PLANE2); glDisable(GL_CLIP_PLANE3); cursorIcons.Enable(true); setSurfaceCircleFunc(NULL); setSurfaceSquareFunc(NULL); }
void klsMiniMap::setViewport() { // Set the projection matrix: glMatrixMode (GL_PROJECTION); glLoadIdentity (); wxSize sz = GetClientSize(); float minX = FLT_MAX, minY = FLT_MAX, maxX = -FLT_MAX, maxY = -FLT_MAX; hash_map < unsigned long, guiGate* >::iterator gateWalk = gateList->begin(); while (gateWalk != gateList->end()) { float x, y; (gateWalk->second)->getGLcoords(x, y); if (x < minX) minX = x; if (y < minY) minY = y; if (x > maxX) maxX = x; if (y > maxY) maxY = y; gateWalk++; } if (origin.x < minX) minX = origin.x; if (origin.y > maxY) maxY = origin.y; if (endpoint.x > maxX) maxX = endpoint.x; if (endpoint.y < minY) minY = endpoint.y; minCorner = GLPoint2f(minX-5,maxY+5); maxCorner = GLPoint2f(maxX+5,minY-5); double screenAspect = (double) sz.GetHeight() / (double) sz.GetWidth(); double mapWidth = maxCorner.x - minCorner.x; double mapHeight = minCorner.y - maxCorner.y; // max and min corner's defs are weird... GLPoint2f orthoBoxTL, orthoBoxBR; // If the map's width is the limiting factor: if( screenAspect * mapWidth >= mapHeight ) { // Fit to width: double imageHeight = screenAspect * mapWidth; // Set the ortho box width equal to the map width, and center the // height in the box: orthoBoxTL = GLPoint2f( minCorner.x, minCorner.y + 0.5*(imageHeight - mapHeight) ); orthoBoxBR = GLPoint2f( maxCorner.x, maxCorner.y - 0.5*(imageHeight - mapHeight) ); } else { // Fit to height: double imageWidth = mapHeight / screenAspect; // Set the ortho box height equal to the map height, and center the // width in the box: orthoBoxTL = GLPoint2f( minCorner.x - 0.5*(imageWidth - mapWidth), minCorner.y ); orthoBoxBR = GLPoint2f( maxCorner.x + 0.5*(imageWidth - mapWidth), maxCorner.y ); } // gluOrtho2D(left, right, bottom, top); (In world-space coords.) gluOrtho2D(orthoBoxTL.x, orthoBoxBR.x, orthoBoxBR.y, orthoBoxTL.y); glViewport(0, 0, (GLint) sz.GetWidth(), (GLint) sz.GetHeight()); // Store minCorner and maxCorner for use in mouse handler: minCorner = orthoBoxTL; maxCorner = orthoBoxBR; // Set the model matrix: glMatrixMode (GL_MODELVIEW); glLoadIdentity (); }
void CWorldDrawer::Draw() { SCOPED_TIMER("WorldDrawer::Total"); CBaseGroundDrawer* gd = readmap->GetGroundDrawer(); if (globalRendering->drawSky) { sky->Draw(); } if (globalRendering->drawGround) { SCOPED_TIMER("WorldDrawer::Terrain"); gd->Draw(DrawPass::Normal); smoothHeightMeshDrawer->Draw(1.0f); treeDrawer->DrawGrass(); gd->DrawTrees(); } if (globalRendering->drawWater && !mapInfo->map.voidWater) { SCOPED_TIMER("WorldDrawer::Water"); water->OcclusionQuery(); if (water->IsDrawSolid()) { water->UpdateWater(game); water->Draw(); } } selectedUnits.Draw(); eventHandler.DrawWorldPreUnit(); { SCOPED_TIMER("WorldDrawer::Models"); DebugColVolDrawer::Draw(); unitDrawer->Draw(false); modelDrawer->Draw(); featureDrawer->Draw(); pathDrawer->DrawAll(); } //! transparent stuff glEnable(GL_BLEND); glDepthFunc(GL_LEQUAL); const bool noAdvShading = shadowHandler->shadowsLoaded; static const double plane_below[4] = {0.0f, -1.0f, 0.0f, 0.0f}; static const double plane_above[4] = {0.0f, 1.0f, 0.0f, 0.0f}; { glClipPlane(GL_CLIP_PLANE3, plane_below); glEnable(GL_CLIP_PLANE3); //! draw cloaked objects below water surface unitDrawer->DrawCloakedUnits(noAdvShading); featureDrawer->DrawFadeFeatures(noAdvShading); glDisable(GL_CLIP_PLANE3); } //! draw water if (globalRendering->drawWater && !mapInfo->map.voidWater) { SCOPED_TIMER("WorldDrawer::Water"); if (!water->IsDrawSolid()) { //! Water rendering will overwrite features, so save them featureDrawer->SwapFeatures(); water->UpdateWater(game); water->Draw(); featureDrawer->SwapFeatures(); } } { glClipPlane(GL_CLIP_PLANE3, plane_above); glEnable(GL_CLIP_PLANE3); //! draw cloaked objects above water surface unitDrawer->DrawCloakedUnits(noAdvShading); featureDrawer->DrawFadeFeatures(noAdvShading); glDisable(GL_CLIP_PLANE3); } { SCOPED_TIMER("WorldDrawer::Projectiles"); projectileDrawer->Draw(false); } if (globalRendering->drawSky) { sky->DrawSun(); } eventHandler.DrawWorld(); LuaUnsyncedCtrl::DrawUnitCommandQueues(); if (cmdColors.AlwaysDrawQueue() || guihandler->GetQueueKeystate()) { selectedUnits.DrawCommands(); } lineDrawer.DrawAll(); cursorIcons.Draw(); cursorIcons.Clear(); mouse->DrawSelectionBox(); guihandler->DrawMapStuff(false); if (globalRendering->drawMapMarks && !game->hideInterface) { inMapDrawerView->Draw(); } //! underwater overlay if (camera->pos.y < 0.0f) { glEnableClientState(GL_VERTEX_ARRAY); const float3& cpos = camera->pos; const float vr = globalRendering->viewRange * 0.5f; glDepthMask(GL_FALSE); glDisable(GL_TEXTURE_2D); glColor4f(0.0f, 0.5f, 0.3f, 0.50f); { float3 verts[] = { float3(cpos.x - vr, 0.0f, cpos.z - vr), float3(cpos.x - vr, 0.0f, cpos.z + vr), float3(cpos.x + vr, 0.0f, cpos.z + vr), float3(cpos.x + vr, 0.0f, cpos.z - vr) }; glVertexPointer(3, GL_FLOAT, 0, verts); glDrawArrays(GL_QUADS, 0, 4); } { float3 verts[] = { float3(cpos.x - vr, 0.0f, cpos.z - vr), float3(cpos.x - vr, -vr, cpos.z - vr), float3(cpos.x - vr, 0.0f, cpos.z + vr), float3(cpos.x - vr, -vr, cpos.z + vr), float3(cpos.x + vr, 0.0f, cpos.z + vr), float3(cpos.x + vr, -vr, cpos.z + vr), float3(cpos.x + vr, 0.0f, cpos.z - vr), float3(cpos.x + vr, -vr, cpos.z - vr), float3(cpos.x - vr, 0.0f, cpos.z - vr), float3(cpos.x - vr, -vr, cpos.z - vr), }; glVertexPointer(3, GL_FLOAT, 0, verts); glDrawArrays(GL_QUAD_STRIP, 0, 10); } glDepthMask(GL_TRUE); glDisableClientState(GL_VERTEX_ARRAY); } //reset fov glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0,1,0,1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glEnable(GL_BLEND); glDisable(GL_DEPTH_TEST); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // underwater overlay, part 2 if (camera->pos.y < 0.0f) { glEnableClientState(GL_VERTEX_ARRAY); glDisable(GL_TEXTURE_2D); glColor4f(0.0f, 0.2f, 0.8f, 0.333f); float3 verts[] = { float3 (0.f, 0.f, -1.f), float3 (1.f, 0.f, -1.f), float3 (1.f, 1.f, -1.f), float3 (0.f, 1.f, -1.f), }; glVertexPointer(3, GL_FLOAT, 0, verts); glDrawArrays(GL_QUADS, 0, 4); glDisableClientState(GL_VERTEX_ARRAY); } }
/* Draws the string on the window at the given pixel position. Newlines and tab stops are honored. Any text inside [] will be rendered as a subscript. Assumes the font has been loaded as with load_font(). */ void print_gl_string (Display *dpy, XFontStruct *font, GLuint font_dlist, int window_width, int window_height, GLfloat x, GLfloat y, const char *string, Bool clear_background_p) { GLfloat line_height = font->ascent + font->descent; GLfloat sub_shift = (line_height * 0.3); int cw = string_width (font, "m", 0); int tabs = cw * 7; y -= line_height; /* Sadly, this causes a stall of the graphics pipeline (as would the equivalent calls to glGet*.) But there's no way around this, short of having each caller set up the specific display matrix we need here, which would kind of defeat the purpose of centralizing this code in one file. */ glPushAttrib (GL_TRANSFORM_BIT | /* for matrix contents */ GL_ENABLE_BIT | /* for various glDisable calls */ GL_CURRENT_BIT | /* for glColor3f() */ GL_LIST_BIT); /* for glListBase() */ { # ifdef DEBUG check_gl_error ("glPushAttrib"); # endif /* disable lighting and texturing when drawing bitmaps! (glPopAttrib() restores these.) */ glDisable (GL_TEXTURE_2D); glDisable (GL_LIGHTING); glDisable (GL_BLEND); glDisable (GL_DEPTH_TEST); glDisable (GL_CULL_FACE); /* glPopAttrib() does not restore matrix changes, so we must push/pop the matrix stacks to be non-intrusive there. */ glMatrixMode(GL_PROJECTION); glPushMatrix(); { # ifdef DEBUG check_gl_error ("glPushMatrix"); # endif glLoadIdentity(); /* Each matrix mode has its own stack, so we need to push/pop them separately. */ glMatrixMode(GL_MODELVIEW); glPushMatrix(); { unsigned int i; int x2 = x; Bool sub_p = False; # ifdef DEBUG check_gl_error ("glPushMatrix"); # endif glLoadIdentity(); gluOrtho2D (0, window_width, 0, window_height); # ifdef DEBUG check_gl_error ("gluOrtho2D"); # endif if (clear_background_p) { int w, h; int lh = font->ascent + font->descent; w = string_width (font, string, &h); glColor3f (0, 0, 0); glRecti (x - font->descent, y + lh, x + w + 2*font->descent, y + lh - h - font->descent); glColor3f (1, 1, 1); } /* draw the text */ glRasterPos2f (x, y); /* glListBase (font_dlist);*/ for (i = 0; i < strlen(string); i++) { unsigned char c = (unsigned char) string[i]; if (c == '\n') { glRasterPos2f (x, (y -= line_height)); x2 = x; } else if (c == '\t') { x2 -= x; x2 = ((x2 + tabs) / tabs) * tabs; /* tab to tab stop */ x2 += x; glRasterPos2f (x2, y); } else if (c == '[' && (isdigit (string[i+1]))) { sub_p = True; glRasterPos2f (x2, (y -= sub_shift)); } else if (c == ']' && sub_p) { sub_p = False; glRasterPos2f (x2, (y += sub_shift)); } else { /* glCallLists (s - string, GL_UNSIGNED_BYTE, string);*/ glCallList (font_dlist + (int)(c)); x2 += (font->per_char ? font->per_char[c - font->min_char_or_byte2].width : font->min_bounds.width); } } # ifdef DEBUG check_gl_error ("print_gl_string"); # endif } glPopMatrix(); } glMatrixMode(GL_PROJECTION); glPopMatrix(); } glPopAttrib(); # ifdef DEBUG check_gl_error ("glPopAttrib"); # endif glMatrixMode(GL_MODELVIEW); }
//---------- // 表示関数 //---------- void display( void ) { glClear( GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT ); glDisable( GL_CULL_FACE ); glDisable( GL_ALPHA_TEST ); glDisable( GL_BLEND ); g_clARTK.display(); glEnable( GL_CULL_FACE ); glEnable( GL_ALPHA_TEST ); glEnable( GL_BLEND ); if( g_clARTK.isMarkerFound() ) { double dMat[16]; glMatrixMode( GL_PROJECTION ); g_clARTK.getProjectionMat( dMat ); glLoadMatrixd( dMat ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); glPushMatrix(); g_clARTK.getModelViewMat( dMat ); glLoadMatrixd( dMat ); glScalef( 1.0f, 1.0f, -1.0f ); // 左手系 → 右手系 g_clPMDModel.render(); // ステンシルバッファに影の形を描画 glDisable( GL_CULL_FACE ); glDisable( GL_TEXTURE_2D ); glDisable( GL_LIGHTING ); glEnable( GL_STENCIL_TEST ); glStencilFunc( GL_ALWAYS, 1, ~0 ); glStencilOp( GL_REPLACE, GL_KEEP, GL_REPLACE ); glColorMask( 0, 0, 0, 0 ); glDepthMask( 0 ); glMultMatrixf( (const float *)g_matPlanarProjection ); g_clPMDModel.renderForShadow(); // 影用の描画 glColorMask( 1, 1, 1, 1 ); // ステンシルバッファの影の形を塗りつぶす float fWndW = glutGet( GLUT_WINDOW_WIDTH ), fWndH = glutGet( GLUT_WINDOW_HEIGHT ); glStencilFunc( GL_EQUAL, 1, ~0); glStencilOp( GL_KEEP, GL_KEEP ,GL_KEEP ); glDisable( GL_DEPTH_TEST ); glMatrixMode( GL_PROJECTION ); glPushMatrix(); glLoadIdentity(); gluOrtho2D( 0.0f, fWndW, 0.0f, fWndH ); glMatrixMode( GL_MODELVIEW ); glPushMatrix(); glLoadIdentity(); glColor4f( 0.2f, 0.2f, 0.2f, 0.5f ); glBegin( GL_TRIANGLE_FAN ); glVertex2f( 0.0f, fWndH ); glVertex2f( fWndW, fWndH ); glVertex2f( fWndW, 0.0f ); glVertex2f( 0.0f, 0.0f ); glEnd(); glMatrixMode( GL_PROJECTION ); glPopMatrix(); glMatrixMode( GL_MODELVIEW ); glPopMatrix(); glDepthMask( 1 ); glEnable( GL_LIGHTING ); glEnable( GL_DEPTH_TEST ); glDisable( GL_STENCIL_TEST ); glPopMatrix(); } calcFps(); glutSwapBuffers(); }