MSYS2のMINGW64 → pacman やOpenGL関連ライブラリがインストール可能 な環境でGLSLを描画できた。その3

こちらが
マウス追従・時間変化付き・テクスチャあり・GLSLフル対応の完全版です

🔵
main.c(Cのグラフィックコード)

#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>

char* load_shader_source(const char* filepath) {
    FILE* file = fopen(filepath, "rb");
    if (!file) {
        fprintf(stderr, "Failed to open %s\n", filepath);
        exit(1);
    }
    fseek(file, 0, SEEK_END);
    long len = ftell(file);
    rewind(file);
    char* buffer = (char*)malloc(len + 1);
    fread(buffer, 1, len, file);
    buffer[len] = '\0';
    fclose(file);
    return buffer;
}

GLuint compile_shader(GLenum type, const char* source) {
    GLuint shader = glCreateShader(type);
    glShaderSource(shader, 1, &source, NULL);
    glCompileShader(shader);
    GLint success;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
    if (!success) {
        char infoLog[512];
        glGetShaderInfoLog(shader, 512, NULL, infoLog);
        fprintf(stderr, "Shader compile error: %s\n", infoLog);
        exit(1);
    }
    return shader;
}

int main() {
    if (!glfwInit()) {
        fprintf(stderr, "GLFW init failed\n");
        return -1;
    }

    GLFWwindow* window = glfwCreateWindow(800, 600, "GLSL Sample", NULL, NULL);
    if (!window) {
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);
    glewInit();

    char* vertexSource = load_shader_source("../shader.vert");
    char* fragmentSource = load_shader_source("../shader.frag");

    GLuint vertexShader = compile_shader(GL_VERTEX_SHADER, vertexSource);
    GLuint fragmentShader = compile_shader(GL_FRAGMENT_SHADER, fragmentSource);

    GLuint shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);

    glUseProgram(shaderProgram);

    GLint mouseLoc = glGetUniformLocation(shaderProgram, "u_mouse");
    GLint timeLoc = glGetUniformLocation(shaderProgram, "u_time");
    GLint resLoc   = glGetUniformLocation(shaderProgram, "u_resolution");
    glUniform1i(glGetUniformLocation(shaderProgram, "u_texture"), 0);

    float vertices[] = {
        // x, y, u, v
        -1.0f,  1.0f,  0.0f, 1.0f,
         1.0f,  1.0f,  1.0f, 1.0f,
         1.0f, -1.0f,  1.0f, 0.0f,
        -1.0f, -1.0f,  0.0f, 0.0f
    };
    unsigned int indices[] = {
        0, 1, 2,
        2, 3, 0
    };

    GLuint VAO, VBO, EBO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glGenBuffers(1, &EBO);

    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
    glEnableVertexAttribArray(1);

    int width, height, nrChannels;
    unsigned char *data = stbi_load("../texture.png", &width, &height, &nrChannels, 0);
    if (!data) {
        fprintf(stderr, "Failed to load texture\n");
        return -1;
    }

    GLuint texture;
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    GLenum format = (nrChannels == 4) ? GL_RGBA : GL_RGB;
    glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
    glGenerateMipmap(GL_TEXTURE_2D);
    stbi_image_free(data);

    while (!glfwWindowShouldClose(window)) {
        float timeValue = (float)glfwGetTime();
        int winW, winH;
        double mouseX, mouseY;

        glfwGetCursorPos(window, &mouseX, &mouseY);
        glfwGetWindowSize(window, &winW, &winH);
        mouseY = winH - mouseY;

        glClearColor(0.1f, 0.1f, 0.15f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glUseProgram(shaderProgram);
        glUniform1f(timeLoc, timeValue);
        glUniform2f(mouseLoc, (float)mouseX, (float)mouseY);
        glUniform2f(resLoc, (float)winW, (float)winH);

        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, texture);
        glBindVertexArray(VAO);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glDeleteProgram(shaderProgram);
    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);
    glfwTerminate();
    return 0;
}

🔴
shader.vert(頂点シェーダー)

#version 330 core
layout (location = 0) in vec2 aPos;
layout (location = 1) in vec2 aTexCoord;

out vec2 TexCoord;

void main() {
    gl_Position = vec4(aPos, 0.0, 1.0);
    TexCoord = aTexCoord;
}

🟣
shader.frag(フラグメントシェーダー)

#version 330 core
out vec4 fragColor;

in vec2 TexCoord;

uniform sampler2D u_texture;
uniform vec2 u_mouse;
uniform vec2 u_resolution;
uniform float u_time;

void main() {
    vec2 fragCoord = gl_FragCoord.xy;
    float dist = distance(fragCoord, u_mouse);
    float glow = 1.0 - smoothstep(0.0, 100.0, dist);

    vec4 tex = texture(u_texture, TexCoord);
    vec3 color = mix(tex.rgb, vec3(1.0, 1.0, 1.0), glow);

    fragColor = vec4(color, 1.0);
}

で実行


furcr@furcraea_built MINGW64 /c/glsl_sample
$ cd glsl_sample_040

furcr@furcraea_built MINGW64 /c/glsl_sample/glsl_sample_040
$ mkdir build

furcr@furcraea_built MINGW64 /c/glsl_sample/glsl_sample_040
$ cd build

furcr@furcraea_built MINGW64 /c/glsl_sample/glsl_sample_040/build
$ cmake -G "MSYS Makefiles" ..
CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):
  Compatibility with CMake < 3.10 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value.  Or, use the <min>...<max> syntax
  to tell CMake that the project requires at least <min> but has been updated
  to work with policies introduced by <max> or earlier.


-- The C compiler identification is GNU 15.1.0
-- The CXX compiler identification is GNU 15.1.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/msys64/mingw64/bin/cc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/msys64/mingw64/bin/c++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenGL: opengl32
-- Configuring done (1.2s)
-- Generating done (0.0s)
-- Build files have been written to: C:/glsl_sample/glsl_sample_040/build

furcr@furcraea_built MINGW64 /c/glsl_sample/glsl_sample_040/build
$ make
[ 50%] Building C object CMakeFiles/glsl_sample.dir/main.c.obj
[100%] Linking C executable glsl_sample.exe
[100%] Built target glsl_sample

furcr@furcraea_built MINGW64 /c/glsl_sample/glsl_sample_040/build
$ ./glsl_sample.exe

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です