openclonk/src/graphics/C4DrawGL.h

222 lines
6.8 KiB
C
Raw Normal View History

2009-05-08 13:28:41 +00:00
/*
* OpenClonk, http://www.openclonk.org
*
* 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
*
* Distributed under the terms of the ISC license; see accompanying file
* "COPYING" for details.
2009-05-08 13:28:41 +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
*
* 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 */
#if !defined(INC_StdGL) && !defined(USE_CONSOLE)
2009-05-08 13:28:41 +00:00
#define INC_StdGL
#ifdef _WIN32
#include <C4windowswrapper.h>
#endif
2009-05-08 13:28:41 +00:00
#include <GL/glew.h>
#ifdef USE_COCOA
#import "ObjectiveCAssociated.h"
#endif
#include <C4Draw.h>
#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;
};
// Uniform data we give the sprite shader (constants from its viewpoint)
enum C4SS_Uniforms
{
C4SSU_ProjectionMatrix, // 4x4
C4SSU_ModelViewMatrix, // 4x4
C4SSU_NormalMatrix, // 3x3, transpose-inverse of modelview matrix
C4SSU_ClrMod, // always
C4SSU_Gamma, // always
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
C4SSU_Bones, // for meshes
C4SSU_CullMode, // for meshes
C4SSU_Count
};
2009-05-08 13:28:41 +00:00
// one OpenGL context
class CStdGLCtx
#ifdef USE_COCOA
: public ObjectiveCAssociated
#endif
{
public:
CStdGLCtx(); // ctor
~CStdGLCtx() { Clear(); }; // dtor
2009-05-08 13:28:41 +00:00
void Clear(); // clear objects
2010-12-29 14:19:46 +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
bool Select(bool verbose = false); // select this context
void Deselect(); // select this context
2009-05-08 13:28:41 +00:00
bool PageFlip(); // present scene
2009-05-08 13:28:41 +00:00
static void Reinitialize();
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
#ifdef USE_WIN32_WINDOWS
static HGLRC hrc; // rendering context
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)
/*GLXContext*/void * ctx;
2009-05-08 13:28:41 +00:00
#endif
friend class CStdGL;
friend class C4Surface;
};
2009-05-08 13:28:41 +00:00
// OpenGL encapsulation
2011-10-03 14:34:08 +00:00
class CStdGL : public C4Draw
{
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;
// The orthographic projection matrix
float ProjectionMatrix[16];
// programs for drawing points, lines, quads
// 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;
public:
// General
void Clear();
void Default();
virtual bool IsOpenGL() { return true; }
2014-11-02 21:57:28 +00:00
virtual bool IsShaderific() { return true; }
virtual bool OnResolutionChanged(unsigned int iXRes, unsigned int iYRes); // reinit clipper for new resolution
// Clipper
bool UpdateClipper(); // set current clipper to render target
void UpdateProjectionMatrix();
virtual bool PrepareMaterial(StdMeshMatManager& mat_manager, StdMeshMaterialLoader& loader, StdMeshMaterial& mat);
// Surface
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);
#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
// Blit
void SetupMultiBlt(C4ShaderCall& call, const C4BltTransform* pTransform, GLuint baseTex, GLuint overlayTex, GLuint normalTex, DWORD dwOverlayModClr, StdMeshMatrix* out_modelview);
virtual void PerformMesh(StdMeshInstance &instance, float tx, float ty, float twdt, float thgt, DWORD dwPlayerColor, C4BltTransform* pTransform);
void FillBG(DWORD dwClr=0);
// Drawing
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);
// device objects
bool RestoreDeviceObjects(); // restore device dependent objects
bool InvalidateDeviceObjects(); // free device dependent objects
bool DeviceReady() { return !!pMainCtx; }
bool InitShaders(C4GroupSet* pGroups); // load shaders from given group
C4Shader* GetSpriteShader(int ssc);
C4Shader* GetSpriteShader(bool haveBase, bool haveOverlay, bool haveNormal);
struct
{
bool LowMaxVertexUniformCount;
} Workarounds;
protected:
2014-11-04 16:24:55 +00:00
bool CreatePrimarySurfaces(unsigned int iXRes, unsigned int iYRes, int iColorDepth, unsigned int iMonitor);
bool CheckGLError(const char *szAtOp);
virtual bool Error(const char *szMsg);
2009-05-08 13:28:41 +00:00
friend class C4Surface;
friend class C4TexRef;
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;
friend class C4ShaderCall;
};
2009-05-08 13:28:41 +00:00
// Global access pointer
extern CStdGL *pGL;
#endif // INC_StdGL