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)
|
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(glad)
|
||||||
add_subdirectory(glfw)
|
add_subdirectory(glfw)
|
||||||
|
|
||||||
@@ -14,3 +22,7 @@ set(SRC
|
|||||||
pd_add_lib(pd-desktop SRC_FILES ${SRC})
|
pd_add_lib(pd-desktop SRC_FILES ${SRC})
|
||||||
target_include_directories(pd-desktop PUBLIC include)
|
target_include_directories(pd-desktop PUBLIC include)
|
||||||
target_link_libraries(pd-desktop PUBLIC palladium glad glfw)
|
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 pLocProjection;
|
||||||
GLuint pLocTex;
|
GLuint pLocTex;
|
||||||
GLuint pLocAlfa;
|
GLuint pLocAlfa;
|
||||||
GLuint VBO, IBO;
|
GLuint VBO, IBO, VAO; // vao is only used in > 3.3
|
||||||
};
|
};
|
||||||
} // namespace PD
|
} // namespace PD
|
||||||
@@ -25,6 +25,7 @@ SOFTWARE.
|
|||||||
#include <pd-desktop/bknd-gfx.hpp>
|
#include <pd-desktop/bknd-gfx.hpp>
|
||||||
|
|
||||||
namespace PD {
|
namespace PD {
|
||||||
|
#if PD_OPENGL >= 21 && PD_OPENGL < 33
|
||||||
const char* vertex_shader = R"(
|
const char* vertex_shader = R"(
|
||||||
#version 120
|
#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 compileShader(const std::string& source, GLenum type) {
|
||||||
GLuint shader = glCreateShader(type);
|
GLuint shader = glCreateShader(type);
|
||||||
@@ -133,12 +179,27 @@ void GfxGL2::Init() {
|
|||||||
Shader = createShaderProgram(vertex_shader, frag_shader);
|
Shader = createShaderProgram(vertex_shader, frag_shader);
|
||||||
glUseProgram(Shader);
|
glUseProgram(Shader);
|
||||||
|
|
||||||
|
#if PD_OPENGL >= 33
|
||||||
|
glGenVertexArrays(1, &VAO);
|
||||||
|
glBindVertexArray(VAO);
|
||||||
|
#endif
|
||||||
|
|
||||||
glGenBuffers(1, &VBO);
|
glGenBuffers(1, &VBO);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
|
|
||||||
// Attribs Setup
|
#if PD_OPENGL < 33
|
||||||
SetupShaderAttribs(Shader);
|
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);
|
glGenBuffers(1, &IBO);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
|
||||||
|
|
||||||
@@ -148,11 +209,17 @@ void GfxGL2::Init() {
|
|||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||||
|
#if PD_OPENGL >= 33
|
||||||
|
glBindVertexArray(0);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void GfxGL2::Deinit() {
|
void GfxGL2::Deinit() {
|
||||||
glDeleteBuffers(1, &VBO);
|
glDeleteBuffers(1, &VBO);
|
||||||
glDeleteBuffers(1, &IBO);
|
glDeleteBuffers(1, &IBO);
|
||||||
|
#if PD_OPENGL >= 33
|
||||||
|
glDeleteBuffers(1, &VAO);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void GfxGL2::NewFrame() {
|
void GfxGL2::NewFrame() {
|
||||||
@@ -215,12 +282,20 @@ void GfxGL2::RenderDrawData(const Li::CmdPool& Commands) {
|
|||||||
glDisable(GL_SCISSOR_TEST);
|
glDisable(GL_SCISSOR_TEST);
|
||||||
}
|
}
|
||||||
BindTex(Tex->Address);
|
BindTex(Tex->Address);
|
||||||
|
|
||||||
|
#if PD_OPENGL >= 33
|
||||||
|
glBindVertexArray(VAO);
|
||||||
|
#endif
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||||
glBufferData(GL_ARRAY_BUFFER, CurrentVertex * sizeof(PD::Li::Vertex),
|
glBufferData(GL_ARRAY_BUFFER, CurrentVertex * sizeof(PD::Li::Vertex),
|
||||||
&VertexBuffer[0], GL_DYNAMIC_DRAW);
|
&VertexBuffer[0], GL_DYNAMIC_DRAW);
|
||||||
|
|
||||||
|
#if PD_OPENGL < 33
|
||||||
// For some reason we need to set these every frame for every buffer
|
// For some reason we need to set these every frame for every buffer
|
||||||
// Found that out when creating My 3d Engine
|
// Found that out when creating My 3d Engine
|
||||||
SetupShaderAttribs(Shader);
|
SetupShaderAttribs(Shader);
|
||||||
|
#endif
|
||||||
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, CurrentIndex * sizeof(PD::u16),
|
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_ARRAY_BUFFER, 0);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||||
BindTex(0);
|
BindTex(0);
|
||||||
|
#if PD_OPENGL >= 33
|
||||||
|
glBindVertexArray(0);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,13 @@ int main() {
|
|||||||
/** Setup Window and Context */
|
/** Setup Window and Context */
|
||||||
glfwInit();
|
glfwInit();
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
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);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
|
||||||
|
#endif
|
||||||
glfwWindowHint(GLFW_SAMPLES, 8);
|
glfwWindowHint(GLFW_SAMPLES, 8);
|
||||||
auto win = glfwCreateWindow(1280, 720, "Test", nullptr, nullptr);
|
auto win = glfwCreateWindow(1280, 720, "Test", nullptr, nullptr);
|
||||||
glfwMakeContextCurrent(win);
|
glfwMakeContextCurrent(win);
|
||||||
@@ -82,7 +88,7 @@ int main() {
|
|||||||
PD::Gfx::pGfx->ViewPort = PD::ivec2(wx, wy);
|
PD::Gfx::pGfx->ViewPort = PD::ivec2(wx, wy);
|
||||||
ui7->GetIO()->CurrentViewPort = PD::ivec4(0, 0, wx, wy);
|
ui7->GetIO()->CurrentViewPort = PD::ivec4(0, 0, wx, wy);
|
||||||
glViewport(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);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
// ui7->pIO->GetViewPort(VpTop)->pSize = PD::ivec4(0, 0, wx, wy);
|
// ui7->pIO->GetViewPort(VpTop)->pSize = PD::ivec4(0, 0, wx, wy);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user