float center[3] = {0, 0, 0};drPlane (center, 50, 50, 10, 10);void VMMutomo::drawPlane (float center[3], float sizePU, float sizePV, int numU, int numV){ float sRectU = sizePU/numU; float sRectV = sizePV/numV; float centSpaceU = sizePU/2; float centSpaceV = sizePV/2; float StartPLeft = center[0] - centSpaceU; float StartPTop = center[1] - centSpaceV; //center[0] -= centSpaceU; //center[1] -= centSpaceV; glBegin(GL_QUADS); for(int v = 0; v < numV; v++){ for(int u = 0; u < numU; u++){ glVertex3f(StartPLeft + sRectU*u, StartPTop + sRectV*v, center[2] + 0.0f); glVertex3f(StartPLeft + sRectU*(u+1), StartPTop + sRectV*v, center[2] + 0.0f); glVertex3f(StartPLeft + sRectU*(u+1), StartPTop + sRectV*(v+1), center[2] + 0.0f); glVertex3f(StartPLeft + sRectU*u, StartPTop + sRectV*(v+1), center[2] + 0.0f); } } glEnd();}
float offset[3] = { center[0] - sizeU / 2, center[1] - sizeV / 2, center[2] };
inline void OutVertex( float x, float y, float z, const float offset[3] ){ glVertex3f(x + offset[0], y + offset[1], z + offset[2]);}
void VMMutomo::drawPlane (const float center[3], float sizePU, float sizePV, int numU, int numV){ float sRectU = sizePU/numU; float sRectV = sizePV/numV; float startPoint[3] = { center[0] - sizePU / 2, center[1] - sizePV / 2, center[2] }; glBegin(GL_QUADS); for(int v = 0; v < numV; v++){ for(int u = 0; u < numU; u++){ glVertex3f(startPoint[0] + sRectU*u, startPoint[1] + sRectV*v, startPoint[2]); glVertex3f(startPoint[0] + sRectU*(u+1), startPoint[1] + sRectV*v, startPoint[2]); glVertex3f(startPoint[0] + sRectU*(u+1), startPoint[1] + sRectV*(v+1), startPoint[2]); glVertex3f(startPoint[0] + sRectU*u, startPoint[1] + sRectV*(v+1), startPoint[2]); } } glEnd();}
#define PI 3.141592f#define PI2 (PI * 2.0f)inline void OutVertex( float x, float y, float z, const float scale[3], const float center[3] ){ glVertex3f(x * scale[0] + center[0], y * scale]1] + center[1], z * scale[2] + center[2]);}void VMMutomo::drawPlane ( const float center[3], float sizePU, float sizePV, int numU, int numV ){ if (numU < 1) numU = 1; if (numV < 1) numV = 1; float u = -0.5f, v = -0.5f; float du = 1.0f / numU; float dv = 1.0f / numV; float scale[3] = { sizePU, sizePV, 1.0f }; glBegin(GL_QUADS); for(int j = 0; j < numV; ++j, v += dv) { for(int i = 0; i < numU; ++i, u += du) { OutVertex(u, v, 0.0f, scale, center); OutVertex(u + du, v, 0.0f, scale, center); OutVertex(u + du, v + dv, 0.0f, scale, center); OutVertex(u, v + dv, 0.0f, scale, center); } } glEnd();}void VMMutomo::drawCylinder( const float center[3], float height, float radius, int numU, int numV ){ if (numU < 1) numU = 1; if (numV < 1) numV = 1; float u = -0.5f, v = -0.5f; float du = 1.0f / numU; float dv = 1.0f / numV; float scale[3] = { radius, height, radius }; glBegin(GL_QUADS); for(int j = 0; j < numV; ++j, v += dv) { for(int i = 0; i < numU; ++i, u += du) { float angle1 = u * PI2 + PI * 1.5f; float x1 = cos(angle1); float z1 = sin(angle1); float angle2 = (u + du) * PI2 + PI * 1.5f; float x2 = cos(angle2); float z2 = sin(angle2); OutVertex(x1, v, z1, scale, center); OutVertex(x2, v, z2, scale, center); OutVertex(x2, v + dv, z2, scale, center); OutVertex(x1, v + dv, z1, scale, center); } } glEnd();}
void VMMutomo::drawPlane (const float center[3], float sizePU, float sizePV, int numU, int numV){ if (numU < 1) numU = 1; if (numV < 1) numV = 1; float sRectU = sizePU/numU; float sRectV = sizePV/numV; float startPoint[3] = { center[0] - sizePU / 2, center[1] - sizePV / 2, center[2] }; glBegin(GL_QUADS); for(int v = 0; v < numV; v++){ for(int u = 0; u < numU; u++){ glVertex3f(startPoint[0] + sRectU*u, startPoint[1] + sRectV*v, startPoint[2]); glVertex3f(startPoint[0] + sRectU*(u+1), startPoint[1] + sRectV*v, startPoint[2]); glVertex3f(startPoint[0] + sRectU*(u+1), startPoint[1] + sRectV*(v+1), startPoint[2]); glVertex3f(startPoint[0] + sRectU*u, startPoint[1] + sRectV*(v+1), startPoint[2]); } } glEnd();}void VMMutomo::drawCylinder (const float center[3], float longCyl, float radius, int numU, int numV){ if (numU < 1) numU = 1; if (numV < 1) numV = 1; float angle = 0; float x, z; float sRectV = longCyl/numU; float startPoint[3] = { center[0] - radius, center[1] - longCyl / 2, center[2] }; glBegin(GL_QUADS); for(int v = 0; v < numV; v++){ for(int u = 0; u < numU; u++){ angle = float (u) / numU * PI * 2 - PI / 2; x = center[0] + cos(angle) * radius; z = center[2] + sin(angle) * radius; float angle1 = float (u+1) / numU * PI * 2 - PI / 2; float x1 = center[0] + cos(angle) * radius; float z1 = center[2] + sin(angle) * radius; glVertex3f(x, startPoint[1] + sRectV*v, z); glVertex3f(x1, startPoint[1] + sRectV*v, z1); glVertex3f(x1, startPoint[1] + sRectV*(v+1), z1); glVertex3f(x, startPoint[1] + sRectV*(v+1), z); } } glEnd();}
angle = float (u) / numU * PI * 2 - PI / 2; // неверноangle = float (u) / (numU - 1) * PI * 2 + PI / 2; // правильно
void VMMutomo::drawCylinder (const float center[3], float longCyl, float radius, int numU, int numV){ if (numU < 1) numU = 1; if (numV < 1) numV = 1; float u1 = -0.5f, v1 = -0.5f; float du = 1.0f / numU; float dv = 1.0f / numV; float startPoint[3] = { center[0] - radius, center[1], center[2] }; glBegin(GL_QUADS); for(int v = 0; v < numV; v++, v1+=dv){ for(int u = 0; u < numU; u++, u1+=du){ float angle1 = u1 * PI2 + PI * 1.5f; float x1 = center[0] + cos(angle1) * radius; float z1 = center[2] + sin(angle1) * radius; float angle2 = (u1 + du) * PI2 + PI * 1.5f; float x2 = center[0] + cos(angle2) * radius; float z2 = center[2] + sin(angle2) * radius; glVertex3f(x1, startPoint[1] + longCyl*v1, z1); glVertex3f(x2, startPoint[1] + longCyl*v1, z2); glVertex3f(x2, startPoint[1] + longCyl*(v1+dv), z2); glVertex3f(x1, startPoint[1] + longCyl*(v1+dv), z1); } } glEnd(); glPushMatrix(); glTranslated(0.0, longCyl/2, 0.0); glRotated(90.0,1.0,0.0,0.0); gluDisk(quadratic,0.0f,radius,numU,numV); glTranslated(0.0, 0.0, longCyl); gluDisk(quadratic,0.0f,radius,numU,numV); glPopMatrix();}
void VMMutomo::drawCircle (const float center[3], float radius, int numU, int numV){ if (numU < 1) numU = 1; if (numV < 1) numV = 1; float u1 = -0.5f, v1 = -0.5f; float du = 1.0f / numU; float dv = 1.0f / numV; float startPoint[3] = { center[0] - radius, center[1], center[2] }; glBegin(GL_QUADS); for(int v = 0; v < numV; v++, v1+=dv){ for(int u = 0; u < numU; u++, u1+=du){ float angle1 = u1 * PI2 + PI * 1.5f; float x1 = center[0] + cos(angle1) * u1; float y1 = center[1] + sin(angle1) * v1; float angle2 = (u1 + du) * PI2 + PI * 1.5f; float x2 = center[0] + cos(angle2) * (u1+du); float y2 = center[1] + sin(angle2) * (v1+dv); glVertex3f(x1, y1, center[3]); glVertex3f(x2, y1, center[3]); glVertex3f(x2, y2, center[3]); glVertex3f(x1, y2, center[3]); } } glEnd();}