使用しているライブラリAPI等は以下の通りです。
- MacOS
- OpenGL3.3
- C++
- GLFW
- GLEW
(opengl-tutorialなるサイトを見ながら立方体を作成するtutorial4までやっています。最後の演習にて、)
オブジェクトを2つ表示させたいです。
下記のコードを書きましたが、オブジェクトは最初に読み込んだオブジェクトのみ表示されます。
複数表示させるにはどう修正すれば良いですか?
(コード長いですけどワンパターンですし、一つのオブジェクト表示に使われているコードを二つ書いてるだけです。)
C++
1#include <stdio.h> 2#include <stdlib.h> 3#include <GL/glew.h> 4#include <GLFW/glfw3.h> 5GLFWwindow* window; 6#include <glm/glm.hpp> 7#include <glm/gtc/matrix_transform.hpp> 8using namespace glm; 9 10#include "shader.hpp" /// シェーダーコンパイル用ファイル 11 12int main( void ) 13{ 14 if( !glfwInit() ) 15 { 16 fprintf( stderr, "Failed to initialize GLFW\n" ); 17 getchar(); 18 return -1; 19 } 20 21 glfwWindowHint(GLFW_SAMPLES, 4); 22 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 23 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); 24 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 25 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 26 window = glfwCreateWindow( 1024, 768, "Tutorial 04 - Colored Cube", NULL, NULL); 27 if( window == NULL ){ 28 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" ); 29 getchar(); 30 glfwTerminate(); 31 return -1; 32 } 33 glfwMakeContextCurrent(window); 34 35 glewExperimental = true; 36 if (glewInit() != GLEW_OK) { 37 fprintf(stderr, "Failed to initialize GLEW\n"); 38 getchar(); 39 glfwTerminate(); 40 return -1; 41 } 42 43 glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); 44 45 glClearColor(0.0f, 0.0f, 0.4f, 0.0f); 46 47 glEnable(GL_DEPTH_TEST); 48 glDepthFunc(GL_LESS); 49 50 GLuint VertexArrayID; 51 glGenVertexArrays(1, &VertexArrayID); 52 glBindVertexArray(VertexArrayID); 53 54 GLuint programID = LoadShaders( "TransformVertexShader.vertexshader", "ColorFragmentShader.fragmentshader" ); 55 GLuint MatrixID = glGetUniformLocation(programID, "MVP"); 56 57 58 /// 1つ目のオブジェクト 59 60 glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 4.0f / 3.0f, 0.1f, 100.0f); 61 glm::mat4 View = glm::lookAt( 62 glm::vec3(4,3,-3), 63 glm::vec3(0,0,0), 64 glm::vec3(0,1,0) 65 ); 66 glm::mat4 Model = glm::mat4(1.0f); 67 glm::mat4 MVP = Projection * View * Model; 68 static const GLfloat g_vertex_buffer_data[] = { 69 -1.0f,-1.0f,-1.0f, 70 -1.0f,-1.0f, 1.0f, 71 -1.0f, 1.0f, 1.0f, 72 1.0f, 1.0f,-1.0f, 73 -1.0f,-1.0f,-1.0f, 74 -1.0f, 1.0f,-1.0f, 75 1.0f,-1.0f, 1.0f, 76 -1.0f,-1.0f,-1.0f, 77 1.0f,-1.0f,-1.0f, 78 1.0f, 1.0f,-1.0f, 79 1.0f,-1.0f,-1.0f, 80 -1.0f,-1.0f,-1.0f, 81 -1.0f,-1.0f,-1.0f, 82 -1.0f, 1.0f, 1.0f, 83 -1.0f, 1.0f,-1.0f, 84 1.0f,-1.0f, 1.0f, 85 -1.0f,-1.0f, 1.0f, 86 -1.0f,-1.0f,-1.0f, 87 -1.0f, 1.0f, 1.0f, 88 -1.0f,-1.0f, 1.0f, 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 }; 106 107 static const GLfloat g_color_buffer_data[] = { 108 0.583f, 0.771f, 0.014f, 109 0.609f, 0.115f, 0.436f, 110 0.327f, 0.483f, 0.844f, 111 0.822f, 0.569f, 0.201f, 112 0.435f, 0.602f, 0.223f, 113 0.310f, 0.747f, 0.185f, 114 0.597f, 0.770f, 0.761f, 115 0.559f, 0.436f, 0.730f, 116 0.359f, 0.583f, 0.152f, 117 0.483f, 0.596f, 0.789f, 118 0.559f, 0.861f, 0.639f, 119 0.195f, 0.548f, 0.859f, 120 0.014f, 0.184f, 0.576f, 121 0.771f, 0.328f, 0.970f, 122 0.406f, 0.615f, 0.116f, 123 0.676f, 0.977f, 0.133f, 124 0.971f, 0.572f, 0.833f, 125 0.140f, 0.616f, 0.489f, 126 0.997f, 0.513f, 0.064f, 127 0.945f, 0.719f, 0.592f, 128 0.543f, 0.021f, 0.978f, 129 0.279f, 0.317f, 0.505f, 130 0.167f, 0.620f, 0.077f, 131 0.347f, 0.857f, 0.137f, 132 0.055f, 0.953f, 0.042f, 133 0.714f, 0.505f, 0.345f, 134 0.783f, 0.290f, 0.734f, 135 0.722f, 0.645f, 0.174f, 136 0.302f, 0.455f, 0.848f, 137 0.225f, 0.587f, 0.040f, 138 0.517f, 0.713f, 0.338f, 139 0.053f, 0.959f, 0.120f, 140 0.393f, 0.621f, 0.362f, 141 0.673f, 0.211f, 0.457f, 142 0.820f, 0.883f, 0.371f, 143 0.982f, 0.099f, 0.879f 144 }; 145 146 GLuint vertexbuffer; 147 glGenBuffers(1, &vertexbuffer); 148 glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); 149 glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); 150 151 GLuint colorbuffer; 152 glGenBuffers(1, &colorbuffer); 153 glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); 154 glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW); 155 156 157 /// 2つ目のオブジェクト 158 159 glm::mat4 Projection2 = glm::perspective(glm::radians(45.0f), 4.0f / 3.0f, 0.1f, 100.0f); 160 glm::mat4 View2 = glm::lookAt( 161 glm::vec3(4,3,-3), 162 glm::vec3(0,0,0), 163 glm::vec3(0,1,0) 164 ); 165 glm::mat4 Model2 = glm::mat4(1.0f); 166 glm::mat4 MVP2 = Projection2 * View2 * Model2; 167 static const GLfloat g_vertex_buffer_data2[] = { 168 -2.0f,-2.0f,-2.0f, 169 -2.0f,-2.0f, 2.0f, 170 -2.0f, 2.0f, 2.0f, 171 2.0f, 2.0f,-2.0f, 172 -2.0f,-2.0f,-2.0f, 173 -2.0f, 2.0f,-2.0f, 174 2.0f,-2.0f, 2.0f, 175 -2.0f,-2.0f,-2.0f, 176 2.0f,-2.0f,-2.0f, 177 2.0f, 2.0f,-2.0f, 178 2.0f,-2.0f,-2.0f, 179 -2.0f,-2.0f,-2.0f, 180 -2.0f,-2.0f,-2.0f, 181 -2.0f, 2.0f, 2.0f, 182 -2.0f, 2.0f,-2.0f, 183 2.0f,-2.0f, 2.0f, 184 -2.0f,-2.0f, 2.0f, 185 -2.0f,-2.0f,-2.0f, 186 -2.0f, 2.0f, 2.0f, 187 -2.0f,-2.0f, 2.0f, 188 2.0f,-2.0f, 2.0f, 189 2.0f, 2.0f, 2.0f, 190 2.0f,-2.0f,-2.0f, 191 2.0f, 2.0f,-2.0f, 192 2.0f,-2.0f,-2.0f, 193 2.0f, 2.0f, 2.0f, 194 2.0f,-2.0f, 2.0f, 195 2.0f, 2.0f, 2.0f, 196 2.0f, 2.0f,-2.0f, 197 -2.0f, 2.0f,-2.0f, 198 2.0f, 2.0f, 2.0f, 199 -2.0f, 2.0f,-2.0f, 200 -2.0f, 2.0f, 2.0f, 201 2.0f, 2.0f, 2.0f, 202 -2.0f, 2.0f, 2.0f, 203 2.0f,-2.0f, 2.0f 204 }; 205 206 static const GLfloat g_color_buffer_data2[] = { 207 0.583f, 0.771f, 0.014f, 208 0.609f, 0.115f, 0.436f, 209 0.327f, 0.483f, 0.844f, 210 0.822f, 0.569f, 0.201f, 211 0.435f, 0.602f, 0.223f, 212 0.310f, 0.747f, 0.185f, 213 0.597f, 0.770f, 0.761f, 214 0.559f, 0.436f, 0.730f, 215 0.359f, 0.583f, 0.152f, 216 0.483f, 0.596f, 0.789f, 217 0.559f, 0.861f, 0.639f, 218 0.195f, 0.548f, 0.859f, 219 0.014f, 0.184f, 0.576f, 220 0.771f, 0.328f, 0.970f, 221 0.406f, 0.615f, 0.116f, 222 0.676f, 0.977f, 0.133f, 223 0.971f, 0.572f, 0.833f, 224 0.140f, 0.616f, 0.489f, 225 0.997f, 0.513f, 0.064f, 226 0.945f, 0.719f, 0.592f, 227 0.543f, 0.021f, 0.978f, 228 0.279f, 0.317f, 0.505f, 229 0.167f, 0.620f, 0.077f, 230 0.347f, 0.857f, 0.137f, 231 0.055f, 0.953f, 0.042f, 232 0.714f, 0.505f, 0.345f, 233 0.783f, 0.290f, 0.734f, 234 0.722f, 0.645f, 0.174f, 235 0.302f, 0.455f, 0.848f, 236 0.225f, 0.587f, 0.040f, 237 0.517f, 0.713f, 0.338f, 238 0.053f, 0.959f, 0.120f, 239 0.393f, 0.621f, 0.362f, 240 0.673f, 0.211f, 0.457f, 241 0.820f, 0.883f, 0.371f, 242 0.982f, 0.099f, 0.879f 243 }; 244 245 GLuint vertexbuffer2; 246 glGenBuffers(1, &vertexbuffer2); 247 glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer2); 248 glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data2), g_vertex_buffer_data2, GL_STATIC_DRAW); 249 250 GLuint colorbuffer2; 251 glGenBuffers(1, &colorbuffer2); 252 glBindBuffer(GL_ARRAY_BUFFER, colorbuffer2); 253 glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data2), g_color_buffer_data2, GL_STATIC_DRAW); 254 255 256 257 do{ 258 259 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 260 glUseProgram(programID); 261 262 263 /// 1つ目の設定 264 glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]); 265 glEnableVertexAttribArray(0); 266 glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); 267 glVertexAttribPointer( 268 0, 269 3, 270 GL_FLOAT, 271 GL_FALSE, 272 0, 273 (void*)0 274 ); 275 276 glEnableVertexAttribArray(1); 277 glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); 278 glVertexAttribPointer( 279 1, 280 3, 281 GL_FLOAT, 282 GL_FALSE, 283 0, 284 (void*)0 285 ); 286 287 glDrawArrays(GL_TRIANGLES, 0, 12*3); 288 289 glDisableVertexAttribArray(0); 290 glDisableVertexAttribArray(1); 291 292 293 /// 2つ目の設定 294 glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP2[0][0]); 295 glEnableVertexAttribArray(2); 296 glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); 297 glVertexAttribPointer( 298 2, 299 3, 300 GL_FLOAT, 301 GL_FALSE, 302 0, 303 (void*)0 304 ); 305 306 glEnableVertexAttribArray(3); 307 glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); 308 glVertexAttribPointer( 309 3, 310 3, 311 GL_FLOAT, 312 GL_FALSE, 313 0, 314 (void*)0 315 ); 316 317 glDrawArrays(GL_TRIANGLES, 0, 12*3); 318 319 glDisableVertexAttribArray(2); 320 glDisableVertexAttribArray(3); 321 322 323 324 glfwSwapBuffers(window); 325 glfwPollEvents(); 326 327 } 328 while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && 329 glfwWindowShouldClose(window) == 0 ); 330 331 /// 1つ目のデリートバッファ 332 glDeleteBuffers(1, &vertexbuffer); 333 glDeleteBuffers(1, &colorbuffer); 334 335 /// 2つ目のデリートバッファ 336 glDeleteBuffers(1, &vertexbuffer2); 337 glDeleteBuffers(1, &colorbuffer2); 338 339 glDeleteProgram(programID); 340 glDeleteVertexArrays(1, &VertexArrayID); 341 342 glfwTerminate(); 343 344 return 0; 345} 346 347
VertexShader
1#version 330 core 2 3layout(location = 0) in vec3 vertexPosition_modelspace; 4layout(location = 1) in vec3 vertexColor; 5 6layout(location = 2) in vec3 vertexPosition_modelspace; 7layout(location = 3) in vec3 vertexColor; 8 9out vec3 fragmentColor; 10 11out vec3 fragmentColor2; 12 13uniform mat4 MVP; 14 15uniform mat4 MVP2; 16 17void main(){ 18 19 gl_Position = MVP * vec4(vertexPosition_modelspace,1); 20 gl_Position = MVP2 * vec4(vertexPosition_modelspace2,1); 21 22 fragmentColor = vertexColor; 23 fragmentColor2 = vertexColor2; 24 25} 26 27
FragmentShader
1#version 330 core 2 3in vec3 fragmentColor; 4 5in vec3 fragmentColor2; 6 7out vec3 color; 8 9out vec3 color2; 10 11void main(){ 12 13 color = fragmentColor; 14 15 color2 = fragmentColor2; 16 17} 18
回答1件
あなたの回答
tips
プレビュー