いつもありがとうございます!!
テクスチャについて調べてみますが、全然わからなくて、、
シェーダコンパイルでエラーが出ます。
texture2Dは削除されたとあるみたいですが、調べてみてもよくわかりませんでした、、
#####error
pointvert
1 #version 330 core 2 in vec4 position; 3 in vec2 uv; 4 out vec2 vuv; 5 void main() 6 { 7 gl_Position = position; 8 vuv = uv; 9 }
pointfrag
1#version 330 core 2in vec2 vuv; 3out vec4 fragment; 4uniform sampler2D tex; 5void main(void){ 6 fragment = texture(tex, vuv); 7}
main
1#include <cstdlib> 2#include <iostream> 3#include <fstream> 4#include <vector> 5#include <memory> 6#include <GL/glew.h> 7#include <GLFW/glfw3.h> 8#include <opencv2/core.hpp> 9#include <opencv2/highgui.hpp> 10#include <opencv2/imgproc.hpp> 11#include <opencv2/imgcodecs.hpp> 12 13#include "Object.h" 14#include "Shape.h" 15 16using std::vector; 17 18GLboolean printShaderInfoLog(GLuint shader, const char *str) 19{ 20 GLint status; 21 glGetShaderiv(shader, GL_COMPILE_STATUS, &status); 22 if (status == GL_FALSE) std::cerr << "Compile Error in" << str << std::endl; 23 24 GLsizei bufSize; 25 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &bufSize); 26 27 if (bufSize > 1) 28 { 29 std::vector<GLchar> infoLog(bufSize); 30 GLsizei length; 31 glGetShaderInfoLog(shader, bufSize, &length, &infoLog[0]); 32 std::cerr << &infoLog[0] << std::endl; 33 } 34 35 return static_cast<GLboolean>(status); 36} 37 38// プログラムオブジェクトのリンク結果を表示する 39// program: プログラムオブジェクト名 40GLboolean printProgramInfoLog(GLuint program) 41{ 42 // リンク結果を取得する 43 GLint status; 44 glGetProgramiv(program, GL_LINK_STATUS, &status); 45 if (status == GL_FALSE) std::cerr << "Link Error." << std::endl; 46 // シェーダのリンク時のログの長さを取得する 47 GLsizei bufSize; 48 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufSize); 49 if (bufSize > 1) 50 { 51 // シェーダのリンク時のログの内容を取得する 52 std::vector<GLchar> infoLog(bufSize); 53 GLsizei length; 54 glGetProgramInfoLog(program, bufSize, &length, &infoLog[0]); 55 std::cerr << &infoLog[0] << std::endl; 56 } 57 return static_cast<GLboolean>(status); 58} 59 60//プログラムオブジェクト作成 61// vsrc: バーテックシェーダ 62// fsrc: フラグメントシェーダ 63GLuint createProgram(const char *vsrc, const char *fsrc) 64{ 65 const GLuint program(glCreateProgram()); 66 67 if (vsrc != NULL) 68 { 69 const GLuint vobj(glCreateShader(GL_VERTEX_SHADER)); 70 glShaderSource(vobj, 1, &vsrc, NULL); 71 glCompileShader(vobj); 72 73 if (printShaderInfoLog(vobj, "vertex shader")) 74 glAttachShader(program, vobj); 75 glDeleteShader(vobj); 76 } 77 78 if (fsrc != NULL) 79 { 80 const GLuint fobj(glCreateShader(GL_FRAGMENT_SHADER)); 81 glShaderSource(fobj, 1, &fsrc, NULL); 82 glCompileShader(fobj); 83 84 if (printShaderInfoLog(fobj, "fragment shader")) 85 glAttachShader(program, fobj); 86 glDeleteShader(fobj); 87 } 88 glBindFragDataLocation(program, 0, "fragment"); 89 glLinkProgram(program); 90 91 if (printProgramInfoLog(program)) 92 return program; 93 94 glDeleteProgram(program); 95 return program; 96} 97 98bool readShaderSource(const char *name, std::vector<GLchar> &buffer) 99{ 100 if (name == NULL) return false; 101 102 std::ifstream file(name, std::ios::binary); 103 if (file.fail()) 104 { 105 std::cerr << "Error: Can't open source file:" << name << std::endl; 106 return false; 107 } 108 109 file.seekg(0L, std::ios::end); 110 111 GLsizei length = static_cast<GLsizei>(file.tellg()); 112 113 buffer.resize(length + 1); 114 115 file.seekg(0L, std::ios::beg); 116 file.read(buffer.data(), length); 117 buffer[length] = '\0'; 118 119 if (file.fail()) 120 { 121 std::cerr << "Error: Could not read source file:" << name << std::endl; 122 file.close(); 123 return false; 124 } 125} 126 127GLuint loadProgram(const char *vert, const char *frag) 128{ 129 // シェーダのソースファイルを読み込む 130 std::vector<GLchar> vsrc; 131 const bool vstat(readShaderSource(vert, vsrc)); 132 std::vector<GLchar> fsrc; 133 const bool fstat(readShaderSource(frag, fsrc)); 134 // プログラムオブジェクトを作成する 135 return vstat && fstat ? createProgram(vsrc.data(), fsrc.data()) : 0; 136} 137 138Object::Vertex vertex[] = { 139 0.5f, 0.5f, 140 -0.5f, 0.5f, 141 -0.5f, -0.5f, 142 0.5f, -0.5f 143}; 144 145 Object::Vertex uv[] = { 146 1, 0, 147 0, 0, 148 0, 1, 149 1, 1, 150}; 151 152 153int main() 154{ 155 if (glfwInit() == GL_FALSE) 156 { 157 std::cerr << "Can't initialize GLFW" << std::endl; 158 return 1; 159 } 160 161 atexit(glfwTerminate); 162 163 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 164 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); 165 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 166 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 167 168 GLFWwindow *const window = glfwCreateWindow(640, 480, "Hello!", NULL, NULL); 169 170 if (window == NULL) 171 { 172 std::cerr << "Can't create GLFW Window." << std::endl; 173 exit(1); 174 } 175 176 glfwMakeContextCurrent(window); 177 178 glewExperimental = GL_TRUE; 179 if (glewInit() != GLEW_OK) 180 { 181 std::cerr << "Can't, initialize GLEW" << std::endl; 182 exit(1); 183 } 184 185 glfwSwapInterval(1); 186 187 glClearColor(1.0f, 1.0f, 1.0f, 0.0f); 188 189 const GLuint program(loadProgram("point.vert", "point.frag")); 190 191 std::unique_ptr<const Shape> shape(new Shape(2, 4, vertex, program, uv,"texture0.png")); 192 193 194 while (!(glfwWindowShouldClose(window))) 195 { 196 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 197 glUseProgram(program); 198 199 shape->draw(); 200 201 glfwSwapBuffers(window); 202 glfwPollEvents(); 203 } 204}
object
1#pragma once 2#include <GL/glew.h> 3#include <vector> 4 5class Object 6{ 7 GLuint vao; 8 GLuint vbo[2]; 9 10protected: 11 12public: 13 GLuint texID; 14 struct Vertex 15 { 16 GLfloat position[2]; 17 }; 18 19 Object(GLint size, GLsizei vertexcount, const Vertex *vertex, const GLuint program, const Vertex *uv, std::string texname) 20 { 21 glGenVertexArrays(1, &vao); 22 glBindVertexArray(vao); 23 24 glGenBuffers(2, vbo); 25 glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); 26 glBufferData(GL_ARRAY_BUFFER, vertexcount * sizeof(Vertex), vertex, GL_STATIC_DRAW); 27 int positionLocation = glGetAttribLocation(program, "position"); 28 glVertexAttribPointer(positionLocation, size, GL_FLOAT, GL_FALSE, 0, 0); 29 30 glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); 31 glBufferData(GL_ARRAY_BUFFER, 2 * sizeof(Vertex), uv, GL_STATIC_DRAW); 32 int uvLocation = glGetAttribLocation(program, "uv"); 33 glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, 0, 0); 34 35 glEnableVertexAttribArray(positionLocation); 36 glEnableVertexAttribArray(uvLocation); 37 38 glGenTextures(1, &texID); 39 glBindTexture(GL_TEXTURE_2D, texID); 40 41 //画像の読み込み 42 cv::Mat img = cv::imread(texname, cv::IMREAD_UNCHANGED); 43 //BGRAからRGBAへ変換 44 cv::cvtColor(img, img, cv::COLOR_BGRA2RGBA); 45 46 //テクスチャにデータを紐付ける 47 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.cols, img.rows, 0, GL_RGBA, GL_UNSIGNED_BYTE, 48 img.data); 49 50 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 51 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 52 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 53 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 54 55 glBindTexture(GL_TEXTURE_2D, 0); 56 int textureLocation = glGetUniformLocation(program, "tex"); 57 glUniform1i(textureLocation, 0); 58 } 59 60 void bind() const 61 { 62 glBindVertexArray(vao); 63 } 64 65 virtual ~Object() 66 { 67 glDeleteBuffers(1, &vao); 68 glDeleteBuffers(1, &vbo[0]); 69 glDeleteBuffers(1, &vbo[1]); 70 } 71 72private: 73 //コピーコンストラクタの禁止 74 Object(const Object &o); 75 //代入によるコピーの禁止 76 Object &operator=(const Object &o); 77};
#pragma once #include <memory> #include <vector> #include "Object.h" class Shape { // 図形データ std::shared_ptr<const Object> object; protected: const GLsizei vertexcount; public: // コンストラクタ // size: 頂点の位置の次元 // vertexcount: 頂点の数 // vertex: 頂点属性を格納した配列 Shape(GLint size, GLsizei vertexcount, const Object::Vertex *vertex, const GLuint program, const Object::Vertex *uv, std::string texname) : object(new Object(size, vertexcount, vertex, program, uv, texname)) , vertexcount(vertexcount) { } // 描画 void draw() const { object->bind(); execute(); } // 描画の実行 virtual void execute() const { // 折れ線で描画する glBindTexture(GL_TEXTURE_2D, object->texID); glDrawArrays(GL_LINE_LOOP, 0, vertexcount); } };
#####実行結果
四角は表示されます。
回答してくれると嬉しいです!!

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/23 12:45
2019/04/23 20:30
2019/04/24 06:57