OldSchoolHack

Register / Login English

More advanced ISurface drawing

not available
  • Category: Sourcecode
  • Developer:
  • Uploaded by: System
  • Uploaded at:
  • System: Windows
Download (32.96 KB)

VirusTotal Result: 0/56

virustotal

Description

Firstly u need more classes from Source Engine - it is IIndexBuffer and VertexBuffer:

CPP Code:
  1. struct VertexDesc_t
  2. {
  3. // These can be set to zero if there are pointers to dummy buffers, when the
  4. // actual buffer format doesn't contain the data but it needs to be safe to
  5. // use all the CMeshBuilder functions.
  6. int m_VertexSize_Position;
  7. int m_VertexSize_BoneWeight;
  8. int m_VertexSize_BoneMatrixIndex;
  9. int m_VertexSize_Normal;
  10. int m_VertexSize_Color;
  11. int m_VertexSize_Specular;
  12. int m_VertexSize_TexCoord[8];
  13. int m_VertexSize_TangentS;
  14. int m_VertexSize_TangentT;
  15. int m_VertexSize_Wrinkle;
  16.  
  17. int m_VertexSize_UserData;
  18.  
  19. int m_ActualVertexSize; // Size of the vertices.. Some of the m_VertexSize_ elements above
  20. // are set to this value and some are set to zero depending on which
  21. // fields exist in a buffer's vertex format.
  22.  
  23. // The type of compression applied to this vertex data
  24. VertexCompressionType_t m_CompressionType;
  25.  
  26. // Number of bone weights per vertex...
  27. int m_NumBoneWeights;
  28.  
  29. // Pointers to our current vertex data
  30. float *m_pPosition;
  31.  
  32. float *m_pBoneWeight;
  33.  
  34. #ifndef NEW_SKINNING
  35. unsigned char *m_pBoneMatrixIndex;
  36. #else
  37. float *m_pBoneMatrixIndex;
  38. #endif
  39.  
  40. float *m_pNormal;
  41.  
  42. unsigned char *m_pColor;
  43. unsigned char *m_pSpecular;
  44. float *m_pTexCoord[8];
  45.  
  46. // Tangent space *associated with one particular set of texcoords*
  47. float *m_pTangentS;
  48. float *m_pTangentT;
  49.  
  50. float *m_pWrinkle;
  51.  
  52. // user data
  53. float *m_pUserData;
  54.  
  55. // The first vertex index (used for buffered vertex buffers, or cards that don't support stream offset)
  56. int m_nFirstVertex;
  57.  
  58. // The offset in bytes of the memory we're writing into
  59. // from the start of the D3D buffer (will be 0 for static meshes)
  60. unsigned int m_nOffset;
  61.  
  62. #ifdef DEBUG_WRITE_COMBINE
  63. int m_nLastWrittenField;
  64. unsigned char* m_pLastWrittenAddress;
  65. #endif
  66. };
  67.  
  68. class IVertexBuffer
  69. {
  70. public:
  71. // NOTE: The following two methods are only valid for static vertex buffers
  72. // Returns the number of vertices and the format of the vertex buffer
  73. virtual int VertexCount() const = 0;
  74. virtual VertexFormat_t GetVertexFormat() const = 0;
  75.  
  76. // Is this vertex buffer dynamic?
  77. virtual bool IsDynamic() const = 0;
  78.  
  79. // NOTE: For dynamic vertex buffers only!
  80. // Casts the memory of the dynamic vertex buffer to the appropriate type
  81. virtual void BeginCastBuffer(VertexFormat_t format) = 0;
  82. virtual void EndCastBuffer() = 0;
  83.  
  84. // Returns the number of vertices that can still be written into the buffer
  85. virtual int GetRoomRemaining() const = 0;
  86.  
  87. virtual bool Lock(int nVertexCount, bool bAppend, VertexDesc_t &desc) = 0;
  88. virtual void Unlock(int nVertexCount, VertexDesc_t &desc) = 0;
  89.  
  90. // Spews the mesh data
  91. virtual void Spew(int nVertexCount, const VertexDesc_t &desc) = 0;
  92.  
  93. // Call this in debug mode to make sure our data is good.
  94. virtual void ValidateData(int nVertexCount, const VertexDesc_t & desc) = 0;
  95. };
  96.  
  97. struct IndexDesc_t
  98. {
  99. // Pointers to the index data
  100. unsigned short *m_pIndices;
  101.  
  102. // The offset in bytes of the memory we're writing into
  103. // from the start of the D3D buffer (will be 0 for static meshes)
  104. unsigned int m_nOffset;
  105.  
  106. // The first index (used for buffered index buffers, or cards that don't support stream offset)
  107. unsigned int m_nFirstIndex;
  108.  
  109. // 1 if the device is active, 0 if the device isn't active.
  110. // Faster than doing if checks for null m_pIndices if someone is
  111. // trying to write the m_pIndices while the device is inactive.
  112. unsigned char m_nIndexSize;
  113. };
  114.  
  115.  
  116. class IIndexBuffer
  117. {
  118. public:
  119. // NOTE: The following two methods are only valid for static index buffers
  120. // Returns the number of indices and the format of the index buffer
  121. virtual int IndexCount() const = 0;
  122. virtual MaterialIndexFormat_t IndexFormat() const = 0;
  123.  
  124. // Is this index buffer dynamic?
  125. virtual bool IsDynamic() const = 0;
  126.  
  127. // NOTE: For dynamic index buffers only!
  128. // Casts the memory of the dynamic index buffer to the appropriate type
  129. virtual void BeginCastBuffer(MaterialIndexFormat_t format) = 0;
  130. virtual void EndCastBuffer() = 0;
  131.  
  132. // Returns the number of indices that can still be written into the buffer
  133. virtual int GetRoomRemaining() const = 0;
  134.  
  135. // Locks, unlocks the index buffer
  136. virtual bool Lock(int nMaxIndexCount, bool bAppend, IndexDesc_t &desc) = 0;
  137. virtual void Unlock(int nWrittenIndexCount, IndexDesc_t &desc) = 0;
  138.  
  139. // FIXME: Remove this!!
  140. // Locks, unlocks the index buffer for modify
  141. virtual void ModifyBegin(bool bReadOnly, int nFirstIndex, int nIndexCount, IndexDesc_t& desc) = 0;
  142. virtual void ModifyEnd(IndexDesc_t& desc) = 0;
  143.  
  144. // Spews the mesh data
  145. virtual void Spew(int nIndexCount, const IndexDesc_t &desc) = 0;
  146.  
  147. // Ensures the data in the index buffer is valid
  148. virtual void ValidateData(int nIndexCount, const IndexDesc_t &desc) = 0;
  149. };

Then IMesh, which combines both classes:
CPP Code:
  1.  
  2. struct MeshDesc_t : VertexDesc_t, IndexDesc_t
  3. {
  4. };
  5.  
  6. //-----------------------------------------------------------------------------
  7. // Interface to the mesh - needs to contain an IVertexBuffer and an IIndexBuffer to emulate old mesh behavior
  8. //-----------------------------------------------------------------------------
  9. class IMesh : public IVertexBuffer, public IIndexBuffer
  10. {
  11. public:
  12. // -----------------------------------
  13.  
  14. // Sets/gets the primitive type
  15. virtual void SetPrimitiveType(MaterialPrimitiveType_t type) = 0;
  16.  
  17. // Draws the mesh
  18. virtual void Draw(int nFirstIndex = -1, int nIndexCount = 0) = 0;
  19.  
  20. virtual void SetColorMesh(IMesh *pColorMesh, int nVertexOffset) = 0;
  21.  
  22. // Draw a list of (lists of) primitives. Batching your lists together that use
  23. // the same lightmap, material, vertex and index buffers with multipass shaders
  24. // can drastically reduce state-switching overhead.
  25. // NOTE: this only works with STATIC meshes.
  26. virtual void Draw(CPrimList *pLists, int nLists) = 0;
  27.  
  28. // Copy verts and/or indices to a mesh builder. This only works for temp meshes!
  29. virtual void CopyToMeshBuilder(
  30. int iStartVert, // Which vertices to copy.
  31. int nVerts,
  32. int iStartIndex, // Which indices to copy.
  33. int nIndices,
  34. int indexOffset, // This is added to each index.
  35. CMeshBuilder &builder) = 0;
  36.  
  37. // Spews the mesh data
  38. virtual void Spew(int nVertexCount, int nIndexCount, const MeshDesc_t &desc) = 0;
  39.  
  40. // Call this in debug mode to make sure our data is good.
  41. virtual void ValidateData(int nVertexCount, int nIndexCount, const MeshDesc_t &desc) = 0;
  42.  
  43. // New version
  44. // Locks/unlocks the mesh, providing space for nVertexCount and nIndexCount.
  45. // nIndexCount of -1 means don't lock the index buffer...
  46. virtual void LockMesh(int nVertexCount, int nIndexCount, MeshDesc_t &desc, void* pad = nullptr) = 0;
  47. virtual void ModifyBegin(int nFirstVertex, int nVertexCount, int nFirstIndex, int nIndexCount, MeshDesc_t& desc) = 0;
  48. virtual void ModifyEnd(MeshDesc_t& desc) = 0;
  49. virtual void UnlockMesh(int nVertexCount, int nIndexCount, MeshDesc_t &desc) = 0;
  50.  
  51. virtual void ModifyBeginEx(bool bReadOnly, int nFirstVertex, int nVertexCount, int nFirstIndex, int nIndexCount, MeshDesc_t &desc) = 0;
  52.  
  53. virtual void SetFlexMesh(IMesh *pMesh, int nVertexOffset) = 0;
  54.  
  55. virtual void DisableFlexMesh() = 0;
  56.  
  57. virtual void MarkAsDrawn() = 0;
  58.  
  59. };
  60.  

These classes are managed by the appropriate builders:

CPP Code:
  1.  
  2. class CIndexBuilder : IndexDesc_t
  3. {
  4. public:
  5. CIndexBuilder();
  6. CIndexBuilder(IIndexBuffer *pIndexBuffer, MaterialIndexFormat_t fmt = MATERIAL_INDEX_FORMAT_UNKNOWN);
  7. ~CIndexBuilder(){}
  8.  
  9. // Begins, ends modification of the index buffer (returns true if the lock succeeded)
  10. // A lock may not succeed if append is set to true and there isn't enough room
  11. // NOTE: Append is only used with dynamic index buffers; it's ignored for static buffers
  12. bool Lock(int nMaxIndexCount, int nIndexOffset, bool bAppend = false);
  13. void Unlock();
  14.  
  15. // Spews the current data
  16. // NOTE: Can only be called during a lock/unlock block
  17. void SpewData() const;
  18.  
  19. // Returns the number of indices we can fit into the buffer without needing to discard
  20. int GetRoomRemaining() const;
  21.  
  22. // Binds this index buffer
  23. void Bind(IMatRenderContext *pContext) const;
  24.  
  25. // Returns the byte offset
  26. int Offset() const;
  27.  
  28. // Begins, ends modification of the index buffer
  29. // NOTE: IndexOffset is the number to add to all indices written into the buffer;
  30. // useful when using dynamic vertex buffers.
  31. void Begin(IIndexBuffer *pIndexBuffer, int nMaxIndexCount, int nIndexOffset = 0);
  32. void End(bool bSpewData = false);
  33.  
  34. // Locks the index buffer to modify existing data
  35. // Passing nVertexCount == -1 says to lock all the vertices for modification.
  36. // Pass 0 for nIndexCount to not lock the index buffer.
  37. void BeginModify(IIndexBuffer *pIndexBuffer, int nFirstIndex = 0, int nIndexCount = 0, int nIndexOffset = 0);
  38. void EndModify(bool bSpewData = false);
  39.  
  40. // returns the number of indices
  41. int IndexCount() const;
  42.  
  43. // Returns the total number of indices across all Locks()
  44. int TotalIndexCount() const;
  45.  
  46. // Resets the mesh builder so it points to the start of everything again
  47. void Reset() const;
  48.  
  49. // Selects the nth Index
  50. void SelectIndex(int nBufferIndex) const;
  51.  
  52. // Advances the current index by one
  53. void AdvanceIndex();
  54. void AdvanceIndices(int nIndexCount);
  55.  
  56. int GetCurrentIndex() const;
  57. int GetFirstIndex() const;
  58.  
  59. unsigned short const* Index() const;
  60.  
  61. // Used to define the indices (only used if you aren't using primitives)
  62. void Index(unsigned short nIndex) const;
  63.  
  64. // Fast Index! No need to call advance index, and no random access allowed
  65. void FastIndex(unsigned short nIndex);
  66.  
  67. // NOTE: This version is the one you really want to achieve write-combining;
  68. // Write combining only works if you write in 4 bytes chunks.
  69. void FastIndex2(unsigned short nIndex1, unsigned short nIndex2);
  70.  
  71. // Generates indices for a particular primitive type
  72. void GenerateIndices(MaterialPrimitiveType_t primitiveType, int nIndexCount);
  73.  
  74. // FIXME: Remove! Backward compat so we can use this from a CMeshBuilder.
  75. void AttachBegin(IMesh* pMesh, int nMaxIndexCount, const MeshDesc_t &desc);
  76. void AttachEnd();
  77. void AttachBeginModify(IMesh* pMesh, int nFirstIndex, int nIndexCount, const MeshDesc_t &desc);
  78. void AttachEndModify();
  79.  
  80. private:
  81. // The mesh we're modifying
  82. IIndexBuffer *m_pIndexBuffer;
  83.  
  84. // Max number of indices
  85. int m_nMaxIndexCount;
  86.  
  87. // Number of indices
  88. int m_nIndexCount;
  89.  
  90. // Offset to add to each index as it's written into the buffer
  91. int m_nIndexOffset;
  92.  
  93. // The current index
  94. mutable int m_nCurrentIndex;
  95.  
  96. // Total number of indices appended
  97. int m_nTotalIndexCount;
  98.  
  99. // First index buffer offset + first index
  100. unsigned int m_nBufferOffset;
  101. unsigned int m_nBufferFirstIndex;
  102.  
  103. // Used to make sure Begin/End calls and BeginModify/EndModify calls match.
  104. bool m_bModify;
  105. };
  106.  
  107. class CVertexBuilder : VertexDesc_t
  108. {
  109. public:
  110. CVertexBuilder();
  111. CVertexBuilder(IVertexBuffer *pVertexBuffer, VertexFormat_t fmt = 0);
  112. ~CVertexBuilder();
  113.  
  114. // Begins, ends modification of the index buffer (returns true if the lock succeeded)
  115. // A lock may not succeed if append is set to true and there isn't enough room
  116. // NOTE: Append is only used with dynamic index buffers; it's ignored for static buffers
  117. bool Lock(int nMaxIndexCount, bool bAppend = false);
  118. void Unlock();
  119.  
  120. // Spews the current data
  121. // NOTE: Can only be called during a lock/unlock block
  122. void SpewData() const;
  123.  
  124. // Returns the number of indices we can fit into the buffer without needing to discard
  125. int GetRoomRemaining() const;
  126.  
  127. // Binds this vertex buffer
  128. void Bind(IMatRenderContext *pContext, int nStreamID, VertexFormat_t usage = 0) const;
  129.  
  130. // Returns the byte offset
  131. int Offset() const;
  132.  
  133. // This must be called before Begin, if a vertex buffer with a compressed format is to be used
  134. void SetCompressionType(VertexCompressionType_t compressionType);
  135. static void ValidateCompressionType();
  136.  
  137. void Begin(IVertexBuffer *pVertexBuffer, int nVertexCount, int *nFirstVertex);
  138. void Begin(IVertexBuffer *pVertexBuffer, int nVertexCount);
  139.  
  140. // Use this when you're done writing
  141. // Set bDraw to true to call m_pMesh->Draw automatically.
  142. void End(bool bSpewData = false);
  143.  
  144. // Locks the vertex buffer to modify existing data
  145. // Passing nVertexCount == -1 says to lock all the vertices for modification.
  146. void BeginModify(IVertexBuffer *pVertexBuffer, int nFirstVertex = 0, int nVertexCount = -1);
  147. void EndModify(bool bSpewData = false);
  148.  
  149. // returns the number of vertices
  150. int VertexCount() const;
  151.  
  152. // Returns the total number of vertices across all Locks()
  153. int TotalVertexCount() const;
  154.  
  155. // Resets the mesh builder so it points to the start of everything again
  156. void Reset();
  157.  
  158. // Returns the size of the vertex
  159. int VertexSize() { return m_ActualVertexSize; }
  160.  
  161. // returns the data size of a given texture coordinate
  162. int TextureCoordinateSize(int nTexCoordNumber) { return m_VertexSize_TexCoord[nTexCoordNumber]; }
  163.  
  164. // Returns the base vertex memory pointer
  165. void* BaseVertexData() const;
  166.  
  167. // Selects the nth Vertex and Index
  168. void SelectVertex(int idx);
  169.  
  170. // Advances the current vertex and index by one
  171. void AdvanceVertex();
  172. void AdvanceVertices(int nVerts);
  173.  
  174. int GetCurrentVertex() const;
  175. int GetFirstVertex() const;
  176.  
  177. // Data retrieval...
  178. const float *Position() const;
  179.  
  180. const float *Normal() const;
  181.  
  182. unsigned int Color() const;
  183.  
  184. unsigned char *Specular() const;
  185.  
  186. const float *TexCoord(int stage) const;
  187.  
  188. const float *TangentS() const;
  189. const float *TangentT() const;
  190.  
  191. const float *BoneWeight() const;
  192. float Wrinkle() const;
  193.  
  194. int NumBoneWeights() const;
  195. #ifndef NEW_SKINNING
  196. unsigned char *BoneMatrix() const;
  197. #else
  198. float *BoneMatrix() const;
  199. #endif
  200.  
  201. // position setting
  202. void Position3f(float x, float y, float z) const;
  203. void Position3fv(const float *v) const;
  204.  
  205. // normal setting
  206. void Normal3f(float nx, float ny, float nz) const;
  207. void Normal3fv(const float *n) const;
  208. void NormalDelta3fv(const float *n) const;
  209. void NormalDelta3f(float nx, float ny, float nz) const;
  210. // normal setting (templatized for code which needs to support compressed vertices)
  211. template <VertexCompressionType_t T> void CompressedNormal3f(float nx, float ny, float nz) const;
  212. template <VertexCompressionType_t T> void CompressedNormal3fv(const float *n) const;
  213.  
  214. // color setting
  215. void Color3f(float r, float g, float b) const;
  216. void Color3fv(const float *rgb) const;
  217. void Color4f(float r, float g, float b, float a) const;
  218. void Color4fv(const float *rgba) const;
  219.  
  220. // Faster versions of color
  221. void Color3ub(unsigned char r, unsigned char g, unsigned char b) const;
  222. void Color3ubv(unsigned char const* rgb) const;
  223. void Color4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a) const;
  224. void Color4ubv(unsigned char const* rgba) const;
  225.  
  226. // specular color setting
  227. void Specular3f(float r, float g, float b) const;
  228. void Specular3fv(const float *rgb) const;
  229. void Specular4f(float r, float g, float b, float a) const;
  230. void Specular4fv(const float *rgba) const;
  231.  
  232. // Faster version of specular
  233. void Specular3ub(unsigned char r, unsigned char g, unsigned char b) const;
  234. void Specular3ubv(unsigned char const *c) const;
  235. void Specular4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a) const;
  236. void Specular4ubv(unsigned char const *c) const;
  237.  
  238. // texture coordinate setting
  239. void TexCoord1f(int stage, float s) const;
  240. void TexCoord2f(int stage, float s, float t) const;
  241. void TexCoord2fv(int stage, const float *st) const;
  242. void TexCoord3f(int stage, float s, float t, float u) const;
  243. void TexCoord3fv(int stage, const float *stu) const;
  244. void TexCoord4f(int stage, float s, float t, float u, float w) const;
  245. void TexCoord4fv(int stage, const float *stuv) const;
  246.  
  247. void TexCoordSubRect2f(int stage, float s, float t, float offsetS, float offsetT, float scaleS, float scaleT) const;
  248. void TexCoordSubRect2fv(int stage, const float *st, const float *offset, const float *scale) const;
  249.  
  250. // tangent space
  251. void TangentS3f(float sx, float sy, float sz) const;
  252. void TangentS3fv(const float* s) const;
  253.  
  254. void TangentT3f(float tx, float ty, float tz) const;
  255. void TangentT3fv(const float* t) const;
  256.  
  257. // Wrinkle
  258. void Wrinkle1f(float flWrinkle) const;
  259.  
  260. // bone weights
  261. void BoneWeight(int idx, float weight) const;
  262. // bone weights (templatized for code which needs to support compressed vertices)
  263. template <VertexCompressionType_t T> void CompressedBoneWeight3fv(const float * pWeights) const;
  264.  
  265. // bone matrix index
  266. void BoneMatrix(int idx, int matrixIndex) const;
  267.  
  268. // Generic per-vertex data
  269. void UserData(const float* pData) const;
  270. // Generic per-vertex data (templatized for code which needs to support compressed vertices)
  271. template <VertexCompressionType_t T> void CompressedUserData(const float* pData) const;
  272.  
  273. // Fast Vertex! No need to call advance vertex, and no random access allowed.
  274. // WARNING - these are low level functions that are intended only for use
  275. // in the software vertex skinner.
  276. void FastVertex(/*ModelVertexDX7_t*/ const int &vertex);
  277. void FastVertexSSE(/*ModelVertexDX7_t*/ const int &vertex);
  278.  
  279. // store 4 dx7 vertices fast. for special sse dx7 pipeline
  280. void Fast4VerticesSSE(
  281. /*ModelVertexDX7_t*/ const int *vtx_a,
  282. /*ModelVertexDX7_t*/ const int *vtx_b,
  283. /*ModelVertexDX7_t*/ const int *vtx_c,
  284. /*ModelVertexDX7_t*/ const int *vtx_d);
  285.  
  286. void FastVertex(/*ModelVertexDX8_t*/ const float &vertex);
  287. void FastVertexSSE(/*ModelVertexDX8_t*/ const float &vertex);
  288.  
  289. // Add number of verts and current vert since FastVertex routines do not update.
  290. void FastAdvanceNVertices(int n);
  291.  
  292. #if defined( _X360 )
  293. void VertexDX8ToX360(const ModelVertexDX8_t &vertex);
  294. #endif
  295.  
  296. // FIXME: Remove! Backward compat so we can use this from a CMeshBuilder.
  297. void AttachBegin(IMesh* pMesh, int nMaxVertexCount, const MeshDesc_t &desc);
  298. void AttachEnd();
  299. void AttachBeginModify(IMesh* pMesh, int nFirstVertex, int nVertexCount, const MeshDesc_t &desc);
  300. void AttachEndModify();
  301.  
  302. private:
  303. // The vertex buffer we're modifying
  304. IVertexBuffer *m_pVertexBuffer;
  305.  
  306. // Used to make sure Begin/End calls and BeginModify/EndModify calls match.
  307. bool m_bModify;
  308.  
  309. // Max number of indices and vertices
  310. int m_nMaxVertexCount;
  311.  
  312. // Number of indices and vertices
  313. int m_nVertexCount;
  314.  
  315. // The current vertex and index
  316. mutable int m_nCurrentVertex;
  317.  
  318. // Optimization: Pointer to the current pos, norm, texcoord, and color
  319. mutable float *m_pCurrPosition;
  320. mutable float *m_pCurrNormal;
  321. mutable float *m_pCurrTexCoord[8];
  322. mutable unsigned char *m_pCurrColor;
  323.  
  324. // Total number of vertices appended
  325. int m_nTotalVertexCount;
  326.  
  327. // First vertex buffer offset + index
  328. unsigned int m_nBufferOffset;
  329. unsigned int m_nBufferFirstVertex;
  330.  
  331. #if ( COMPRESSED_NORMALS_TYPE == COMPRESSED_NORMALS_COMBINEDTANGENTS_UBYTE4 )
  332. // Debug checks to make sure we write userdata4/tangents AFTER normals
  333. bool m_bWrittenNormal : 1;
  334. bool m_bWrittenUserData : 1;
  335. #endif
  336.  
  337. friend class CMeshBuilder;
  338. };
  339.  
  340. class CMeshBuilder : public MeshDesc_t
  341. {
  342. public:
  343. CMeshBuilder();
  344. ~CMeshBuilder() { /*if(m_pMesh) throw std::exception("CMeshBuilder: Begin() without an End()");*/ } // if this fires you did a Begin() without an End()
  345.  
  346. // This must be called before Begin, if a vertex buffer with a compressed format is to be used
  347. void SetCompressionType(VertexCompressionType_t compressionType);
  348.  
  349. // Locks the vertex buffer
  350. // (*cannot* use the Index() call below)
  351. void Begin(IMesh *pMesh, MaterialPrimitiveType_t type, int numPrimitives);
  352.  
  353. // Locks the vertex buffer, can specify arbitrary index lists
  354. // (must use the Index() call below)
  355. void Begin(IMesh *pMesh, MaterialPrimitiveType_t type, int nVertexCount, int nIndexCount, int *nFirstVertex);
  356. void Begin(IMesh *pMesh, MaterialPrimitiveType_t type, int nVertexCount, int nIndexCount);
  357.  
  358. // forward compat
  359. static void Begin(IVertexBuffer *pVertexBuffer, MaterialPrimitiveType_t type, int numPrimitives);
  360. static void Begin(IVertexBuffer *pVertexBuffer, IIndexBuffer *pIndexBuffer, MaterialPrimitiveType_t type, int nVertexCount, int nIndexCount, int *nFirstVertex);
  361. static void Begin(IVertexBuffer *pVertexBuffer, IIndexBuffer *pIndexBuffer, MaterialPrimitiveType_t type, int nVertexCount, int nIndexCount);
  362.  
  363. // Use this when you're done writing
  364. // Set bDraw to true to call m_pMesh->Draw automatically.
  365. void End(bool bSpewData = false, bool bDraw = false);
  366.  
  367. // Locks the vertex buffer to modify existing data
  368. // Passing nVertexCount == -1 says to lock all the vertices for modification.
  369. // Pass 0 for nIndexCount to not lock the index buffer.
  370. void BeginModify(IMesh *pMesh, int nFirstVertex = 0, int nVertexCount = -1, int nFirstIndex = 0, int nIndexCount = 0);
  371. void EndModify(bool bSpewData = false);
  372.  
  373. // A helper method since this seems to be done a whole bunch.
  374. void DrawQuad(IMesh* pMesh, const float *v1, const float *v2,
  375. const float *v3, const float *v4, unsigned char const *pColor, bool wireframe = false);
  376.  
  377. // returns the number of indices and vertices
  378. int VertexCount() const;
  379. int IndexCount() const;
  380.  
  381. // Resets the mesh builder so it points to the start of everything again
  382. void Reset();
  383.  
  384. // Returns the size of the vertex
  385. int VertexSize() { return m_ActualVertexSize; }
  386.  
  387. // returns the data size of a given texture coordinate
  388. int TextureCoordinateSize(int nTexCoordNumber) { return m_VertexSize_TexCoord[nTexCoordNumber]; }
  389.  
  390. // Returns the base vertex memory pointer
  391. void* BaseVertexData() const;
  392.  
  393. // Selects the nth Vertex and Index
  394. void SelectVertex(int idx);
  395. void SelectIndex(int idx) const;
  396.  
  397. // Given an index, point to the associated vertex
  398. void SelectVertexFromIndex(int idx);
  399.  
  400. // Advances the current vertex and index by one
  401. void AdvanceVertex();
  402. void AdvanceVertices(int nVerts);
  403. void AdvanceIndex();
  404. void AdvanceIndices(int nIndices);
  405.  
  406. int GetCurrentVertex() const;
  407. int GetCurrentIndex() const;
  408.  
  409. // Data retrieval...
  410. const float *Position() const;
  411.  
  412. const float *Normal() const;
  413.  
  414. unsigned int Color() const;
  415.  
  416. unsigned char *Specular() const;
  417.  
  418. const float *TexCoord(int stage) const;
  419.  
  420. const float *TangentS() const;
  421. const float *TangentT() const;
  422.  
  423. const float *BoneWeight() const;
  424. float Wrinkle() const;
  425.  
  426. int NumBoneWeights() const;
  427. #ifndef NEW_SKINNING
  428. unsigned char *BoneMatrix() const;
  429. #else
  430. float *BoneMatrix() const;
  431. #endif
  432. unsigned short const *Index() const;
  433.  
  434. // position setting
  435. void Position3f(float x, float y, float z) const;
  436. void Position3fv(const float *v) const;
  437.  
  438. // normal setting
  439. void Normal3f(float nx, float ny, float nz) const;
  440. void Normal3fv(const float *n) const;
  441. void NormalDelta3fv(const float *n) const;
  442. void NormalDelta3f(float nx, float ny, float nz) const;
  443.  
  444. // normal setting (templatized for code which needs to support compressed vertices)
  445. template <VertexCompressionType_t T> void CompressedNormal3f(float nx, float ny, float nz) const;
  446. template <VertexCompressionType_t T> void CompressedNormal3fv(const float *n) const;
  447.  
  448. // color setting
  449. void Color3f(float r, float g, float b) const;
  450. void Color3fv(const float *rgb) const;
  451. void Color4f(float r, float g, float b, float a) const;
  452. void Color4fv(const float *rgba) const;
  453.  
  454. // Faster versions of color
  455. void Color3ub(unsigned char r, unsigned char g, unsigned char b) const;
  456. void Color3ubv(unsigned char const* rgb) const;
  457. void Color4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a) const;
  458. void Color4ubv(unsigned char const* rgba) const;
  459.  
  460. // specular color setting
  461. void Specular3f(float r, float g, float b) const;
  462. void Specular3fv(const float *rgb) const;
  463. void Specular4f(float r, float g, float b, float a) const;
  464. void Specular4fv(const float *rgba) const;
  465.  
  466. // Faster version of specular
  467. void Specular3ub(unsigned char r, unsigned char g, unsigned char b) const;
  468. void Specular3ubv(unsigned char const *c) const;
  469. void Specular4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a) const;
  470. void Specular4ubv(unsigned char const *c) const;
  471.  
  472. // texture coordinate setting
  473. void TexCoord1f(int stage, float s) const;
  474. void TexCoord2f(int stage, float s, float t) const;
  475. void TexCoord2fv(int stage, const float *st) const;
  476. void TexCoord3f(int stage, float s, float t, float u) const;
  477. void TexCoord3fv(int stage, const float *stu) const;
  478. void TexCoord4f(int stage, float s, float t, float u, float w) const;
  479. void TexCoord4fv(int stage, const float *stuv) const;
  480.  
  481. void TexCoordSubRect2f(int stage, float s, float t, float offsetS, float offsetT, float scaleS, float scaleT) const;
  482. void TexCoordSubRect2fv(int stage, const float *st, const float *offset, const float *scale) const;
  483.  
  484. // tangent space
  485. void TangentS3f(float sx, float sy, float sz) const;
  486. void TangentS3fv(const float *s) const;
  487.  
  488. void TangentT3f(float tx, float ty, float tz) const;
  489. void TangentT3fv(const float *t) const;
  490.  
  491. // Wrinkle
  492. void Wrinkle1f(float flWrinkle) const;
  493.  
  494. // bone weights
  495. void BoneWeight(int idx, float weight) const;
  496. // bone weights (templatized for code which needs to support compressed vertices)
  497. template <VertexCompressionType_t T> void CompressedBoneWeight3fv(const float * pWeights) const;
  498.  
  499. // bone matrix index
  500. void BoneMatrix(int idx, int matrixIndex) const;
  501.  
  502. // Generic per-vertex data
  503. void UserData(const float *pData) const;
  504. // Generic per-vertex data (templatized for code which needs to support compressed vertices)
  505. template <VertexCompressionType_t T> void CompressedUserData(const float* pData) const;
  506.  
  507. // Used to define the indices (only used if you aren't using primitives)
  508. void Index(unsigned short index) const;
  509.  
  510. // NOTE: Use this one to get write combining! Much faster than the other version of FastIndex
  511. // Fast Index! No need to call advance index, and no random access allowed
  512. void FastIndex2(unsigned short nIndex1, unsigned short nIndex2);
  513.  
  514. // Fast Index! No need to call advance index, and no random access allowed
  515. void FastIndex(unsigned short index);
  516.  
  517. // Fast Vertex! No need to call advance vertex, and no random access allowed.
  518. // WARNING - these are low level functions that are intended only for use
  519. // in the software vertex skinner.
  520. void FastVertex(/*ModelVertexDX7_t*/ const int &vertex);
  521. void FastVertexSSE(/*ModelVertexDX7_t*/ const int &vertex);
  522.  
  523. // store 4 dx7 vertices fast. for special sse dx7 pipeline
  524. void Fast4VerticesSSE(
  525. /*ModelVertexDX7_t*/ const int *vtx_a,
  526. /*ModelVertexDX7_t*/ const int *vtx_b,
  527. /*ModelVertexDX7_t*/ const int *vtx_c,
  528. /*ModelVertexDX7_t*/ const int *vtx_d);
  529.  
  530. void FastVertex(/*ModelVertexDX8_t*/ const float &vertex);
  531. void FastVertexSSE(/*ModelVertexDX8_t*/ const float &vertex);
  532.  
  533. // Add number of verts and current vert since FastVertexxx routines do not update.
  534. void FastAdvanceNVertices(int n);
  535.  
  536. #if defined( _X360 )
  537. void VertexDX8ToX360(const ModelVertexDX8_t &vertex);
  538. #endif
  539.  
  540. private:
  541. // Computes number of verts and indices
  542. static void ComputeNumVertsAndIndices(int *pMaxVertices, int *pMaxIndices,
  543. MaterialPrimitiveType_t type, int nPrimitiveCount);
  544. static int IndicesFromVertices(MaterialPrimitiveType_t type, int nVertexCount);
  545.  
  546. // The mesh we're modifying
  547. IMesh *m_pMesh;
  548.  
  549. MaterialPrimitiveType_t m_Type;
  550.  
  551. // Generate indices?
  552. bool m_bGenerateIndices;
  553.  
  554. CIndexBuilder m_IndexBuilder;
  555. CVertexBuilder m_VertexBuilder;
  556. };
  557.  

This is only the beginning, then we need to access the rendering context, interface (Some functions may be incorrect):

CPP Code:
  1.  
  2. class IMatRenderContext : public IRefCounted
  3. {
  4. public:
  5. virtual void BeginRender() = 0;
  6. virtual void EndRender() = 0;
  7.  
  8. virtual void Flush(bool flushHardware = false) = 0;
  9.  
  10. virtual void BindLocalCubemap(ITexture *pTexture) = 0;
  11.  
  12. // pass in an ITexture (that is build with "rendertarget" "1") or
  13. // pass in NULL for the regular backbuffer.
  14. virtual void SetRenderTarget(ITexture *pTexture) = 0;
  15. virtual ITexture * GetRenderTarget() = 0;
  16.  
  17. virtual void GetRenderTargetDimensions(int &width, int &height) const = 0;
  18.  
  19. // Bind a material is current for rendering.
  20. virtual void Bind(IMaterial *material, void *proxyData = nullptr) = 0;
  21. // Bind a lightmap page current for rendering. You only have to
  22. // do this for materials that require lightmaps.
  23. virtual void BindLightmapPage(int lightmapPageID) = 0;
  24.  
  25. // inputs are between 0 and 1
  26. virtual void DepthRange(float zNear, float zFar) = 0;
  27.  
  28. virtual void ClearBuffers(bool bClearColor, bool bClearDepth, bool bClearStencil = false) = 0;
  29.  
  30. // read to a unsigned char rgb image.
  31. virtual void ReadPixels(int x, int y, int width, int height, unsigned char *data, ImageFormat dstFormat) = 0;
  32. virtual void ReadPixelsAsync(int, int, int, int, unsigned char *, ImageFormat, ITexture *,/* CThreadEvent * */ void*) = 0;
  33. virtual void ReadPixelsAsyncGetResult(int, int, int, int, unsigned char *, ImageFormat,/* CThreadEvent * */ void*) = 0;
  34. // Sets lighting
  35. virtual void SetLightingState(MaterialLightingState_t const&) = 0;
  36. //virtual void SetAmbientLight(float r, float g, float b) = 0;
  37. virtual void SetLights(int lightNum, const LightDesc_t& desc) = 0;
  38.  
  39. // The faces of the cube are specified in the same order as cubemap textures
  40. virtual void SetAmbientLightCube(Vector4D cube[6]) = 0;
  41.  
  42. // Blit the backbuffer to the framebuffer texture
  43. virtual void CopyRenderTargetToTexture(ITexture *pTexture) = 0;
  44.  
  45. // Set the current texture that is a copy of the framebuffer.
  46. virtual void SetFrameBufferCopyTexture(ITexture *pTexture, int textureIndex = 0) = 0;
  47. virtual ITexture *GetFrameBufferCopyTexture(int textureIndex) = 0;
  48.  
  49. //
  50. // end vertex array api
  51. //
  52.  
  53. // matrix api
  54. virtual void MatrixMode(MaterialMatrixMode_t matrixMode) = 0;
  55. virtual void PushMatrix() = 0;
  56. virtual void PopMatrix() = 0;
  57. virtual void LoadMatrix(VMatrix const& matrix) = 0;
  58. virtual void LoadMatrix(matrix3x4_t const& matrix) = 0;
  59. virtual void MultMatrix(VMatrix const& matrix) = 0;
  60. virtual void MultMatrix(matrix3x4_t const& matrix) = 0;
  61. virtual void MultMatrixLocal(VMatrix const& matrix) = 0;
  62. virtual void MultMatrixLocal(matrix3x4_t const& matrix) = 0;
  63. virtual void GetMatrix(MaterialMatrixMode_t matrixMode, VMatrix *matrix) = 0;
  64. virtual void GetMatrix(MaterialMatrixMode_t matrixMode, matrix3x4_t *matrix) = 0;
  65. virtual void LoadIdentity(void) = 0;
  66. virtual void Ortho(double left, double top, double right, double bottom, double zNear, double zFar) = 0;
  67. virtual void PerspectiveX(double fovx, double aspect, double zNear, double zFar) = 0;
  68. virtual void PickMatrix(int x, int y, int width, int height) = 0;
  69. virtual void Rotate(float angle, float x, float y, float z) = 0;
  70. virtual void Translate(float x, float y, float z) = 0;
  71. virtual void Scale(float x, float y, float z) = 0;
  72. // end matrix api
  73.  
  74. // Sets/gets the viewport
  75. virtual void Viewport(int x, int y, int width, int height) = 0;
  76. virtual void GetViewport(int& x, int& y, int& width, int& height) const = 0;
  77.  
  78. // The cull mode
  79. virtual void CullMode(MaterialCullMode_t cullMode) = 0;
  80. virtual void FlipCullMode() = 0;
  81. // end matrix api
  82.  
  83. virtual void BeginGeneratingCSMs() = 0;
  84. virtual void EndGeneratingCSMs() = 0;
  85. virtual void PerpareForCascadeDraw(int, float, float) = 0;
  86.  
  87. // This could easily be extended to a general user clip plane
  88. virtual void SetHeightClipMode(MaterialHeightClipMode_t nHeightClipMode) = 0;
  89. // garymcthack : fog z is always used for heightclipz for now.
  90. virtual void SetHeightClipZ(float z) = 0;
  91.  
  92. // Fog methods...
  93. virtual void FogMode(MaterialFogMode_t fogMode) = 0;
  94. virtual void FogStart(float fStart) = 0;
  95. virtual void FogEnd(float fEnd) = 0;
  96. virtual void SetFogZ(float fogZ) = 0;
  97. virtual MaterialFogMode_t GetFogMode(void) = 0;
  98.  
  99. virtual void FogColor3f(float r, float g, float b) = 0;
  100. virtual void FogColor3fv(float const* rgb) = 0;
  101. virtual void FogColor3ub(unsigned char r, unsigned char g, unsigned char b) = 0;
  102. virtual void FogColor3ubv(unsigned char const* rgb) = 0;
  103.  
  104. virtual void GetFogColor(unsigned char *rgb) = 0;
  105.  
  106. // Sets the number of bones for skinning
  107. virtual void SetNumBoneWeights(int numBones) = 0;
  108.  
  109. // Creates/destroys Mesh
  110. virtual IMesh* CreateStaticMesh(VertexFormat_t fmt, const char *pTextureBudgetGroup, IMaterial * pMaterial = nullptr) = 0;
  111. virtual void DestroyStaticMesh(IMesh* mesh) = 0;
  112.  
  113. // Gets the dynamic mesh associated with the currently bound material
  114. // note that you've got to render the mesh before calling this function
  115. // a second time. Clients should *not* call DestroyStaticMesh on the mesh
  116. // returned by this call.
  117. // Use buffered = false if you want to not have the mesh be buffered,
  118. // but use it instead in the following pattern:
  119. // meshBuilder.Begin
  120. // meshBuilder.End
  121. // Draw partial
  122. // Draw partial
  123. // Draw partial
  124. // meshBuilder.Begin
  125. // meshBuilder.End
  126. // etc
  127. // Use Vertex or Index Override to supply a static vertex or index buffer
  128. // to use in place of the dynamic buffers.
  129. //
  130. // If you pass in a material in pAutoBind, it will automatically bind the
  131. // material. This can be helpful since you must bind the material you're
  132. // going to use BEFORE calling GetDynamicMesh.
  133. virtual IMesh* GetDynamicMesh(
  134. bool buffered = true,
  135. IMesh* pVertexOverride = nullptr,
  136. IMesh* pIndexOverride = nullptr,
  137. IMaterial *pAutoBind = nullptr) = 0;
  138.  
  139. // ------------ New Vertex/Index Buffer interface ----------------------------
  140. // Do we need support for bForceTempMesh and bSoftwareVertexShader?
  141. // I don't think we use bSoftwareVertexShader anymore. .need to look into bForceTempMesh.
  142. virtual IVertexBuffer *CreateStaticVertexBuffer(VertexFormat_t fmt, int nVertexCount, const char *pTextureBudgetGroup) = 0;
  143. virtual IIndexBuffer *CreateStaticIndexBuffer(MaterialIndexFormat_t fmt, int nIndexCount, const char *pTextureBudgetGroup) = 0;
  144. virtual void DestroyVertexBuffer(IVertexBuffer *) = 0;
  145. virtual void DestroyIndexBuffer(IIndexBuffer *) = 0;
  146. // Do we need to specify the stream here in the case of locking multiple dynamic VBs on different streams?
  147. virtual IVertexBuffer *GetDynamicVertexBuffer(int streamID, VertexFormat_t vertexFormat, bool bBuffered = true) = 0;
  148. virtual IIndexBuffer *GetDynamicIndexBuffer(MaterialIndexFormat_t fmt, bool bBuffered = true) = 0;
  149. virtual void BindVertexBuffer(int streamID, IVertexBuffer *pVertexBuffer, int nOffsetInBytes, int nFirstVertex, int nVertexCount, VertexFormat_t fmt, int nRepetitions = 1) = 0;
  150. virtual void BindIndexBuffer(IIndexBuffer *pIndexBuffer, int nOffsetInBytes) = 0;
  151. virtual void Draw(MaterialPrimitiveType_t primitiveType, int firstIndex, int numIndices) = 0;
  152. // ------------ End ----------------------------
  153.  
  154. // Selection mode methods
  155. virtual int SelectionMode(bool selectionMode) = 0;
  156. virtual void SelectionBuffer(unsigned int* pBuffer, int size) = 0;
  157. virtual void ClearSelectionNames() = 0;
  158. virtual void LoadSelectionName(int name) = 0;
  159. virtual void PushSelectionName(int name) = 0;
  160. virtual void PopSelectionName() = 0;
  161.  
  162. // Sets the Clear Color for ClearBuffer....
  163. virtual void ClearColor3ub(unsigned char r, unsigned char g, unsigned char b) = 0;
  164. virtual void ClearColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a) = 0;
  165.  
  166. // Allows us to override the depth buffer setting of a material
  167. virtual void OverrideDepthEnable(bool bEnable, bool bDepthEnable) = 0;
  168.  
  169. // FIXME: This is a hack required for NVidia/XBox, can they fix in drivers?
  170. virtual void DrawScreenSpaceQuad(IMaterial* pMaterial) = 0;
  171.  
  172. // For debugging and building recording files. This will stuff a token into the recording file,
  173. // then someone doing a playback can watch for the token.
  174. virtual void SyncToken(const char *pToken) = 0;
  175.  
  176. // FIXME: REMOVE THIS FUNCTION!
  177. // The only reason why it's not gone is because we're a week from ship when I found the bug in it
  178. // and everything's tuned to use it.
  179. // It's returning values which are 2x too big (it's returning sphere diameter x2)
  180. // Use ComputePixelDiameterOfSphere below in all new code instead.
  181. virtual float ComputePixelWidthOfSphere(const Vector& origin, float flRadius) = 0;
  182.  
  183. //
  184. // Occlusion query support
  185. //
  186.  
  187. // Allocate and delete query objects.
  188. virtual OcclusionQueryObjectHandle_t CreateOcclusionQueryObject(void) = 0;
  189. virtual void DestroyOcclusionQueryObject(OcclusionQueryObjectHandle_t) = 0;
  190.  
  191. // Bracket drawing with begin and end so that we can get counts next frame.
  192. virtual void BeginOcclusionQueryDrawing(OcclusionQueryObjectHandle_t) = 0;
  193. virtual void EndOcclusionQueryDrawing(OcclusionQueryObjectHandle_t) = 0;
  194.  
  195. // Get the number of pixels rendered between begin and end on an earlier frame.
  196. // Calling this in the same frame is a huge perf hit!
  197. virtual int OcclusionQuery_GetNumPixelsRendered(OcclusionQueryObjectHandle_t) = 0;
  198.  
  199. virtual void SetFlashlightMode(bool bEnable) = 0;
  200.  
  201. virtual void SetFlashlightState(const FlashlightState_t &state, const VMatrix &worldToTexture) = 0;
  202.  
  203. virtual bool IsCascadedShadowMapping() = 0;
  204. virtual void SetCascadedShadowMapping(bool) = 0;
  205. virtual void SetCascadedShadowMappingState(/*CascadedShadowMappingState_t*/ int const&, ITexture *) = 0;
  206.  
  207. // Gets the current height clip mode
  208. virtual MaterialHeightClipMode_t GetHeightClipMode() = 0;
  209.  
  210. // This returns the diameter of the sphere in pixels based on
  211. // the current model, view, + projection matrices and viewport.
  212. virtual float ComputePixelDiameterOfSphere(const Vector& vecAbsOrigin, float flRadius) = 0;
  213.  
  214. // By default, the material system applies the VIEW and PROJECTION matrices to the user clip
  215. // planes (which are specified in world space) to generate projection-space user clip planes
  216. // Occasionally (for the particle system in hl2, for example), we want to override that
  217. // behavior and explictly specify a ViewProj transform for user clip planes
  218. virtual void EnableUserClipTransformOverride(bool bEnable) = 0;
  219. virtual void UserClipTransform(const VMatrix &worldToView) = 0;
  220.  
  221. virtual bool GetFlashlightMode() const = 0;
  222.  
  223. virtual bool IsCullingEnabledForSinglePassFlashlight() = 0;
  224. virtual void EnableCullingForSinglePassFlashlight(bool) = 0;
  225. // Used to make the handle think it's never had a successful query before
  226. virtual void ResetOcclusionQueryObject(OcclusionQueryObjectHandle_t) = 0;
  227.  
  228. // Creates/destroys morph data associated w/ a particular material
  229. virtual IMorph *CreateMorph(MorphFormat_t format, const char *pDebugName) = 0;
  230. virtual void DestroyMorph(IMorph *pMorph) = 0;
  231.  
  232. // Binds the morph data for use in rendering
  233. virtual void BindMorph(IMorph *pMorph) = 0;
  234.  
  235. // Sets flexweights for rendering
  236. virtual void SetFlexWeights(int nFirstWeight, int nCount, const MorphWeight_t* pWeights) = 0;
  237.  
  238. virtual void LockRenderData(int) = 0;
  239. virtual void UnlockRenderData() = 0;
  240. virtual void AddRefRenderData() = 0;
  241. virtual void ReleaseRenderData() = 0;
  242. virtual void IsRenderData(void const*) = 0;
  243.  
  244. // Read w/ stretch to a host-memory buffer
  245. virtual void ReadPixelsAndStretch(Rect_t *pSrcRect, Rect_t *pDstRect, unsigned char *pBuffer, ImageFormat dstFormat, int nDstStride) = 0;
  246.  
  247. // Gets the window size
  248. virtual void GetWindowSize(int &width, int &height) const = 0;
  249.  
  250. // This function performs a texture map from one texture map to the render destination, doing
  251. // all the necessary pixel/texel coordinate fix ups. fractional values can be used for the
  252. // src_texture coordinates to get linear sampling - integer values should produce 1:1 mappings
  253. // for non-scaled operations.
  254. virtual void DrawScreenSpaceRectangle(
  255. IMaterial *pMaterial,
  256. int destx, int desty,
  257. int width, int height,
  258. float src_texture_x0, float src_texture_y0, // which texel you want to appear at
  259. // destx/y
  260. float src_texture_x1, float src_texture_y1, // which texel you want to appear at
  261. // destx+width-1, desty+height-1
  262. int src_texture_width, int src_texture_height, // needed for fixup
  263. void *pClientRenderable = nullptr,
  264. int nXDice = 1,
  265. int nYDice = 1) = 0;
  266.  
  267. virtual void LoadBoneMatrix(int boneIndex, const matrix3x4_t& matrix) = 0;
  268.  
  269. // This version will push the current rendertarget + current viewport onto the stack
  270. virtual void PushRenderTargetAndViewport() = 0;
  271.  
  272. // This version will push a new rendertarget + a maximal viewport for that rendertarget onto the stack
  273. virtual void PushRenderTargetAndViewport(ITexture *pTexture) = 0;
  274.  
  275. // This version will push a new rendertarget + a specified viewport onto the stack
  276. virtual void PushRenderTargetAndViewport(ITexture *pTexture, int nViewX, int nViewY, int nViewW, int nViewH) = 0;
  277.  
  278. // This version will push a new rendertarget + a specified viewport onto the stack
  279. virtual void PushRenderTargetAndViewport(ITexture *pTexture, ITexture *pDepthTexture, int nViewX, int nViewY, int nViewW, int nViewH) = 0;
  280.  
  281. // This will pop a rendertarget + viewport
  282. virtual void PopRenderTargetAndViewport() = 0;
  283.  
  284. // Binds a particular texture as the current lightmap
  285. virtual void BindLightmapTexture(ITexture *pLightmapTexture) = 0;
  286.  
  287. // Blit a subrect of the current render target to another texture
  288. virtual void CopyRenderTargetToTextureEx(ITexture *pTexture, int nRenderTargetID, Rect_t *pSrcRect, Rect_t *pDstRect = nullptr) = 0;
  289. virtual void CopyRenderTargetToTextureExCopyTextureToRenderTargetEx(int, ITexture *, Rect_t *, Rect_t *) = 0;
  290. // Special off-center perspective matrix for DoF, MSAA jitter and poster rendering
  291. virtual void PerspectiveOffCenterX(double fovx, double aspect, double zNear, double zFar, double bottom, double top, double left, double right) = 0;
  292.  
  293. // Rendering parameters control special drawing modes withing the material system, shader
  294. // system, shaders, and engine. renderparm.h has their definitions.
  295. virtual void SetFloatRenderingParameter(int parm_number, float value) = 0;
  296. virtual void SetIntRenderingParameter(int parm_number, int value) = 0;
  297. virtual void SetVectorRenderingParameter(int parm_number, Vector const &value) = 0;
  298.  
  299. // stencil buffer operations.
  300. virtual void SetStencilState(/*ShaderStencilState_t*/ int const&) = 0;
  301. //virtual void SetStencilEnable(bool onoff) = 0;
  302. //virtual void SetStencilFailOperation(StencilOperation_t op) = 0;
  303. //virtual void SetStencilZFailOperation(StencilOperation_t op) = 0;
  304. //virtual void SetStencilPassOperation(StencilOperation_t op) = 0;
  305. //virtual void SetStencilCompareFunction(StencilComparisonFunction_t cmpfn) = 0;
  306. //virtual void SetStencilReferenceValue(int ref) = 0;
  307. //virtual void SetStencilTestMask(uint32 msk) = 0;
  308. //virtual void SetStencilWriteMask(uint32 msk) = 0;
  309. virtual void ClearStencilBufferRectangle(int xmin, int ymin, int xmax, int ymax, int value) = 0;
  310.  
  311. virtual void SetRenderTargetEx(int nRenderTargetID, ITexture *pTexture) = 0;
  312.  
  313. // rendering clip planes, beware that only the most recently pushed plane will actually be used in a sizeable chunk of hardware configurations
  314. // and that changes to the clip planes mid-frame while UsingFastClipping() is true will result unresolvable depth inconsistencies
  315. virtual void PushCustomClipPlane(const float *pPlane) = 0;
  316. virtual void PopCustomClipPlane() = 0;
  317.  
  318. // Returns the number of vertices + indices we can render using the dynamic mesh
  319. // Passing true in the second parameter will return the max # of vertices + indices
  320. // we can use before a flush is provoked and may return different values
  321. // if called multiple times in succession.
  322. // Passing false into the second parameter will return
  323. // the maximum possible vertices + indices that can be rendered in a single batch
  324. virtual void GetMaxToRender(IMesh *pMesh, bool bMaxUntilFlush, int *pMaxVerts, int *pMaxIndices) = 0;
  325.  
  326. // Returns the max possible vertices + indices to render in a single draw call
  327. virtual int GetMaxVerticesToRender(IMaterial *pMaterial) = 0;
  328. virtual int GetMaxIndicesToRender() = 0;
  329. virtual void DisableAllLocalLights() = 0;
  330. virtual int CompareMaterialCombos(IMaterial *pMaterial1, IMaterial *pMaterial2, int lightMapID1, int lightMapID2) = 0;
  331.  
  332. virtual IMesh *GetFlexMesh() = 0;
  333.  
  334. virtual void SetFlashlightStateEx(const FlashlightState_t &state, const VMatrix &worldToTexture, ITexture *pFlashlightDepthTexture) = 0;
  335.  
  336. // Returns the currently bound local cubemap
  337. virtual ITexture *GetLocalCubemap() = 0;
  338.  
  339. // This is a version of clear buffers which will only clear the buffer at pixels which pass the stencil test
  340. virtual void ClearBuffersObeyStencil(bool bClearColor, bool bClearDepth) = 0;
  341.  
  342. //enables/disables all entered clipping planes, returns the input from the last time it was called.
  343. virtual bool EnableClipping(bool bEnable) = 0;
  344.  
  345. //get fog distances entered with FogStart(), FogEnd(), and SetFogZ()
  346. virtual void GetFogDistances(float *fStart, float *fEnd, float *fFogZ) = 0;
  347.  
  348. // Hooks for firing PIX events from outside the Material System...
  349. virtual void BeginPIXEvent(unsigned long color, const char *szName) = 0;
  350. virtual void EndPIXEvent() = 0;
  351. virtual void SetPIXMarker(unsigned long color, const char *szName) = 0;
  352.  
  353. // Batch API
  354. // from changelist 166623:
  355. // - replaced obtuse material system batch usage with an explicit and easier to thread API
  356. virtual void BeginBatch(IMesh* pIndices) = 0;
  357. virtual void BindBatch(IMesh* pVertices, IMaterial *pAutoBind = nullptr) = 0;
  358. virtual void DrawBatch(int firstIndex, int numIndices) = 0;
  359. virtual void EndBatch() = 0;
  360.  
  361. // Raw access to the call queue, which can be NULL if not in a queued mode
  362. virtual ICallQueue *GetCallQueue() = 0;
  363.  
  364. // Returns the world-space camera position
  365. virtual void GetWorldSpaceCameraPosition(Vector *pCameraPos) = 0;
  366. virtual void GetWorldSpaceCameraVectors(Vector *pVecForward, Vector *pVecRight, Vector *pVecUp) = 0;
  367.  
  368. // Tone mapping
  369. //virtual void ResetToneMappingScale(float monoscale) = 0; // set scale to monoscale instantly with no chasing
  370. //virtual void SetGoalToneMappingScale(float monoscale) = 0; // set scale to monoscale instantly with no chasing
  371.  
  372. // call TurnOnToneMapping before drawing the 3d scene to get the proper interpolated brightness
  373. // value set.
  374. //virtual void TurnOnToneMapping() = 0;
  375.  
  376. // Set a linear vector color scale for all 3D rendering.
  377. // A value of [1.0f, 1.0f, 1.0f] should match non-tone-mapped rendering.
  378. virtual void SetToneMappingScaleLinear(const Vector &scale) = 0;
  379.  
  380. virtual Vector GetToneMappingScaleLinear() = 0;
  381. virtual void SetShadowDepthBiasFactors(float fSlopeScaleDepthBias, float fDepthBias) = 0;
  382.  
  383. // Apply stencil operations to every pixel on the screen without disturbing depth or color buffers
  384. virtual void PerformFullScreenStencilOperation(void) = 0;
  385.  
  386. // Sets lighting origin for the current model (needed to convert directional lights to points)
  387. virtual void SetLightingOrigin(Vector vLightingOrigin) = 0;
  388.  
  389. // Set scissor rect for rendering
  390. virtual void PushScissorRect(const int nLeft, const int nTop, const int nRight, const int nBottom) = 0;
  391. virtual void PopScissorRect() = 0;
  392.  
  393. // Methods used to build the morph accumulator that is read from when HW morph<ing is enabled.
  394. virtual void BeginMorphAccumulation() = 0;
  395. virtual void EndMorphAccumulation() = 0;
  396. virtual void AccumulateMorph(IMorph* pMorph, int nMorphCount, const MorphWeight_t* pWeights) = 0;
  397.  
  398. virtual void PushDeformation(DeformationBase_t const *Deformation) = 0;
  399. virtual void PopDeformation() = 0;
  400. virtual int GetNumActiveDeformations() const = 0;
  401.  
  402. virtual bool GetMorphAccumulatorTexCoord(Vector2D *pTexCoord, IMorph *pMorph, int nVertex) = 0;
  403.  
  404. // Version of get dynamic mesh that specifies a specific vertex format
  405. virtual IMesh* GetDynamicMeshEx(VertexFormat_t vertexFormat, bool bBuffered = true,
  406. IMesh* pVertexOverride = nullptr, IMesh* pIndexOverride = nullptr, IMaterial *pAutoBind = nullptr) = 0;
  407.  
  408. virtual void FogMaxDensity(float flMaxDensity) = 0;
  409.  
  410. virtual IMaterial *GetCurrentMaterial() = 0;
  411. virtual int GetCurrentNumBones() const = 0;
  412. virtual void *GetCurrentProxy() = 0;
  413.  
  414. // Color correction related methods..
  415. // Client cannot call IColorCorrectionSystem directly because it is not thread-safe
  416. // FIXME: Make IColorCorrectionSystem threadsafe?
  417. virtual void EnableColorCorrection(bool bEnable) = 0;
  418. virtual ColorCorrectionHandle_t AddLookup(const char *pName) = 0;
  419. virtual bool RemoveLookup(ColorCorrectionHandle_t handle) = 0;
  420. virtual void LockLookup(ColorCorrectionHandle_t handle) = 0;
  421. virtual void LoadLookup(ColorCorrectionHandle_t handle, const char *pLookupName) = 0;
  422. virtual void UnlockLookup(ColorCorrectionHandle_t handle) = 0;
  423. virtual void SetLookupWeight(ColorCorrectionHandle_t handle, float flWeight) = 0;
  424. virtual void ResetLookupWeights() = 0;
  425. virtual void SetResetable(ColorCorrectionHandle_t handle, bool bResetable) = 0;
  426.  
  427. //There are some cases where it's simply not reasonable to update the full screen depth texture (mostly on PC).
  428. //Use this to mark it as invalid and use a dummy texture for depth reads.
  429. virtual void SetFullScreenDepthTextureValidityFlag(bool bIsValid) = 0;
  430.  
  431. // A special path used to tick the front buffer while loading on the 360
  432. virtual void SetNonInteractivePacifierTexture(ITexture *pTexture, float flNormalizedX, float flNormalizedY, float flNormalizedSize) = 0;
  433. virtual void SetNonInteractiveTempFullscreenBuffer(ITexture *pTexture, MaterialNonInteractiveMode_t mode) = 0;
  434. virtual void EnableNonInteractiveMode(MaterialNonInteractiveMode_t mode) = 0;
  435. virtual void RefreshFrontBufferNonInteractive() = 0;
  436. };
  437.  

You can require context from MaterialSystem (Windows index):
CPP Code:
  1.  
  2. IMatRenderContext* GetRenderContext()
  3. {
  4. return CallVFunction<oGetRenderContext>(this, 115)(this);
  5. }
  6.  

And simple example from source-2007:
CPP Code:
  1.  
  2. void CMatSystemSurface::DrawTexturedLineInternal( const Vertex_t &a, const Vertex_t &b )
  3. {
  4. Assert( !m_bIn3DPaintMode );
  5.  
  6. // Don't bother drawing fully transparent lines
  7. if( m_DrawColor[3] == 0 )
  8. return;
  9.  
  10. vgui::Vertex_t verts[2] = { a, b };
  11.  
  12. verts[0].m_Position.x += m_nTranslateX + g_flPixelOffsetX;
  13. verts[0].m_Position.y += m_nTranslateY + g_flPixelOffsetY;
  14.  
  15. verts[1].m_Position.x += m_nTranslateX + g_flPixelOffsetX;
  16. verts[1].m_Position.y += m_nTranslateY + g_flPixelOffsetY;
  17.  
  18. vgui::Vertex_t clippedVerts[2];
  19.  
  20. if (!ClipLine( verts, clippedVerts ))
  21. return;
  22.  
  23. meshBuilder.Begin( m_pMesh, MATERIAL_LINES, 1 );
  24.  
  25. meshBuilder.Color4ubv( m_DrawColor );
  26. meshBuilder.TexCoord2fv( 0, clippedVerts[0].m_TexCoord.Base() );
  27. meshBuilder.Position3f( clippedVerts[0].m_Position.x, clippedVerts[0].m_Position.y, m_flZPos );
  28. meshBuilder.AdvanceVertex();
  29.  
  30. meshBuilder.Color4ubv( m_DrawColor );
  31. meshBuilder.TexCoord2fv( 0, clippedVerts[1].m_TexCoord.Base() );
  32. meshBuilder.Position3f( clippedVerts[1].m_Position.x, clippedVerts[1].m_Position.y, m_flZPos );
  33. meshBuilder.AdvanceVertex();
  34.  
  35. meshBuilder.End();
  36. m_pMesh->Draw();
  37. }
  38.  

CMatSystemSurface (ISurface) has already instances of classes:
CPP Code:
  1.  
  2. class ISurface : public IAppSystem
  3. {
  4. public:
  5. /*ur vtable defs*/
  6. void* vtable1_pad;
  7. void* vtable2_pad;
  8.  
  9. // Point Translation for current panel
  10. int m_nTranslateX;
  11. int m_nTranslateY;
  12.  
  13. // alpha multiplier for current panel [0..1]
  14. float m_flAlphaMultiplier;
  15.  
  16. char pad_0x0018[0xC]; //0x0018
  17.  
  18. // The size of the window to draw into
  19. //int m_pSurfaceExtents[4];
  20. __int32 m_nWidth; //0x0024
  21. __int32 m_nHeight; //0x0028
  22.  
  23. // Color for drawing all non-text things
  24. unsigned char m_DrawColor[4];
  25.  
  26. // Color for drawing text
  27. unsigned char m_DrawTextColor[4];
  28.  
  29. // Location of text rendering
  30. int m_pDrawTextPos[2];
  31.  
  32. // Meshbuilder used for drawing
  33. IMesh* m_pMesh;
  34. CMeshBuilder meshBuilder;
  35.  
  36. // White material used for drawing non-textured things
  37. CMaterialReference m_pWhite;
  38. };
  39.  

Download More advanced ISurface drawing