2009-05-08 13:28:41 +00:00
|
|
|
/*
|
|
|
|
* OpenClonk, http://www.openclonk.org
|
|
|
|
*
|
2013-12-17 20:01:09 +00:00
|
|
|
* Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
|
|
|
|
* Copyright (c) 2010-2013, The OpenClonk Team and contributors
|
2009-05-08 13:28:41 +00:00
|
|
|
*
|
2013-12-17 20:01:09 +00:00
|
|
|
* Distributed under the terms of the ISC license; see accompanying file
|
|
|
|
* "COPYING" for details.
|
2009-05-08 13:28:41 +00:00
|
|
|
*
|
2013-12-17 20:01:09 +00:00
|
|
|
* "Clonk" is a registered trademark of Matthes Bender, used with permission.
|
|
|
|
* See accompanying file "TRADEMARK" for details.
|
2009-05-08 13:28:41 +00:00
|
|
|
*
|
2013-12-17 20:01:09 +00:00
|
|
|
* To redistribute this file separately, substitute the full license texts
|
|
|
|
* for the above references.
|
2009-05-08 13:28:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* OpenGL implementation of NewGfx */
|
|
|
|
|
2013-10-29 13:27:18 +00:00
|
|
|
|
|
|
|
#if !defined(INC_StdGL) && !defined(USE_CONSOLE)
|
2009-05-08 13:28:41 +00:00
|
|
|
#define INC_StdGL
|
|
|
|
|
2011-03-14 15:19:29 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <C4windowswrapper.h>
|
|
|
|
#endif
|
2015-03-25 22:07:50 +00:00
|
|
|
|
2009-05-08 13:28:41 +00:00
|
|
|
#include <GL/glew.h>
|
|
|
|
|
2012-09-02 11:56:53 +00:00
|
|
|
#ifdef USE_COCOA
|
|
|
|
#import "ObjectiveCAssociated.h"
|
|
|
|
#endif
|
2012-04-27 17:04:43 +00:00
|
|
|
#include <C4Draw.h>
|
2014-11-24 19:50:05 +00:00
|
|
|
#include <C4Shader.h>
|
2009-05-08 13:28:41 +00:00
|
|
|
|
2011-08-27 21:12:01 +00:00
|
|
|
class C4Window;
|
2009-05-08 13:28:41 +00:00
|
|
|
|
2014-10-07 19:57:02 +00:00
|
|
|
class C4DrawGLError: public std::exception
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
C4DrawGLError(const StdStrBuf& buf): Buf(buf) {}
|
|
|
|
virtual ~C4DrawGLError() throw() {}
|
|
|
|
|
|
|
|
virtual const char* what() const throw() { return Buf.getData(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
StdCopyStrBuf Buf;
|
|
|
|
};
|
|
|
|
|
2014-11-24 19:50:05 +00:00
|
|
|
// Uniform data we give the sprite shader (constants from its viewpoint)
|
|
|
|
enum C4SS_Uniforms
|
|
|
|
{
|
2015-12-20 06:37:36 +00:00
|
|
|
C4SSU_ProjectionMatrix, // 4x4
|
|
|
|
C4SSU_ModelViewMatrix, // 4x4
|
|
|
|
C4SSU_NormalMatrix, // 3x3, transpose-inverse of modelview matrix
|
|
|
|
|
2014-11-24 19:50:05 +00:00
|
|
|
C4SSU_ClrMod, // always
|
2015-08-31 15:34:01 +00:00
|
|
|
C4SSU_Gamma, // always
|
|
|
|
|
2014-11-24 19:50:05 +00:00
|
|
|
C4SSU_BaseTex, // C4SSC_BASE
|
|
|
|
C4SSU_OverlayTex, // C4SSC_OVERLAY
|
|
|
|
C4SSU_OverlayClr, // C4SSC_OVERLAY
|
|
|
|
|
|
|
|
C4SSU_LightTex, // C4SSC_LIGHT
|
|
|
|
C4SSU_LightTransform, // C4SSC_LIGHT
|
|
|
|
C4SSU_NormalTex, // C4SSC_LIGHT | C4SSC_NORMAL
|
|
|
|
|
|
|
|
C4SSU_AmbientTex, // C4SSC_LIGHT
|
|
|
|
C4SSU_AmbientTransform, // C4SSC_LIGHT
|
|
|
|
C4SSU_AmbientBrightness, // C4SSC_LIGHT
|
|
|
|
|
2015-02-27 20:24:29 +00:00
|
|
|
C4SSU_Bones, // for meshes
|
2015-07-21 02:11:19 +00:00
|
|
|
C4SSU_CullMode, // for meshes
|
2015-02-27 20:24:29 +00:00
|
|
|
|
2014-11-24 19:50:05 +00:00
|
|
|
C4SSU_Count
|
|
|
|
};
|
|
|
|
|
2009-05-08 13:28:41 +00:00
|
|
|
// one OpenGL context
|
|
|
|
class CStdGLCtx
|
2012-09-02 11:56:53 +00:00
|
|
|
#ifdef USE_COCOA
|
|
|
|
: public ObjectiveCAssociated
|
|
|
|
#endif
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CStdGLCtx(); // ctor
|
|
|
|
~CStdGLCtx() { Clear(); }; // dtor
|
2009-05-08 13:28:41 +00:00
|
|
|
|
2010-03-28 17:58:21 +00:00
|
|
|
void Clear(); // clear objects
|
2010-12-29 14:19:46 +00:00
|
|
|
|
2012-03-23 21:53:56 +00:00
|
|
|
#ifdef USE_WIN32_WINDOWS
|
2011-08-27 21:12:01 +00:00
|
|
|
bool Init(C4Window * pWindow, C4AbstractApp *pApp, HWND hWindow = NULL);
|
2010-12-29 14:19:46 +00:00
|
|
|
std::vector<int> EnumerateMultiSamples() const;
|
2009-05-08 13:28:41 +00:00
|
|
|
#else
|
2011-08-27 21:12:01 +00:00
|
|
|
bool Init(C4Window * pWindow, C4AbstractApp *pApp);
|
2009-05-08 13:28:41 +00:00
|
|
|
#endif
|
2010-12-29 14:19:46 +00:00
|
|
|
|
2010-03-28 17:58:21 +00:00
|
|
|
bool Select(bool verbose = false); // select this context
|
|
|
|
void Deselect(); // select this context
|
2009-05-08 13:28:41 +00:00
|
|
|
|
2010-03-28 17:58:21 +00:00
|
|
|
bool PageFlip(); // present scene
|
2009-05-08 13:28:41 +00:00
|
|
|
|
2015-03-26 14:17:51 +00:00
|
|
|
static void Reinitialize();
|
|
|
|
|
2010-03-28 17:58:21 +00:00
|
|
|
protected:
|
|
|
|
void SelectCommon();
|
|
|
|
// this handles are declared as pointers to structs
|
2011-08-27 21:12:01 +00:00
|
|
|
C4Window * pWindow; // window to draw in
|
2012-03-23 21:53:56 +00:00
|
|
|
#ifdef USE_WIN32_WINDOWS
|
2015-03-26 14:17:51 +00:00
|
|
|
static HGLRC hrc; // rendering context
|
2010-03-28 17:58:21 +00:00
|
|
|
HWND hWindow; // used if pWindow==NULL
|
|
|
|
HDC hDC; // device context handle
|
2010-12-29 14:19:46 +00:00
|
|
|
static bool InitGlew(HINSTANCE hInst);
|
2009-05-08 13:28:41 +00:00
|
|
|
#elif defined(USE_X11)
|
2010-03-28 17:58:21 +00:00
|
|
|
/*GLXContext*/void * ctx;
|
2009-05-08 13:28:41 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
friend class CStdGL;
|
2011-10-03 14:06:41 +00:00
|
|
|
friend class C4Surface;
|
2010-03-28 17:58:21 +00:00
|
|
|
};
|
2009-05-08 13:28:41 +00:00
|
|
|
|
|
|
|
// OpenGL encapsulation
|
2011-10-03 14:34:08 +00:00
|
|
|
class CStdGL : public C4Draw
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CStdGL();
|
|
|
|
~CStdGL();
|
|
|
|
protected:
|
|
|
|
|
|
|
|
int iPixelFormat; // used pixel format
|
|
|
|
|
|
|
|
GLenum sfcFmt; // texture surface format
|
|
|
|
CStdGLCtx * pMainCtx; // main GL context
|
|
|
|
CStdGLCtx *pCurrCtx; // current context (owned if fullscreen)
|
|
|
|
int iClrDpt; // color depth
|
|
|
|
// texture for smooth lines
|
|
|
|
GLuint lines_tex;
|
2015-12-20 06:37:36 +00:00
|
|
|
|
|
|
|
// The orthographic projection matrix
|
|
|
|
float ProjectionMatrix[16];
|
|
|
|
|
2014-10-08 01:48:11 +00:00
|
|
|
// programs for drawing points, lines, quads
|
2014-11-24 19:50:05 +00:00
|
|
|
|
|
|
|
// Sprite shaders -- there is a variety of shaders to avoid
|
|
|
|
// conditionals in the GLSL code.
|
|
|
|
C4Shader SpriteShader;
|
|
|
|
C4Shader SpriteShaderMod2;
|
|
|
|
C4Shader SpriteShaderBase;
|
|
|
|
C4Shader SpriteShaderBaseMod2;
|
|
|
|
C4Shader SpriteShaderBaseOverlay;
|
|
|
|
C4Shader SpriteShaderBaseOverlayMod2;
|
|
|
|
|
|
|
|
C4Shader SpriteShaderLight;
|
|
|
|
C4Shader SpriteShaderLightMod2;
|
|
|
|
C4Shader SpriteShaderLightBase;
|
|
|
|
C4Shader SpriteShaderLightBaseMod2;
|
|
|
|
C4Shader SpriteShaderLightBaseOverlay;
|
|
|
|
C4Shader SpriteShaderLightBaseOverlayMod2;
|
|
|
|
C4Shader SpriteShaderLightBaseNormal;
|
|
|
|
C4Shader SpriteShaderLightBaseNormalMod2;
|
|
|
|
C4Shader SpriteShaderLightBaseNormalOverlay;
|
|
|
|
C4Shader SpriteShaderLightBaseNormalOverlayMod2;
|
2010-03-28 17:58:21 +00:00
|
|
|
public:
|
|
|
|
// General
|
|
|
|
void Clear();
|
|
|
|
void Default();
|
|
|
|
virtual bool IsOpenGL() { return true; }
|
2014-11-02 21:57:28 +00:00
|
|
|
virtual bool IsShaderific() { return true; }
|
2010-03-28 17:58:21 +00:00
|
|
|
virtual bool OnResolutionChanged(unsigned int iXRes, unsigned int iYRes); // reinit clipper for new resolution
|
|
|
|
// Clipper
|
|
|
|
bool UpdateClipper(); // set current clipper to render target
|
2015-12-20 06:37:36 +00:00
|
|
|
void UpdateProjectionMatrix();
|
2015-09-12 22:14:18 +00:00
|
|
|
virtual bool PrepareMaterial(StdMeshMatManager& mat_manager, StdMeshMaterialLoader& loader, StdMeshMaterial& mat);
|
2010-03-28 17:58:21 +00:00
|
|
|
// Surface
|
2015-09-12 22:14:18 +00:00
|
|
|
virtual bool PrepareRendering(C4Surface * sfcToSurface); // check if/make rendering possible to given surface
|
|
|
|
virtual bool PrepareSpriteShader(C4Shader& shader, const char* name, int ssc, C4GroupSet* pGroups, const char* const* additionalDefines, const char* const* additionalSlices);
|
|
|
|
|
2011-08-27 21:12:01 +00:00
|
|
|
virtual CStdGLCtx *CreateContext(C4Window * pWindow, C4AbstractApp *pApp);
|
2012-03-23 21:53:56 +00:00
|
|
|
#ifdef USE_WIN32_WINDOWS
|
2011-08-27 14:20:39 +00:00
|
|
|
virtual CStdGLCtx *CreateContext(HWND hWindow, C4AbstractApp *pApp);
|
2009-05-08 13:28:41 +00:00
|
|
|
#endif
|
2010-03-28 17:58:21 +00:00
|
|
|
// Blit
|
2015-12-20 06:37:36 +00:00
|
|
|
void SetupMultiBlt(C4ShaderCall& call, const C4BltTransform* pTransform, GLuint baseTex, GLuint overlayTex, GLuint normalTex, DWORD dwOverlayModClr, StdMeshMatrix* out_modelview);
|
2011-09-30 20:15:51 +00:00
|
|
|
virtual void PerformMesh(StdMeshInstance &instance, float tx, float ty, float twdt, float thgt, DWORD dwPlayerColor, C4BltTransform* pTransform);
|
2010-03-28 17:58:21 +00:00
|
|
|
void FillBG(DWORD dwClr=0);
|
|
|
|
// Drawing
|
2015-09-12 22:14:18 +00:00
|
|
|
virtual void PerformMultiPix(C4Surface* sfcTarget, const C4BltVertex* vertices, unsigned int n_vertices, C4ShaderCall* shader_call);
|
|
|
|
virtual void PerformMultiLines(C4Surface* sfcTarget, const C4BltVertex* vertices, unsigned int n_vertices, float width, C4ShaderCall* shader_call);
|
|
|
|
virtual void PerformMultiTris(C4Surface* sfcTarget, const C4BltVertex* vertices, unsigned int n_vertices, const C4BltTransform* pTransform, C4TexRef* pTex, C4TexRef* pOverlay, C4TexRef* pNormal, DWORD dwOverlayClrMod, C4ShaderCall* shader_call);
|
|
|
|
void PerformMultiBlt(C4Surface* sfcTarget, DrawOperation op, const C4BltVertex* vertices, unsigned int n_vertices, bool has_tex);
|
2010-03-28 17:58:21 +00:00
|
|
|
// device objects
|
|
|
|
bool RestoreDeviceObjects(); // restore device dependent objects
|
|
|
|
bool InvalidateDeviceObjects(); // free device dependent objects
|
|
|
|
bool DeviceReady() { return !!pMainCtx; }
|
2014-11-25 16:40:10 +00:00
|
|
|
bool InitShaders(C4GroupSet* pGroups); // load shaders from given group
|
|
|
|
C4Shader* GetSpriteShader(int ssc);
|
|
|
|
C4Shader* GetSpriteShader(bool haveBase, bool haveOverlay, bool haveNormal);
|
2010-03-28 17:58:21 +00:00
|
|
|
|
2015-09-04 00:38:53 +00:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
bool LowMaxVertexUniformCount;
|
|
|
|
} Workarounds;
|
|
|
|
|
2010-03-28 17:58:21 +00:00
|
|
|
protected:
|
2014-11-04 16:24:55 +00:00
|
|
|
bool CreatePrimarySurfaces(unsigned int iXRes, unsigned int iYRes, int iColorDepth, unsigned int iMonitor);
|
2010-03-28 17:58:21 +00:00
|
|
|
|
|
|
|
bool CheckGLError(const char *szAtOp);
|
2010-08-03 17:02:00 +00:00
|
|
|
virtual bool Error(const char *szMsg);
|
2009-05-08 13:28:41 +00:00
|
|
|
|
2011-10-03 14:06:41 +00:00
|
|
|
friend class C4Surface;
|
2011-10-03 14:16:38 +00:00
|
|
|
friend class C4TexRef;
|
2011-09-30 20:15:51 +00:00
|
|
|
friend class C4Pattern;
|
2009-05-08 13:28:41 +00:00
|
|
|
friend class CStdGLCtx;
|
|
|
|
friend class C4StartupOptionsDlg;
|
|
|
|
friend class C4FullScreen;
|
2011-08-27 21:12:01 +00:00
|
|
|
friend class C4Window;
|
2015-02-16 14:34:53 +00:00
|
|
|
friend class C4ShaderCall;
|
2010-03-28 17:58:21 +00:00
|
|
|
};
|
2009-05-08 13:28:41 +00:00
|
|
|
|
|
|
|
// Global access pointer
|
|
|
|
extern CStdGL *pGL;
|
|
|
|
|
|
|
|
#endif // INC_StdGL
|