00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 #include "guichan/opengl/openglgraphics.hpp"
00062
00063 #ifdef _WIN32
00064 #define WIN32_LEAN_AND_MEAN
00065 #include <windows.h>
00066 #endif
00067
00068 #ifdef __amigaos4__
00069 #include <mgl/gl.h>
00070 #define glVertex3i glVertex3f
00071 #else
00072 #include <GL/gl.h>
00073 #endif
00074
00075 #include "guichan/exception.hpp"
00076 #include "guichan/image.hpp"
00077 #include "guichan/opengl/openglimage.hpp"
00078
00079 namespace gcn
00080 {
00081 OpenGLGraphics::OpenGLGraphics()
00082 {
00083 setTargetPlane(640, 480);
00084 mAlpha = false;
00085 }
00086
00087 OpenGLGraphics::OpenGLGraphics(int width, int height)
00088 {
00089 setTargetPlane(width, height);
00090 }
00091
00092 OpenGLGraphics::~OpenGLGraphics()
00093 {
00094
00095 }
00096
00097 void OpenGLGraphics::_beginDraw()
00098 {
00099 glPushAttrib(
00100 GL_COLOR_BUFFER_BIT |
00101 GL_CURRENT_BIT |
00102 GL_DEPTH_BUFFER_BIT |
00103 GL_ENABLE_BIT |
00104 GL_FOG_BIT |
00105 GL_LIGHTING_BIT |
00106 GL_LINE_BIT |
00107 GL_POINT_BIT |
00108 GL_POLYGON_BIT |
00109 GL_SCISSOR_BIT |
00110 GL_STENCIL_BUFFER_BIT |
00111 GL_TEXTURE_BIT |
00112 GL_TRANSFORM_BIT |
00113 GL_POINT_BIT |
00114 GL_LINE_BIT
00115 );
00116
00117 glMatrixMode(GL_MODELVIEW);
00118 glPushMatrix();
00119 glLoadIdentity();
00120
00121 glMatrixMode(GL_TEXTURE);
00122 glPushMatrix();
00123 glLoadIdentity();
00124
00125 glMatrixMode(GL_PROJECTION);
00126 glPushMatrix();
00127 glLoadIdentity();
00128
00129 glOrtho(0.0, (double)mWidth, (double)mHeight, 0.0, -1.0, 1.0);
00130
00131 glDisable(GL_LIGHTING);
00132 glDisable(GL_CULL_FACE);
00133 glDisable(GL_DEPTH_TEST);
00134 glDisable(GL_TEXTURE_2D);
00135
00136 glEnable(GL_SCISSOR_TEST);
00137 glPointSize(1.0);
00138 glLineWidth(1.0);
00139
00140 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00141
00142 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
00143 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
00144 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00145 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00146
00147 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00148
00149 pushClipArea(Rectangle(0, 0, mWidth, mHeight));
00150 }
00151
00152 void OpenGLGraphics::_endDraw()
00153 {
00154 glMatrixMode(GL_MODELVIEW);
00155 glPopMatrix();
00156
00157 glMatrixMode(GL_TEXTURE);
00158 glPopMatrix();
00159
00160 glMatrixMode(GL_PROJECTION);
00161 glPopMatrix();
00162
00163 glPopAttrib();
00164
00165 popClipArea();
00166 }
00167
00168 bool OpenGLGraphics::pushClipArea(Rectangle area)
00169 {
00170 bool result = Graphics::pushClipArea(area);
00171
00172 glScissor(mClipStack.top().x,
00173 mHeight - mClipStack.top().y - mClipStack.top().height,
00174 mClipStack.top().width,
00175 mClipStack.top().height);
00176
00177 return result;
00178 }
00179
00180 void OpenGLGraphics::popClipArea()
00181 {
00182 Graphics::popClipArea();
00183
00184 if (mClipStack.empty())
00185 {
00186 return;
00187 }
00188
00189 glScissor(mClipStack.top().x,
00190 mHeight - mClipStack.top().y - mClipStack.top().height,
00191 mClipStack.top().width,
00192 mClipStack.top().height);
00193 }
00194
00195 void OpenGLGraphics::setTargetPlane(int width, int height)
00196 {
00197 mWidth = width;
00198 mHeight = height;
00199 }
00200
00201 void OpenGLGraphics::drawImage(const Image* image, int srcX, int srcY,
00202 int dstX, int dstY, int width,
00203 int height)
00204 {
00205 const OpenGLImage* srcImage = dynamic_cast<const OpenGLImage*>(image);
00206
00207 if (srcImage == NULL)
00208 {
00209 throw GCN_EXCEPTION("Trying to draw an image of unknown format, must be an OpenGLImage.");
00210 }
00211
00212 dstX += mClipStack.top().xOffset;
00213 dstY += mClipStack.top().yOffset;
00214
00215
00216 float texX1 = srcX / (float)srcImage->getTextureWidth();
00217 float texY1 = srcY / (float)srcImage->getTextureHeight();
00218 float texX2 = (srcX+width) / (float)srcImage->getTextureWidth();
00219 float texY2 = (srcY+height) / (float)srcImage->getTextureHeight();
00220
00221 glBindTexture(GL_TEXTURE_2D, srcImage->getTextureHandle());
00222
00223 glEnable(GL_TEXTURE_2D);
00224
00225
00226 if (!mAlpha)
00227 {
00228 glEnable(GL_BLEND);
00229 }
00230
00231
00232 glBegin(GL_QUADS);
00233 glTexCoord2f(texX1, texY1);
00234 glVertex3i(dstX, dstY, 0);
00235
00236 glTexCoord2f(texX1, texY2);
00237 glVertex3i(dstX, dstY + height, 0);
00238
00239 glTexCoord2f(texX2, texY2);
00240 glVertex3i(dstX + width, dstY + height, 0);
00241
00242 glTexCoord2f(texX2, texY1);
00243 glVertex3i(dstX + width, dstY, 0);
00244 glEnd();
00245 glDisable(GL_TEXTURE_2D);
00246
00247
00248 if (!mAlpha)
00249 {
00250 glDisable(GL_BLEND);
00251 }
00252 }
00253
00254 void OpenGLGraphics::drawPoint(int x, int y)
00255 {
00256 x += mClipStack.top().xOffset;
00257 y += mClipStack.top().yOffset;
00258
00259 glBegin(GL_POINTS);
00260 glVertex3i(x, y, 0);
00261 glEnd();
00262 }
00263
00264 void OpenGLGraphics::drawLine(int x1, int y1, int x2, int y2)
00265 {
00266 x1 += mClipStack.top().xOffset;
00267 y1 += mClipStack.top().yOffset;
00268 x2 += mClipStack.top().xOffset;
00269 y2 += mClipStack.top().yOffset;
00270
00271 glBegin(GL_LINES);
00272 glVertex3f(x1+0.5f, y1+0.5f, 0);
00273 glVertex3f(x2+0.5f, y2+0.5f, 0);
00274 glEnd();
00275
00276 glBegin(GL_POINTS);
00277 glVertex3f(x2+0.5f, y2+0.5f, 0);
00278 glEnd();
00279 }
00280
00281 void OpenGLGraphics::drawRectangle(const Rectangle& rectangle)
00282 {
00283 glBegin(GL_LINE_LOOP);
00284 glVertex3f(rectangle.x + mClipStack.top().xOffset + 0.5f,
00285 rectangle.y + mClipStack.top().yOffset + 0.5f, 0);
00286 glVertex3f(rectangle.x + rectangle.width - 0.5f + mClipStack.top().xOffset,
00287 rectangle.y + mClipStack.top().yOffset + 0.5f, 0);
00288 glVertex3f(rectangle.x + rectangle.width - 0.5f + mClipStack.top().xOffset,
00289 rectangle.y + rectangle.height + mClipStack.top().yOffset - 0.5f, 0);
00290 glVertex3f(rectangle.x + mClipStack.top().xOffset + 0.5f,
00291 rectangle.y + rectangle.height + mClipStack.top().yOffset - 0.5f, 0);
00292 glEnd();
00293 }
00294
00295 void OpenGLGraphics::fillRectangle(const Rectangle& rectangle)
00296 {
00297 glBegin(GL_QUADS);
00298 glVertex3i(rectangle.x + mClipStack.top().xOffset,
00299 rectangle.y + mClipStack.top().yOffset, 0);
00300 glVertex3i(rectangle.x + rectangle.width + mClipStack.top().xOffset,
00301 rectangle.y + mClipStack.top().yOffset, 0);
00302 glVertex3i(rectangle.x + rectangle.width + mClipStack.top().xOffset,
00303 rectangle.y + rectangle.height + mClipStack.top().yOffset, 0);
00304 glVertex3i(rectangle.x + mClipStack.top().xOffset,
00305 rectangle.y + rectangle.height + mClipStack.top().yOffset, 0);
00306 glEnd();
00307 }
00308
00309 void OpenGLGraphics::setColor(const Color& color)
00310 {
00311 mColor = color;
00312 glColor4ub(color.r, color.g, color.b, color.a);
00313
00314 mAlpha = color.a != 255;
00315
00316 if (mAlpha)
00317 {
00318 glEnable(GL_BLEND);
00319 }
00320 }
00321
00322 const Color& OpenGLGraphics::getColor()
00323 {
00324 return mColor;
00325 }
00326 }