OpenGLなのですがポリゴンにテクスチャを貼り付けたいのですがなぜ上手くいかないのでしょうか?ちゃんと画像を読み込んで IDを取得して OpneGLとバインドしてると思うのですが。
提示コードは一番重要と思われる部分であり一部のコードです全文はGithubにあります。
Github :
https://github.com/Shigurechan/DrawShader_test
//初期化
bool Game::Initialization()
{
//初期化 関係
//------------------------------------------------------------------------------------------------------------------------------------------------
//glfwの初期化に失敗
if (glfwInit() != GL_TRUE)
{
printf("glfwInit()失敗\n");
int _c = getchar();
return 0;
}
//コンテキストを作成
Window = glfwCreateWindow(640, 400, "Hello", NULL, NULL);
//OpenGLコンテキストの作成に失敗
if (Window == NULL)
{
printf("glfCreateWindow()失敗\n");
int c = getchar();
return 0;
}
//コンテキストをOpenGLの描画対象にする。
glfwMakeContextCurrent(Window);
//glewを初期化する。
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
printf("glewInit() の初期化に失敗しました。");
return 0;
}
//OpenGLバージョン指定
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 0);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//垂直同期のタイミングを待つ
glfwSwapInterval(1);
//ワールド行列を生成
//------------------------------------------------------------------------------------------------------------------------------------------------
if (LoadShader("Basic.vert","Basic.frag") == true)//シェーダーを読み込む
{
//printf("シェーダーを読み込む\n");
}
//シェーダーでの画面描画配列を設定
CreateWorldMatrix();
Set_move(0, 0, 0);
Set_rotate(0, 0, 0, 0);
Set_scale(1, 1, 1);
//テクスチャを設定
glGenTextures(1, &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);
int width, height, nrChannels;
//画像読み込み
unsigned char* data = stbi_load("sample.png",&width,&height,&nrChannels,0);
if (data != nullptr)
{
printf("あああああ\n");
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
static const GLfloat texuv[] = {
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 0.0f,
};
glTexCoordPointer(2, GL_FLOAT, 0, texuv);
glBindTexture(GL_TEXTURE_2D, Texture);
glActiveTexture(GL_TEXTURE0);
glDeleteTextures(1, &Texture);
//頂点バッファを設定
glGenVertexArrays(1, &vbo);
glBindVertexArray(vbo);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 4 * 8 * sizeof(float), Vertex, GL_STATIC_DRAW);
//glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE,0, 0);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
//インデックスバッファを設定
glGenVertexArrays(1, &vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vao);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * 6, index, GL_STATIC_DRAW);
mIsRunLoop = true;
return mIsRunLoop;
}
void Game::CreateWorldMatrix()
{
float r1[16];
mat4_mul(move, rotate, r1);
mat4_mul(r1, scale, wordlMatrix);
}
//アップデート
void Game::Update()
{
if (glfwGetKey(Window, GLFW_KEY_ESCAPE) == GLFW_PRESS || glfwWindowShouldClose(Window) != GL_FALSE)
{
mIsRunLoop = false;
}
//左右上下移動
if (glfwGetKey(Window, GLFW_KEY_LEFT) == GLFW_PRESS)
{
pos.x -= 0.03f;
pos.y += 0.0f;
pos.z += 0.0f;
Set_move(pos.x, pos.y, pos.z);
// mat4_mul(move,viewPro,viewPro);
printf("LEFT\n");
}
else if (glfwGetKey(Window, GLFW_KEY_RIGHT) == GLFW_PRESS)
{
pos.x += 0.03f;
pos.y -= 0.0f;
pos.z -= 0.0f;
Set_move(pos.x, pos.y, pos.z);
printf("RIGHT\n");
}
else if (glfwGetKey(Window, GLFW_KEY_UP) == GLFW_PRESS)
{
pos.x -= 0.0f;
pos.y += 0.03f;
pos.z -= 0.0f;
Set_move(pos.x, pos.y, pos.z);
printf("UP\n");
}
else if (glfwGetKey(Window, GLFW_KEY_DOWN) == GLFW_PRESS)
{
pos.x -= 0.0f;
pos.y -= 0.03f;
pos.z -= 0.0f;
Set_move(pos.x, pos.y, pos.z);
printf("DOWN\n");
}
//Z軸移動
if (glfwGetKey(Window, GLFW_KEY_Z) == GLFW_PRESS)
{
pos.x -= 0.0f;
pos.y -= 0.0f;
pos.z -= 0.03f;
Set_move(pos.x, pos.y, pos.z);
// Set_rotate(pos.x, pos.y, pos.z,(3.14f / 180.0f));
printf("Z: %.2f\n",pos.z);
}
else if (glfwGetKey(Window, GLFW_KEY_X) == GLFW_PRESS)
{
pos.x += 0.0f;
pos.y += 0.0f;
pos.z += 0.03f;
Set_move(pos.x, pos.y, pos.z);
//Set_rotate(pos.x, pos.y, pos.z, (3.14f / 180.0f));
printf("X: %.2f\n", pos.z);
}
}
//描画アップデート
void Game::GenerateOutput()
{
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(ShaderProgram);
CreateWorldMatrix();
create_matri_mp(1.0f, -1.0f, -1.0f, 1.0f, 0.1f, 9999999.0f);
glUniformMatrix4fv(glGetUniformLocation(ShaderProgram, "world"), 1, GL_FALSE, wordlMatrix);
glUniformMatrix4fv(glGetUniformLocation(ShaderProgram, "MP"), 1, GL_FALSE, viewPro);
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
// glDrawArrays(GL_TRIANGLES,0,6);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT,0);
glViewport(0, 0, WIDTH, HEIGHT);
glfwSwapBuffers(Window);
glfwPollEvents();
}
OpenGLなのですがポリゴンにテクスチャを貼り付けたいのですがなぜ上手くいかないのでしょうか?ちゃんと画像を読み込んで IDを取得して OpneGLとバインドしてると思うのですが。
提示コードは一番重要と思われる部分であり一部のコードです全文はGithubにあります。
Github : https://github.com/Shigurechan/DrawShader_test
//初期化
bool Game::Initialization()
{
//初期化 関係
//------------------------------------------------------------------------------------------------------------------------------------------------
//glfwの初期化に失敗
if (glfwInit() != GL_TRUE)
{
printf("glfwInit()失敗\n");
int _c = getchar();
return 0;
}
//コンテキストを作成
Window = glfwCreateWindow(640, 400, "Hello", NULL, NULL);
//OpenGLコンテキストの作成に失敗
if (Window == NULL)
{
printf("glfCreateWindow()失敗\n");
int c = getchar();
return 0;
}
//コンテキストをOpenGLの描画対象にする。
glfwMakeContextCurrent(Window);
//glewを初期化する。
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
printf("glewInit() の初期化に失敗しました。");
return 0;
}
//OpenGLバージョン指定
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 0);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//垂直同期のタイミングを待つ
glfwSwapInterval(1);
//ワールド行列を生成
//------------------------------------------------------------------------------------------------------------------------------------------------
if (LoadShader("Basic.vert","Basic.frag") == true)//シェーダーを読み込む
{
//printf("シェーダーを読み込む\n");
}
//シェーダーでの画面描画配列を設定
CreateWorldMatrix();
Set_move(0, 0, 0);
Set_rotate(0, 0, 0, 0);
Set_scale(1, 1, 1);
//テクスチャを設定
glGenTextures(1, &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);
int width, height, nrChannels;
//画像読み込み
unsigned char* data = stbi_load("sample.png",&width,&height,&nrChannels,0);
if (data != nullptr)
{
printf("あああああ\n");
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
static const GLfloat texuv[] = {
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 0.0f,
};
glTexCoordPointer(2, GL_FLOAT, 0, texuv);
glBindTexture(GL_TEXTURE_2D, Texture);
glActiveTexture(GL_TEXTURE0);
glDeleteTextures(1, &Texture);
//頂点バッファを設定
glGenVertexArrays(1, &vbo);
glBindVertexArray(vbo);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 4 * 8 * sizeof(float), Vertex, GL_STATIC_DRAW);
//glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE,0, 0);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
//インデックスバッファを設定
glGenVertexArrays(1, &vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vao);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * 6, index, GL_STATIC_DRAW);
mIsRunLoop = true;
return mIsRunLoop;
}
void Game::CreateWorldMatrix()
{
float r1[16];
mat4_mul(move, rotate, r1);
mat4_mul(r1, scale, wordlMatrix);
}
//アップデート
void Game::Update()
{
if (glfwGetKey(Window, GLFW_KEY_ESCAPE) == GLFW_PRESS || glfwWindowShouldClose(Window) != GL_FALSE)
{
mIsRunLoop = false;
}
//左右上下移動
if (glfwGetKey(Window, GLFW_KEY_LEFT) == GLFW_PRESS)
{
pos.x -= 0.03f;
pos.y += 0.0f;
pos.z += 0.0f;
Set_move(pos.x, pos.y, pos.z);
// mat4_mul(move,viewPro,viewPro);
printf("LEFT\n");
}
else if (glfwGetKey(Window, GLFW_KEY_RIGHT) == GLFW_PRESS)
{
pos.x += 0.03f;
pos.y -= 0.0f;
pos.z -= 0.0f;
Set_move(pos.x, pos.y, pos.z);
printf("RIGHT\n");
}
else if (glfwGetKey(Window, GLFW_KEY_UP) == GLFW_PRESS)
{
pos.x -= 0.0f;
pos.y += 0.03f;
pos.z -= 0.0f;
Set_move(pos.x, pos.y, pos.z);
printf("UP\n");
}
else if (glfwGetKey(Window, GLFW_KEY_DOWN) == GLFW_PRESS)
{
pos.x -= 0.0f;
pos.y -= 0.03f;
pos.z -= 0.0f;
Set_move(pos.x, pos.y, pos.z);
printf("DOWN\n");
}
//Z軸移動
if (glfwGetKey(Window, GLFW_KEY_Z) == GLFW_PRESS)
{
pos.x -= 0.0f;
pos.y -= 0.0f;
pos.z -= 0.03f;
Set_move(pos.x, pos.y, pos.z);
// Set_rotate(pos.x, pos.y, pos.z,(3.14f / 180.0f));
printf("Z: %.2f\n",pos.z);
}
else if (glfwGetKey(Window, GLFW_KEY_X) == GLFW_PRESS)
{
pos.x += 0.0f;
pos.y += 0.0f;
pos.z += 0.03f;
Set_move(pos.x, pos.y, pos.z);
//Set_rotate(pos.x, pos.y, pos.z, (3.14f / 180.0f));
printf("X: %.2f\n", pos.z);
}
}
//描画アップデート
void Game::GenerateOutput()
{
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(ShaderProgram);
CreateWorldMatrix();
create_matri_mp(1.0f, -1.0f, -1.0f, 1.0f, 0.1f, 9999999.0f);
glUniformMatrix4fv(glGetUniformLocation(ShaderProgram, "world"), 1, GL_FALSE, wordlMatrix);
glUniformMatrix4fv(glGetUniformLocation(ShaderProgram, "MP"), 1, GL_FALSE, viewPro);
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
// glDrawArrays(GL_TRIANGLES,0,6);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT,0);
glViewport(0, 0, WIDTH, HEIGHT);
glfwSwapBuffers(Window);
glfwPollEvents();
}