Skip to content

Commit

Permalink
project 3d vertices to 2d screen coordinates when calculating render …
Browse files Browse the repository at this point in the history
…rectangle
  • Loading branch information
pieh committed Oct 24, 2011
1 parent bc49ac0 commit 8e04ad3
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 3 deletions.
4 changes: 4 additions & 0 deletions xbmc/guilib/GraphicContext.cpp
Expand Up @@ -696,15 +696,19 @@ CRect CGraphicContext::generateAABB(const CRect &rect) const


float z = 0.0f; float z = 0.0f;
ScaleFinalCoords(x1, y1, z); ScaleFinalCoords(x1, y1, z);
g_Windowing.Project(x1, y1, z);


z = 0.0f; z = 0.0f;
ScaleFinalCoords(x2, y2, z); ScaleFinalCoords(x2, y2, z);
g_Windowing.Project(x2, y2, z);


z = 0.0f; z = 0.0f;
ScaleFinalCoords(x3, y3, z); ScaleFinalCoords(x3, y3, z);
g_Windowing.Project(x3, y3, z);


z = 0.0f; z = 0.0f;
ScaleFinalCoords(x4, y4, z); ScaleFinalCoords(x4, y4, z);
g_Windowing.Project(x4, y4, z);


return CRect( min(min(min(x1, x2), x3), x4), return CRect( min(min(min(x1, x2), x3), x4),
min(min(min(y1, y2), y3), y4), min(min(min(y1, y2), y3), y4),
Expand Down
45 changes: 45 additions & 0 deletions xbmc/guilib/MatrixGLES.cpp
Expand Up @@ -341,6 +341,51 @@ void CMatrixGLES::LookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez, GLfloat cente
Translatef(-eyex, -eyey, -eyez); Translatef(-eyex, -eyey, -eyez);
} }


static void __gluMultMatrixVecf(const GLfloat matrix[16], const GLfloat in[4], GLfloat out[4])
{
int i;

for (i=0; i<4; i++)
{
out[i] = in[0] * matrix[0*4+i] +
in[1] * matrix[1*4+i] +
in[2] * matrix[2*4+i] +
in[3] * matrix[3*4+i];
}
}

// gluProject implementation taken from Mesa3D
bool CMatrixGLES::Project(GLfloat objx, GLfloat objy, GLfloat objz, const GLfloat modelMatrix[16], const GLfloat projMatrix[16], const GLint viewport[4], GLfloat* winx, GLfloat* winy, GLfloat* winz)
{
GLfloat in[4];
GLfloat out[4];

in[0]=objx;
in[1]=objy;
in[2]=objz;
in[3]=1.0;
__gluMultMatrixVecf(modelMatrix, in, out);
__gluMultMatrixVecf(projMatrix, out, in);
if (in[3] == 0.0)
return false;
in[0] /= in[3];
in[1] /= in[3];
in[2] /= in[3];
/* Map x, y and z to range 0-1 */
in[0] = in[0] * 0.5 + 0.5;
in[1] = in[1] * 0.5 + 0.5;
in[2] = in[2] * 0.5 + 0.5;

/* Map x,y to viewport */
in[0] = in[0] * viewport[2] + viewport[0];
in[1] = in[1] * viewport[3] + viewport[1];

*winx=in[0];
*winy=in[1];
*winz=in[2];
return true;
}

void CMatrixGLES::PrintMatrix(void) void CMatrixGLES::PrintMatrix(void)
{ {
for (int i=0; i<(int)MM_MATRIXSIZE; i++) for (int i=0; i<(int)MM_MATRIXSIZE; i++)
Expand Down
1 change: 1 addition & 0 deletions xbmc/guilib/MatrixGLES.h
Expand Up @@ -57,6 +57,7 @@ class CMatrixGLES
void MultMatrixf(const GLfloat *matrix); void MultMatrixf(const GLfloat *matrix);
void LookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez, GLfloat centerx, GLfloat centery, GLfloat centerz, GLfloat upx, GLfloat upy, GLfloat upz); void LookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez, GLfloat centerx, GLfloat centery, GLfloat centerz, GLfloat upx, GLfloat upy, GLfloat upz);
void PrintMatrix(void); void PrintMatrix(void);
bool Project(GLfloat objx, GLfloat objy, GLfloat objz, const GLfloat modelMatrix[16], const GLfloat projMatrix[16], const GLint viewport[4], GLfloat* winx, GLfloat* winy, GLfloat* winz);


protected: protected:
vector<GLfloat*> m_matrices[(int)MM_MATRIXSIZE]; vector<GLfloat*> m_matrices[(int)MM_MATRIXSIZE];
Expand Down
5 changes: 5 additions & 0 deletions xbmc/rendering/RenderSystem.h
Expand Up @@ -98,6 +98,11 @@ class CRenderSystemBase


virtual bool TestRender() = 0; virtual bool TestRender() = 0;


/**
* Project (x,y,z) 3d scene coordinates to (x,y) 2d screen coordinates
*/
virtual void Project(float &x, float &y, float &z) { }

void GetRenderVersion(unsigned int& major, unsigned int& minor) const; void GetRenderVersion(unsigned int& major, unsigned int& minor) const;
const CStdString& GetRenderVendor() const { return m_RenderVendor; } const CStdString& GetRenderVendor() const { return m_RenderVendor; }
const CStdString& GetRenderRenderer() const { return m_RenderRenderer; } const CStdString& GetRenderRenderer() const { return m_RenderRenderer; }
Expand Down
16 changes: 16 additions & 0 deletions xbmc/rendering/dx/RenderSystemDX.cpp
Expand Up @@ -806,6 +806,22 @@ void CRenderSystemDX::SetCameraPosition(const CPoint &camera, int screenWidth, i
D3DXMATRIX mtxProjection; D3DXMATRIX mtxProjection;
D3DXMatrixPerspectiveOffCenterLH(&mtxProjection, (-w - offset.x)*0.5f, (w - offset.x)*0.5f, (-h + offset.y)*0.5f, (h + offset.y)*0.5f, h, 100*h); D3DXMatrixPerspectiveOffCenterLH(&mtxProjection, (-w - offset.x)*0.5f, (w - offset.x)*0.5f, (-h + offset.y)*0.5f, (h + offset.y)*0.5f, h, 100*h);
m_pD3DDevice->SetTransform(D3DTS_PROJECTION, &mtxProjection); m_pD3DDevice->SetTransform(D3DTS_PROJECTION, &mtxProjection);

m_world = mtxWorld;
m_view = mtxView;
m_projection = mtxProjection;
m_viewPort = viewport;
}

void CRenderSystemDX::Project(float &x, float &y, float &z)
{
D3DXVECTOR3 vScreenCoord;
D3DXVECTOR3 vLocation(x, y, z);

D3DXVec3Project(&vScreenCoord, &vLocation, &m_viewPort, &m_projection, &m_view, &m_world);
x = vScreenCoord.x;
y = vScreenCoord.y;
z = 0;
} }


bool CRenderSystemDX::TestRender() bool CRenderSystemDX::TestRender()
Expand Down
7 changes: 7 additions & 0 deletions xbmc/rendering/dx/RenderSystemDX.h
Expand Up @@ -76,6 +76,8 @@ class CRenderSystemDX : public CRenderSystemBase


virtual bool TestRender(); virtual bool TestRender();


virtual void Project(float &x, float &y, float &z);

LPDIRECT3DDEVICE9 Get3DDevice() { return m_pD3DDevice; } LPDIRECT3DDEVICE9 Get3DDevice() { return m_pD3DDevice; }
int GetBackbufferCount() const { return m_D3DPP.BackBufferCount; } int GetBackbufferCount() const { return m_D3DPP.BackBufferCount; }


Expand Down Expand Up @@ -151,6 +153,11 @@ class CRenderSystemDX : public CRenderSystemBase
std::vector<ID3DResource*> m_resources; std::vector<ID3DResource*> m_resources;


bool m_inScene; ///< True if we're in a BeginScene()/EndScene() block bool m_inScene; ///< True if we're in a BeginScene()/EndScene() block

D3DVIEWPORT9 m_viewPort;
D3DXMATRIX m_projection;
D3DXMATRIX m_view;
D3DXMATRIX m_world;
}; };


#endif #endif
Expand Down
19 changes: 16 additions & 3 deletions xbmc/rendering/gl/RenderSystemGL.cpp
Expand Up @@ -19,14 +19,12 @@
* *
*/ */



#include "RenderSystemGL.h"
#include "system.h"


#ifdef HAS_GL #ifdef HAS_GL


#include "guilib/GraphicContext.h" #include "guilib/GraphicContext.h"
#include "settings/AdvancedSettings.h" #include "settings/AdvancedSettings.h"
#include "RenderSystemGL.h"
#include "utils/log.h" #include "utils/log.h"
#include "utils/GLUtils.h" #include "utils/GLUtils.h"
#include "utils/TimeUtils.h" #include "utils/TimeUtils.h"
Expand Down Expand Up @@ -445,9 +443,24 @@ void CRenderSystemGL::SetCameraPosition(const CPoint &camera, int screenWidth, i
glFrustum( (-w - offset.x)*0.5f, (w - offset.x)*0.5f, (-h + offset.y)*0.5f, (h + offset.y)*0.5f, h, 100*h); glFrustum( (-w - offset.x)*0.5f, (w - offset.x)*0.5f, (-h + offset.y)*0.5f, (h + offset.y)*0.5f, h, 100*h);
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);


glGetIntegerv(GL_VIEWPORT, m_viewPort);
glGetDoublev(GL_MODELVIEW_MATRIX, m_view);
glGetDoublev(GL_PROJECTION_MATRIX, m_projection);

g_graphicsContext.EndPaint(); g_graphicsContext.EndPaint();
} }


void CRenderSystemGL::Project(float &x, float &y, float &z)
{
GLdouble coordX, coordY, coordZ;
if (gluProject(x, y, z, m_view, m_projection, m_viewPort, &coordX, &coordY, &coordZ) == GLU_TRUE)
{
x = (float)coordX;
y = (float)(m_viewPort[3] - coordY);
z = 0;
}
}

bool CRenderSystemGL::TestRender() bool CRenderSystemGL::TestRender()
{ {
static float theta = 0.0; static float theta = 0.0;
Expand Down
9 changes: 9 additions & 0 deletions xbmc/rendering/gl/RenderSystemGL.h
Expand Up @@ -24,6 +24,7 @@


#pragma once #pragma once


#include "system.h"
#include "rendering/RenderSystem.h" #include "rendering/RenderSystem.h"


class CRenderSystemGL : public CRenderSystemBase class CRenderSystemGL : public CRenderSystemBase
Expand Down Expand Up @@ -60,6 +61,8 @@ class CRenderSystemGL : public CRenderSystemBase


virtual bool TestRender(); virtual bool TestRender();


virtual void Project(float &x, float &y, float &z);

virtual void GetGLSLVersion(int& major, int& minor); virtual void GetGLSLVersion(int& major, int& minor);


virtual void ResetGLErrors(); virtual void ResetGLErrors();
Expand All @@ -82,6 +85,12 @@ class CRenderSystemGL : public CRenderSystemBase


int m_glslMajor; int m_glslMajor;
int m_glslMinor; int m_glslMinor;

#ifdef HAS_GL
GLdouble m_view[16];
GLdouble m_projection[16];
GLint m_viewPort[4];
#endif
}; };


#endif // RENDER_SYSTEM_H #endif // RENDER_SYSTEM_H
15 changes: 15 additions & 0 deletions xbmc/rendering/gles/RenderSystemGLES.cpp
Expand Up @@ -395,9 +395,24 @@ void CRenderSystemGLES::SetCameraPosition(const CPoint &camera, int screenWidth,
g_matrices.Frustum( (-w - offset.x)*0.5f, (w - offset.x)*0.5f, (-h + offset.y)*0.5f, (h + offset.y)*0.5f, h, 100*h); g_matrices.Frustum( (-w - offset.x)*0.5f, (w - offset.x)*0.5f, (-h + offset.y)*0.5f, (h + offset.y)*0.5f, h, 100*h);
g_matrices.MatrixMode(MM_MODELVIEW); g_matrices.MatrixMode(MM_MODELVIEW);


glGetIntegerv(GL_VIEWPORT, m_viewPort);
glGetFloatv(GL_MODELVIEW_MATRIX, m_view);
glGetFloatv(GL_PROJECTION_MATRIX, m_projection);

g_graphicsContext.EndPaint(); g_graphicsContext.EndPaint();
} }


void CRenderSystemGLES::Platform(float &x, float &y, float &z)
{
GLfloat coordX, coordY, coordZ;
if (g_matrices.Project(x, y, z, m_view, m_projection, m_viewPort, &coordX, &coordY, &coordZ))
{
x = coordX;
y = (float)(m_viewPort[3] - coordY);
z = 0;
}
}

bool CRenderSystemGLES::TestRender() bool CRenderSystemGLES::TestRender()
{ {
static float theta = 0.0; static float theta = 0.0;
Expand Down
6 changes: 6 additions & 0 deletions xbmc/rendering/gles/RenderSystemGLES.h
Expand Up @@ -73,6 +73,8 @@ class CRenderSystemGLES : public CRenderSystemBase
virtual void RestoreHardwareTransform(); virtual void RestoreHardwareTransform();


virtual bool TestRender(); virtual bool TestRender();

virtual void Project(float &x, float &y, float &z);


void InitialiseGUIShader(); void InitialiseGUIShader();
void EnableGUIShader(ESHADERMETHOD method); void EnableGUIShader(ESHADERMETHOD method);
Expand Down Expand Up @@ -101,6 +103,10 @@ class CRenderSystemGLES : public CRenderSystemBase


CGUIShader **m_pGUIshader; // One GUI shader for each method CGUIShader **m_pGUIshader; // One GUI shader for each method
ESHADERMETHOD m_method; // Current GUI Shader method ESHADERMETHOD m_method; // Current GUI Shader method

GLfloat m_view[16];
GLfloat m_projection[16];
GLint m_viewPort[4];
}; };


#endif // RENDER_SYSTEM_H #endif // RENDER_SYSTEM_H

0 comments on commit 8e04ad3

Please sign in to comment.