OpenGL初心者です。
立方体を描画したいのですが、ビルドではなんの問題もないのに実行するとwindowが透明になって後ろで開いているwindowが写ってしまいます。
OpenGL
1// Include standard headers 2#include <stdio.h> 3#include <stdlib.h> 4 5// Include GLEW 6#include <GL/glew.h> 7 8// Include GLFW 9#include <glfw3.h> 10GLFWwindow* window; 11 12// Include GLM 13#include <glm/glm.hpp> 14#include <glm/gtc/matrix_transform.hpp> 15using namespace glm; 16 17#include <common/shader.hpp> 18 19int main( void ) 20{ 21 // Initialise GLFW 22 if( !glfwInit() ) 23 { 24 fprintf( stderr, "Failed to initialize GLFW\n" ); 25 getchar(); 26 return -1; 27 } 28 29 glfwWindowHint(GLFW_SAMPLES, 4); 30 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); 31 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); 32 33 34 // Open a window and create its OpenGL context 35 window = glfwCreateWindow( 1024, 768, "Tutorial 04 - Colored Cube", NULL, NULL); 36 if( window == NULL ){ 37 fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" ); 38 getchar(); 39 glfwTerminate(); 40 return -1; 41 } 42 glfwMakeContextCurrent(window); 43 44 // Initialize GLEW 45 if (glewInit() != GLEW_OK) { 46 fprintf(stderr, "Failed to initialize GLEW\n"); 47 getchar(); 48 glfwTerminate(); 49 return -1; 50 } 51 52 // Ensure we can capture the escape key being pressed below 53 glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); 54 55 // Dark blue background 56 glClearColor(0.0f, 0.0f, 0.4f, 0.0f); 57 58 // Enable depth test 59 glEnable(GL_DEPTH_TEST); 60 // Accept fragment if it closer to the camera than the former one 61 glDepthFunc(GL_LESS); 62 63 // Create and compile our GLSL program from the shaders 64 GLuint programID = LoadShaders( "TransformVertexShader.vertexshader", "ColorFragmentShader.fragmentshader" ); 65 66 // Get a handle for our "MVP" uniform 67 GLuint MatrixID = glGetUniformLocation(programID, "MVP"); 68 69 // Get a handle for our buffers 70 GLuint vertexPosition_modelspaceID = glGetAttribLocation(programID, "vertexPosition_modelspace"); 71 GLuint vertexColorID = glGetAttribLocation(programID, "vertexColor"); 72 73 // Projection matrix : 45� Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units 74 glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f); 75 // Camera matrix 76 glm::mat4 View = glm::lookAt( 77 glm::vec3(4,3,-3), // Camera is at (4,3,-3), in World Space 78 glm::vec3(0,0,0), // and looks at the origin 79 glm::vec3(0,1,0) // Head is up (set to 0,-1,0 to look upside-down) 80 ); 81 // Model matrix : an identity matrix (model will be at the origin) 82 glm::mat4 Model = glm::mat4(1.0f); 83 // Our ModelViewProjection : multiplication of our 3 matrices 84 glm::mat4 MVP = Projection * View * Model; // Remember, matrix multiplication is the other way around 85 86 // Our vertices. Tree consecutive floats give a 3D vertex; Three consecutive vertices give a triangle. 87 // A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices 88 static const GLfloat g_vertex_buffer_data[] = { 89 -1.0f,-1.0f,-1.0f, 90 -1.0f,-1.0f, 1.0f, 91 -1.0f, 1.0f, 1.0f, 92 1.0f, 1.0f,-1.0f, 93 -1.0f,-1.0f,-1.0f, 94 -1.0f, 1.0f,-1.0f, 95 1.0f,-1.0f, 1.0f, 96 -1.0f,-1.0f,-1.0f, 97 1.0f,-1.0f,-1.0f, 98 1.0f, 1.0f,-1.0f, 99 1.0f,-1.0f,-1.0f, 100 -1.0f,-1.0f,-1.0f, 101 -1.0f,-1.0f,-1.0f, 102 -1.0f, 1.0f, 1.0f, 103 -1.0f, 1.0f,-1.0f, 104 1.0f,-1.0f, 1.0f, 105 -1.0f,-1.0f, 1.0f, 106 -1.0f,-1.0f,-1.0f, 107 -1.0f, 1.0f, 1.0f, 108 -1.0f,-1.0f, 1.0f, 109 1.0f,-1.0f, 1.0f, 110 1.0f, 1.0f, 1.0f, 111 1.0f,-1.0f,-1.0f, 112 1.0f, 1.0f,-1.0f, 113 1.0f,-1.0f,-1.0f, 114 1.0f, 1.0f, 1.0f, 115 1.0f,-1.0f, 1.0f, 116 1.0f, 1.0f, 1.0f, 117 1.0f, 1.0f,-1.0f, 118 -1.0f, 1.0f,-1.0f, 119 1.0f, 1.0f, 1.0f, 120 -1.0f, 1.0f,-1.0f, 121 -1.0f, 1.0f, 1.0f, 122 1.0f, 1.0f, 1.0f, 123 -1.0f, 1.0f, 1.0f, 124 1.0f,-1.0f, 1.0f 125 }; 126 127 // One color for each vertex. They were generated randomly. 128 static const GLfloat g_color_buffer_data[] = { 129 0.583f, 0.771f, 0.014f, 130 0.609f, 0.115f, 0.436f, 131 0.327f, 0.483f, 0.844f, 132 0.822f, 0.569f, 0.201f, 133 0.435f, 0.602f, 0.223f, 134 0.310f, 0.747f, 0.185f, 135 0.597f, 0.770f, 0.761f, 136 0.559f, 0.436f, 0.730f, 137 0.359f, 0.583f, 0.152f, 138 0.483f, 0.596f, 0.789f, 139 0.559f, 0.861f, 0.639f, 140 0.195f, 0.548f, 0.859f, 141 0.014f, 0.184f, 0.576f, 142 0.771f, 0.328f, 0.970f, 143 0.406f, 0.615f, 0.116f, 144 0.676f, 0.977f, 0.133f, 145 0.971f, 0.572f, 0.833f, 146 0.140f, 0.616f, 0.489f, 147 0.997f, 0.513f, 0.064f, 148 0.945f, 0.719f, 0.592f, 149 0.543f, 0.021f, 0.978f, 150 0.279f, 0.317f, 0.505f, 151 0.167f, 0.620f, 0.077f, 152 0.347f, 0.857f, 0.137f, 153 0.055f, 0.953f, 0.042f, 154 0.714f, 0.505f, 0.345f, 155 0.783f, 0.290f, 0.734f, 156 0.722f, 0.645f, 0.174f, 157 0.302f, 0.455f, 0.848f, 158 0.225f, 0.587f, 0.040f, 159 0.517f, 0.713f, 0.338f, 160 0.053f, 0.959f, 0.120f, 161 0.393f, 0.621f, 0.362f, 162 0.673f, 0.211f, 0.457f, 163 0.820f, 0.883f, 0.371f, 164 0.982f, 0.099f, 0.879f 165 }; 166 167 GLuint vertexbuffer; 168 glGenBuffers(1, &vertexbuffer); 169 glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); 170 glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); 171 172 GLuint colorbuffer; 173 glGenBuffers(1, &colorbuffer); 174 glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); 175 glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW); 176 177 do{ 178 179 // Clear the screen 180 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 181 182 // Use our shader 183 glUseProgram(programID); 184 185 // Send our transformation to the currently bound shader, 186 // in the "MVP" uniform 187 glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]); 188 189 // 1rst attribute buffer : vertices 190 glEnableVertexAttribArray(vertexPosition_modelspaceID); 191 glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); 192 glVertexAttribPointer( 193 vertexPosition_modelspaceID, // The attribute we want to configure 194 3, // size 195 GL_FLOAT, // type 196 GL_FALSE, // normalized? 197 0, // stride 198 (void*)0 // array buffer offset 199 ); 200 201 // 2nd attribute buffer : colors 202 glEnableVertexAttribArray(vertexColorID); 203 glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); 204 glVertexAttribPointer( 205 vertexColorID, // The attribute we want to configure 206 3, // size 207 GL_FLOAT, // type 208 GL_FALSE, // normalized? 209 0, // stride 210 (void*)0 // array buffer offset 211 ); 212 213 // Draw the triangleS ! 214 glDrawArrays(GL_TRIANGLES, 0, 12*3); // 12*3 indices starting at 0 -> 12 triangles 215 216 glDisableVertexAttribArray(vertexPosition_modelspaceID); 217 glDisableVertexAttribArray(vertexColorID); 218 219 // Swap buffers 220 glfwSwapBuffers(window); 221 glfwPollEvents(); 222 223 } // Check if the ESC key was pressed or the window was closed 224 while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && 225 glfwWindowShouldClose(window) == 0 ); 226 227 // Cleanup VBO and shader 228 glDeleteBuffers(1, &vertexbuffer); 229 glDeleteBuffers(1, &colorbuffer); 230 glDeleteProgram(programID); 231 232 // Close OpenGL window and terminate GLFW 233 glfwTerminate(); 234 235 return 0; 236} 237
コードは上のような感じです。
開発環境はQt CreaterでArch Linux上でやっています。
上のように画面キャプチャのようになってしまいます
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/01/10 05:17
2016/01/10 05:40
2016/01/10 05:43
2016/01/10 14:46