00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef OVR_Render_GL_Device_h
00025 #define OVR_Render_GL_Device_h
00026
00027 #include "RenderTiny_Device.h"
00028
00029 #if defined(OVR_OS_WIN32)
00030 #include <Windows.h>
00031 #endif
00032
00033 #if defined(OVR_OS_MAC)
00034 #include <OpenGL/gl.h>
00035 #include <OpenGL/glext.h>
00036 #else
00037 #define GL_GLEXT_PROTOTYPES
00038 #include <GL/gl.h>
00039 #include <GL/glext.h>
00040 #endif
00041
00042 namespace OVR { namespace RenderTiny { namespace GL {
00043
00044 class RenderDevice;
00045
00046 class Buffer : public RenderTiny::Buffer
00047 {
00048 public:
00049 RenderDevice* Ren;
00050 size_t Size;
00051 GLenum Use;
00052 GLuint GLBuffer;
00053
00054 public:
00055 Buffer(RenderDevice* r) : Ren(r), Size(0), Use(0), GLBuffer(0) {}
00056 ~Buffer();
00057
00058 GLuint GetBuffer() { return GLBuffer; }
00059
00060 virtual size_t GetSize() { return Size; }
00061 virtual void* Map(size_t start, size_t size, int flags = 0);
00062 virtual bool Unmap(void *m);
00063 virtual bool Data(int use, const void* buffer, size_t size);
00064 };
00065
00066 class Texture : public RenderTiny::Texture
00067 {
00068 public:
00069 RenderDevice* Ren;
00070 GLuint TexId;
00071 int Width, Height;
00072
00073 Texture(RenderDevice* r, int w, int h);
00074 ~Texture();
00075
00076 virtual int GetWidth() const { return Width; }
00077 virtual int GetHeight() const { return Height; }
00078
00079 virtual void SetSampleMode(int);
00080
00081 virtual void Set(int slot, ShaderStage stage = Shader_Fragment) const;
00082 };
00083
00084 class Shader : public RenderTiny::Shader
00085 {
00086 public:
00087 GLuint GLShader;
00088
00089 Shader(RenderDevice*, ShaderStage st, GLuint s) : RenderTiny::Shader(st), GLShader(s) {}
00090 Shader(RenderDevice*, ShaderStage st, const char* src) : RenderTiny::Shader(st), GLShader(0)
00091 {
00092 Compile(src);
00093 }
00094 ~Shader()
00095 {
00096 if (GLShader)
00097 glDeleteShader(GLShader);
00098 }
00099 bool Compile(const char* src);
00100
00101 GLenum GLStage() const
00102 {
00103 switch (Stage)
00104 {
00105 default: OVR_ASSERT(0); return GL_NONE;
00106 case Shader_Vertex: return GL_VERTEX_SHADER;
00107 case Shader_Fragment: return GL_FRAGMENT_SHADER;
00108 }
00109 }
00110
00111
00112
00113 };
00114
00115 class ShaderSet : public RenderTiny::ShaderSet
00116 {
00117 public:
00118 GLuint Prog;
00119
00120 struct Uniform
00121 {
00122 String Name;
00123 int Location, Size;
00124 int Type;
00125 };
00126 Array<Uniform> UniformInfo;
00127
00128 int ProjLoc, ViewLoc;
00129 int TexLoc[8];
00130 bool UsesLighting;
00131 int LightingVer;
00132
00133 ShaderSet();
00134 ~ShaderSet();
00135
00136 virtual void SetShader(RenderTiny::Shader *s)
00137 {
00138 Shaders[s->GetStage()] = s;
00139 Shader* gls = (Shader*)s;
00140 glAttachShader(Prog, gls->GLShader);
00141 if (Shaders[Shader_Vertex] && Shaders[Shader_Fragment])
00142 Link();
00143 }
00144 virtual void UnsetShader(int stage)
00145 {
00146 Shader* gls = (Shader*)(RenderTiny::Shader*)Shaders[stage];
00147 if (gls)
00148 glDetachShader(Prog, gls->GLShader);
00149 Shaders[stage] = NULL;
00150 Link();
00151 }
00152
00153 virtual void Set(PrimitiveType prim) const;
00154
00155
00156
00157
00158 virtual bool SetUniform(const char* name, int n, const float* v);
00159 virtual bool SetUniform4x4f(const char* name, const Matrix4f& m);
00160
00161 bool Link();
00162 };
00163
00164 class RBuffer : public RefCountBase<RBuffer>
00165 {
00166 public:
00167 int Width, Height;
00168 GLuint BufId;
00169
00170 RBuffer(GLenum format, GLint w, GLint h);
00171 ~RBuffer();
00172 };
00173
00174 class RenderDevice : public RenderTiny::RenderDevice
00175 {
00176 Ptr<Shader> VertexShaders[VShader_Count];
00177 Ptr<Shader> FragShaders[FShader_Count];
00178
00179 Ptr<ShaderFill> DefaultFill;
00180
00181 Matrix4f Proj;
00182
00183 Ptr<Texture> CurRenderTarget;
00184 Array<Ptr<RBuffer> > DepthBuffers;
00185 GLuint CurrentFbo;
00186
00187 const LightingParams* Lighting;
00188
00189
00190 public:
00191 RenderDevice(const RendererParams& p);
00192
00193 virtual void SetRealViewport(const Viewport& vp);
00194
00195
00196
00197 virtual void Clear(float r = 0, float g = 0, float b = 0, float a = 1, float depth = 1);
00198 virtual void Rect(float left, float top, float right, float bottom) { OVR_UNUSED4(left,top,right,bottom); }
00199
00200 virtual void BeginRendering();
00201 virtual void SetDepthMode(bool enable, bool write, CompareFunc func = Compare_Less);
00202 virtual void SetWorldUniforms(const Matrix4f& proj);
00203
00204 RBuffer* GetDepthBuffer(int w, int h, int ms);
00205
00206 virtual void SetRenderTarget(RenderTiny::Texture* color,
00207 RenderTiny::Texture* depth = NULL, RenderTiny::Texture* stencil = NULL);
00208
00209 virtual void SetLighting(const LightingParams* lt);
00210
00211 virtual void Render(const Matrix4f& matrix, Model* model);
00212 virtual void Render(const ShaderFill* fill, RenderTiny::Buffer* vertices, RenderTiny::Buffer* indices,
00213 const Matrix4f& matrix, int offset, int count, PrimitiveType prim = Prim_Triangles);
00214
00215 virtual Buffer* CreateBuffer();
00216 virtual Texture* CreateTexture(int format, int width, int height, const void* data, int mipcount=1);
00217 virtual ShaderSet* CreateShaderSet() { return new ShaderSet; }
00218
00219 virtual ShaderFill *CreateSimpleFill() { return DefaultFill; }
00220
00221 virtual Shader *LoadBuiltinShader(ShaderStage stage, int shader);
00222
00223 void SetTexture(RenderTiny::ShaderStage, int slot, const Texture* t);
00224 };
00225
00226 }}}
00227
00228 #endif