0
球の作成の後ろの数学について学んでいるIm。私はちょうど開始時に私はいくつかの研究を行い、私はポイントのグリッドを作成し、球を作成するために数式を使用する必要があると言う。私の質問は、キューブ上の四角を含む別の方法ですか?そうでない場合は、私のプロジェクトを傷つけ、キューブの面の四角形ではなく、点グリッドを持つ立方体の作成を開始します。キューブの球へのモーフィング
#include <GL/glut.h>
#include <iostream>
void DrawArea(){
glColor3f(1,1, 1);
glBegin(GL_QUADS);
glVertex3f(-10, -1.1, 10);
glVertex3f(10, -1.1, 10);
glVertex3f(10, -1.1, -10);
glVertex3f(-10, -1.1, -10);
glEnd();
}
GLfloat n[6][3] = { /* Normals for the 6 faces of a cube. */
{-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, { 1.0, 0.0, 0.0},
{0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0} };
GLint faces[6][4] = { /* Vertex indices for the 6 faces of a cube. */
{0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
{4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} };
GLfloat v[8][3]; /* Will be filled in with X,Y,Z vertexes. */
void init(void)
{
/* Setup cube vertex data. */
v[0][0] = v[1][0] = v[2][0] = v[3][0] = -1;
v[4][0] = v[5][0] = v[6][0] = v[7][0] = 1;
v[0][1] = v[1][1] = v[4][1] = v[5][1] = -1;
v[2][1] = v[3][1] = v[6][1] = v[7][1] = 1;
v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1;
v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1;
/* Use depth buffering for hidden surface elimination. */
glEnable(GL_DEPTH_TEST);
/* Setup the view of the cube. */
glMatrixMode(GL_PROJECTION);
gluPerspective(/* field of view in degree */ 70.0,
/* aspect ratio */ 1.0,
/* Z near */ 1.0, /* Z far */ 500.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0, 0.0, 10, /* eye is at (0,0,5) */
0.0, 0.0, 0.0, /* center is at (0,0,0) */
0.0, 1.0, 0.); /* up is in positive Y direction */
/* Adjust cube position to be asthetic angle. */
//glTranslatef(0.0, 0.0, -1.0);
}
void drawLines() {
glBegin(GL_LINES);
int y = 0;
for (int i = 10; i != 0; i--) {
// po x osi
glVertex2f(-i, -0.1);
glVertex2f(-i, 0.1);
glVertex2f(i, -0.1);
glVertex2f(i, 0.1);
//po y osi
glVertex2f(-0.1, -i);
glVertex2f(0.1, -i);
glVertex2f(-0.1, i);
glVertex2f(0.1, i);
//
// //po z osi
// // glColor3f(0.0f, 1.0f, 0.0f); //seems it doesnt register
glVertex3f(0, -0.3, -i);
glVertex3f(0, 0.3, -i);
glVertex3f(0, -0.3, i);
glVertex3f(0, 0.3, i);
}
glEnd();
}
void drawGrid(){
for(int a = 0; a<=10; a++){
glBegin(GL_LINES);
//po x-u
glVertex3f(-a, 0, 10);
glVertex3f(-a, 0, -10);
glVertex3f(a, 0, 10);
glVertex3f(a, 0, -10);
//po z
glVertex3f(-10, 0, -a);
glVertex3f(10, 0, -a);
glVertex3f(10, 0, a);
glVertex3f(-10, 0, a);
glEnd();
}
}
void drawCP() {
drawLines();
glBegin(GL_LINES);
glVertex2f(-10.0f, 0);
glVertex2f(10.0f, 0);
glVertex3f(0.0f, 0.0f, -10);
glVertex2f(0,0);
glEnd();
}
GLfloat color[8][3] =
{
{0.0,1.0,1.0},
{1.0,0.0,0.0},
{1.0,1.0,0.0},
{0.0,1.0,0.0},
{0.0,0.0,1.0},
{1.0,0.0,1.0},
{1.0,1.0,1.0},
{0.0,1.0,1.0},
};
void drawBox()
{
int i;
for (i = 0; i < 6; i++) {
glBegin(GL_QUADS);
glNormal3fv(&n[i][0]);
glColor3fv(color[i]);
glVertex3fv(&v[faces[i][0]][0]);
glVertex3fv(&v[faces[i][1]][0]);
glVertex3fv(&v[faces[i][2]][0]);
glVertex3fv(&v[faces[i][3]][0]);
glEnd();
}
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawBox();
glColor3f(0.0,0.0,0.0);
drawGrid();
glColor3f(1.0,1.0,1.0);
drawCP();
glPopMatrix();
glutSwapBuffers();
}
void timer (int mili){
glutPostRedisplay();
glutTimerFunc(mili, timer, mili);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 800);
glEnable(GL_COLOR_MATERIAL);
glutCreateWindow("red 3D lighted cube");
init();
glutDisplayFunc(display);
glClearColor(0.6, 0.6, 0.6, 1);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
球の(近似した)面を描くには、いくつかの方法があります。私は、常にクワッドではなく三角形を使用することを覚えています。 – Elemental