I am really really new to opengl, and I am learning basics now. I have a high level question. If I want to create an object, say a column which are all the options?
My questions are:
A) Is there any other way of doing it?
B) Which is the correct way of creating normal objects/ animated objects in opengl if you do NOT use a 3DStudio mesh?
C) Is this code correct to create a column in 2)? (In case it is, I will try to parametrize it using x,y,z variables to have a column for a given floor tile with a given height)
//Column
//Wall
glBegin(GL_POLYGON);
glVertex3f(150.0f, 250.0f,50); // x1, y1 - top-left corner
glVertex3f(50.0f, 250.0f,50); // x2, y1 - top-right corner
glVertex3f(50.0f, 50.0f,50); // x2, y2 - bottom-right corner
glVertex3f(150.0f, 50.0f,50); // x1, y2 - bottom-left corner
glEnd( );
//Wall
glBegin(GL_POLYGON);
glVertex3f(50.0f, 250.0f,0); // x1, y1 - top-left corner
glVertex3f(150.0f, 250.0f,0); // x2, y1 - top-right corner
glVertex3f(150.0f, 50.0f,0); // x2, y2 - bottom-right corner
glVertex3f(50.0f, 50.0f,0); // x1, y2 - bottom-left corner
glEnd( );
//Wall
glBegin(GL_POLYGON);
glVertex3f(150.0f, 250.0f,0); // x1, y1 - top-left corner
glVertex3f(150.0f, 250.0f,50.0f); // x2, y1 - top-right corner
glVertex3f(150.0f, 50.0f,50.0f); // x2, y2 - bottom-right corner
glVertex3f(150.0f, 50.0f,0); // x1, y2 - bottom-left corner
glEnd( );
//Wall
glBegin(GL_POLYGON);
glVertex3f(50.0f, 250.0f,50); // x1, y1 - top-left corner
glVertex3f(50.0f, 250.0f,0); // x2, y1 - top-right corner
glVertex3f(50.0f, 50.0f,0); // x2, y2 - bottom-right corner
glVertex3f(50.0f, 50.0f,50); // x1, y2 - bottom-left corner
glEnd( );
//Floor
glBegin(GL_POLYGON);
glVertex3f(50.0f, 50.0f, 0); // x1, y1 - top-left corner
glVertex3f(50.0f, 50.0f, 50); // x2, y1 - top-right corner
glVertex3f(0, 50.0f, 50); // x2, y2 - bottom-right corner
glVertex3f(0, 50.0f, 0); // x1, y2 - bottom-left corner
glEnd( );
//Ceiling
glBegin(GL_POLYGON);
glVertex3f(50.0f, 250.0f, 0); // x1, y1 - top-left corner
glVertex3f(50.0f, 250.0f, 50); // x2, y1 - top-right corner
glVertex3f(0, 250.0f, 50); // x2, y2 - bottom-right corner
glVertex3f(0, 250.0f, 0); // x1, y2 - bottom-left corner
glEnd( );
It depends on your definition of correct. To answer one by one:
Re: glVertex3f and glBegin/glEnd calls. They're fine, at present, when you're learning, as a simple means to let you investigate other parts of the rendering pipeline. However, they're deprecated and suboptimal so aren't something you'd want to become permanently bound to. Because of this, they aren't replicated in OpenGL ES or WebGL.
There is a cost associated with any communication between the CPU and GPU. The more data you transfer in the communication, the greater the cost. However, even small calls cost quite a lot due to the temporary synchronisation required between the two asynchronous and parallel devices.
glBegin/glVertex3f/glEnd are therefore bad because all data sits on the CPU and is transferred to the driver piecemeal, through lots of calls. As a first step, you probably want to switch to using glVertexPointer (to supply all vertex data in one step) and either glDrawArrays or glDrawElements (to perform the equivalent of all glBegin/glEnd calls in one step). From there you can move to storing the data on the GPU rather than the CPU via vertex array objects.