From 48c3562a0ea76a26db5322b5a76979dcd849ee13 Mon Sep 17 00:00:00 2001 From: tovjemam Date: Thu, 15 Feb 2024 21:20:41 +0100 Subject: [PATCH] Add project files. --- karo.cpp | 199 +++++++++++++++++++++++++++++++++++++++++++ karo.sln | 31 +++++++ karo.vcxproj | 135 +++++++++++++++++++++++++++++ karo.vcxproj.filters | 22 +++++ 4 files changed, 387 insertions(+) create mode 100644 karo.cpp create mode 100644 karo.sln create mode 100644 karo.vcxproj create mode 100644 karo.vcxproj.filters diff --git a/karo.cpp b/karo.cpp new file mode 100644 index 0000000..4fa8326 --- /dev/null +++ b/karo.cpp @@ -0,0 +1,199 @@ +// karo.cpp : This file contains the 'main' function. Program execution begins and ends there. +// + +//#define GLEW_STATIC +#include +#include + +#include +#include + +#include +#include +#include + +struct Vertex { + glm::vec3 pos; + glm::vec3 color; +}; + +static const char* s_vs_src = R"GLSL( +#version 330 core + +layout (location = 0) in vec3 a_pos; +layout (location = 1) in vec3 a_color; + +out vec3 u_color; + +uniform mat4 u_mvp; + +void main() { + gl_Position = u_mvp * vec4(a_pos, 1.0); + u_color = a_color; +} + +)GLSL"; + +static const char* s_fs_src = R"GLSL( +#version 330 core + +in vec3 u_color; + +out vec4 o_color; + +void main() { + o_color = vec4(u_color, 1.0); +} +)GLSL"; + +void CheckShaderCompileErrors(GLuint shader) { + GLint success; + GLchar infoLog[512]; + glGetShaderiv(shader, GL_COMPILE_STATUS, &success); + if (!success) { + glGetShaderInfoLog(shader, 512, NULL, infoLog); + std::cerr << "Shader compilation failed: " << infoLog << std::endl; + exit(1); + } +} + +void CheckProgramLinkErrors(GLuint program) { + GLint success; + GLchar infoLog[512]; + glGetProgramiv(program, GL_LINK_STATUS, &success); + if (!success) { + glGetProgramInfoLog(program, 512, NULL, infoLog); + std::cerr << "Program link failed: " << infoLog << std::endl; + exit(1); + } +} + + +int main() { + if (!glfwInit()) { + std::cout << "Failed to initialize GLFW\n"; + return 1; + } + + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); + glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); + glfwWindowHint(GLFW_MAXIMIZED, GL_TRUE); + glfwWindowHint(GLFW_DEPTH_BITS, 0); + //glfwWindowHint(GLFW_DEPTH_BITS, 0); + glfwWindowHint(GLFW_STENCIL_BITS, 0); + glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); + + GLFWwindow* window = glfwCreateWindow(800, 600, "Karo", NULL, NULL); + if (!window) { + std::cerr << "Failed to create window\n"; + glfwTerminate(); + return 1; + } + + glfwMakeContextCurrent(window); + + if (glewInit() != GLEW_OK) { + std::cerr << "Failed to initialize GLEW\n"; + glfwTerminate(); + return 1; + } + + std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl; + + GLuint shader; + shader = glCreateProgram(); + + GLuint vs = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vs, 1, &s_vs_src, NULL); + glCompileShader(vs); + CheckShaderCompileErrors(vs); + + GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fs, 1, &s_fs_src, NULL); + glCompileShader(fs); + CheckShaderCompileErrors(fs); + + glAttachShader(shader, vs); + glAttachShader(shader, fs); + glLinkProgram(shader); + CheckProgramLinkErrors(shader); + + glDeleteShader(vs); + glDeleteShader(fs); + + glUseProgram(shader); + + GLuint u_mvp = glGetUniformLocation(shader, "u_mvp"); + + GLuint vao; + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + + GLuint vbo; + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + + //setup for stream draw + glBufferData(GL_ARRAY_BUFFER, 0, NULL, GL_STREAM_DRAW); + + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, pos)); + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, color)); + + glBindVertexArray(0); + + //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + + glfwShowWindow(window); + + //list of lines + std::vector vertices; + + while (!glfwWindowShouldClose(window)) { + glfwPollEvents(); + + int width, height; + glfwGetFramebufferSize(window, &width, &height); + + glViewport(0, 0, width, height); + + glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 100.0f); + glm::mat4 view = glm::lookAt(glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0, 1.0, 0.0)); + + glm::mat4 mvp = proj * view; + + glUniformMatrix4fv(u_mvp, 1, GL_FALSE, glm::value_ptr(mvp)); + + //glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + + // test data + vertices.push_back({ glm::vec3(-1, 0, 0), glm::vec3(1.0f, 0.0f, 0.0f) }); + vertices.push_back({ glm::vec3(1, 0, 0), glm::vec3(0.0f, 1.0f, 0.0f) }); + + glBindVertexArray(vao); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STREAM_DRAW); + + glDrawArrays(GL_LINES, 0, vertices.size()); + + vertices.clear(); + + glfwSwapBuffers(window); + } +} + +// Run program: Ctrl + F5 or Debug > Start Without Debugging menu +// Debug program: F5 or Debug > Start Debugging menu + +// Tips for Getting Started: +// 1. Use the Solution Explorer window to add/manage files +// 2. Use the Team Explorer window to connect to source control +// 3. Use the Output window to see build output and other messages +// 4. Use the Error List window to view errors +// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project +// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file diff --git a/karo.sln b/karo.sln new file mode 100644 index 0000000..30ae1db --- /dev/null +++ b/karo.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.7.34031.279 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "karo", "karo.vcxproj", "{8496A96C-82C8-47B3-9F3F-953B8224822B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {8496A96C-82C8-47B3-9F3F-953B8224822B}.Debug|x64.ActiveCfg = Debug|x64 + {8496A96C-82C8-47B3-9F3F-953B8224822B}.Debug|x64.Build.0 = Debug|x64 + {8496A96C-82C8-47B3-9F3F-953B8224822B}.Debug|x86.ActiveCfg = Debug|Win32 + {8496A96C-82C8-47B3-9F3F-953B8224822B}.Debug|x86.Build.0 = Debug|Win32 + {8496A96C-82C8-47B3-9F3F-953B8224822B}.Release|x64.ActiveCfg = Release|x64 + {8496A96C-82C8-47B3-9F3F-953B8224822B}.Release|x64.Build.0 = Release|x64 + {8496A96C-82C8-47B3-9F3F-953B8224822B}.Release|x86.ActiveCfg = Release|Win32 + {8496A96C-82C8-47B3-9F3F-953B8224822B}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F379E9DC-9B24-4981-8B1F-724EF520B550} + EndGlobalSection +EndGlobal diff --git a/karo.vcxproj b/karo.vcxproj new file mode 100644 index 0000000..e4c0aa0 --- /dev/null +++ b/karo.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {8496a96c-82c8-47b3-9f3f-953b8224822b} + karo + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/karo.vcxproj.filters b/karo.vcxproj.filters new file mode 100644 index 0000000..543ac9f --- /dev/null +++ b/karo.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file