summaryrefslogtreecommitdiff
path: root/Runtime/Graphics
diff options
context:
space:
mode:
Diffstat (limited to 'Runtime/Graphics')
-rw-r--r--Runtime/Graphics/CustomVertexLayout.h10
-rw-r--r--Runtime/Graphics/Quad.cpp52
-rw-r--r--Runtime/Graphics/Quad.h25
-rw-r--r--Runtime/Graphics/SharedVertexBuffer.cpp2
-rw-r--r--Runtime/Graphics/Texture.cpp15
-rw-r--r--Runtime/Graphics/UIQuad.cpp61
-rw-r--r--Runtime/Graphics/UIQuad.h18
-rw-r--r--Runtime/Graphics/VertexAttribute.h17
8 files changed, 33 insertions, 167 deletions
diff --git a/Runtime/Graphics/CustomVertexLayout.h b/Runtime/Graphics/CustomVertexLayout.h
index 86dcf90..d72c71d 100644
--- a/Runtime/Graphics/CustomVertexLayout.h
+++ b/Runtime/Graphics/CustomVertexLayout.h
@@ -11,6 +11,15 @@ struct CustomVertexLayout
{
GLuint buffer; // 创建时留空
std::vector<VertexAttributeDescriptor> attributes;
+
+ // 重置pointer(startOffset)为0
+ void ResetPointer()
+ {
+ for (int i = 0; i < attributes.size(); ++i)
+ {
+ attributes[i].pointer = 0;
+ }
+ }
};
namespace VertexLayout
@@ -19,4 +28,3 @@ namespace VertexLayout
extern void SetupCustomVertexLayout(CustomVertexLayout& layout);
}
-
diff --git a/Runtime/Graphics/Quad.cpp b/Runtime/Graphics/Quad.cpp
deleted file mode 100644
index d769dd4..0000000
--- a/Runtime/Graphics/Quad.cpp
+++ /dev/null
@@ -1,52 +0,0 @@
-#include "../Math/Vector2.h"
-#include "../Math/Vector3.h"
-#include "Quad.h"
-#include "GfxDevice.h"
-
-struct QuadVBOLayout
-{
- Internal::Vector3 position;
- Internal::Vector2 uv;
-};
-
-void Quad::Draw()
-{
- const int nVerts = 4;
- const int nIndices = 6;
-
- float pos[] = {
- m_Left, m_Bottom, 0, // left-bottom
- m_Right, m_Bottom, 0, // right-bottom
- m_Right, m_Top, 0, // right-top
- m_Left, m_Top, 0, // top-left
- };
- float uv[] = {
- 0, 0,
- 1, 0,
- 1, 1,
- 0, 1,
- };
- int indices[] = {
- 0, 1, 3, // right-top
- 1, 2, 3, // left-bottom
- };
-
- uint8* vb;
- uint16* ib;
-
- uint attrs = Mask(VertexAttr_Position) | Mask(VertexAttr_UV);
- g_SharedVBO.GetChunk(attrs, 4, 6, Primitive_Triangle, (void**)&vb, (void**)&ib);
-
- QuadVBOLayout* dst = (QuadVBOLayout*)vb;
- for (int i = 0; i < nVerts; ++i)
- {
- dst[i].position.Set(pos[3 * i], pos[3 * i + 1], pos[3 * i + 2]);
- dst[i].uv.Set(uv[2 * i], uv[2 * i + 1]);
- }
-
- for (int i = 0; i < nIndices; ++i)
- ib[i] = indices[i];
-
- g_SharedVBO.ReleaseChunk(4, 6);
- g_SharedVBO.DrawChunk();
-} \ No newline at end of file
diff --git a/Runtime/Graphics/Quad.h b/Runtime/Graphics/Quad.h
deleted file mode 100644
index 0e38ec4..0000000
--- a/Runtime/Graphics/Quad.h
+++ /dev/null
@@ -1,25 +0,0 @@
-#ifndef QUAD_H
-#define QUAD_H
-
-#include "../Utilities/UtilMacros.h"
-
-class Quad
-{
-public:
- Quad(float l, float r, float t, float b);
-
- void Set(float l, float r, float t, float b);
-
- GET_SET(float, Left, m_Left);
- GET_SET(float, Right, m_Right);
- GET_SET(float, Top, m_Top);
- GET_SET(float, Bottom, m_Bottom);
-
- void Draw();
-
-private:
- float m_Left, m_Right, m_Top, m_Bottom;
-
-};
-
-#endif \ No newline at end of file
diff --git a/Runtime/Graphics/SharedVertexBuffer.cpp b/Runtime/Graphics/SharedVertexBuffer.cpp
index 464a919..e787630 100644
--- a/Runtime/Graphics/SharedVertexBuffer.cpp
+++ b/Runtime/Graphics/SharedVertexBuffer.cpp
@@ -173,6 +173,8 @@ void SharedVertexBuffer::DrawChunk(CustomVertexLayout& layout)
VertexLayout::SetupCustomVertexLayout(layout);
+ layout.ResetPointer();
+
const void* indexPtr = m_CurIB ? 0 : (m_CurIBData.empty() ? 0 : &m_CurIBData[0]);
if (m_CurIB)
diff --git a/Runtime/Graphics/Texture.cpp b/Runtime/Graphics/Texture.cpp
index f92c094..eed0b90 100644
--- a/Runtime/Graphics/Texture.cpp
+++ b/Runtime/Graphics/Texture.cpp
@@ -158,12 +158,13 @@ Texture::Texture(TextureSetting setting, int w, int h)
if (m_Format == ETextureFormat::R8)
{
internalFormat = GL_RED;
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
}
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
-
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
+
CheckGLError(
glDeleteTextures(1, &m_GPUID);
glBindTexture(GL_TEXTURE_2D, 0);
@@ -185,6 +186,12 @@ void Texture::UpdateSubImage(Internal::Rect rect, int format, int type, const vo
throw TextureException(error);
);
+ int alignment = 4;
+ if (m_Format == ETextureFormat::R8)
+ {
+ alignment = 1;
+ }
+
GLenum fmt = GL_RED;
switch (format)
{
@@ -205,8 +212,12 @@ void Texture::UpdateSubImage(Internal::Rect rect, int format, int type, const vo
case EPixelElementType::PixelType_FLOAT: t = GL_FLOAT; break;
}
+ glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
+
glTexSubImage2D(GL_TEXTURE_2D, 0, rect.x, rect.y, rect.width, rect.height, fmt, t, data);
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
+
CheckGLError(
glBindTexture(GL_TEXTURE_2D, 0);
throw TextureException(error);
diff --git a/Runtime/Graphics/UIQuad.cpp b/Runtime/Graphics/UIQuad.cpp
deleted file mode 100644
index 73cf645..0000000
--- a/Runtime/Graphics/UIQuad.cpp
+++ /dev/null
@@ -1,61 +0,0 @@
-#include "../Math/Vector2.h"
-#include "GfxDevice.h"
-#include "UIQuad.h"
-
-struct UIQuadLayout
-{
- Internal::Vector2 position;
- Internal::Vector2 uv;
-};
-
-void UIQuad::Draw()
-{
- CustomVertexLayout layout;
-
- VertexAttributeDescriptor POSITION = VertexAttributeDescriptor(0, 2, VertexAttrFormat_Float, sizeof(UIQuadLayout));
- VertexAttributeDescriptor UV = VertexAttributeDescriptor(sizeof(Internal::Vector2), 2, VertexAttrFormat_Float, sizeof(UIQuadLayout));
-
- layout.attributes.push_back(POSITION);
- layout.attributes.push_back(UV);
-
- const int nVerts = 4;
- const int nIndices = 6;
-
- float pos[] = {
- m_Left, m_Bottom, // left-bottom
- m_Right, m_Bottom, // right-bottom
- m_Right, m_Top, // right-top
- m_Left, m_Top, // top-left
- };
-
- float uv[] = {
- 0, 0,
- 1, 0,
- 1, 1,
- 0, 1,
- };
-
- int indices[] = {
- 0, 1, 3, // right-top
- 1, 2, 3, // left-bottom
- };
-
- uint8* vb;
- uint16* ib;
-
- g_SharedVBO.GetChunk(sizeof(UIQuadLayout), sizeof(uint16), 4, 6, Primitive_Triangle, (void**)&vb, (void**)&ib);
-
- UIQuadLayout* dst = (UIQuadLayout*)vb;
-
- for (int i = 0; i < nVerts; ++i)
- {
- dst[i].position.Set(pos[2 * i], pos[2 * i + 1]);
- dst[i].uv.Set(uv[2 * i], uv[2 * i + 1]);
- }
-
- for (int i = 0; i < nIndices; ++i)
- ib[i] = indices[i];
-
- g_SharedVBO.ReleaseChunk(4, 6);
- g_SharedVBO.DrawChunk(layout);
-} \ No newline at end of file
diff --git a/Runtime/Graphics/UIQuad.h b/Runtime/Graphics/UIQuad.h
deleted file mode 100644
index a0d77a5..0000000
--- a/Runtime/Graphics/UIQuad.h
+++ /dev/null
@@ -1,18 +0,0 @@
-#pragma once
-
-class UIQuad
-{
-public :
- UIQuad(float l, float r, float t, float b)
- {
- m_Left = l;
- m_Right = r;
- m_Top = t;
- m_Bottom = b;
- }
- void Draw();
-
-private:
- float m_Left, m_Right, m_Top, m_Bottom;
-
-};
diff --git a/Runtime/Graphics/VertexAttribute.h b/Runtime/Graphics/VertexAttribute.h
index 4e50a1a..2757c70 100644
--- a/Runtime/Graphics/VertexAttribute.h
+++ b/Runtime/Graphics/VertexAttribute.h
@@ -21,6 +21,15 @@ enum VertexAttrFormat
struct VertexAttributeDescriptor
{
+ //union {
+ const void* pointer; // 内存地址,刚创建时留空
+ int startOffset; // 显存中相对VBO的偏移值
+ //};
+ uint componentNum; // 向量维度1,2,3,4
+ uint componentFormat; // 每个分量的类型
+ uint16 stride; // 间隔
+ bool normalize; // 是否归一化,只用于CustomVertexLayout
+
// for default vertex layout
VertexAttributeDescriptor() {}
// for custom vertex layout
@@ -33,14 +42,6 @@ struct VertexAttributeDescriptor
normalize = normalized;
}
- union {
- const void* pointer; // 内存地址或显存中相对VBO的偏移值
- int startOffset;
- };
- uint componentNum; // 向量维度1,2,3,4
- uint componentFormat; // 每个分量的类型
- uint16 stride; // 间隔
- bool normalize; // 是否归一化,只用于CustomVertexLayout
};
namespace VertexAttribute