Add MacOS support as well as OpenGL 3.3 support
This commit is contained in:
@@ -2,6 +2,14 @@ cmake_minimum_required(VERSION 3.22)
|
||||
|
||||
project(pd-desktop LANGUAGES CXX VERSION 0.5.0)
|
||||
|
||||
set(PD_GL_VERSION 21 CACHE STRING "OpenGL Version (2.1 -> 21, 3.3 -> 33)")
|
||||
|
||||
if(APPLE)
|
||||
set(PD_GL_VERSION 33 CACHE STRING "33" FORCE)
|
||||
endif()
|
||||
|
||||
message("Using OpenGL: " ${PD_GL_VERSION})
|
||||
|
||||
add_subdirectory(glad)
|
||||
add_subdirectory(glfw)
|
||||
|
||||
@@ -14,3 +22,7 @@ set(SRC
|
||||
pd_add_lib(pd-desktop SRC_FILES ${SRC})
|
||||
target_include_directories(pd-desktop PUBLIC include)
|
||||
target_link_libraries(pd-desktop PUBLIC palladium glad glfw)
|
||||
|
||||
target_compile_definitions(pd-desktop
|
||||
PRIVATE PD_OPENGL=${PD_GL_VERSION}
|
||||
)
|
||||
@@ -57,6 +57,6 @@ class GfxGL2 : public GfxDriver {
|
||||
GLuint pLocProjection;
|
||||
GLuint pLocTex;
|
||||
GLuint pLocAlfa;
|
||||
GLuint VBO, IBO;
|
||||
GLuint VBO, IBO, VAO; // vao is only used in > 3.3
|
||||
};
|
||||
} // namespace PD
|
||||
@@ -25,6 +25,7 @@ SOFTWARE.
|
||||
#include <pd-desktop/bknd-gfx.hpp>
|
||||
|
||||
namespace PD {
|
||||
#if PD_OPENGL >= 21 && PD_OPENGL < 33
|
||||
const char* vertex_shader = R"(
|
||||
#version 120
|
||||
|
||||
@@ -64,6 +65,51 @@ const char* frag_shader = R"(
|
||||
}
|
||||
}
|
||||
)";
|
||||
#elif PD_OPENGL >= 33
|
||||
const char* vertex_shader = R"(
|
||||
#version 330 core
|
||||
|
||||
layout(location = 0) in vec2 pos;
|
||||
layout(location = 1) in vec2 uv;
|
||||
layout(location = 2) in vec4 color;
|
||||
|
||||
out vec2 oUV;
|
||||
out vec4 oColor;
|
||||
|
||||
// Probably forgot about this matrix and
|
||||
// searched hours for why the rendering isn't working :/
|
||||
uniform mat4 projection;
|
||||
|
||||
void main() {
|
||||
gl_Position = projection*vec4(pos, 0.0, 1.0);
|
||||
oUV = uv;
|
||||
oColor = color;
|
||||
}
|
||||
)";
|
||||
|
||||
const char* frag_shader = R"(
|
||||
#version 330 core
|
||||
|
||||
in vec2 oUV;
|
||||
in vec4 oColor;
|
||||
|
||||
uniform sampler2D tex;
|
||||
uniform bool alfa;
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
void main() {
|
||||
vec4 tc = texture(tex, oUV);
|
||||
if (alfa) {
|
||||
FragColor = vec4(oColor.rgb, tc.a * oColor.a);
|
||||
} else {
|
||||
FragColor = tc * oColor;
|
||||
}
|
||||
}
|
||||
)";
|
||||
#else
|
||||
// Need to show error
|
||||
#endif
|
||||
|
||||
GLuint compileShader(const std::string& source, GLenum type) {
|
||||
GLuint shader = glCreateShader(type);
|
||||
@@ -133,12 +179,27 @@ void GfxGL2::Init() {
|
||||
Shader = createShaderProgram(vertex_shader, frag_shader);
|
||||
glUseProgram(Shader);
|
||||
|
||||
#if PD_OPENGL >= 33
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glBindVertexArray(VAO);
|
||||
#endif
|
||||
|
||||
glGenBuffers(1, &VBO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
|
||||
// Attribs Setup
|
||||
SetupShaderAttribs(Shader);
|
||||
|
||||
#if PD_OPENGL < 33
|
||||
SetupShaderAttribs(Shader); // GL 2.1
|
||||
#else
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(PD::Li::Vertex),
|
||||
(void*)offsetof(PD::Li::Vertex, Pos));
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(PD::Li::Vertex),
|
||||
(void*)offsetof(PD::Li::Vertex, UV));
|
||||
glEnableVertexAttribArray(2);
|
||||
glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(PD::Li::Vertex),
|
||||
(void*)offsetof(PD::Li::Vertex, Color));
|
||||
#endif
|
||||
glGenBuffers(1, &IBO);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
|
||||
|
||||
@@ -148,11 +209,17 @@ void GfxGL2::Init() {
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
#if PD_OPENGL >= 33
|
||||
glBindVertexArray(0);
|
||||
#endif
|
||||
}
|
||||
|
||||
void GfxGL2::Deinit() {
|
||||
glDeleteBuffers(1, &VBO);
|
||||
glDeleteBuffers(1, &IBO);
|
||||
#if PD_OPENGL >= 33
|
||||
glDeleteBuffers(1, &VAO);
|
||||
#endif
|
||||
}
|
||||
|
||||
void GfxGL2::NewFrame() {
|
||||
@@ -215,12 +282,20 @@ void GfxGL2::RenderDrawData(const Li::CmdPool& Commands) {
|
||||
glDisable(GL_SCISSOR_TEST);
|
||||
}
|
||||
BindTex(Tex->Address);
|
||||
|
||||
#if PD_OPENGL >= 33
|
||||
glBindVertexArray(VAO);
|
||||
#endif
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, CurrentVertex * sizeof(PD::Li::Vertex),
|
||||
&VertexBuffer[0], GL_DYNAMIC_DRAW);
|
||||
|
||||
#if PD_OPENGL < 33
|
||||
// For some reason we need to set these every frame for every buffer
|
||||
// Found that out when creating My 3d Engine
|
||||
SetupShaderAttribs(Shader);
|
||||
#endif
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, CurrentIndex * sizeof(PD::u16),
|
||||
@@ -232,6 +307,9 @@ void GfxGL2::RenderDrawData(const Li::CmdPool& Commands) {
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
BindTex(0);
|
||||
#if PD_OPENGL >= 33
|
||||
glBindVertexArray(0);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,12 +21,18 @@ int TheScale = 1;
|
||||
bool AboutOderSo = true;
|
||||
|
||||
int main() {
|
||||
void *PD_INIT_DATA = nullptr;
|
||||
void* PD_INIT_DATA = nullptr;
|
||||
#ifndef __3DS__
|
||||
/** Setup Window and Context */
|
||||
glfwInit();
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
#ifdef __APPLE__
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, 0);
|
||||
#else
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
|
||||
#endif
|
||||
glfwWindowHint(GLFW_SAMPLES, 8);
|
||||
auto win = glfwCreateWindow(1280, 720, "Test", nullptr, nullptr);
|
||||
glfwMakeContextCurrent(win);
|
||||
@@ -36,9 +42,9 @@ int main() {
|
||||
osSetSpeedupEnable(true);
|
||||
gfxInitDefault();
|
||||
C3D_Init(C3D_DEFAULT_CMDBUF_SIZE * 2);
|
||||
C3D_RenderTarget *Top =
|
||||
C3D_RenderTarget* Top =
|
||||
C3D_RenderTargetCreate(240, 400, GPU_RB_RGBA8, GPU_RB_DEPTH24_STENCIL8);
|
||||
C3D_RenderTarget *Bottom =
|
||||
C3D_RenderTarget* Bottom =
|
||||
C3D_RenderTargetCreate(240, 320, GPU_RB_RGBA8, GPU_RB_DEPTH24_STENCIL8);
|
||||
C3D_RenderTargetSetOutput(Top, GFX_TOP, GFX_LEFT, DisplayTransferFlags);
|
||||
C3D_RenderTargetSetOutput(Bottom, GFX_BOTTOM, GFX_LEFT, DisplayTransferFlags);
|
||||
@@ -82,7 +88,7 @@ int main() {
|
||||
PD::Gfx::pGfx->ViewPort = PD::ivec2(wx, wy);
|
||||
ui7->GetIO()->CurrentViewPort = PD::ivec4(0, 0, wx, wy);
|
||||
glViewport(0, 0, wx, wy);
|
||||
glClearColor(0, 0, 0, 0);
|
||||
glClearColor(0.1, 0.1, 0.1, 1);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
// ui7->pIO->GetViewPort(VpTop)->pSize = PD::ivec4(0, 0, wx, wy);
|
||||
#endif
|
||||
@@ -143,7 +149,7 @@ int main() {
|
||||
menu->Label(std::format("FocusedMenuRect: {}",
|
||||
ui7->pIO->InputHandler->FocusedMenuRect));
|
||||
menu->SeparatorText("Menu Order");
|
||||
for (auto &it : ui7->pDFO) {
|
||||
for (auto& it : ui7->pDFO) {
|
||||
menu->Label(std::format("{}", ui7->pMenus[it]->pID.GetName()));
|
||||
}
|
||||
ui7->EndMenu();
|
||||
|
||||
Reference in New Issue
Block a user