Created
October 14, 2021 07:42
-
-
Save doccaico/0b4df99fb416f8f37263a511d139f144 to your computer and use it in GitHub Desktop.
glfw glwe opengl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // http://codelabo.com/posts/20200228150223 | |
| // Ubuntu | |
| // sudo apt get install libglfw3-dev libglew-dev | |
| // Build | |
| // g++ main.cc -lGL -lglfw -lGLEW && ./a.out | |
| #include <fstream> | |
| #include <iostream> | |
| #include <sstream> | |
| #include <string> | |
| #include <GL/glew.h> | |
| #include <GLFW/glfw3.h> | |
| int readShaderSource(GLuint shaderObj, std::string fileName) | |
| { | |
| //ファイルの読み込み | |
| std::ifstream ifs(fileName); | |
| if (!ifs) | |
| { | |
| std::cout << "error" << std::endl; | |
| return -1; | |
| } | |
| std::string source; | |
| std::string line; | |
| while (getline(ifs, line)) | |
| { | |
| source += line + "\n"; | |
| } | |
| // シェーダのソースプログラムをシェーダオブジェクトへ読み込む | |
| const GLchar *sourcePtr = (const GLchar *)source.c_str(); | |
| GLint length = source.length(); | |
| glShaderSource(shaderObj, 1, &sourcePtr, &length); | |
| return 0; | |
| } | |
| GLint makeShader(std::string vertexFileName, std::string fragmentFileName) | |
| { | |
| // シェーダーオブジェクト作成 | |
| GLuint vertShaderObj = glCreateShader(GL_VERTEX_SHADER); | |
| GLuint fragShaderObj = glCreateShader(GL_FRAGMENT_SHADER); | |
| GLuint shader; | |
| // シェーダーコンパイルとリンクの結果用変数 | |
| GLint compiled, linked; | |
| /* シェーダーのソースプログラムの読み込み */ | |
| if (readShaderSource(vertShaderObj, vertexFileName)) return -1; | |
| if (readShaderSource(fragShaderObj, fragmentFileName)) return -1; | |
| /* バーテックスシェーダーのソースプログラムのコンパイル */ | |
| glCompileShader(vertShaderObj); | |
| glGetShaderiv(vertShaderObj, GL_COMPILE_STATUS, &compiled); | |
| if (compiled == GL_FALSE) | |
| { | |
| fprintf(stderr, "Compile error in vertex shader.\n"); | |
| return -1; | |
| } | |
| /* フラグメントシェーダーのソースプログラムのコンパイル */ | |
| glCompileShader(fragShaderObj); | |
| glGetShaderiv(fragShaderObj, GL_COMPILE_STATUS, &compiled); | |
| if (compiled == GL_FALSE) | |
| { | |
| fprintf(stderr, "Compile error in fragment shader.\n"); | |
| return -1; | |
| } | |
| /* プログラムオブジェクトの作成 */ | |
| shader = glCreateProgram(); | |
| /* シェーダーオブジェクトのシェーダープログラムへの登録 */ | |
| glAttachShader(shader, vertShaderObj); | |
| glAttachShader(shader, fragShaderObj); | |
| /* シェーダーオブジェクトの削除 */ | |
| glDeleteShader(vertShaderObj); | |
| glDeleteShader(fragShaderObj); | |
| /* シェーダープログラムのリンク */ | |
| glLinkProgram(shader); | |
| glGetProgramiv(shader, GL_LINK_STATUS, &linked); | |
| if (linked == GL_FALSE) | |
| { | |
| fprintf(stderr, "Link error.\n"); | |
| return -1; | |
| } | |
| return shader; | |
| } | |
| int main() | |
| { | |
| // GLFW初期化 | |
| if (glfwInit() == GL_FALSE) | |
| { | |
| return -1; | |
| } | |
| // ウィンドウ生成 | |
| GLFWwindow* window = glfwCreateWindow(640, 480, "OpenGL Sample", NULL, NULL); | |
| if (!window) | |
| { | |
| glfwTerminate(); | |
| return -1; | |
| } | |
| // バージョン2.1指定 | |
| glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); | |
| glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); | |
| glfwMakeContextCurrent(window); | |
| glfwSwapInterval(0); | |
| // GLEW初期化 | |
| if (glewInit() != GLEW_OK) | |
| { | |
| return -1; | |
| } | |
| GLint shader = makeShader("shader.vert", "shader.frag"); | |
| // フレームループ | |
| while (glfwWindowShouldClose(window) == GL_FALSE) | |
| { | |
| // バッファのクリア | |
| glClearColor(0.2f, 0.2f, 0.2f, 0.0f); | |
| glClear(GL_COLOR_BUFFER_BIT); | |
| glUseProgram(shader); | |
| // 色指定 | |
| glColor4f(1.0, 0.0, 0.0, 1.0); | |
| // 3つの頂点座標をGPUに転送 | |
| glBegin(GL_TRIANGLES); | |
| glVertex2f( 0, 0.5); | |
| glVertex2f(-0.5, -0.5); | |
| glVertex2f( 0.5, -0.5); | |
| glEnd(); | |
| // ダブルバッファのスワップ | |
| glfwSwapBuffers(window); | |
| glfwPollEvents(); | |
| } | |
| // GLFWの終了処理 | |
| glfwTerminate(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment