class Object { public: struct Vertex { GLfloat position[3]; GLfloat normal[3]; }; };
constexpr Object::Vertex solidCubeVertex[] = { // 左 { -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f }, { -1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 0.0f }, { -1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f }, { -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f }, { -1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f }, { -1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f }, // 裏 { 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f }, ・ ・ ・
このように初期化した場合最初の3つがpositionにはいってあとの3つがnormalに入るんですか?
あと{ 1.0f, -1.0f, -1.0f)のように3つだけ書いたらどうなりますか?
#pragma once #include <GL/glew.h> class Object { GLuint vao; GLuint vbo; GLuint ibo; public: struct Vertex { GLfloat position[3]; GLfloat normal[3]; }; Object(GLint size, GLsizei vertexcount, const Vertex *vertex, GLsizei indexcount = 0, const GLuint *index = NULL) { glGenVertexArrays(1, &vao); glBindVertexArray(vao); glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, vertexcount * sizeof(Vertex), vertex, GL_STATIC_DRAW); glVertexAttribPointer(0, size, GL_FLOAT, GL_FALSE, sizeof (Vertex), static_cast<Vertex *>(0)->position); glEnableVertexAttribArray(0); glVertexAttribPointer(1, size, GL_FLOAT, GL_FALSE, sizeof(Vertex), static_cast<Vertex *>(0)->normal); glEnableVertexAttribArray(1); glGenBuffers(1, &ibo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexcount * sizeof(GLuint), index, GL_STATIC_DRAW); } void bind() const { glBindVertexArray(vao); } virtual ~Object() { glDeleteBuffers(1, &vao); glDeleteBuffers(1, &vbo); glDeleteBuffers(1, &ibo); } private: //コピーコンストラクタの禁止 Object(const Object &o); //代入によるコピーの禁止 Object &operator=(const Object &o); };
#include <cstdlib> #include <iostream> #include <fstream> #include <vector> #include <memory> #include <GL/glew.h> #include <GLFW/glfw3.h> #include "Window.h" #include "Matrix.h" #include "Shape.h" #include "ShapeIndex.h" #include "SolidShapeIndex.h" #include "SolidShape.h" GLboolean printShaderInfoLog(GLuint shader, const char *str) { GLint status; glGetShaderiv(shader, GL_COMPILE_STATUS, &status); if (status == GL_FALSE) std::cerr << "Compile Error in" << str << std::endl; GLsizei bufSize; glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &bufSize); if (bufSize > 1) { std::vector<GLchar> infoLog(bufSize); GLsizei length; glGetShaderInfoLog(shader, bufSize, &length, &infoLog[0]); std::cerr << &infoLog[0] << std::endl; } return static_cast<GLboolean>(status); } // プログラムオブジェクトのリンク結果を表示する // program: プログラムオブジェクト名 GLboolean printProgramInfoLog(GLuint program) { // リンク結果を取得する GLint status; glGetProgramiv(program, GL_LINK_STATUS, &status); if (status == GL_FALSE) std::cerr << "Link Error." << std::endl; // シェーダのリンク時のログの長さを取得する GLsizei bufSize; glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufSize); if (bufSize > 1) { // シェーダのリンク時のログの内容を取得する std::vector<GLchar> infoLog(bufSize); GLsizei length; glGetProgramInfoLog(program, bufSize, &length, &infoLog[0]); std::cerr << &infoLog[0] << std::endl; } return static_cast<GLboolean>(status); } //プログラムオブジェクト作成 // vsrc: バーテックシェーダ // fsrc: フラグメントシェーダ GLuint createProgram(const char *vsrc, const char *fsrc) { const GLuint program(glCreateProgram()); if (vsrc != NULL) { const GLuint vobj(glCreateShader(GL_VERTEX_SHADER)); glShaderSource(vobj, 1, &vsrc, NULL); glCompileShader(vobj); if (printShaderInfoLog(vobj, "vertex shader")) glAttachShader(program, vobj); glDeleteShader(vobj); } if (fsrc != NULL) { const GLuint fobj(glCreateShader(GL_FRAGMENT_SHADER)); glShaderSource(fobj, 1, &fsrc, NULL); glCompileShader(fobj); if (printShaderInfoLog(fobj, "fragment shader")) glAttachShader(program, fobj); glDeleteShader(fobj); } glBindAttribLocation(program, 0, "position"); glBindAttribLocation(program, 1, "normal"); glBindFragDataLocation(program, 0, "fragment"); glLinkProgram(program); if (printProgramInfoLog(program)) return program; glDeleteProgram(program); return program; } bool readShaderSource(const char *name, std::vector<GLchar> &buffer) { if (name == NULL) return false; std::ifstream file(name, std::ios::binary); if (file.fail()) { std::cerr << "Error: Can't open source file:" << name << std::endl; return false; } file.seekg(0L, std::ios::end); GLsizei length = static_cast<GLsizei>(file.tellg()); buffer.resize(length + 1); file.seekg(0L, std::ios::beg); file.read(buffer.data(), length); buffer[length] = '\0'; if (file.fail()) { std::cerr << "Error: Could not read source file:" << name << std::endl; file.close(); return false; } } GLuint loadProgram(const char *vert, const char *frag) { // シェーダのソースファイルを読み込む std::vector<GLchar> vsrc; const bool vstat(readShaderSource(vert, vsrc)); std::vector<GLchar> fsrc; const bool fstat(readShaderSource(frag, fsrc)); // プログラムオブジェクトを作成する return vstat && fstat ? createProgram(vsrc.data(), fsrc.data()) : 0; } constexpr Object::Vertex solidCubeVertex[] = { // 左 { -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f }, { -1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 0.0f }, { -1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f }, { -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f }, { -1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f }, { -1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f }, // 裏 { 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f }, { -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f }, { -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f }, { 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f }, { -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f }, { 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f }, // 下 { -1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f }, { 1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f }, { 1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f }, { -1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f }, { 1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f }, { -1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f }, // 右 { 1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f }, { 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f }, { 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f }, { 1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f }, { 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f }, { 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f }, // 上 { -1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f }, { -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f }, { 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f }, { -1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f }, { 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f }, { 1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f }, // 前 { -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f }, { 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f }, { 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f }, { -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f }, { 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f }, { -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f } }; constexpr GLuint solidCubeIndex[] = { 0, 1, 2, 3, 4, 5, // 左 6, 7, 8, 9, 10, 11, // 裏 12, 13, 14, 15, 16, 17, // 下 18, 19, 20, 21, 22, 23, // 右 24, 25, 26, 27, 28, 29, // 上 30, 31, 32, 33, 34, 35 // 前 }; Object::Vertex tri_vertex[] = { {-1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f}, {1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f }, {0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f } }; int main () { if (glfwInit() == GL_FALSE) { std::cerr << "Can't initialize GLFW" << std::endl; return 1; } atexit(glfwTerminate); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); Window window; glClearColor(1.0f, 1.0f, 1.0f, 0.0f); glFrontFace(GL_CCW); //CCW = 反時計回り(デフォルト); CW = 時計回り glCullFace(GL_BACK); //削除 front(デフォルト) or back? glEnable(GL_CULL_FACE); //背面カリング有効 glClearDepth(1.0); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); const GLuint program(loadProgram("point.vert", "point.frag")); const GLint modelviewLoc(glGetUniformLocation(program, "modelview")); const GLint projectionLoc(glGetUniformLocation(program, "projection")); std::unique_ptr<const Shape> shape(new SolidShapeIndex(3, 36, solidCubeVertex, 36, solidCubeIndex)); glfwSetTime(0.0); while (window.shouldClose() == GL_FALSE) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(program); const GLfloat *const size(window.getSize()); const GLfloat fovy(window.getScale() * 0.01f); const GLfloat aspect(size[0] / size[1]); const Matrix projection(Matrix::perspective(fovy, aspect, 1.0f, 10.0f)); const GLfloat *const location(window.getLocation()); const Matrix r(Matrix::rotate(static_cast<GLfloat>(glfwGetTime()), 0.0f, 1.0f, 0.0f)); const Matrix model(Matrix::translate(location[0], location[1], 0.0f) * r); const Matrix view(Matrix::lookat(3.0f, 4.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f)); const Matrix modelview(view * model); glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, projection.data()); glUniformMatrix4fv(modelviewLoc, 1, GL_FALSE, modelview.data()); shape->draw(); const Matrix modelview1(modelview * Matrix::translate(0.0f, 0.0f, 3.0f)); glUniformMatrix4fv(modelviewLoc, 1, GL_FALSE, modelview1.data()); shape->draw(); window.swapBuffers(); } };
回答1件
あなたの回答
tips
プレビュー