OpenGLでテクスチャを表示しようとしたのですが、どうやっても出来ません。
原因は全く分かりません。分かった事はglGenTexture関数の後、glIsTextureを
呼び出すとGL_FALSEで返ってくるという事だけです。
開発環境はVisual Studio 2015、OSはWindwos 10 64 bit、メモリは4GB、CPUはCore i5 2.9GHz、GPUはインテルHDグラフィックス、コンパイル環境はDebugのX64、GLFWを用いています。コードは以下のものになります。
C++
1 2#include <GL/glfw.h> 3#include <cstdlib> 4#include <cmath> 5#include <fstream> 6#include <vector> 7#include <cassert> 8 9const int g_windowWidth = 640; 10const int g_windowHeight = 480; 11GLuint g_texID; 12 13void render() 14{ 15 static const GLfloat vtx[] = { 16 200, 120, 17 440, 120, 18 440, 360, 19 200, 360, 20 }; 21 glVertexPointer(2, GL_FLOAT, 0, vtx); 22 23 // Step5. テクスチャの領域指定 24 static const GLfloat texuv[] = { 25 0.0f, 1.0f, 26 1.0f, 1.0f, 27 1.0f, 0.0f, 28 0.0f, 0.0f, 29 }; 30 glTexCoordPointer(2, GL_FLOAT, 0, texuv); 31 32 // Step6. テクスチャの画像指定 33 glBindTexture(GL_TEXTURE_2D, g_texID); 34 35 // Step7. テクスチャの描画 36 glEnable(GL_TEXTURE_2D); 37 glEnableClientState(GL_VERTEX_ARRAY); 38 glEnableClientState(GL_TEXTURE_COORD_ARRAY); 39 glDrawArrays(GL_QUADS, 0, 4); 40 glDisableClientState(GL_TEXTURE_COORD_ARRAY); 41 glDisableClientState(GL_VERTEX_ARRAY); 42 glDisable(GL_TEXTURE_2D); 43} 44 45void setupTexture(GLuint texID, const char *file, const int width, const int height) 46{ 47 // Step2. 画像データのロード 48 std::ifstream fstr(file, std::ios::binary); 49 assert(fstr); 50 51 const size_t fileSize = static_cast<size_t>(fstr.seekg(0, fstr.end).tellg()); 52 fstr.seekg(0, fstr.beg); 53 std::vector<char> textureBuffer(fileSize); 54 fstr.read(&textureBuffer[0], fileSize); 55 56 // Step3. 画像データとテクスチャiDを結びつける 57 glBindTexture(GL_TEXTURE_2D, texID); 58 // Step4. テクスチャの各種設定 59 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 60 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 61 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 62 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 63 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &textureBuffer[0]); 64 65 66} 67 68 69int main() 70{ 71 if (!glfwInit()) { 72 return -1; 73 } 74 GLFWwindow* window = glfwCreateWindow(g_windowWidth, g_windowHeight, "SAMPLE", NULL, NULL); 75 if (window == nullptr) { 76 return -1; 77 } 78 79 // モニタとの同期 80 glfwSwapInterval(1); 81 82 // 描画範囲の指定 83 glMatrixMode(GL_PROJECTION); 84 glLoadIdentity(); 85 glOrtho(0.0f, g_windowWidth, 0.0f, g_windowHeight, -1.0f, 1.0f); 86 87 // Step1. テクスチャのロード 88 glGenTextures(1, &g_texID); 89 setupTexture(g_texID, "test.raw", 16, 16); 90 91 92 glfwMakeContextCurrent(window); 93 // ゲームループ 94 while (!glfwWindowShouldClose(window)) { 95 96 // 画面の初期化 97 glClearColor(1.0f, 0.5f, 0.5f, 0.0f); 98 glClear(GL_COLOR_BUFFER_BIT); 99 render(); 100 101 Sleep(32); 102 glFlush(); 103 // バッファの入れ替え 104 glfwSwapBuffers(window); 105 glfwWaitEvents(); 106 } 107 108 glfwTerminate(); 109 110 return 0; 111}
ソースコードは下記のURLを参考にして、おかしな部分を書き直した物です。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/07 22:12