OGL32CTX: Implement a draw2DImage function using shaders.

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@14903 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
vincentlj
2014-01-04 17:12:30 +00:00
parent aa1bdcf10f
commit b28044e603
3 changed files with 91 additions and 2 deletions

View File

@@ -0,0 +1,9 @@
#version 130
uniform sampler2D texture;
in vec2 tc;
void main()
{
gl_FragColor = texture2D(texture, tc);
}

View File

@@ -0,0 +1,13 @@
#version 130
uniform vec2 center;
uniform vec2 size;
in vec2 position;
in vec2 texcoord;
out vec2 tc;
void main()
{
tc = texcoord;
gl_Position = vec4(position * size + center, 0., 1.);
}

View File

@@ -665,6 +665,7 @@ namespace GUIEngine
#include <iostream>
#include <assert.h>
#include <irrlicht.h>
#include "graphics/glwrap.hpp"
using namespace irr::gui;
using namespace irr::video;
@@ -1275,6 +1276,72 @@ namespace GUIEngine
} // render
// -----------------------------------------------------------------------
static GLuint quad_buffer;
static GLuint TexturedQuadShader;
static GLuint TexturedQuadAttribPosition;
static GLuint TexturedQuadAttribTexCoord;
static GLuint TexturedQuadUniformTexture;
static GLuint TexturedQuadUniformCenter;
static GLuint TexturedQuadUniformSize;
void draw2DImage(const video::ITexture* texture, const core::rect<s32>& destRect,
const core::rect<s32>& sourceRect, const core::rect<s32>* clipRect,
const video::SColor* const colors, bool useAlphaChannelOfTexture)
{
#ifndef OGL32CTX
GUIEngine::getDriver()->draw2DImage(texture, destRect, sourceRect, clipRect, colors, useAlphaChannelOfTexture);
#else
core::dimension2d<u32> frame_size =
GUIEngine::getDriver()->getCurrentRenderTargetSize();
const int screen_w = frame_size.Width;
const int screen_h = frame_size.Height;
float center_pos_x = destRect.UpperLeftCorner.X + destRect.LowerRightCorner.X;
center_pos_x /= screen_w;
center_pos_x -= 1;
float center_pos_y = destRect.UpperLeftCorner.Y + destRect.LowerRightCorner.Y;
center_pos_y /= screen_h;
center_pos_y = 1 - center_pos_y;
float width = destRect.LowerRightCorner.X - destRect.UpperLeftCorner.X;
width /= screen_w;
float height = destRect.LowerRightCorner.Y - destRect.UpperLeftCorner.Y;
height /= screen_h;
initGL();
const float quad_vertex[] = {
-1., -1., 0., 1.,
-1., 1., 0., 0.,
1., -1., 1., 1.,
1., 1., 1., 0.
};
if (!TexturedQuadShader) {
TexturedQuadShader = LoadProgram(file_manager->getAsset("shaders/texturedquad.vert").c_str(), file_manager->getAsset("shaders/texturedquad.frag").c_str());
glGenBuffers(1, &quad_buffer);
glBindBuffer(GL_ARRAY_BUFFER, quad_buffer);
glBufferData(GL_ARRAY_BUFFER, 16 * sizeof(float), quad_vertex, GL_STATIC_DRAW);
TexturedQuadAttribPosition = glGetAttribLocation(TexturedQuadShader, "position");
TexturedQuadAttribTexCoord = glGetAttribLocation(TexturedQuadShader, "texcoord");
TexturedQuadUniformTexture = glGetUniformLocation(TexturedQuadShader, "texture");
TexturedQuadUniformCenter = glGetUniformLocation(TexturedQuadShader, "center");
TexturedQuadUniformSize = glGetUniformLocation(TexturedQuadShader, "size");
}
glUseProgram(TexturedQuadShader);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, static_cast<const irr::video::COpenGLTexture*>(texture)->getOpenGLTextureName());
glUniform1i(TexturedQuadUniformTexture, 0);
glUniform2f(TexturedQuadUniformCenter, center_pos_x, center_pos_y);
glUniform2f(TexturedQuadUniformSize, width, height);
glEnableVertexAttribArray(TexturedQuadAttribPosition);
glEnableVertexAttribArray(TexturedQuadAttribTexCoord);
glBindBuffer(GL_ARRAY_BUFFER, quad_buffer);
glVertexAttribPointer(TexturedQuadAttribPosition, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glVertexAttribPointer(TexturedQuadAttribTexCoord, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (GLvoid *) (2 * sizeof(float)));
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisableVertexAttribArray(TexturedQuadAttribPosition);
glDisableVertexAttribArray(TexturedQuadAttribTexCoord);
glBindBuffer(GL_ARRAY_BUFFER, 0);
#endif
}
std::vector<irr::video::ITexture*> g_loading_icons;
@@ -1309,7 +1376,7 @@ namespace GUIEngine
const core::rect< s32 > source_area =
core::rect< s32 >(0, 0, texture_w, texture_h);
GUIEngine::getDriver()->draw2DImage( loading, dest_area, source_area,
draw2DImage( loading, dest_area, source_area,
0 /* no clipping */, 0,
true /* alpha */);
@@ -1329,7 +1396,7 @@ namespace GUIEngine
int y = screen_h - icon_size - ICON_MARGIN;
for (int n=0; n<icon_count; n++)
{
g_driver->draw2DImage(g_loading_icons[n],
draw2DImage(g_loading_icons[n],
core::rect<s32>(x, y, x+icon_size, y+icon_size),
core::rect<s32>(core::position2d<s32>(0, 0),
g_loading_icons[n]->getSize()),