Reputation: 3
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.
Upvotes: 0
Views: 932
Reputation: 51903
Camera
I do not see you are setting the matrices anywhere... So my bet is that your matrices are unit resulting in 2D view no matter how you tweak the rendered axis aligned data.
Use gluPerspective
for the GL_PROJECTION
. Its a bit tricky to set up the perspective 3D view properly for rookies (as it views in -Z
direction which is exact opposite than identity matrix) so your mesh is in viewable area.
CULL_FACE
for starters try glDisable(GL_CULL_FACE)
as your data will most likely contain winding errors and you will not know what faces you already have or not otherwise.
cube mesh
right now you are hardcoding it using glVertex
calls its much easier to use separate arrays for points and faces (QUADS or TRIANGLES). Also as BDL pointed out you are using forbidden commands inside glBegin/glEnd
calls they are ignored and slows things down due to error handling.
lighting
to feel the 3D better you should also add normals for each face and enable lights. Otherwise you would need to color each face with different color to see the "3D".
Take a look at my:
just ignore the advanced stuff (GLSL,VAO/VBO...) and look for view settings and the cube mesh data. You just need to render it with for loop instead of VAO/VBO. Here example:
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
#ifndef vao_indices
glBegin(GL_QUADS);
for (int i=0;i<6*4*3;i+=3)
{
glNormal3fv(vao_nor+i);
glColor3fv (vao_col+i);
glVertex3fv(vao_pos+i);
}
glEnd();
#else
int i,j,k;
const GLfloat vao_nor[]=
{
// nx ny nz
0.0, 0.0,-1.0,
0.0, 0.0,+1.0,
0.0,-1.0, 0.0,
+1.0, 0.0, 0.0,
0.0,+1.0, 0.0,
-1.0, 0.0, 0.0,
};
glBegin(GL_QUADS);
for (j=0;j<6*4;j++)
{
i=vao_ix[j]; i+=i+i;
k=j>>2; k+=k+k;
glNormal3fv(vao_nor+k);
glColor3fv (vao_col+i);
glVertex3fv(vao_pos+i);
}
glEnd();
#endif
glDisable(GL_COLOR_MATERIAL);
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
using the mesh from gl_simple
in that linked QA in both modes (indices and direct).
Upvotes: 0
Reputation: 22175
You are drawing a single polygon with 24 vertices. Since OpenGL requires the polygon to be concave and planar (which your data isn't) it draws some random stuff.
If you want to draw a cube, you either have to split the drawing the call glBegin(GL_POLYGON)
and glEnd()
for each face separately (as the tutorial shows in the second code sample in section 4), or you can change the primitive mode to GL_QUADS
.
Note, that the OpenGL version you are using (fixed function pipeline) is outdated for more than 10 years now. You might want to consider switching to (at least) OpenGL 3.3 core profile and use shader.
Upvotes: 4