OpenGL 2Dの雛形

最終更新日01 Aug 2017 05:28

#include "../../Common/shaderutility.h"
#include <GLFW/glfw3.h>
#pragma comment(lib, "glfw3.lib")
 
using namespace System::Drawing;
GLuint vao;
GLuint textureName;
GLuint shaderID;
GLFWwindow* window;
//@param aspectRatio=width/height
void resize(float aspectRatio) {
    int width, height;
    glfwGetWindowSize(window, &width, &height);
    height = width / aspectRatio;
    glfwSetWindowSize(window, width, height);
    glViewport(0, 0, width, height);
}
//reference https://stackoverflow.com/questions/4907713/how-to-properly-convert-managed-bitmap-into-unmanaged-bitmap
void loadImage(const char* filepath) {
    Bitmap^ bitmap = gcnew Bitmap(gcnew System::String(filepath));
    int width = bitmap->Width;
    int height = bitmap->Height;
    //アスペクト比を直す
    float aspect = width / float(height);
    resize(aspect);
    System::Drawing::Rectangle rect = System::Drawing::Rectangle(0, 0, bitmap->Width, bitmap->Height);
    Imaging::BitmapData ^  bitmapData = bitmap->LockBits(rect, Imaging::ImageLockMode::ReadWrite, bitmap->PixelFormat);
 
    //default Format32bppArgb
    int internalformat = GL_RGBA32F;
    int format = GL_BGRA_EXT;
    int channel = 4;
    if(bitmap->PixelFormat  == Imaging::PixelFormat::Format24bppRgb)
    {
        internalformat = GL_RGB32F;
        format = GL_BGR_EXT;
        channel = 3;
    }
 
    int size = bitmap->Width*bitmap->Height*channel;
    unsigned char* dataTexture = new unsigned char[size];
    memcpy(dataTexture, bitmapData->Scan0.ToPointer(), size);
 
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, textureName);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    glTexImage2D(GL_TEXTURE_2D, 0, internalformat, bitmap->Width, bitmap->Height, 0, format, GL_UNSIGNED_BYTE, dataTexture);
 
    bitmap->UnlockBits(bitmapData);
 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    delete dataTexture;
 
}
void drop_callback(GLFWwindow* window, int count, const char** paths)
{
    int i;
    cout << "ファイルがドロップされました" << endl;
    if (count > 1)cout << "注意!複数のfbxファイル受け付けは未サポート" << endl;
    for (i = 0; i < count; i++) {
        cout << paths[i] << endl;
        loadImage(paths[i]);
    }
}
void capture(GLFWwindow *window) {
    int width, height;
    glfwGetWindowSize(window, &width, &height);
    glReadBuffer(GL_FRONT);// フロントを読み込む様に設定する
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    unsigned char* data = new unsigned char[width*height * 4];
    glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, data);
    System::IntPtr scan0(data);
    Bitmap ^bitmap = gcnew Bitmap(width, height, width * 4, Imaging::PixelFormat::Format32bppArgb, scan0);
    bitmap->Save("capture.png");
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if (action == GLFW_RELEASE) return;
    if (key == GLFW_KEY_ESCAPE) {
        glfwSetWindowShouldClose(window, GL_TRUE);
        return;
    }
    if (key == GLFW_KEY_C) {
        capture(window);
        return;
    }
}
void init() {
    shaderID = miffy::CreateShaderFromFile("passthrough.vert","passthrough.frag");
    GLuint vbo;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);
    glGenBuffers(1,&vbo);
 
    float vertices[8] = { 0.0f,0.0f,1.0f,0.0f,1.0f,1.0f,0.0f,1.0f };
    glBindBuffer(GL_ARRAY_BUFFER,vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices),vertices, GL_STATIC_DRAW);
    unsigned short index[] = { 0,1,2,0,3 };
    GLuint ebo = 0;
    glGenBuffers(1, &ebo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(index), index, GL_STATIC_DRAW);
 
    glUseProgram(shaderID);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
    loadImage("image.png");
}
void resize(int width,int height){
    glViewport(0, 0, width, height);
}
void render() {
    glClearColor(0.3f, 0.3f, 1.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glUseProgram(shaderID);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, textureName);
    glActiveTexture(GL_TEXTURE0);
    glUniform1i(glGetUniformLocation(shaderID, "tex"), 0);
 
    glBindVertexArray(vao);
    glDrawElements(GL_TRIANGLE_STRIP, 5, GL_UNSIGNED_SHORT, 0);
    glUseProgram(0);
}
int main()
{
    glfwInit();
    window = glfwCreateWindow(640, 480, "Hello World", nullptr, nullptr);
    glfwMakeContextCurrent(window);
    glewInit();
    glfwSetKeyCallback(window, key_callback);
    glfwSetDropCallback(window, drop_callback);
 
    init();
    while (!glfwWindowShouldClose(window))
    {
        float ratio;
        int width, height;
        glfwGetFramebufferSize(window, &width, &height);
        resize(width, height);
        render();
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
 
    glfwTerminate();
    return 0;
}

total 153行

2d


ファイル

サポートサイト Wikidot.com 2d