mirror of
https://github.com/edubart/otclient.git
synced 2025-12-14 12:49:47 +01:00
Merge branch 'revgraphics'
Conflicts: src/framework/CMakeLists.txt src/framework/application.cpp src/framework/graphics/graphics.cpp
This commit is contained in:
@@ -30,310 +30,47 @@ Graphics g_graphics;
|
||||
void Graphics::init()
|
||||
{
|
||||
// setup opengl
|
||||
glEnable(GL_ALPHA_TEST); // enable alpha by default
|
||||
glAlphaFunc(GL_GREATER, 0.0f); // default alpha func
|
||||
glDisable(GL_DEPTH_TEST); // we are rendering 2D only, we don't need depth buffer
|
||||
glEnable(GL_TEXTURE_2D); // enable textures by default
|
||||
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
|
||||
glShadeModel(GL_SMOOTH);
|
||||
glEnable(GL_BLEND);
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
|
||||
logInfo("GPU ", glGetString(GL_RENDERER));
|
||||
logInfo("OpenGL ", glGetString(GL_VERSION));
|
||||
|
||||
m_drawing = false;
|
||||
m_opacity = 255;
|
||||
//if(!isExtensionSupported("GL_ARB_framebuffer_object"))
|
||||
// logFatal("Your graphics card is not supported.");
|
||||
|
||||
m_emptyTexture = TexturePtr(new Texture);
|
||||
|
||||
bindColor(Fw::white);
|
||||
bindBlendFunc(Fw::BlendDefault);
|
||||
g_painter.init();
|
||||
}
|
||||
|
||||
void Graphics::terminate()
|
||||
{
|
||||
g_fonts.releaseFonts();
|
||||
g_painter.terminate();
|
||||
m_emptyTexture.reset();
|
||||
}
|
||||
|
||||
bool Graphics::isExtensionSupported(const char *extension)
|
||||
{
|
||||
const GLubyte *extensions = NULL;
|
||||
const GLubyte *start;
|
||||
GLubyte *where, *terminator;
|
||||
where = (GLubyte *)strchr(extension, ' ');
|
||||
|
||||
if(where || *extension == '\0')
|
||||
return 0;
|
||||
|
||||
extensions = glGetString(GL_EXTENSIONS);
|
||||
|
||||
start = extensions;
|
||||
while(true) {
|
||||
where = (GLubyte *) strstr((const char *)start, extension);
|
||||
if(!where)
|
||||
break;
|
||||
|
||||
terminator = where + strlen(extension);
|
||||
|
||||
if(where == start || *(where - 1) == ' ')
|
||||
if(*terminator == ' ' || *terminator == '\0')
|
||||
return 1;
|
||||
|
||||
start = terminator;
|
||||
}
|
||||
return 0;
|
||||
std::string extensionsString = (const char*)glGetString(GL_EXTENSIONS);
|
||||
auto extensions = Fw::split(extensionsString);
|
||||
return std::find(extensions.begin(), extensions.end(), extension) != extensions.end();
|
||||
}
|
||||
|
||||
void Graphics::resize(const Size& size)
|
||||
{
|
||||
m_screenSize = size;
|
||||
restoreViewport();
|
||||
}
|
||||
|
||||
void Graphics::restoreViewport()
|
||||
{
|
||||
const int& width = m_screenSize.width();
|
||||
const int& height = m_screenSize.height();
|
||||
|
||||
// resize gl viewport
|
||||
glViewport(0, 0, width, height);
|
||||
|
||||
/*
|
||||
0,0---------0,w
|
||||
| |
|
||||
| |
|
||||
| |
|
||||
h,0---------h,w
|
||||
*/
|
||||
// setup view region like above
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
glOrtho(0.0f, width, height, 0.0f, -1, 1);
|
||||
|
||||
// back to model view
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
glViewport(0, 0, size.width(), size.height());
|
||||
g_painter.updateProjectionMatrix(size);
|
||||
m_viewportSize = size;
|
||||
}
|
||||
|
||||
void Graphics::beginRender()
|
||||
{
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glLoadIdentity();
|
||||
}
|
||||
|
||||
void Graphics::endRender()
|
||||
{
|
||||
assert(!m_drawing);
|
||||
}
|
||||
|
||||
void Graphics::drawTexturedRect(const Rect& screenCoords,
|
||||
const TexturePtr& texture,
|
||||
const Rect& textureCoords,
|
||||
bool upsideDown)
|
||||
{
|
||||
if(screenCoords.isEmpty() || texture->getId() == 0)
|
||||
return;
|
||||
|
||||
// rect correction for opengl
|
||||
int right = screenCoords.right() + 1;
|
||||
int bottom = screenCoords.bottom() + 1;
|
||||
int top = screenCoords.top();
|
||||
int left = screenCoords.left();
|
||||
|
||||
float textureRight;
|
||||
float textureBottom;
|
||||
float textureTop;
|
||||
float textureLeft;
|
||||
const Size& textureSize = texture->getGlSize();
|
||||
|
||||
if(textureCoords.isEmpty()) {
|
||||
textureRight = texture->getWidth() / (float)textureSize.width();
|
||||
if(upsideDown) {
|
||||
textureBottom = 0.0f;
|
||||
textureTop = texture->getHeight() / (float)textureSize.height();
|
||||
} else {
|
||||
textureBottom = texture->getHeight() / (float)textureSize.height();
|
||||
textureTop = 0.0f;
|
||||
}
|
||||
textureLeft = 0.0f;
|
||||
} else {
|
||||
textureRight = (textureCoords.right() + 1) / (float)textureSize.width();
|
||||
if(upsideDown) {
|
||||
textureTop = (textureCoords.bottom() + 1) / (float)textureSize.height();
|
||||
textureBottom = textureCoords.top() / (float)textureSize.height();
|
||||
} else {
|
||||
textureBottom = (textureCoords.bottom() + 1) / (float)textureSize.height();
|
||||
textureTop = textureCoords.top() / (float)textureSize.height();
|
||||
}
|
||||
textureLeft = textureCoords.left() / (float)textureSize.width();
|
||||
}
|
||||
|
||||
if(!m_drawing) {
|
||||
bindTexture(texture);
|
||||
glBegin(GL_QUADS);
|
||||
}
|
||||
|
||||
glTexCoord2f(textureLeft, textureTop); glVertex2i(left, top);
|
||||
glTexCoord2f(textureLeft, textureBottom); glVertex2i(left, bottom);
|
||||
glTexCoord2f(textureRight, textureBottom); glVertex2i(right, bottom);
|
||||
glTexCoord2f(textureRight, textureTop); glVertex2i(right, top);
|
||||
|
||||
if(!m_drawing)
|
||||
glEnd();
|
||||
}
|
||||
|
||||
void Graphics::drawRepeatedTexturedRect(const Rect& screenCoords,
|
||||
const TexturePtr& texture,
|
||||
const Rect& textureCoords)
|
||||
{
|
||||
if(screenCoords.isEmpty() || texture->getId() == 0 || textureCoords.isEmpty())
|
||||
return;
|
||||
|
||||
bool mustStopDrawing = false;
|
||||
if(!m_drawing) {
|
||||
bindTexture(texture);
|
||||
startDrawing();
|
||||
mustStopDrawing = true;
|
||||
}
|
||||
|
||||
// render many repeated texture rects
|
||||
Rect virtualScreenCoords(0,0,screenCoords.size());
|
||||
for(int y = 0; y <= virtualScreenCoords.height(); y += textureCoords.height()) {
|
||||
for(int x = 0; x <= virtualScreenCoords.width(); x += textureCoords.width()) {
|
||||
Rect partialCoords(x, y, textureCoords.size());
|
||||
Rect partialTextureCoords = textureCoords;
|
||||
|
||||
// partialCoords to screenCoords bottomRight
|
||||
if(partialCoords.bottom() > virtualScreenCoords.bottom()) {
|
||||
partialTextureCoords.setBottom(partialTextureCoords.bottom() +
|
||||
(virtualScreenCoords.bottom() - partialCoords.bottom()));
|
||||
partialCoords.setBottom(virtualScreenCoords.bottom());
|
||||
}
|
||||
if(partialCoords.right() > virtualScreenCoords.right()) {
|
||||
partialTextureCoords.setRight(partialTextureCoords.right() +
|
||||
(virtualScreenCoords.right() - partialCoords.right()));
|
||||
partialCoords.setRight(virtualScreenCoords.right());
|
||||
}
|
||||
|
||||
partialCoords.translate(screenCoords.topLeft());
|
||||
drawTexturedRect(partialCoords, texture, partialTextureCoords);
|
||||
}
|
||||
}
|
||||
|
||||
if(mustStopDrawing)
|
||||
stopDrawing();
|
||||
}
|
||||
|
||||
void Graphics::drawFilledRect(const Rect& screenCoords)
|
||||
{
|
||||
assert(!m_drawing);
|
||||
|
||||
if(screenCoords.isEmpty())
|
||||
return;
|
||||
|
||||
// rect correction for opengl
|
||||
int right = screenCoords.right() + 1;
|
||||
int bottom = screenCoords.bottom() + 1;
|
||||
int top = screenCoords.top();
|
||||
int left = screenCoords.left();
|
||||
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
glBegin(GL_QUADS);
|
||||
|
||||
glVertex2i(left, top);
|
||||
glVertex2i(left, bottom);
|
||||
glVertex2i(right, bottom);
|
||||
glVertex2i(right, top);
|
||||
|
||||
glEnd();
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
}
|
||||
|
||||
|
||||
void Graphics::drawBoundingRect(const Rect& screenCoords,
|
||||
int innerLineWidth)
|
||||
{
|
||||
assert(!m_drawing);
|
||||
|
||||
if(screenCoords.isEmpty() || 2 * innerLineWidth > screenCoords.height())
|
||||
return;
|
||||
|
||||
// rect correction for opengl
|
||||
int right = screenCoords.right()+1;
|
||||
int bottom = screenCoords.bottom()+1;
|
||||
int top = screenCoords.top();
|
||||
int left = screenCoords.left();
|
||||
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
glBegin(GL_QUADS);
|
||||
|
||||
// top line
|
||||
glVertex2i(left, top);
|
||||
glVertex2i(left, top + innerLineWidth);
|
||||
glVertex2i(right, top + innerLineWidth);
|
||||
glVertex2i(right, top);
|
||||
|
||||
// left
|
||||
glVertex2i(left, screenCoords.top() + innerLineWidth);
|
||||
glVertex2i(left, bottom - innerLineWidth);
|
||||
glVertex2i(left + innerLineWidth, bottom - innerLineWidth);
|
||||
glVertex2i(left + innerLineWidth, screenCoords.top() + innerLineWidth);
|
||||
|
||||
// bottom line
|
||||
glVertex2i(left, bottom);
|
||||
glVertex2i(left, bottom - innerLineWidth);
|
||||
glVertex2i(right, bottom - innerLineWidth);
|
||||
glVertex2i(right, bottom);
|
||||
|
||||
// right line
|
||||
glVertex2i(right , top + innerLineWidth);
|
||||
glVertex2i(right , bottom - innerLineWidth);
|
||||
glVertex2i(right - innerLineWidth, bottom - innerLineWidth);
|
||||
glVertex2i(right - innerLineWidth, top + innerLineWidth);
|
||||
|
||||
glEnd();
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
}
|
||||
|
||||
void Graphics::bindColor(const Color& color)
|
||||
{
|
||||
Color tmp = color;
|
||||
tmp.setAlpha(std::min((uint8)m_opacity, color.a()));
|
||||
glColor4ubv(tmp.rgbaPtr());
|
||||
}
|
||||
|
||||
void Graphics::bindTexture(const TexturePtr& texture)
|
||||
{
|
||||
glBindTexture(GL_TEXTURE_2D, texture->getId());
|
||||
}
|
||||
|
||||
void Graphics::bindBlendFunc(Fw::BlendFunc blendType)
|
||||
{
|
||||
switch(blendType) {
|
||||
case Fw::BlendDefault:
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
break;
|
||||
case Fw::BlendColorzing:
|
||||
glBlendFunc(GL_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA);
|
||||
break;
|
||||
case Fw::BlendParticles:
|
||||
glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_ONE);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Graphics::startDrawing()
|
||||
{
|
||||
assert(!m_drawing);
|
||||
glBegin(GL_QUADS);
|
||||
m_drawing = true;
|
||||
}
|
||||
|
||||
void Graphics::stopDrawing()
|
||||
{
|
||||
assert(m_drawing);
|
||||
glEnd();
|
||||
m_drawing = false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user