|
Blender
V2.59
|
00001 /* 00002 * $Id: KX_BlenderCanvas.cpp 35417 2011-03-09 01:25:59Z campbellbarton $ 00003 * ***** BEGIN GPL LICENSE BLOCK ***** 00004 * 00005 * This program is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU General Public License 00007 * as published by the Free Software Foundation; either version 2 00008 * of the License, or (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software Foundation, 00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00018 * 00019 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00020 * All rights reserved. 00021 * 00022 * The Original Code is: all of this file. 00023 * 00024 * Contributor(s): none yet. 00025 * 00026 * ***** END GPL LICENSE BLOCK ***** 00027 */ 00028 00034 #include "KX_BlenderCanvas.h" 00035 #include "DNA_screen_types.h" 00036 #include <stdio.h> 00037 00038 00039 KX_BlenderCanvas::KX_BlenderCanvas(struct wmWindow *win, RAS_Rect &rect, struct ARegion *ar) : 00040 m_win(win), 00041 m_frame_rect(rect) 00042 { 00043 // area boundaries needed for mouse coordinates in Letterbox framing mode 00044 m_area_left = ar->winrct.xmin; 00045 m_area_top = ar->winrct.ymax; 00046 } 00047 00048 KX_BlenderCanvas::~KX_BlenderCanvas() 00049 { 00050 } 00051 00052 void KX_BlenderCanvas::Init() 00053 { 00054 glDepthFunc(GL_LEQUAL); 00055 } 00056 00057 00058 void KX_BlenderCanvas::SwapBuffers() 00059 { 00060 BL_SwapBuffers(m_win); 00061 } 00062 00063 void KX_BlenderCanvas::BeginFrame() 00064 { 00065 glEnable(GL_DEPTH_TEST); 00066 glDepthFunc(GL_LEQUAL); 00067 00068 } 00069 00070 00071 void KX_BlenderCanvas::EndFrame() 00072 { 00073 // this is needed, else blender distorts a lot 00074 glPopAttrib(); 00075 glPushAttrib(GL_ALL_ATTRIB_BITS); 00076 00077 glDisable(GL_FOG); 00078 } 00079 00080 00081 00082 void KX_BlenderCanvas::ClearColor(float r,float g,float b,float a) 00083 { 00084 glClearColor(r,g,b,a); 00085 } 00086 00087 00088 00089 void KX_BlenderCanvas::ClearBuffer(int type) 00090 { 00091 int ogltype = 0; 00092 00093 if (type & RAS_ICanvas::COLOR_BUFFER ) 00094 ogltype |= GL_COLOR_BUFFER_BIT; 00095 00096 if (type & RAS_ICanvas::DEPTH_BUFFER ) 00097 ogltype |= GL_DEPTH_BUFFER_BIT; 00098 glClear(ogltype); 00099 } 00100 00101 int KX_BlenderCanvas::GetWidth( 00102 ) const { 00103 return m_frame_rect.GetWidth(); 00104 } 00105 00106 int KX_BlenderCanvas::GetHeight( 00107 ) const { 00108 return m_frame_rect.GetHeight(); 00109 } 00110 00111 int KX_BlenderCanvas::GetMouseX(int x) 00112 { 00113 float left = GetWindowArea().GetLeft(); 00114 return float(x - (left - m_area_left)); 00115 } 00116 00117 int KX_BlenderCanvas::GetMouseY(int y) 00118 { 00119 float top = GetWindowArea().GetTop(); 00120 return float(y - (m_area_top - top)); 00121 } 00122 00123 float KX_BlenderCanvas::GetMouseNormalizedX(int x) 00124 { 00125 int can_x = GetMouseX(x); 00126 return float(can_x)/this->GetWidth(); 00127 } 00128 00129 float KX_BlenderCanvas::GetMouseNormalizedY(int y) 00130 { 00131 int can_y = GetMouseY(y); 00132 return float(can_y)/this->GetHeight(); 00133 } 00134 00135 RAS_Rect & 00136 KX_BlenderCanvas:: 00137 GetWindowArea( 00138 ){ 00139 return m_area_rect; 00140 } 00141 00142 void 00143 KX_BlenderCanvas:: 00144 SetViewPort( 00145 int x1, int y1, 00146 int x2, int y2 00147 ){ 00148 /* x1 and y1 are the min pixel coordinate (e.g. 0) 00149 x2 and y2 are the max pixel coordinate 00150 the width,height is calculated including both pixels 00151 therefore: max - min + 1 00152 */ 00153 int vp_width = (x2 - x1) + 1; 00154 int vp_height = (y2 - y1) + 1; 00155 int minx = m_frame_rect.GetLeft(); 00156 int miny = m_frame_rect.GetBottom(); 00157 00158 m_area_rect.SetLeft(minx + x1); 00159 m_area_rect.SetBottom(miny + y1); 00160 m_area_rect.SetRight(minx + x2); 00161 m_area_rect.SetTop(miny + y2); 00162 00163 glViewport(minx + x1, miny + y1, vp_width, vp_height); 00164 glScissor(minx + x1, miny + y1, vp_width, vp_height); 00165 } 00166 00167 00168 void KX_BlenderCanvas::SetMouseState(RAS_MouseState mousestate) 00169 { 00170 m_mousestate = mousestate; 00171 00172 switch (mousestate) 00173 { 00174 case MOUSE_INVISIBLE: 00175 { 00176 BL_HideMouse(m_win); 00177 break; 00178 } 00179 case MOUSE_WAIT: 00180 { 00181 BL_WaitMouse(m_win); 00182 break; 00183 } 00184 case MOUSE_NORMAL: 00185 { 00186 BL_NormalMouse(m_win); 00187 break; 00188 } 00189 default: 00190 { 00191 } 00192 } 00193 } 00194 00195 00196 00197 // (0,0) is top left, (width,height) is bottom right 00198 void KX_BlenderCanvas::SetMousePosition(int x,int y) 00199 { 00200 int winX = m_frame_rect.GetLeft(); 00201 int winY = m_frame_rect.GetBottom(); 00202 int winH = m_frame_rect.GetHeight(); 00203 00204 BL_warp_pointer(m_win, winX + x, winY + (winH-y)); 00205 } 00206 00207 00208 00209 void KX_BlenderCanvas::MakeScreenShot(const char* filename) 00210 { 00211 ScrArea area_dummy= {0}; 00212 area_dummy.totrct.xmin = m_frame_rect.GetLeft(); 00213 area_dummy.totrct.xmax = m_frame_rect.GetRight(); 00214 area_dummy.totrct.ymin = m_frame_rect.GetBottom(); 00215 area_dummy.totrct.ymax = m_frame_rect.GetTop(); 00216 00217 BL_MakeScreenShot(&area_dummy, filename); 00218 }