C ++ OpenGL, GLFW Dessiner un simple cube

J’essaie donc de dessiner un cube simple dans openGL et GLFW.

Dans le code ci-dessous, je peux dessiner le cube, mais il apparaît simplement sous la forme d’un rectangle simple. Que se passe-t-il ici?

J’ai essayé “glTransformf (0,0, -10);”, mais si je fais quelque chose de moins que -2, le cube disparaîtra. à -2, la face avant apparaît. à la position par défaut de 0, je peux voir le côté arrière du cube.

de plus, lorsque j’essaie de le faire pivoter, tout ce qui apparaît est un rectangle se déplaçant du haut de la fenêtre vers le bas. Cela semble très étrange.

Quelqu’un peut-il m’aider à comprendre pourquoi le programme se comporte de cette manière?

#if defined(_WIN32) || defined(_WIN64) #include  #endif #include  #include  #include  #include  #define GLEW_STATIC #include  #include  #include  const char* gameTitle = "TEST"; GLFWwindow* window; GLfloat vertices[] = { -1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1 }; GLfloat colors[] = { 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1 }; static void controls(GLFWwindow* window, int key, int scancode, int action, int mods) { if(action == GLFW_PRESS) if(key == GLFW_KEY_ESCAPE) glfwSetWindowShouldClose(window, GL_TRUE); } bool initWindow(const int resX, const int resY) { if(!glfwInit()) { fprintf(stderr, "Failed to initialize GLFW\n"); return false; } glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing // Open a window and create its OpenGL context window = glfwCreateWindow(resX, resY, gameTitle, NULL, NULL); if(window == NULL) { fprintf(stderr, "Failed to open GLFW window.\n"); glfwTerminate(); return false; } glfwMakeContextCurrent(window); glfwSetKeyCallback(window, controls); // Get info of GPU and supported OpenGL version printf("Renderer: %s\n", glGetSsortingng(GL_RENDERER)); printf("OpenGL version supported %s\n", glGetSsortingng(GL_VERSION)); glEnable(GL_DEPTH_TEST); // Depth Testing glDepthFunc(GL_LEQUAL); glDisable(GL_CULL_FACE); glCullFace(GL_BACK); return true; } static void drawCube() { static float alpha = 0; glMasortingxMode(GL_PROJECTION_MATRIX); glLoadIdentity(); glTranslatef(0,0,-2); glMasortingxMode(GL_MODELVIEW_MATRIX); //attempt to rotate cube //glRotatef(alpha, 1, 0, 0); /* We have a color array and a vertex array */ glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glVertexPointer(3, GL_FLOAT, 0, vertices); glColorPointer(3, GL_FLOAT, 0, colors); /* Send data : 24 vertices */ glDrawArrays(GL_QUADS, 0, 24); /* Cleanup states */ glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); alpha += 0.1; } static void display() { glClearColor(0.0, 0.8, 0.3, 1.0); while(!glfwWindowShouldClose(window)) { // Draw stuff glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); drawCube(); // Update Screen //glFlush(); glfwSwapBuffers(window); // Check for any input, or window movement glfwPollEvents(); // Scale to window size GLint windowWidth, windowHeight; glfwGetWindowSize(window, &windowWidth, &windowHeight); glViewport(0, 0, windowWidth, windowHeight); } } int main(int argc, char** argv) { if(initWindow(1024, 620)) { display(); } printf("Goodbye!\n"); glfwDestroyWindow(window); glfwTerminate(); return 0; } 

  1. Les globales sont mauvaises.
  2. Vous ne définissez jamais une masortingce de projection (significative).
  3. N’abusez pas de la stack de masortingce de projection .
  4. Ne définissez pas vos masortingces dans drawCube() , principe de responsabilité unique et tout ça.
  5. Définissez votre fenêtre d’affichage avant d’ essayer de dessiner.
  6. C ++ a des versions préfixées par c ( stdio.h -> cstdio ) des en-têtes C. Utilisez ceux à la place.

Tous ensemble:

 #include  #include  #include  void controls(GLFWwindow* window, int key, int scancode, int action, int mods) { if(action == GLFW_PRESS) if(key == GLFW_KEY_ESCAPE) glfwSetWindowShouldClose(window, GL_TRUE); } GLFWwindow* initWindow(const int resX, const int resY) { if(!glfwInit()) { fprintf(stderr, "Failed to initialize GLFW\n"); return NULL; } glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing // Open a window and create its OpenGL context GLFWwindow* window = glfwCreateWindow(resX, resY, "TEST", NULL, NULL); if(window == NULL) { fprintf(stderr, "Failed to open GLFW window.\n"); glfwTerminate(); return NULL; } glfwMakeContextCurrent(window); glfwSetKeyCallback(window, controls); // Get info of GPU and supported OpenGL version printf("Renderer: %s\n", glGetSsortingng(GL_RENDERER)); printf("OpenGL version supported %s\n", glGetSsortingng(GL_VERSION)); glEnable(GL_DEPTH_TEST); // Depth Testing glDepthFunc(GL_LEQUAL); glDisable(GL_CULL_FACE); glCullFace(GL_BACK); return window; } void drawCube() { GLfloat vertices[] = { -1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1 }; GLfloat colors[] = { 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1 }; static float alpha = 0; //attempt to rotate cube glRotatef(alpha, 0, 1, 0); /* We have a color array and a vertex array */ glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glVertexPointer(3, GL_FLOAT, 0, vertices); glColorPointer(3, GL_FLOAT, 0, colors); /* Send data : 24 vertices */ glDrawArrays(GL_QUADS, 0, 24); /* Cleanup states */ glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); alpha += 1; } void display( GLFWwindow* window ) { while(!glfwWindowShouldClose(window)) { // Scale to window size GLint windowWidth, windowHeight; glfwGetWindowSize(window, &windowWidth, &windowHeight); glViewport(0, 0, windowWidth, windowHeight); // Draw stuff glClearColor(0.0, 0.8, 0.3, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMasortingxMode(GL_PROJECTION_MATRIX); glLoadIdentity(); gluPerspective( 60, (double)windowWidth / (double)windowHeight, 0.1, 100 ); glMasortingxMode(GL_MODELVIEW_MATRIX); glTranslatef(0,0,-5); drawCube(); // Update Screen glfwSwapBuffers(window); // Check for any input, or window movement glfwPollEvents(); } } int main(int argc, char** argv) { GLFWwindow* window = initWindow(1024, 620); if( NULL != window ) { display( window ); } glfwDestroyWindow(window); glfwTerminate(); return 0; } 

Je pense que votre problème est que vous utilisez essentiellement une projection orthographique, ou certainement quelque chose qui n’est pas une perspective, ce qui donnera au cube une apparence plus “3D” que je pense que vous recherchez.

Essayez quelque chose comme ceci pour définir une masortingce de projection de perspective correcte:

 glMasortingxMode(GL_PROJECTION_MATRIX); glLoadIdentity(); gluPerspective(45, windowWidth / windowHeight, 0.1f, 100.0f); // Draw calls.