I am starting to use tinker with OpenGL and I want to draw a cube. I went to a tutorial, followed that, and only got this weird square.
Here's my code.
#include <GLFW/glfw3.h>
void drawCube() {
float rotate_x{ 193 };
float rotate_y{ 112 };
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0); glVertex3f(0.5, -0.5, -0.5);
glColor3f(0.0, 1.0, 0.0); glVertex3f(0.5, 0.5, -0.5);
glColor3f(0.0, 0.0, 1.0); glVertex3f(-0.5, 0.5, -0.5);
glColor3f(1.0, 0.0, 1.0); glVertex3f(-0.5, -0.5, -0.5);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(0.5, -0.5, 0.5);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, -0.5, -0.5);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, 0.5);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(-0.5, -0.5, -0.5);
glLoadIdentity();
glRotatef(30, 0.0, 1.0, 0.0);
glEnd();
}
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Triangle", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
drawCube();
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
I'm using GLFW to do this, and I'm building it in Visual Studio 2017 Community Edition. I also tried to rotate it at the end of drawCube(). The tutorial I followed was https://www.wikihow.com/Make-a-Cube-in-OpenGL but I tweaked it a bit to use glfw.