void display(void) { // Buffer clearen glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // View Matrix erstellen glLoadIdentity(); float x = distance * sin(theta) * cos(phi); float y = distance * cos(theta); float z = distance * sin(theta) * sin(phi); gluLookAt(x, y, z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); // Teekanne rendern. glutSolidTeapot(1); // Den Matrix-Stack sichern. glPushMatrix(); // Zeichnen der Kugelkreisbahn. drawCircle(10.0f, 50); // Zeichnen der Kugel. // Wenden Sie eine Translation und eine Rotation an, bevor sie die Kugel zeichnen. Sie können die Variable 'angle' für die Rotation verwenden. // Bedenken Sie dabei die richtige Reihenfolge der beiden Transformationen. glRotated(angle, 0, 1.0f, 0); glTranslatef(10.0f, 0, 0); glutSolidSphere(1.0f, 32, 32); // Zeichnen der Würfelkreisbahn. // Hinweis: Der Ursprung des Koordinatensystems befindet sich nun im Zentrum des Würfels. // Drehen Sie das Koordinatensystem um 90° entlang der Achse, die für die Verschiebung des Würfels genutzt wurde. // Danach steht die Würfelkreisbahn senkrecht zur Tangentialrichtung der Kugelkreisbahn. glRotated(90.0f, 1.0f, 0, 0); drawCircle(5.0f, 50); // Zeichnen des Würfels. // Wenden Sie die entsprechende Translation und Rotation an, bevor sie den Würfel zeichnen. glRotated(angle, 0, 1.0f, 0); glTranslatef(5.0f, 0, 0); glutSolidCube(1.0f); // Zeichnen einer Linie von Würfel zu Kegel. glDisable(GL_LIGHTING); glTranslatef(3.0f, 0, 0); glBegin(GL_LINE_STRIP); glVertex3f(0, 0, 0); glVertex3f(-3.0f, 0, 0); glEnd(); glEnable(GL_LIGHTING); // Drehung anwenden, sodass Koordinatensystem in Richtung Ursprung orientiert ist. (Hinweis: Implementieren Sie dies zuletzt.) GLfloat height = 8*cos(toRad(angle-90)); GLfloat d = 8*sin(toRad(angle-90)); GLfloat e = 10 - d; GLfloat l = pow(pow(height,2) + pow(e,2), 0.5f); GLfloat alpha = toDeg(acos(e/l)) - 90; if(static_cast<int>(angle)%360 > 180) alpha = -alpha; glRotated(alpha, 0, 1.0f, 0); if(static_cast<int>(angle)%360 > 180) { glRotated(180 - angle, 0, 1.0f, 0); std::cout << alpha + 180 - (int)angle%360 << std::endl; } else { glRotated(-angle, 0, 1.0f, 0); std::cout << alpha - (int)angle%360 << std::endl; } // Zeichnen der Linie von Kegel zu Urpsrung. glDisable(GL_LIGHTING); glBegin(GL_LINE_STRIP); glVertex3f(0, 0, 0); glVertex3f(0, 0, l); glEnd(); glEnable(GL_LIGHTING); // Zeichnen des Kegels. glutSolidCone(0.5f, 1.0f, 32, 4); // Den Matrix-Stack wiederherstellen. glPopMatrix(); glutSwapBuffers(); angle += 5.0f / 60.0f; }
void example_3d_primitives::render_cube() { // Use predefined methods here glutSolidCube(2.0); }
void Cube::draw(DrawData& data) { //float halfSize = size/2.0; //Apply the material properties //From here forward anything drawn will be drawn with this material //material.apply(); //Set the OpenGL Matrix mode to ModelView (used when drawing geometry) glMatrixMode(GL_MODELVIEW); //Push a save state onto the matrix stack, and multiply in the toWorld matrix glPushMatrix(); glMultMatrixf(toWorld.ptr()); //Make cube! //Note: The glBegin, and glEnd should always be as close to the glNormal/glVertex calls as possible //These are special calls that 'freeze' many internal states of OpenGL. //Once the glBegin state is active many of the calls made to OpenGL (like glMultMatrixf) will be IGNORED! //As a good habit, only call glBegin just before you need to draw, and call end just after you finish /* glBegin(GL_QUADS); // Draw front face: glNormal3f(0.0, 0.0, 1.0); glVertex3f(-halfSize, halfSize, halfSize); glVertex3f( halfSize, halfSize, halfSize); glVertex3f( halfSize, -halfSize, halfSize); glVertex3f(-halfSize, -halfSize, halfSize); // Draw left side: glNormal3f(-1.0, 0.0, 0.0); glVertex3f(-halfSize, halfSize, halfSize); glVertex3f(-halfSize, halfSize, -halfSize); glVertex3f(-halfSize, -halfSize, -halfSize); glVertex3f(-halfSize, -halfSize, halfSize); // Draw right side: glNormal3f(1.0, 0.0, 0.0); glVertex3f( halfSize, halfSize, halfSize); glVertex3f( halfSize, halfSize, -halfSize); glVertex3f( halfSize, -halfSize, -halfSize); glVertex3f( halfSize, -halfSize, halfSize); // Draw back face: glNormal3f(0.0, 0.0, -1.0); glVertex3f(-halfSize, halfSize, -halfSize); glVertex3f( halfSize, halfSize, -halfSize); glVertex3f( halfSize, -halfSize, -halfSize); glVertex3f(-halfSize, -halfSize, -halfSize); // Draw top side: glNormal3f(0.0, 1.0, 0.0); glVertex3f(-halfSize, halfSize, halfSize); glVertex3f( halfSize, halfSize, halfSize); glVertex3f( halfSize, halfSize, -halfSize); glVertex3f(-halfSize, halfSize, -halfSize); // Draw bottom side: glNormal3f(0.0, -1.0, 0.0); glVertex3f(-halfSize, -halfSize, -halfSize); glVertex3f( halfSize, -halfSize, -halfSize); glVertex3f( halfSize, -halfSize, halfSize); glVertex3f(-halfSize, -halfSize, halfSize); glEnd(); */ //The above glBegin, glEnd, glNormal and glVertex calls can be replaced with a glut convenience function glutSolidCube(size); //Pop the save state off the matrix stack //This will undo the multiply we did earlier glPopMatrix(); }
int DrawGLScene(GLvoid) { int i, j; GLUquadricObj *quadObj; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); cleanObject(); for (i = 0; i < VERTICAL_LINE + 2; i++) { for (j = 0; j < HORIZONTAL_LINE + 2; j++) { if (background[i][j] != 0) { cleanObject(); glTranslatef((j*-1) + 5.5, (i*-1) + 1, 20); glScalef(1, 1, 1); glColor4f(0.16, 0.16, 0.16, 1.0f); glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); glEnable(GL_COLOR_MATERIAL); glutSolidCube(1); } } } for (i = 0; i < HORIZONTAL_LINE; i++) { for (j = 0; j < VERTICAL_LINE; j++) { if (g_iStok[i][j] > 30) { drawBlockStationary((i*-1) + 4.5, (j*-1), 1.0, 3.0, 0.2); } } } for (int i = 0; i < MAX_MEN; i++) { if (g_man[i] != NULL) { cleanObject(); glPushMatrix(); glTranslated(((float)g_man[i]->x / SIZE_RECT)*-1 + 4.5, -24.0, 20.0); glRotated(120, 1, 0, 0); glRotated(180, 0, 1, 0); quadObj = gluNewQuadric(); glBindTexture(GL_TEXTURE_2D, texture[0]); gluQuadricTexture(quadObj, GL_TRUE); gluQuadricDrawStyle(quadObj, GLU_FILL); glColor3d(0.3, 0.3, 0.3); glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); glEnable(GL_COLOR_MATERIAL); gluSphere(quadObj, 0.45, 50, 50); glPopMatrix(); gluDeleteQuadric(quadObj); auxSwapBuffers(); } } return 0; }
static void createRobot() { // Create body glPushMatrix(); glTranslatef(0, 0, 0); glColor3f(red, green, blue); glScalef(3, 3, 2); glutSolidCube(1); glPopMatrix(); // Create head glPushMatrix(); glTranslatef(0, 2, 0); glColor3f(red, green, blue); glScalef(1.5, 1, 1); glutSolidCube(1); glPopMatrix(); // Create right leg glPushMatrix(); glTranslatef(-1, -2.5, 0); glRotatef(rightRun, 1.0f, 0, 0); glColor3f(red, green, blue); glScalef(1, 2.5, 1); glutSolidCube(1); glPopMatrix(); // Create left leg glPushMatrix(); glTranslatef(1, -2.5, 0); glRotatef(leftRun, 1.0f, 0, 0); glColor3f(red, green, blue); glScalef(1, 2.5, 1); glutSolidCube(1); glPopMatrix(); // Right arm glPushMatrix(); glTranslatef(-armPos, 1, 0); //glRotatef(leftRun, 2.0f, 0, 0); glRotatef(angle, 0, 0, 1.0f); glColor3f(red, green, blue); glScalef(0.3, 1.8, 0.5); glutSolidCube(1); glPopMatrix(); // Left arm glPushMatrix(); glTranslatef(armPos, 1, 0); //glRotatef(rightRun, 1.0f, 0, 0); glRotatef(-angle, 0, 0, 1.0f); glColor3f(red, green, blue); glScalef(0.3, 1.8, 0.5); glutSolidCube(1); glPopMatrix(); // Right eye glPushMatrix(); glTranslatef(-0.5, 2, 0.5); glColor3f(eyeRed, eyeGreen, eyeBlue); glScalef(0.1, 0.1, 0.1); glutSolidCube(1); glPopMatrix(); // Left eye glPushMatrix(); glTranslatef(0.5, 2, 0.5); glColor3f(eyeRed, eyeGreen, eyeBlue); glScalef(0.1, 0.1, 0.1); glutSolidCube(1); glPopMatrix(); }