static void draw_scene(GLFWwindow* window, double t) { double xpos, ypos, zpos, angle_x, angle_y, angle_z; static double t_old = 0.0; float dt; mat4x4 projection; // Calculate frame-to-frame delta time dt = (float) (t - t_old); t_old = t; mat4x4_perspective(projection, 65.f * (float) M_PI / 180.f, aspect_ratio, 1.0, 60.0); glClearColor(0.1f, 0.1f, 0.1f, 1.f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadMatrixf((const GLfloat*) projection); // Setup camera glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // Rotate camera angle_x = 90.0 - 10.0; angle_y = 10.0 * sin(0.3 * t); angle_z = 10.0 * t; glRotated(-angle_x, 1.0, 0.0, 0.0); glRotated(-angle_y, 0.0, 1.0, 0.0); glRotated(-angle_z, 0.0, 0.0, 1.0); // Translate camera xpos = 15.0 * sin((M_PI / 180.0) * angle_z) + 2.0 * sin((M_PI / 180.0) * 3.1 * t); ypos = -15.0 * cos((M_PI / 180.0) * angle_z) + 2.0 * cos((M_PI / 180.0) * 2.9 * t); zpos = 4.0 + 2.0 * cos((M_PI / 180.0) * 4.9 * t); glTranslated(-xpos, -ypos, -zpos); glFrontFace(GL_CCW); glCullFace(GL_BACK); glEnable(GL_CULL_FACE); setup_lights(); glEnable(GL_LIGHTING); glEnable(GL_FOG); glFogi(GL_FOG_MODE, GL_EXP); glFogf(GL_FOG_DENSITY, 0.05f); glFogfv(GL_FOG_COLOR, fog_color); draw_floor(); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glDepthMask(GL_TRUE); draw_fountain(); glDisable(GL_LIGHTING); glDisable(GL_FOG); // Particles must be drawn after all solid objects have been drawn draw_particles(window, t, dt); // Z-buffer not needed anymore glDisable(GL_DEPTH_TEST); }
void createSlideParts() { slideTopWidth = slideWidth + 4*slideBarrierRad; slideCurveVertF = 2 * slideLegLongh - 2 * slideLegShorth - slideIncVertF; handleFrontLength = (slideTopWidth - slideWidth-handleRadius)/2; slidePlatform = glGenLists(1); glNewList(slidePlatform, GL_COMPILE); glPushMatrix(); glScaled(slideTopWidth, slideTopThick, slideTopLength); glCallList(cube); glPopMatrix(); glEndList(); slideLegLong = glGenLists(1); glNewList(slideLegLong, GL_COMPILE); glPushMatrix(); glRotated(90,1,0,0); glScaled(slideLegRadius, slideLegRadius, slideLegLongh); glCallList(cylinder); glPopMatrix(); glEndList(); slideLegShort = glGenLists(1); glNewList(slideLegShort, GL_COMPILE); glPushMatrix(); glRotated(90,1,0,0); glScaled(slideLegRadius, slideLegRadius, slideLegShorth); glCallList(cylinder); glPopMatrix(); glEndList(); slideStepSide = glGenLists(1); glNewList(slideStepSide,GL_COMPILE); glPushMatrix(); glRotated(stepAngle,1,0,0); glTranslated(0,-stepSideHeight + slideTopThick,stepLength); glScaled(stepSideThick, stepSideHeight, stepLength); glCallList(cube); glPopMatrix(); glEndList(); slideStep = glGenLists(1); glNewList(slideStep, GL_COMPILE); glPushMatrix(); glScaled(slideWidth, stepThick, stepLength); glCallList(cube); glPopMatrix(); glEndList(); slideHandleStep = glGenLists(1); glNewList(slideHandleStep, GL_COMPILE); glPushMatrix(); glRotated(stepAngle,1,0,0); glRotated(-90,1,0,0); glTranslated(0,0,0-stepSideHeight); glScaled(handleRadius, handleRadius, stepSideHeight); glCallList(cylinder); glPopMatrix(); glEndList(); slideHandleTopSide = glGenLists(1); glNewList(slideHandleTopSide, GL_COMPILE); glPushMatrix(); glScaled(handleRadius, handleRadius, slideTopLength - handleRadius); glCallList(cylinder); glPopMatrix(); glEndList(); slideHandleSupport = glGenLists(1); glNewList(slideHandleSupport, GL_COMPILE); glPushMatrix(); glRotated(90,1,0,0); glScaled(handleRadius, handleRadius, handleHeight); glCallList(cylinder); glPopMatrix(); glEndList(); slideHandleTopFront = glGenLists(1); glNewList(slideHandleTopFront, GL_COMPILE); glPushMatrix(); glRotated(90,0,1,0); glScaled(handleRadius, handleRadius, slideTopWidth - handleRadius); glCallList(cylinder); glPopMatrix(); glEndList(); slideHandleBack = glGenLists(1); glNewList(slideHandleBack, GL_COMPILE); glPushMatrix(); glRotated(90,0,1,0); glScaled(handleRadius, handleRadius, handleFrontLength); glCallList(cylinder); glPopMatrix(); glEndList(); double angle = stepAngle * PI / 180; double dist = 2*handleHeight * sin(angle)-.07; unsigned int curveSmooth = 20; slideHandleBottom = glGenLists(1); glNewList(slideHandleBottom, GL_COMPILE); glPushMatrix(); glRotated(stepAngle,1,0,0); glTranslated(0,0,dist); glScaled(handleRadius, handleRadius, dist); glCallList(cylinder); glPopMatrix(); glEndList(); double phiD, phi; if(curveSmooth != 0) { phiD = (slideAngle / (curveSmooth)); phi = phiD * PI / 180; } else { phiD = slideAngle; phi = slideAngle * PI / 180; } double slideAngleR = slideAngle * PI / 180; slideIncLength = slideIncVertF / sin(slideAngleR) / 2; slideInc = glGenLists(1); glNewList(slideInc, GL_COMPILE); glPushMatrix(); glPushMatrix(); glTranslated(0,0-slideThick, slideIncLength); glScaled(slideWidth, slideThick, slideIncLength); glCallList(cube); glPopMatrix(); glPushMatrix(); glTranslated(slideWidth + slideBarrierRad, 0, slideIncLength); glScaled(slideBarrierRad, slideBarrierRad, slideIncLength); glCallList(cylinder); glPopMatrix(); glPushMatrix(); glTranslated(0-slideWidth - slideBarrierRad, 0, slideIncLength); glScaled(slideBarrierRad, slideBarrierRad, slideIncLength); glCallList(cylinder); glPopMatrix(); glPopMatrix(); glEndList(); /* calculating curve piece length */ // y = R-Rcos(a); double radius = slideCurveVertF / (1 - cos(slideAngleR)); double angle2 = (180 - slideAngleR) / 2; // get length using law of sines double curveLengthF = radius * sin(phi) / sin(angle2); double curveLength = curveLengthF / 2; slideCurvePiece = glGenLists(1); glNewList(slideCurvePiece, GL_COMPILE); glPushMatrix(); glTranslated(0,0,curveLength); glPushMatrix(); glTranslated(0,0-slideThick,0); glScaled(slideWidth, slideThick, curveLength); glCallList(cube); glPopMatrix(); glPushMatrix(); glTranslated(slideWidth + slideBarrierRad, 0, 0); glScaled(slideBarrierRad, slideBarrierRad, curveLength); glCallList(cylinder); glPopMatrix(); glPushMatrix(); glTranslated(0-slideWidth - slideBarrierRad, 0,0); glScaled(slideBarrierRad, slideBarrierRad, curveLength); glCallList(cylinder); glPopMatrix(); glPopMatrix(); // push the rest out into position glTranslated(0,0,curveLengthF); glRotated(0-phiD,1,0,0); glEndList(); int count = 0; slideCurve = glGenLists(1); glNewList(slideCurve, GL_COMPILE); glRotated(0-phiD,1,0,0); for(count = 0; count < curveSmooth; count++) { glCallList(slideCurvePiece); } glEndList(); slideBottom = glGenLists(1); glNewList(slideBottom, GL_COMPILE); glPushMatrix(); glTranslated(0,0,slideBottomLength); glPushMatrix(); glTranslated(0,0-slideThick, 0); glScaled(slideWidth, slideThick, slideBottomLength); glCallList(cube); glPopMatrix(); //barriers glPushMatrix(); glTranslated(slideWidth + slideBarrierRad, 0, 0); glScaled(slideBarrierRad, slideBarrierRad, slideBottomLength); glCallList(cylinder); glPopMatrix(); glPushMatrix(); glTranslated(0-slideWidth - slideBarrierRad, 0, 0); glScaled(slideBarrierRad, slideBarrierRad, slideBottomLength); glCallList(cylinder); glPopMatrix(); //legs glPopMatrix(); double zTran = slideBottomLength *3/2; glPushMatrix(); glTranslated(slideWidth-slideBarrierRad,0-slideLegShorth,zTran); glRotated(90,1,0,0); glScaled(slideLegRadius, slideLegRadius, slideLegShorth); glCallList(cylinder); glPopMatrix(); glPushMatrix(); glTranslated(0-slideWidth+slideBarrierRad,0-slideLegShorth, zTran); glRotated(90,1,0,0); glScaled(slideLegRadius, slideLegRadius, slideLegShorth); glCallList(cylinder); glPopMatrix(); glEndList(); }
void createSwingParts() { sphere = gluNewQuadric(); sphereEnd = glGenLists(1); glNewList(sphereEnd, GL_COMPILE); gluSphere(sphere,poleRadius,10,10); glEndList(); horizPole = glGenLists(1); glNewList(horizPole, GL_COMPILE); glPushMatrix(); glScaled(poleRadius, poleRadius, setLength); glCallList(cylinder); glPopMatrix(); glEndList(); vertPole = glGenLists(1); glNewList(vertPole, GL_COMPILE); glPushMatrix(); glTranslated(0,0-poleLength,0); glRotated(90, 1,0,0); glScaled(poleRadius, poleRadius, poleLength); glCallList(cylinder); glPopMatrix(); glEndList(); swingSide = glGenLists(1); glNewList(swingSide,GL_COMPILE); glPushMatrix(); glRotated(90,1,0,0); glScaled(stringRadius, stringRadius, stringLength); glCallList(cylinder); glPopMatrix(); glEndList(); swingSeat = glGenLists(1); glNewList(swingSeat, GL_COMPILE); glPushMatrix(); glScaled(seatWidth, seatThick, seatLength); glCallList(cube); glPopMatrix(); glEndList(); swingEnd = glGenLists(1); glNewList(swingEnd, GL_COMPILE); glPushMatrix(); glTranslated(0,0,setLength); glCallList(sphereEnd); glPushMatrix(); glRotated(-30,0,0,1); glCallList(vertPole); glRotated(60,0,0,1); glCallList(vertPole); glPopMatrix(); glRotated(-30,1,0,0); glCallList(vertPole); glPopMatrix(); glEndList(); }
void GLWidget::rotating(){ //glRotated(gear1Rot / 16.0, 0.0, 0.0, 1.0); glRotated(gear1Rot / 16.0, 0.0, 1.0, 0.0); }
void LinearAnimation::animate() const{ glTranslated(currentPosition.x,currentPosition.y,currentPosition.z); if(directional)glRotated(currentAngleDirectionXZ,0,1,0); }
static void truck(double x,double y,double z, double dx,double dy,double dz, double th) { double i = 0.0; // Save transformation glPushMatrix(); // Offset glTranslated(x,y,z); glRotated(th,0,1,0); glScaled(dx,dy,dz); // Front glBegin(GL_POLYGON); glColor3f(1,0,0); //red glVertex3f(0,0, 0); glVertex3f(10,0, 0); glVertex3f(10,4, 0); glVertex3f(4,4,0); glVertex3f(2,2,0); glVertex3f(0,1,0); glEnd(); //back glBegin(GL_POLYGON); glColor3f(0,1,0); //green glVertex3f(0,0, -4); glVertex3f(10,0, -4); glVertex3f(10,4, -4); glVertex3f(4,4,-4); glVertex3f(2,2,-4); glVertex3f(0,1,-4); glEnd(); glBegin(GL_QUADS); // right glColor3f(0,0,1); //blue glVertex3f(10,0,0); glVertex3f(10,0,-4); glVertex3f(10,4,-4); glVertex3f(10,4,0); // left_bottom glColor3f(0,1,0);//green glVertex3f(0, 0, 0); glVertex3f(0,0,-4); glVertex3f(0,1,-4); glVertex3f(0,1,0); // left_bonnet glColor3f(1,1,0); //yellow glVertex3f(0,1,0); glVertex3f(0,1,-4); glVertex3f(2,2,-4); glVertex3f(2,2,0); // left_window glColor3f(0,1,1); //cyan glVertex3f(2,2,0); glVertex3f(2,2,-4); glVertex3f(4,4,-4); glVertex3f(4,4,0); // top glColor3f(1,1,1); glVertex3f(4,4,0); glVertex3f(4,4,-4); glVertex3f(10,4,-4); glVertex3f(10,4,0); // bottom glColor3f(1,1,1); glVertex3f(0,0,0); glVertex3f(0,0,-4); glVertex3f(10,0,-4); glVertex3f(10,0,0); glEnd(); //*************** Tire front *******************// glColor3f(.2, .3, .5); glBegin(GL_POLYGON); for(i = 0; i<2 * PI; i+= PI/4) glVertex3f(cos(i)*RADIUS+3, sin(i)*RADIUS, -.5); glEnd(); glBegin(GL_POLYGON); for(i = 0; i<2 * PI; i+= PI/6) glVertex3f(cos(i)*RADIUS+8, sin(i)*RADIUS, -.5); glEnd(); //*************** Tire back ********************// glBegin(GL_POLYGON); for(i = 0; i<2 * PI; i+= PI/6) glVertex3f(cos(i)*RADIUS+3, sin(i)*RADIUS, -3.5); glEnd(); glBegin(GL_POLYGON); for(i = 0; i<2 * PI; i+= PI/6) glVertex3f(cos(i)*RADIUS+8, sin(i)*RADIUS, -3.5); glEnd(); //********* Side mirro Right********// glBegin(GL_QUADS); //mirror-front glColor3f(1,1,1); glVertex3f(5,2,.5); glVertex3f(5.3,2,.5); glVertex3f(5.3,3,.5); glVertex3f(5,3,.5); // mirror-left glColor3f(1,0,1); glVertex3f(5,2,0); glVertex3f(5,2,.5); glVertex3f(5,3,.5); glVertex3f(5,3,0); // mirror-right glColor3f(1,0,1); glVertex3f(5.3,2,0); glVertex3f(5.3,2,.5); glVertex3f(5.3,3,.5); glVertex3f(5.3,3,0); // mirror-top glColor3f(1,0,1); glVertex3f(5,2,0); glVertex3f(5,2,.5); glVertex3f(5.3,2,.5); glVertex3f(5.3,2,0); // mirror-top glColor3f(0,1,0); glVertex3f(5,3,0); glVertex3f(5,3,.5); glVertex3f(5.3,3,.5); glVertex3f(5.3,3,0); /********* Side mirro Right********/ //mirror-front glColor3f(1,1,1); glVertex3f(5,2,-4.5); glVertex3f(5.3,2,-4.5); glVertex3f(5.3,3,-4.5); glVertex3f(5,3,-4.5); // mirror-left glColor3f(1,0,1); glVertex3f(5,2,-4); glVertex3f(5,2,-4.5); glVertex3f(5,3,-4.5); // mirror-left glColor3f(1,0,1); glVertex3f(5,2,-4); glVertex3f(5,2,-4.5); glVertex3f(5,3,-4.5); glVertex3f(5,3,-4); // mirror-right glColor3f(1,0,1); glVertex3f(5.3,2,-4); glVertex3f(5.3,2,-4.5); glVertex3f(5.3,3,-4.5); glVertex3f(5.3,3,-4); // mirror-bottom glColor3f(0,1,1); glVertex3f(5,2,-4); glVertex3f(5,2,-4.5); glVertex3f(5.3,2,-4.5); glVertex3f(5.3,2,-4); // mirror-top glColor3f(0,1,1); glVertex3f(5,3,-4); glVertex3f(5,3,-4.5); glVertex3f(5.3,3,-4.5); glVertex3f(5.3,3,-4); // End glEnd(); // Undo transformation glPopMatrix(); }
void Car::draw() { if (!active) return; // Reinicia transformações glPushMatrix(); glTranslatef(_position.getX(), _position.getY(), _position.getZ() + 0.2); glRotatef(_turnAngle, 0, 0, 1); //corpo do carro glPushMatrix(); GLfloat bodyAmb[] = { 0.35f,0.0f,0.0f,1.0f }; GLfloat bodyDiff[] = { 0.69f,0.0f,0.0f,1.0f }; GLfloat bodySpec[] = { 0.29f,0.28f,0.29f,1.0f }; GLfloat bodyShine = 46; glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, bodyAmb); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, bodyDiff); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, bodySpec); glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, bodyShine); glColor3f(1.0f, 0.0f, 0.0f); glScalef(1.0f, 0.8f, 0.3f); drawCube(); glPopMatrix(); //cockpit do carro glPushMatrix(); glTranslatef(-0.2f, 0.0f, 0.20f); glScalef(0.4f, 0.6f, 0.1f); GLfloat cockpitAmb[] = { 0.07f,0.07f,0.07f,1.0f }; GLfloat cockpitDiff[] = { 0.13f,0.17f,0.17f,1.0f }; GLfloat cockpitSpec[] = { 0.42f,0.41f,0.41f,1.0f }; GLfloat cockpitShine = 4; glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, cockpitAmb); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, cockpitDiff); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, cockpitSpec); glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, cockpitShine); glColor3f(1.0, 1.0, 1.0); drawCube(); glColor3f(1.0, 0.0, 0.0); glutWireCube(1.0); glPopMatrix(); // roda traseira direita glPushMatrix(); GLfloat wheel1Amb[] = { 0.0f,0.0f,0.0f,1.0f }; GLfloat wheel1Diff[] = { 0.13f,0.13f,0.14f,1.0f }; GLfloat wheel1Spec[] = { 0.17f,0.18f,0.19f,1.0f }; GLfloat wheel1Shine = 12; glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, wheel1Amb); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, wheel1Diff); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, wheel1Spec); glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, wheel1Shine); glColor3f(1.0, 1.0, 1.0); glTranslatef(-0.3f, -0.45f, 0.0f); glRotated(90, 1, 0, 0); glScalef(0.4f, 0.4f, 1.5f); drawWheel(); glColor3f(0, 0, 0); glutWireTorus(0.05, 0.2, 32, 32); glPopMatrix(); //perna dianteira direita glPushMatrix(); GLfloat wheel2Amb[] = { 0.0f,0.0f,0.0f,1.0f }; GLfloat wheel2Diff[] = { 0.13f,0.13f,0.14f,1.0f }; GLfloat wheel2Spec[] = { 0.17f,0.18f,0.19f,1.0f }; GLfloat wheel2Shine = 12; glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, wheel2Amb); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, wheel2Diff); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, wheel2Spec); glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, wheel2Shine); glColor3f(1.0, 1.0, 1.0); glTranslatef(0.3f, -0.45f, 0.0); glRotated(90, 1, 0, 0); glScalef(0.4f, 0.4f, 1.5f); drawWheel(); glColor3f(0.0, 0.0, 0.0); glutWireTorus(0.05, 0.2, 32, 32); glPopMatrix(); // roda traseira direita glPushMatrix(); GLfloat wheel3Amb[] = { 0.0f,0.0f,0.0f,1.0f }; GLfloat wheel3Diff[] = { 0.13f,0.13f,0.14f,1.0f }; GLfloat wheel3Spec[] = { 0.17f,0.18f,0.19f,1.0f }; GLfloat wheel3Shine = 12; glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, wheel3Amb); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, wheel3Diff); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, wheel3Spec); glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, wheel3Shine); glColor3f(1.0, 1.0, 1.0); glTranslatef(-0.3f, 0.45f, 0.0f); glRotated(90, 1, 0, 0); glScalef(0.4f, 0.4f, 1.5f); drawWheel(); glColor3f(0, 0, 0); glutWireTorus(0.05, 0.2, 32, 32); glPopMatrix(); //perna dianteira direita glPushMatrix(); GLfloat wheel4Amb[] = { 0.0f,0.0f,0.0f,1.0f }; GLfloat wheel4Diff[] = { 0.13f,0.13f,0.14f,1.0f }; GLfloat wheel4Spec[] = { 0.17f,0.18f,0.19f,1.0f }; GLfloat wheel4Shine = 12; glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, wheel4Amb); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, wheel4Diff); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, wheel4Spec); glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, wheel4Shine); glColor3f(1.0, 1.0, 1.0); glTranslatef(0.3f, 0.45f, 0.0); glRotated(90, 1, 0, 0); glScalef(0.4f, 0.4f, 1.5f); drawWheel(); glColor3f(0.0, 0.0, 0.0); glutWireTorus(0.05, 0.2, 32, 32); glPopMatrix(); glPopMatrix(); }
void CylinderSlice::drawBase(float yAxis) { float angle = 360.0f / _slices; float last_angle = 0.0f; glPushMatrix(); if (_isClockBase) { float amb[3] = {1, 1, 1}; float dif[3] = {1, 1, 1}; float spec[3] = {1, 1, 1}; float shininess = 10.0f; CGFappearance *clockAppearance = new CGFappearance(amb, dif, spec, shininess); clockAppearance->setTexture("clock.png"); clockAppearance->apply(); } if (yAxis) { glTranslated(0, yAxis, 0); glRotated(180, 0.0f, 0.0f, 1.0f); if (_smooth) glNormal3f(0, 1, 0); } else if (_smooth) glNormal3f(0, -1, 0); /*glBegin(GL_POLYGON); crd first_crd; first_crd.x = -0.5f / _slices; first_crd.y = 0.0f; first_crd.z = 0.5f / _slices; glTexCoord2d((cos(_slices-1 * deg2rad) +1) / 2, (sin(_slices-1 * deg2rad) +1) / 2); glVertex3f(first_crd.x, first_crd.y, first_crd.z); _vertices.push_back(first_crd); crd second_crd; second_crd.x = 0.5f / _slices; second_crd.y = 0.0f; second_crd.z = 0.5f / _slices; glTexCoord2d((cos(_slices-2 * deg2rad) +1) / 2, (sin(_slices-2 * deg2rad) +1) / 2); glVertex3f(second_crd.x, second_crd.y, second_crd.z); _vertices.push_back(second_crd); crd last_crd; last_crd.x = 0.5f / _slices; last_crd.y = 0.0f; last_crd.z = 0.5f / _slices; for (int i = 0; i < _slices - 2; i++) { last_crd.x += cosf(deg2rad * (angle + last_angle)) / _slices; last_crd.z += sinf(deg2rad * (angle + last_angle)) / _slices; last_angle += angle; _vertices.push_back(last_crd); // glTexCoord2d(last_crd.x, last_crd.z); glTexCoord2d((cos(i * deg2rad) +1) / 2, (sin(i * deg2rad) +1) / 2); glVertex3f(last_crd.x, last_crd.y, last_crd.z); } glEnd();*/ glBegin(GL_TRIANGLE_FAN); glNormal3f(0, 1, 0); glTexCoord2f(0.5, 0.5); glVertex3f(0, 0, 0); for (int i =0; i<=_slices;i++) { glTexCoord2f((cos(angle*i)+1)/2,(sin(angle*i)+1)/2); glVertex3f(cos(angle*i), 0, sin(angle*i)); } glEnd(); glBegin(GL_TRIANGLE_FAN); glNormal3f(0, -1, 0); glVertex3f(0, 1, 0); for (int i =0; i<=_slices;i++) { glVertex3f(cos(-angle*i), 1, sin(-angle*i)); } glEnd(); glPopMatrix(); }
void DebugDrawing3D::draw() { SYNC_WITH(*robotConsole); // Convert mm to m. glScaled(0.001, 0.001, 0.001); // Custom scaling. glScaled(scaleX, scaleY, scaleZ); // Custom rotation. if (rotateX != 0) glRotated(toDegrees(rotateX), 1, 0, 0); if (rotateY != 0) glRotated(toDegrees(rotateY), 0, 1, 0); if (rotateZ != 0) glRotated(toDegrees(rotateZ), 0, 0, 1); // Custom translation. glTranslated(transX, transY, transZ); GLboolean lighting; glGetBooleanv(GL_LIGHTING, &lighting); glDisable(GL_LIGHTING); // Draw all quads. std::vector<Quad>::iterator q; for(q = quads.begin(); q != quads.end(); ++q) { glColor4ub(q->color.r, q->color.g, q->color.b, q->color.a); glBegin(GL_QUADS); glVertex3d(q->points[0].x, q->points[0].y, q->points[0].z); glVertex3d(q->points[1].x, q->points[1].y, q->points[1].z); glVertex3d(q->points[2].x, q->points[2].y, q->points[2].z); glVertex3d(q->points[3].x, q->points[3].y, q->points[3].z); glEnd(); } // Draw all polygons/triangles. std::vector<Polygon>::iterator p; for(p = polygons.begin(); p != polygons.end(); ++p) { glColor4ub(p->color.r, p->color.g, p->color.b, p->color.a); glBegin(GL_TRIANGLES); glVertex3d(p->points[0].x, p->points[0].y, p->points[0].z); glVertex3d(p->points[1].x, p->points[1].y, p->points[1].z); glVertex3d(p->points[2].x, p->points[2].y, p->points[2].z); glEnd(); } // Draw all lines. // Get line width so we can restore it later on float lw[1]; glGetFloatv(GL_LINE_WIDTH, lw); std::vector<Line>::iterator l; for(l = lines.begin(); l != lines.end(); ++l) { glLineWidth(l->width); glColor4ub(l->color.r, l->color.g, l->color.b, l->color.a); glBegin(GL_LINES); glVertex3d(l->points[0].x, l->points[0].y, l->points[0].z); glVertex3d(l->points[1].x, l->points[1].y, l->points[1].z); glEnd(); } // Restore line width glLineWidth(lw[0]); // Draw all points. // Get point size so we can restore it later on float ps[1]; glGetFloatv(GL_POINT_SIZE, ps); std::vector<Dot>::iterator d; for(d = dots.begin(); d != dots.end(); ++d) { // Since each point may have a different size we can't handle all // points in a single glBegin(GL_POINTS). // ( glPointSize is not allowed in a glBegin(...). ) glPointSize(d->width); glColor4ub(d->color.r, d->color.g, d->color.b, d->color.a); glBegin(GL_POINTS); glVertex3d(d->point.x,d->point.y,d->point.z); glEnd(); } std::vector<Sphere>::iterator s; for(s = spheres.begin(); s != spheres.end(); ++s) { glColor4ub(s->color.r, s->color.g, s->color.b, s->color.a); glPushMatrix(); glTranslated(s->point.x, s->point.y, s->point.z); GLUquadric* q = gluNewQuadric(); gluSphere(q, s->radius, 16, 16); gluDeleteQuadric(q); glPopMatrix(); } std::vector<Cylinder>::iterator c; for(c = cylinders.begin(); c != cylinders.end(); ++c) { glColor4ub(c->color.r, c->color.g, c->color.b, c->color.a); glPushMatrix(); glTranslated(c->point.x, c->point.y, c->point.z); if(c->rotation.x != 0) glRotated(toDegrees(c->rotation.x), 1, 0, 0); if(c->rotation.y != 0) glRotated(toDegrees(c->rotation.y), 0, 1, 0); if(c->rotation.z != 0) glRotated(toDegrees(c->rotation.z), 0, 0, 1); glTranslated(0,0,-c->height / 2); GLUquadric* q = gluNewQuadric(); gluCylinder(q, c->radius, c->radius, c->height, 16, 1); glRotated(180, 0, 1, 0); gluDisk(q, 0, c->radius, 16, 1); glRotated(180, 0, 1, 0); glTranslated(0, 0, c->height); gluDisk(q, 0, c->radius, 16, 1); gluDeleteQuadric(q); glPopMatrix(); } GLboolean culling; glGetBooleanv(GL_CULL_FACE, &culling); glDisable(GL_CULL_FACE); glEnable(GL_TEXTURE_2D); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); std::vector<Image3D>::iterator i; for(i = images.begin(); i != images.end(); ++i) { GLuint t; glGenTextures(1, &t); glBindTexture(GL_TEXTURE_2D, t); int width, height; char* imageData = copyImage(*i->image, width, height); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); delete [] imageData; glPushMatrix(); glTranslated(i->point.x, i->point.y, i->point.z); if(i->rotation.x != 0) glRotated(toDegrees(i->rotation.x), 1, 0, 0); if(i->rotation.y != 0) glRotated(toDegrees(i->rotation.y), 0, 1, 0); if(i->rotation.z != 0) glRotated(toDegrees(i->rotation.z), 0, 0, 1); glBegin(GL_QUADS); double right = (double) i->image->cameraInfo.resolutionWidth / width; double top = (double) i->image->cameraInfo.resolutionHeight / height; glTexCoord2d(right, top); glVertex3d(0, -i->width / 2, i->height / 2); glTexCoord2d(0, top); glVertex3d(0, i->width / 2, i->height / 2); glTexCoord2d(0, 0); glVertex3d(0, i->width / 2, -i->height / 2); glTexCoord2d(right, 0); glVertex3d(0, -i->width / 2, -i->height / 2); glEnd(); glPopMatrix(); glDeleteTextures(1, &t); } glDisable(GL_TEXTURE_2D); // Restore point size glPointSize(ps[0]); if(lighting) glEnable(GL_LIGHTING); if(culling) glEnable(GL_CULL_FACE); }
void castle() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glColor3d(1, 0.5, 0.0); glTranslated(-1, 0, -4); glRotated(87, 1, 0, 0); glRotated(-30, 0, 0, 1); glBegin (GL_QUADS); //muros //lado esquerdo glVertex3f(-0.1f, 0.0f, 0.1f); glVertex3f(-0.1f, 0.0f, 0.6f); glVertex3f(-0.1f, 2.0f, 0.6f); glVertex3f(-0.1f, 2.0f, 0.1f); //parte de trás glVertex3f(0.0f, -0.1f, 0.1f); glVertex3f(2.0f, -0.1f, 0.6f); glVertex3f(2.0f, -0.1f, 0.6f); glVertex3f(2.0f, -0.1f, 0.1f); //lado direito glVertex3f(2.1f, 2.0f, 0.6f); glVertex3f(2.1f, 2.0f, 0.1f); glVertex3f(2.1f, 0.0f, 0.1f); glVertex3f(2.1f, 0.0f, 0.6f); //frente glVertex3f(2.0f, 2.1f, 0.6f); glVertex3f(2.0f, 2.1f, 0.1f); glVertex3f(0.0f, 2.1f, 0.1f); glVertex3f(0.0f, 2.1f, 0.6f); glEnd; glVertex3f(0.15f, 2.5f, 0.5f); glVertex3f(0.15f, 2.5f, -0.2f); glVertex3f(0.0f, 2.5f, -0.2f); glVertex3f(0.0f, 2.5f, 0.5f); //Entrada //Torres glBegin(GL_QUADS); //Fundos esquerda fundosEsq = gluNewQuadric(); gluCylinder(fundosEsq, 0.2, 0.2, 0.6, slices, stacks); glTranslated(0, 2, 0); //Frente esquerda frenteDir = gluNewQuadric(); gluCylinder(frenteDir, 0.2, 0.2, 0.6, slices, stacks); glTranslated(2, 0, 0); //Frente direita frenteDir = gluNewQuadric(); gluCylinder(frenteDir, 0.2, 0.2, 0.6, slices, stacks); glEnd; glPopMatrix(); //telhado glPushMatrix(); glColor3d(1, 0.0, 0); glTranslated(-1, 0, -4); glRotated(87, 1, 0, 0); glRotated(-30, 0, 0, 1); //Fundos esquerdo telhadoFundosEsq = gluNewQuadric(); glTranslated(0, 0, -0.58); gluCylinder(telhadoFundosEsq, 0.0, 0.23, 0.6, slices, stacks); //Frente esquerda telhadoFrenteEsq = gluNewQuadric(); glTranslated(0, 2, 0); gluCylinder(telhadoFrenteEsq, 0.0, 0.23, 0.6, slices, stacks); //Frente direita telhadoFrenteDir = gluNewQuadric(); glTranslated(2, 0, 0); gluCylinder(telhadoFrenteDir, 0.0, 0.23, 0.6, slices, stacks); //Torre quadrada telhadoTorre = gluNewQuadric(); glTranslated(-0.2, -1.6, -0.8); gluCylinder(telhadoTorre, 0.0, 0.23, 0.6, slices, stacks); glPopMatrix(); glutSwapBuffers(); }
void display(void) //フレーム、ボール、足の描画 { GLint i; //for文用変数 GLdouble rad = M_PI / 180; //ラジアン const GLdouble shin = leg_len - toe_height; //すねの長さ const GLdouble ankle = leg_wigth - toe_wigth; //足首 leg_point1[0] = toe_wigth*cos(kickang * rad) + shin*sin(kickang*rad) + leg_x; leg_point1[1] = toe_wigth*sin(kickang * rad) - shin*cos(kickang * rad) + leg_y; leg_point2[0] = toe_wigth*cos(kickang * rad) + leg_len*sin(kickang*rad) + leg_x; leg_point2[1] = toe_wigth*sin(kickang * rad) - leg_len*cos(kickang * rad) + leg_y; //色バッファのクリア glClear(GL_COLOR_BUFFER_BIT); //フレーム //フレーム描画のための行列スタック glPushMatrix(); //黒 glColor3d(0.0, 0.0, 0.0); //フレームのサイズをウィンドウよりedgesizeだけ小さくする edgesize_master = size - edgesize; //線をつなげる glBegin(GL_LINE_LOOP); //頂点を指定 glVertex2d(-edgesize_master, -edgesize_master); glVertex2d(-edgesize_master, edgesize_master); glVertex2d(edgesize_master, edgesize_master); glVertex2d(edgesize_master, -edgesize_master); glEnd(); //行列スタックを戻す glPopMatrix(); //足 //足描画のための行列スタック glPushMatrix(); //足の初期位置 glTranslated(leg_x, leg_y, 0); //足の回転 glRotated(kickang, 0.0, 0.0, 1.0); //青 glColor3d(0.0, 0.0, 1.0); //線をつなげるモード glBegin(GL_LINE_LOOP); glVertex2d(0.0, 0.0); glVertex2d(0.0, -shin); glVertex2d(toe_wigth, -shin); glVertex2d(toe_wigth, -shin - toe_height); glVertex2d(toe_wigth - leg_wigth, -shin - toe_height); glVertex2d(toe_wigth - leg_wigth, -shin - toe_height + leg_len); glVertex2d(toe_wigth - leg_wigth + ankle, -shin - toe_height + leg_len); //頂点の指定の終了 glEnd(); //行列スタックを戻す glPopMatrix(); //ボール //ボール描画のための行列スタック glPushMatrix(); //ボールの運動 glTranslated(ball_x, ball_y, 0); //ボールの回転 glRotated(ang, 0.0, 0.0, -1.0); //緑 glColor3d(0.0, 1.0, 0.0); Circle(r); //赤 glColor3d(1.0, 0.0, 0.0); //ラインを入れる glBegin(GL_LINES); for (i = 0; i < 180; i += 180 / line){ glVertex2d(r*cos(i*rad), r*sin(i*rad)); glVertex2d(r*cos((i + 180)*rad), r*sin((i + 180)*rad)); } glEnd(); //行列スタックを戻す glPopMatrix(); glutSwapBuffers(); }
void MyTable::draw(){ float ambWood[3] ={0.38, 0.2, 0.0196}; float difWood[3] = {0.38, 0.2, 0.0196}; float specWood[3] = {0.05, 0.05, 0.05}; float shininessWood = 10.f; CGFappearance * wood = new CGFappearance(ambWood,difWood,specWood,shininessWood); float ambMetal[3] = {0.3, 0.3, 0.3}; float difMetal[3] = {0.4, 0.4, 0.4}; float specMetal[3] = {0.6, 0.6, 0.6}; float shininessMetal = 500.f; CGFappearance * metal = new CGFappearance(ambMetal,difMetal,specMetal,shininessMetal); MyUnitCube myUnitCube = MyUnitCube(); glPushMatrix(); glRotated(rPN*zTrans*30,0,1,0); glTranslatef(xTrans,0,zTrans); glPushMatrix(); // top glTranslatef(0, 0.15 + 3.5,0); glScalef(5,0.3,3); wood->apply(); myUnitCube.draw(); glPopMatrix(); metal->apply(); glPushMatrix(); // legs glTranslatef(0, 1.75,0); glPushMatrix(); glTranslatef(-2.5 + 0.15, 0, -1.5 + 0.15); glScalef(0.3,3.5,0.3); myUnitCube.draw(); glPopMatrix(); glPushMatrix(); glTranslatef(2.5 - 0.15, 0, -1.5 + 0.15); glScalef(0.3,3.5,0.3); myUnitCube.draw(); glPopMatrix(); glPushMatrix(); glTranslatef(-2.5 + 0.15, 0, 1.5 - 0.15); glScalef(0.3,3.5,0.3); myUnitCube.draw(); glPopMatrix(); glPushMatrix(); glTranslatef(2.5 - 0.15, 0, 1.5 - 0.15); glScalef(0.3,3.5,0.3); myUnitCube.draw(); glPopMatrix(); glPopMatrix(); glPopMatrix(); }
void EPuckModel::draw(PhysicalObject* object) const { DifferentialWheeled* dw = polymorphic_downcast<DifferentialWheeled*>(object); const double wheelRadius = 2.1; const double wheelCirc = 2 * M_PI * wheelRadius; const double radiosityScale = 1.01; glTranslated(0, 0, wheelRadius); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, textures[0]); glColor3d(1, 1, 1); glCallList(lists[0]); glCallList(lists[1]); //glColor3d(1-object->getColor().components[0], 1+object->getColor().components[1], 1+object->getColor().components[2]); glColor3d(0.6+object->getColor().components[0]-0.3*object->getColor().components[1]-0.3*object->getColor().components[2], 0.6+object->getColor().components[1]-0.3*object->getColor().components[0]-0.3*object->getColor().components[2], 0.6+object->getColor().components[2]-0.3*object->getColor().components[0]-0.3*object->getColor().components[1]); glCallList(lists[2]); glColor3d(1, 1, 1); // wheels glPushMatrix(); glRotated((fmod(dw->leftOdometry, wheelCirc) * 360) / wheelCirc, 0, 1, 0); glCallList(lists[3]); glPopMatrix(); glPushMatrix(); glRotated((fmod(dw->rightOdometry, wheelCirc) * 360) / wheelCirc, 0, 1, 0); glCallList(lists[4]); glPopMatrix(); // shadow glBindTexture(GL_TEXTURE_2D, textures[1]); glDisable(GL_LIGHTING); glEnable(GL_BLEND); glBlendFunc(GL_ZERO, GL_SRC_COLOR); // bottom shadow glPushMatrix(); // disable writing of z-buffer glDepthMask( GL_FALSE ); //glTranslated(0, 0, -wheelRadius+0.01); glTranslated(0, 0, -wheelRadius); glEnable(GL_POLYGON_OFFSET_FILL); glBegin(GL_QUADS); glTexCoord2f(0.49f, 0.01f); glVertex2f(-5.f, -5.f); glTexCoord2f(0.49f, 0.49f); glVertex2f(5.f, -5.f); glTexCoord2f(0.01f, 0.49f); glVertex2f(5.f, 5.f); glTexCoord2f(0.01f, 0.01f); glVertex2f(-5.f, 5.f); glEnd(); glDisable(GL_POLYGON_OFFSET_FILL); glDepthMask( GL_TRUE ); glPopMatrix(); // wheel shadow glPushMatrix(); glScaled(radiosityScale, radiosityScale, radiosityScale); glTranslated(0, -0.025, 0); glCallList(lists[3]); glPopMatrix(); glPushMatrix(); glScaled(radiosityScale, radiosityScale, radiosityScale); glTranslated(0, 0.025, 0); glCallList(lists[4]); glPopMatrix(); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_BLEND); glEnable(GL_LIGHTING); glDisable(GL_TEXTURE_2D); }
void DrawObject(float x,float y,float z,float rotation,float width,float height,unsigned int decal_type,char * text) { glPushMatrix(); //glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); if (frame.try_for_best_render_quality) { glEnable(GL_NORMALIZE); glEnable(GL_LINE_SMOOTH); } glTranslated(x,y,z); if ( rotation!=0 ) { glRotated(rotation,0.0,0.0,1.0); } if ( decal_type != 0 ) { //Decal type zero may only have text :P glEnable ( GL_TEXTURE_2D ); /* DRAW FRAME >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/ glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE); if ( decal_type == 1 ) glBindTexture(GL_TEXTURE_2D, heart->gpu.gl_rgb_texture ); else if ( decal_type == 2 ) glBindTexture(GL_TEXTURE_2D, star->gpu.gl_rgb_texture ); else if ( decal_type == 3 ) glBindTexture(GL_TEXTURE_2D, play_img->gpu.gl_rgb_texture ); else if ( decal_type == 4 ) glBindTexture(GL_TEXTURE_2D, pause_img->gpu.gl_rgb_texture ); else if ( decal_type == 5 ) glBindTexture(GL_TEXTURE_2D, label->gpu.gl_rgb_texture ); else glBindTexture(GL_TEXTURE_2D, heart->gpu.gl_rgb_texture ); /* DEFAULT */ glBegin(GL_QUADS); glColor3f(1.0,1.0,1.0); float size_x=width,size_y=height; float xmin=(-1)*size_x/2,xmax=size_x/2,ymin=(-1)*size_y/2,ymax=size_y/2; glTexCoord2f(1.0f, 0.0f); glVertex3f(0+xmin,0+ymin,0/*-4.1*/); // Bottom Left Of The Texture and Quad glTexCoord2f(0.0f, 0.0f); glVertex3f(0+xmax,0+ymin,0/*-4.1*/); // Bottom Right Of The Texture and Quad glTexCoord2f(0.0f, 1.0f); glVertex3f(0+xmax,0+ymax,0/*-4.1*/); // Top Right Of The Texture and Quad glTexCoord2f(1.0f, 1.0f); glVertex3f(0+xmin,0+ymax,0/*-4.1*/); glEnd(); glDisable ( GL_TEXTURE_2D ); glDisable(GL_BLEND); } if (text!=0) { if (frame.lighting_enabled) { glDisable( GL_LIGHTING ); } glColor3f(1.0,1.0,1.0); glRasterPos3f(width/2,0,0.1); //glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24,(const unsigned char*)text); glPushMatrix(); glRotated(180,0.0,0.0,1.0); glTranslated(-10,0,0); glLineWidth(3.0); static GLfloat f = 1.0/110; glScalef( f, f, f ); glutStrokeString(GLUT_STROKE_ROMAN,(const unsigned char*)text); //GLUT_STROKE_ROMAN GLUT_STROKE_MONO_ROMAN glScalef( 1/f, 1/f, 1/f ); glLineWidth(1.0); glTranslated(0,0,0); glRotated(-180,0.0,0.0,1.0); glPopMatrix(); if (frame.lighting_enabled) { glEnable( GL_LIGHTING ); } } if ( rotation!=0 ) { glRotated(-rotation,0.0,0.0,1.0); } glTranslated(-x,-y,-z); if (frame.try_for_best_render_quality) { glDisable(GL_LINE_SMOOTH); glDisable(GL_NORMALIZE); } glPopMatrix(); }
/* * GLUT's display callback function. * Render the main OpenGL scene. */ static void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); /* Camera rotation */ glTranslated(-eye.x, -eye.y, -eye.z); glRotated(rot.x, 1.0f, 0.0f, 0.0f); glRotated(rot.y, 0.0f, 1.0f, 0.0f); glRotated(rot.z, 0.0f, 0.0f, 1.0f); /* Draw scene */ switch (outlineMode) { case MODE_OUTLINE: { glPushAttrib(GL_POLYGON_BIT); glEnable(GL_CULL_FACE); /* * Draw front-facing polygons as filled */ glPolygonMode(GL_FRONT, GL_FILL); glCullFace(GL_BACK); /* Draw solid object */ glColor3f(1.0f, 1.0f, 1.0f); drawObject(); /* * Draw back-facing polygons as red lines */ /* Disable lighting for outlining */ glPushAttrib(GL_LIGHTING_BIT | GL_LINE_BIT | GL_DEPTH_BUFFER_BIT); glDisable(GL_LIGHTING); glPolygonMode(GL_BACK, GL_LINE); glCullFace(GL_FRONT); glDepthFunc(GL_LEQUAL); glLineWidth(5.0f); /* Draw wire object */ glColor3f(1.0f, 0.0f, 0.0f); drawObject(); /* GL_LIGHTING_BIT | GL_LINE_BIT | GL_DEPTH_BUFFER_BIT */ glPopAttrib(); /* GL_POLYGON_BIT */ glPopAttrib(); break; } case MODE_OUTLINE_ONLY: { glPushAttrib(GL_POLYGON_BIT); glEnable(GL_CULL_FACE); /* * Draw front-facing polygons as filled */ /* Disable color buffer */ glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); glPolygonMode(GL_FRONT, GL_FILL); glCullFace(GL_BACK); /* Draw solid object */ glColor3f(1.0f, 1.0f, 1.0f); drawObject(); /* * Draw back-facing polygons as red lines */ /* Enable color buffer */ glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); /* Disable lighting for outlining */ glPushAttrib(GL_LIGHTING_BIT | GL_LINE_BIT | GL_DEPTH_BUFFER_BIT); glDisable(GL_LIGHTING); glPolygonMode(GL_BACK, GL_LINE); glCullFace(GL_FRONT); glDepthFunc(GL_LEQUAL); glLineWidth(5.0f); /* Draw wire object */ glColor3f(1.0f, 0.0f, 0.0f); drawObject(); /* GL_LIGHTING_BIT | GL_LINE_BIT | GL_DEPTH_BUFFER_BIT */ glPopAttrib(); /* GL_POLYGON_BIT */ glPopAttrib(); break; } case MODE_SILHOUETTE: { glPushAttrib(GL_POLYGON_BIT); glEnable(GL_CULL_FACE); /* * Draw back-facing polygons as red lines */ /* Disable lighting for outlining */ glPushAttrib(GL_LIGHTING_BIT | GL_LINE_BIT | GL_DEPTH_BUFFER_BIT); glDisable(GL_LIGHTING); glPolygonMode(GL_BACK, GL_LINE); glCullFace(GL_FRONT); glDisable(GL_DEPTH_TEST); glLineWidth(5.0f); /* Draw wire object */ glColor3f(1.0f, 0.0f, 0.0f); drawObject(); /* GL_LIGHTING_BIT | GL_LINE_BIT | GL_DEPTH_BUFFER_BIT */ glPopAttrib(); /* * Draw front-facing polygons as filled */ glPolygonMode(GL_FRONT, GL_FILL); glCullFace(GL_BACK); /* Draw solid object */ glColor3f(1.0f, 1.0f, 1.0f); drawObject(); /* GL_POLYGON_BIT */ glPopAttrib(); break; } case MODE_SILHOUETTE_ONLY: { /* Clear stencil buffer */ glClearStencil(0); glClear(GL_STENCIL_BUFFER_BIT); /* * Draw front-facing polygons as filled */ /* Disable color and depth buffers */ glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); glDepthMask(GL_FALSE); /* Setup stencil buffer. Draw always in it */ glEnable(GL_STENCIL_TEST); glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF); glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); /* Draw solid object to create a mask */ glColor3f(1.0f, 1.0f, 1.0f); drawObject(); /* * Draw back-facing polygons as red lines */ /* Enable color and depth buffers */ glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); glDepthMask(GL_TRUE); /* Setup stencil buffer. We don't draw inside the mask */ glStencilFunc(GL_NOTEQUAL, 1, 0xFFFFFFFF); /* Disable lighting for outlining */ glPushAttrib(GL_LIGHTING_BIT | GL_LINE_BIT | GL_POLYGON_BIT); glDisable(GL_LIGHTING); glEnable(GL_CULL_FACE); glPolygonMode(GL_BACK, GL_LINE); glCullFace(GL_FRONT); glLineWidth(5.0f); /* Draw wire object */ glColor3f(1.0f, 0.0f, 0.0f); drawObject(); /* GL_LIGHTING_BIT | GL_LINE_BIT | GL_POLYGON_BIT */ glPopAttrib(); glDisable(GL_STENCIL_TEST); break; } default: break; } glutSwapBuffers(); }
int main() { constexpr int SIZE = 4; RubiksCubeController<SIZE> rcc; glfwInit(); int count; GLFWmonitor ** monitors = glfwGetMonitors(&count); GLFWmonitor * monitor = monitors[0]; GLFWvidmode const * mode = glfwGetVideoMode(monitor); glfwWindowHint(GLFW_RED_BITS, mode->redBits); glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits); glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits); glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate); //GLFWwindow * window = glfwCreateWindow(mode->width, mode->height, "RubiksCube", monitor, nullptr); GLFWwindow * window = glfwCreateWindow(mode->width, mode->height, "RubiksCube", nullptr, nullptr); glfwMakeContextCurrent(window); while(!glfwWindowShouldClose(window)) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST); glPushMatrix(); //window size chagne int width, height; glfwGetFramebufferSize(window, &width, &height); glViewport(0, 0, width, height); glLoadIdentity(); gluPerspective(90, static_cast<double>(width) / static_cast<double>(height), 1, 128); gluLookAt(30, 30, 30, 0, 0, 0, 0, 1, 0); //view static int theta = 0; static int iota = 0; if (glfwGetKey(window, GLFW_KEY_RIGHT)) { ++theta; } if (glfwGetKey(window, GLFW_KEY_LEFT)) { --theta; } if (glfwGetKey(window, GLFW_KEY_UP)) { ++iota; } if (glfwGetKey(window, GLFW_KEY_DOWN)) { --iota; } static int index = 0; static bool key[256] = {}; for (int i = 0; i < 256; ++i) { if (glfwGetKey(window, static_cast<char>(i))) { if (!key[i]) { switch (static_cast<char>(i)) { case '1': index = 0; break; case '2': index = 1; break; case '3': index = 2; break; case 'X': rcc.rotate({{1,0,0}}, index, glfwGetKey(window, GLFW_KEY_LEFT_SHIFT)); break; case 'C': rcc.rotate({{0,1,0}}, index, glfwGetKey(window, GLFW_KEY_LEFT_SHIFT)); break; case 'Z': rcc.rotate({{0,0,1}}, index, glfwGetKey(window, GLFW_KEY_LEFT_SHIFT)); break; case 'R': { static std::mt19937 engine(std::random_device{}()); std::uniform_int_distribution<int> axis(0, 2); std::uniform_int_distribution<int> index(0, SIZE - 1); std::uniform_int_distribution<bool> isPrime(false, true); for (int i = 0; i < 128; ++i) { std::array<int, 3> a = {}; a[axis(engine)] = 1; rcc.rotate(a, index(engine), isPrime(engine)); } break; } default: break; } } key[i] = true; } else { key[i] = false; } } //view glRotated(theta, 0, 1, 0); glRotated(iota, 1, 0, 0); //centralize glTranslated(-(SIZE - 1) * 2, -(SIZE - 1) * 2, -(SIZE - 1) * 2); rcc.run(); rcc.draw(); glPopMatrix(); glfwSwapBuffers(window); glfwPollEvents(); } glfwTerminate(); }
/* * Draw a cube * at (x,y,z) * dimentions (dx,dy,dz) * rotated th about the y axis */ static void cube(double x,double y,double z, double dx,double dy,double dz, double th) { // Set specular color to white float white[] = {1,1,1,1}; float black[] = {0,0,0,1}; glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,shinyvec); glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,white); glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,black); // Save transformation glPushMatrix(); // Offset, scale and rotate glTranslated(x,y,z); glRotated(th,0,1,0); glScaled(dx,dy,dz); // Cube glBegin(GL_QUADS); // Front glColor3f(1,0,0); glNormal3f( 0, 0, 1); glVertex3f(-1,-1, 1); glVertex3f(+1,-1, 1); glVertex3f(+1,+1, 1); glVertex3f(-1,+1, 1); // Back glColor3f(0,0,1); glNormal3f( 0, 0,-1); glVertex3f(+1,-1,-1); glVertex3f(-1,-1,-1); glVertex3f(-1,+1,-1); glVertex3f(+1,+1,-1); // Right glColor3f(1,1,0); glNormal3f(+1, 0, 0); glVertex3f(+1,-1,+1); glVertex3f(+1,-1,-1); glVertex3f(+1,+1,-1); glVertex3f(+1,+1,+1); // Left glColor3f(0,1,0); glNormal3f(-1, 0, 0); glVertex3f(-1,-1,-1); glVertex3f(-1,-1,+1); glVertex3f(-1,+1,+1); glVertex3f(-1,+1,-1); // Top glColor3f(0,1,1); glNormal3f( 0,+1, 0); glVertex3f(-1,+1,+1); glVertex3f(+1,+1,+1); glVertex3f(+1,+1,-1); glVertex3f(-1,+1,-1); // Bottom glColor3f(1,0,1); glNormal3f( 0,-one, 0); glVertex3f(-1,-1,-1); glVertex3f(+1,-1,-1); glVertex3f(+1,-1,+1); glVertex3f(-1,-1,+1); // End glEnd(); // Undo transofrmations glPopMatrix(); }
void drawscene(void) { int i; double angle, goepelang, wheelang; GLfloat ambientcolor[] = { 0.2, 0.2, 0.2, 1.0 }; GLfloat lightcolor[] = { 0.5, 0.5, 0.5, 1.0 }; GLfloat lightpos[] = { 0.0, 6.0, 0.0, 1.0 }; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); calcangles(time, rotspeed, &goepelang, &wheelang); glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientcolor); glLightfv(GL_LIGHT0, GL_DIFFUSE, lightcolor); glLightfv(GL_LIGHT0, GL_POSITION, lightpos); glTranslated(0.0, 0.0, -camdist); glRotated(camangle, 1.0, 1.0, 0.0); /* ground */ glPushMatrix(); glColor3d(0.3, 0.1, 0.0); glBegin(GL_QUADS); glNormal3d(0.0, 1.0, 0.0); glVertex3d(goepellen + 1, -wheelrad, -goepellen - 1); glVertex3d(goepellen + 1, -wheelrad, goepellen + 1); glVertex3d(-goepellen - 1, -wheelrad, goepellen + 1); glVertex3d(-goepellen - 1, -wheelrad, -goepellen - 1); glEnd(); glPopMatrix(); /* middle socket */ glPushMatrix(); glColor3d(0.0, 0.6, 0.0); glBegin(GL_TRIANGLE_FAN); glNormal3d(0.0, 1.0, 0.0); glVertex3d(0.0, goepelrad, 0.0); for (i = 0; i <= sockres; i++) { angle = 2 * M_PI * (double) i / (double) sockres; glVertex3d(sockrad * cos(angle), goepelrad, sockrad * sin(angle)); } glEnd(); glBegin(GL_QUAD_STRIP); for (i = 0; i <= wheelres; i++) { angle = 2 * M_PI * ((double) i - 0.5) / (double) sockres; glNormal3d(cos(angle), sin(angle), 0.0); angle = 2 * M_PI * (double) i / (double) sockres; glVertex3d(sockrad * cos(angle), -wheelrad, sockrad * sin(angle)); glVertex3d(sockrad * cos(angle), goepelrad, sockrad * sin(angle)); } glEnd(); glPopMatrix(); /* goepel arm */ glPushMatrix(); glRotated(goepelang, 0.0, 1.0, 0.0); glBegin(GL_QUADS); glNormal3d(0.0, -1.0, 0.0); glVertex3d(-goepelrad, -goepelrad, 0.0); glVertex3d(goepelrad, -goepelrad, 0.0); glVertex3d(goepelrad, -goepelrad, -goepellen); glVertex3d(-goepelrad, -goepelrad, -goepellen); glNormal3d(1.0, 0.0, 0.0); glVertex3d(goepelrad, -goepelrad, 0.0); glVertex3d(goepelrad, -goepelrad, -goepellen); glVertex3d(goepelrad, goepelrad, -goepellen); glVertex3d(goepelrad, goepelrad, 0.0); glNormal3d(0.0, 1.0, 0.0); glVertex3d(-goepelrad, goepelrad, 0.0); glVertex3d(goepelrad, goepelrad, 0.0); glVertex3d(goepelrad, goepelrad, -goepellen); glVertex3d(-goepelrad, goepelrad, -goepellen); glNormal3d(-1.0, 0.0, 0.0); glVertex3d(-goepelrad, -goepelrad, 0.0); glVertex3d(-goepelrad, -goepelrad, -goepellen); glVertex3d(-goepelrad, goepelrad, -goepellen); glVertex3d(-goepelrad, goepelrad, 0.0); glEnd(); glPopMatrix(); /* wheel */ glPushMatrix(); glColor3d(0.2, 0.2, 0.2); glRotated(goepelang, 0.0, 1.0, 0.0); glTranslated(0.0, 0.0, -goepellen); glRotated(wheelang, 0.0, 0.0, 1.0); glBegin(GL_TRIANGLE_FAN); glNormal3d(0.0, 0.0, 1.0); glVertex3d(0.0, 0.0, 0.0); for (i = 0; i <= wheelres; i++) { angle = 2 * M_PI * (double) i / (double) wheelres; glVertex3d(wheelrad * cos(angle), wheelrad * sin(angle), 0); } glEnd(); glBegin(GL_TRIANGLE_FAN); glNormal3d(0.0, 0.0, -1.0); glVertex3d(0.0, 0.0, -wheeldep); for (i = 0; i <= wheelres; i++) { angle = 2 * M_PI * (double) i / (double) wheelres; glVertex3d(wheelrad * cos(angle), wheelrad * sin(angle), -wheeldep); } glEnd(); glBegin(GL_QUAD_STRIP); for (i = 0; i <= wheelres; i++) { if (i % 2) glColor3d(0.0, 0.0, 0.0); else glColor3d(1.0, 1.0, 0.0); angle = 2 * M_PI * ((double) i - 0.5) / (double) wheelres; glNormal3d(cos(angle), sin(angle), 0.0); angle = 2 * M_PI * (double) i / (double) wheelres; glVertex3d(wheelrad * cos(angle), wheelrad * sin(angle), 0.0); glVertex3d(wheelrad * cos(angle), wheelrad * sin(angle), -wheeldep); } glEnd(); glPopMatrix(); glutSwapBuffers(); }
//! [10] void GLWidget::mouseMoveEvent(QMouseEvent *event) { /* int dx = event->x() - lastPos.x(); int dy = event->y() - lastPos.y(); if (event->buttons() & Qt::LeftButton) { setXRotation(xRot + 8 * dy); setYRotation(yRot + 8 * dx); } else if (event->buttons() & Qt::RightButton) { setXRotation(xRot + 8 * dy); setZRotation(zRot + 8 * dx); } lastPos = event->pos();*/ QPoint point = event->pos(); switch (m_dmMode) { case DM_ROTATION: { double diffx, diffy; diffx = (double)(point.x() - m_ptLast.x()); diffy = (double)(point.y() - m_ptLast.y()); double angleX, angleY; angleX = diffx * 0.5; angleY = diffy * 0.5; //wglMakeCurrent(m_hDC, m_hRC); // make rotation matrix glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotated(angleX, 0.0, 1.0, 0.0); glRotated(angleY, 1.0, 0.0, 0.0); glMultMatrixd(m_matRotation); glGetDoublev(GL_MODELVIEW_MATRIX, m_matRotation); //wglMakeCurrent(NULL, NULL); updateGL(); m_ptLast = point; break; } case DM_PAN: { double diffx, diffy; diffx = (double)(point.x() - m_ptLast.x()); diffy = (double)(point.y() - m_ptLast.y()); QRect rect; rect = this->rect(); m_dPan[X_AXIS] -= diffx / rect.width() * (m_arrViewBox[1] - m_arrViewBox[0]); m_dPan[Y_AXIS] += diffy / rect.height() * (m_arrViewBox[3] - m_arrViewBox[2]); //Invalidate(); updateGL(); m_ptLast = point; break; } } }
template< > inline void glRotate< double > (double angle,double x,double y, double z ) { glRotated(angle,x,y,z); };
void Display( ) { if( DebugOn != 0 ) { fprintf( stderr, "Display\n" ); } // set which window we want to do the graphics into: glutSetWindow( MainWindow ); // erase the background: glDrawBuffer( GL_BACK ); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glEnable( GL_DEPTH_TEST ); // specify shading to be flat: glShadeModel( GL_SMOOTH ); // set the viewport to a square centered in the window: GLsizei vx = glutGet( GLUT_WINDOW_WIDTH ); GLsizei vy = glutGet( GLUT_WINDOW_HEIGHT ); GLsizei v = vx < vy ? vx : vy; // minimum dimension GLint xl = ( vx - v ) / 2; GLint yb = ( vy - v ) / 2; glViewport( xl, yb, v, v ); // set the viewing volume: // remember that the Z clipping values are actually // given as DISTANCES IN FRONT OF THE EYE // USE gluOrtho2D( ) IF YOU ARE DOING 2D ! glMatrixMode( GL_PROJECTION ); glLoadIdentity( ); //if( WhichProjection == ORTHO ) // glOrtho( -3., 3., -3., 3., 0.1, 1000. ); //else gluPerspective( 90., 1., 0.1, 1000. ); // place the objects into the scene: glMatrixMode( GL_MODELVIEW ); glLoadIdentity( ); // set the eye position, look-at position, and up-vector: if (InLook == 0){ gluLookAt(0., 0., 20., 0., 0., 0., 0., 1., 0.); glRotatef((GLfloat)Yrot, 0., 1., 0.); glRotatef((GLfloat)Xrot, 1., 0., 0.); if (Scale < MINSCALE) Scale = MINSCALE; glScalef((GLfloat)Scale, (GLfloat)Scale, (GLfloat)Scale); } else if (InLook == 1) gluLookAt(-0.4, 1.8, -4.9, 0., 2., -14., 0., 1., 0.); // rotate the scene: // uniformly scale the scene: // set the fog parameters: if( DepthCueOn != 0 ) { glFogi( GL_FOG_MODE, FOGMODE ); glFogfv( GL_FOG_COLOR, FOGCOLOR ); glFogf( GL_FOG_DENSITY, FOGDENSITY ); glFogf( GL_FOG_START, FOGSTART ); glFogf( GL_FOG_END, FOGEND ); glEnable( GL_FOG ); } else { glDisable( GL_FOG ); } // possibly draw the axes: if( AxesOn != 0 ) { glColor3fv( &Colors[WhichColor][0] ); glCallList(AxesList); } if (ObjOn != 0) { glCallList(BoxList); } //draw the big 2 blades glPushMatrix(); glTranslatef(0., 2.9, -2.); //4 glRotatef(BladeAngle, 0., 1.,0.); //3 glScalef(5., 1., 1.); //2 glRotatef(90., 1., 0., 0.); //1 glBegin(GL_TRIANGLES); glVertex2f(BLADE_RADIUS, BLADE_WIDTH / 2. ); glVertex2f(0., 0.); glVertex2f(BLADE_RADIUS , -BLADE_WIDTH / 2.); glVertex2f(-BLADE_RADIUS , -BLADE_WIDTH / 2.); glVertex2f(0., 0.); glVertex2f(-BLADE_RADIUS , BLADE_WIDTH / 2.); glEnd(); glPopMatrix(); //draw small2 blades glPushMatrix(); glTranslatef(.5, 2.5, 9.); glRotated(3.*BladeAngle, 1., 0., 0.); glScalef(1.5, 1., 1.); glRotatef(90., 0., 1., 0.); glBegin(GL_TRIANGLES); glVertex2f(BLADE_RADIUS, BLADE_WIDTH / 2.); glVertex2f(0., 0.); glVertex2f(BLADE_RADIUS, -BLADE_WIDTH / 2.); glVertex2f(-BLADE_RADIUS, -BLADE_WIDTH / 2.); glVertex2f(0., 0.); glVertex2f(-BLADE_RADIUS, BLADE_WIDTH / 2.); glEnd(); glPopMatrix(); //Project2 draw the helicopter herer by inputting the heli.550 file if (WireframeOn == 1) { int i; struct edge *ep; struct point *p0, *p1; glPushMatrix(); glTranslatef(0., -1., 0.); glRotatef(97., 0., 1., 0.); glRotatef(-15., 0., 0., 1.); glBegin(GL_LINES); for (i = 0, ep = Heliedges; i < Helinedges; i++, ep++) { p0 = &Helipoints[ep->p0]; p1 = &Helipoints[ep->p1]; glVertex3f(p0->x, p0->y, p0->z); glVertex3f(p1->x, p1->y, p1->z); } glEnd(); glPopMatrix(); } else { int i; struct point *p0, *p1, *p2; struct tri *tp; float p01[3], p02[3], n[3]; glPushMatrix(); glTranslatef(0., -1., 0.); glRotatef(97., 0., 1., 0.); glRotatef(-15., 0., 0., 1.); glBegin(GL_TRIANGLES); for (i = 0, tp = Helitris; i < Helintris; i++, tp++) { p0 = &Helipoints[tp->p0]; p1 = &Helipoints[tp->p1]; p2 = &Helipoints[tp->p2]; /* fake "lighting" from above: */ p01[0] = p1->x - p0->x; p01[1] = p1->y - p0->y; p01[2] = p1->z - p0->z; p02[0] = p2->x - p0->x; p02[1] = p2->y - p0->y; p02[2] = p2->z - p0->z; Cross(p01, p02, n); Unit(n, n); n[1] = fabs(n[1]); n[1] += .25; if (n[1] > 1.) n[1] = 1.; glColor3f(0., n[1], 0.); glVertex3f(p0->x, p0->y, p0->z); glVertex3f(p1->x, p1->y, p1->z); glVertex3f(p2->x, p2->y, p2->z); } glEnd(); glPopMatrix(); } // draw the current object: //glCallList(BoxList); // draw some gratuitous text that just rotates on top of the scene: glDisable(GL_DEPTH_TEST); glColor3f(0., 1., 1.); // since we are using glScalef( ), be sure normals get unitized: glEnable( GL_NORMALIZE ); // draw some gratuitous text that is fixed on the screen: // // the projection matrix is reset to define a scene whose // world coordinate system goes from 0-100 in each axis // // this is called "percent units", and is just a convenience // // the modelview matrix is reset to identity as we don't // want to transform these coordinates glDisable( GL_DEPTH_TEST ); glMatrixMode( GL_PROJECTION ); glLoadIdentity( ); gluOrtho2D( 0., 100., 0., 100. ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity( ); glColor3f( 1., 1., 1. ); // swap the double-buffered framebuffers: glutSwapBuffers( ); // be sure the graphics buffer has been sent: // note: be sure to use glFlush( ) here, not glFinish( ) ! glFlush( ); }
template< > inline void glRotate< LDOUBL > (LDOUBL angle, LDOUBL x, LDOUBL y, LDOUBL z ) { glRotated((double)angle,(double)x,(double)y,(double)z); };
void GLWidget::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //material float ambient0[] = {0.0f, 0.0f, 0.0f, 1.0f}; float ambient1[] = {0.7f, 0.7f, 0.7f, 1.0f}; float ambient2[] = {0.8f, 0.8f, 0.2f, 1.0f}; float diffuse0[] = {0.0f, 0.0f, 0.0f, 1.0f}; float diffuse1[] = {0.1f, 0.5f, 0.8f, 1.0f}; float specular0[] = {0.0f, 0.0f, 0.0f, 1.0f}; float specular1[] = {1.0f, 1.0f, 1.0f, 1.0f}; float shininess0 = 0.0f; float shininess1 = 5.0f; float shininess2 = 100.0f; float emission0[] = {0.0f, 0.0f, 0.0f, 1.0f}; float emission1[] = {0.3f, 0.2f, 0.2f, 0.0f}; //ambient if(modelambient==0){ glMaterialfv(GL_FRONT, GL_AMBIENT, ambient0);} else if(modelambient==1){ glMaterialfv(GL_FRONT, GL_AMBIENT, ambient1);} else{ glMaterialfv(GL_FRONT, GL_AMBIENT, ambient2);} //diffuse if(modeldiffuse==0){ glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse0);} else{ glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse1);} //specular if(modelspecular==0){ glMaterialfv(GL_FRONT, GL_SPECULAR, specular0);} else{ glMaterialfv(GL_FRONT, GL_SPECULAR, specular1);} //shininess if(modelshininess==0){ glMaterialf(GL_FRONT, GL_SHININESS, shininess0);} else if(modelshininess==1){ glMaterialf(GL_FRONT, GL_SHININESS, shininess1);} else{ glMaterialf(GL_FRONT, GL_SHININESS, shininess2);} //emission if(modelemission==0){ glMaterialfv(GL_FRONT, GL_EMISSION, emission0);} else{ glMaterialfv(GL_FRONT, GL_EMISSION, emission1);} //effect if(lighteffect==1){ glColorMaterial(GL_FRONT, GL_AMBIENT);} else if(lighteffect==2){ glColorMaterial(GL_FRONT, GL_DIFFUSE);} else if(lighteffect==10){glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);} //SNOWMAN -------------------------------------------------------------------------------- GLUquadricObj *quadric; quadric = gluNewQuadric(); gluQuadricDrawStyle(quadric, GLU_FILL ); glEnable(GL_COLOR_MATERIAL); glPushMatrix(); glRotated(xRot / 16.0, 1.0, 0.0, 0.0); glRotated(yRot / 16.0, 0.0, 1.0, 0.0); glRotated(zRot / 16.0, 0.0, 0.0, 1.0); rotating(); //head glColor3f(1.0f, 1.0f, 1.0f); glTranslatef(0.0f,4.0f,0.0f); gluSphere(quadric, 1.0f, 50, 50); //left eyes glColor3f(0.0f, 0.0f, 0.0f ); glTranslatef(0.3f,0.3f,1.0f); gluSphere(quadric, 0.15f, 50, 50); glTranslatef(-0.3f,-0.3f,-1.0f); //right eyes glColor3f(0.0f, 0.0f, 0.0f ); glTranslatef(-0.3f,0.3f,1.0f); gluSphere(quadric, 0.15f, 50, 50); glTranslatef(0.3f,-0.3f,-1.0f); //nose glColor3f(1.0f, 0.549f, 0.0f); glTranslatef(0.0f,0.0f,1.0f); drawCylinder(1.0f,0.2f,0.01f,50,50); //body glColor3f(1.0f, 0.0f, 0.0f); glTranslatef(0.0f,-2.5f,-1.0f); gluSphere(quadric, 2.0f, 50, 50); //button glColor3f(0.0f, 0.0f, 0.0f ); glTranslatef(0.0f,1.0f,1.7f); gluSphere(quadric, 0.2f, 50, 50); glTranslatef(0.0f,-1.0f,0.3f); gluSphere(quadric, 0.2f, 50, 50); glTranslatef(0.0f,-1.0f,-0.3f); gluSphere(quadric, 0.2f, 50, 50); glTranslatef(0.0f,1.0f,-1.7f); //left hand glColor3f(0.545f, 0.271f, 0.075f); glTranslatef(1.5f,1.0f,0.0f); glRotated(90,0.0,1.0,0.0); glRotated(-30,1.0,0.0,0.0); drawCylinder(2.5f,0.2f,0.2,50,50); glRotated(30,1.0,0.0,0.0); glRotated(-90,0.0,1.0,0.0); //right hand glTranslatef(-3.0f,0.0f,0.0f); glRotated(-90,0.0,1.0,0.0); glRotated(-30,1.0,0.0,0.0); drawCylinder(2.5f,0.2f,0.2,50,50); glRotated(30,1.0,0.0,0.0); glRotated(90,0.0,1.0,0.0); //bottom glColor3f(0.0f, 1.0f, 0.0f); glTranslatef(1.5f,-5.0f,0.0f); gluSphere(quadric, 3.0f, 50, 50); glPopMatrix(); //hat glColor3f(0.0f, 0.0f, 1.0f ); glPushMatrix(); glRotated(xRot / 16.0, 1.0, 0.0, 0.0); glRotated(yRot / 16.0, 0.0, 1.0, 0.0); glRotated(zRot / 16.0, 0.0, 0.0, 1.0); rotating(); glTranslatef(0.0f,6.0f,0.0f); glRotatef(90,1.0,0.0,0.0); drawCylinder(1.25f, 0.8f, 0.7f, 50,50); glTranslatef(0.0f,0.0f,1.15f); drawCylinder(0.1f, 1.2f, 1.2f, 50,50); glPopMatrix(); glDisable(GL_COLOR_MATERIAL); }
void MyGLWidget::paintGL(){ // glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glClearColor(0,0,0,1); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45,1,1,1000); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslated(0,-2,-10); glScaled(0.5,0.5,0.5); glRotated(x_axis/16,1,0,0); glRotated(y_axis/16,0,1,0); glRotated(z_axis/16,0,0,1); // PFAD "den man nicht mehr sieht" glBegin(GL_LINE_STRIP); for(int i=0;i<15;) { glColor3f(0,0,1); glVertex3d(loftPath[i],loftPath[i+1],loftPath[i+2]); i+=3; } glEnd(); // Deckel Unten glBegin(GL_TRIANGLE_FAN); glColor3f(0,1,0); for(int i=15; i >=0; i-=3) { glVertex3f(loftShape[i],loftShape[i+1],loftShape[i+2]); } glVertex3f(loftShape[15],loftShape[16],loftShape[17]); glEnd(); // Rohr winkel = 0; glBegin(GL_TRIANGLE_STRIP); glColor3f(1,0,0); for(int i=0;i<15;i+=3) { swap(i); for(int j=17;j>=0;j-=3) { glVertex3d(O[j-2],O[j-1],O[j]); glVertex3d(N[j-2],N[j-1],N[j]); } glVertex3d(O[15],O[16],O[17]); glVertex3d(N[15],N[16],N[17]); } glEnd(); // Deckel Oben glBegin(GL_TRIANGLE_FAN); glColor3f(0,1,0); for(int i=0; i <18; i+=3) { glVertex3f(N[i],N[i+1],N[i+2]); } glVertex3f(N[0],N[1],N[2]); glEnd(); }
// Initializes the projection and modelview matrices bool Camera::Draw(int drawmode /* = 0 */) { glMatrixMode(GL_PROJECTION); //glLoadIdentity(); if (shifting > 0) { shifting--; rotX+=theta_shift; rotY+=phi_shift; rho*=rho_shift; if (rotY < EPS) // avoid singularities using EPS constant. rotY = EPS; if (rotY > PI-EPS) rotY = PI-EPS; if (rho < EPS) rho = EPS; } triple right(0, 0, 0); triple up(upvector[X], upvector[Y], upvector[Z]); triple pos(lastCamera[X], lastCamera[Y], lastCamera[Z]); triple target(Xoffset, Yoffset, Zoffset); triple dir = (target - pos); double L, R, B, T, wd2, ndfl, rad = (angle / 360.0) * PI; double focal = length(dir) * focalRatio; // 4.0 if (projo == CAM_LEFTEYE || projo == CAM_RIGHTEYE) { normalize(up); normalize(dir); right = crossProduct(dir, up); normalize(right); right = right * eyeSeparation / 2.0; wd2 = znear * tan(rad); ndfl = znear / focal; } switch (projo) { case CAM_PERSPECTIVE: gluPerspective(angle, aspect, znear, zfar); break; case CAM_ORTHO: glOrtho(aspect*-rho, aspect*rho, -rho, rho, -zfar, zfar); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); break; case CAM_ORTHO2: // deprecated glOrtho(0, 110, -0.5, 107.5, -zfar, zfar); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotated(-90, 1, 0, 0); break; case CAM_LEFTEYE: glLoadIdentity(); L = -aspect*wd2 + 0.5*eyeSeparation*ndfl; R = aspect*wd2 + 0.5*eyeSeparation*ndfl; T = wd2; B = -wd2; glFrustum(L, R, B, T, znear, zfar); break; case CAM_RIGHTEYE: glLoadIdentity(); L = -aspect*wd2 - 0.5*eyeSeparation*ndfl; R = aspect*wd2 - 0.5*eyeSeparation*ndfl; T = wd2; B = -wd2; glFrustum(L, R, B, T, znear, zfar); break; } if (projo == CAM_LEFTEYE) right = right * -1; theta = rotX; phi = rotY; double sphereX = (rho*sin(phi) * cos(theta)); double sphereY = (rho*cos(phi)); double sphereZ = (rho*sin(phi) * sin(theta)); // from spherical coordinates (rho, theta, phi) if (input) { lastCamera[X] = sphereX + Xoffset; lastCamera[Y] = sphereY + Yoffset; lastCamera[Z] = sphereZ + Zoffset; } else { sphereX = lastCamera[X] - Xoffset; sphereY = lastCamera[Y] - Yoffset; sphereZ = lastCamera[Z] - Zoffset; } glMatrixMode(GL_MODELVIEW); glLoadIdentity(); if (!((sphereX == 0) && (sphereZ == 0))) { gluLookAt( lastCamera[X]+right.x, lastCamera[Y]+right.y, lastCamera[Z]+right.z, Xoffset+right.x, Yoffset+right.y, Zoffset+right.z, 0, 1, 0); //upvector[X], upvector[Y], upvector[Z]); } else { // gluLookAt doesn't work when viewer is incident to up-vector // Just orient manually in this case if ((lastCamera[Y] - Yoffset) >= 0) { glRotated(-90, 0, 1, 0); glRotated(-90, 0, 0, 1); } else { glRotated(-90, 0, 1, 0); glRotated(90, 0, 0, 1); } glTranslated(-Xoffset, -lastCamera[Y], -Zoffset); } if (upvector[X]==1) glRotatef(90, 0, 0, 1); if (upvector[Z]==1) glRotatef(-90, 1, 0, 0); return true; }
void rotate(double degrees, const Vector &axis) { glRotated(degrees,axis.x,axis.y,axis.z); }
void createSlideSections() { double slideLegOffCenter = slideLegRadius + .05; double legPosx = slideTopWidth - slideLegOffCenter; double legPosz = slideTopLength - slideLegOffCenter; slideSphere = gluNewQuadric(); slideBase = glGenLists(1); glNewList(slideBase, GL_COMPILE); glPushMatrix(); //platform glColor3f(0,0,1); glCallList(slidePlatform); //legs glTranslated(0,0-slideLegLongh,0); glPushMatrix(); glTranslated(legPosx,0,legPosz); glCallList(slideLegLong); glPopMatrix(); glPushMatrix(); glTranslated(0-legPosx,0,legPosz); glCallList(slideLegLong); glPopMatrix(); glPushMatrix(); glTranslated(0-legPosx,0,0-legPosz); glCallList(slideLegLong); glPopMatrix(); glPushMatrix(); glTranslated(legPosx,0,0-legPosz); glCallList(slideLegLong); glPopMatrix(); glPopMatrix(); glEndList(); double angle = stepAngle * PI / 180; //convert to radians double reflect[] = {-1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1}; slideHandleSide = glGenLists(1); glNewList(slideHandleSide, GL_COMPILE); glPushMatrix(); glColor3f(.5,.5,.5); glCallList(slideHandleTopSide); glTranslated(0,0,slideTopLength - handleRadius); gluSphere(slideSphere, handleRadius, 10, 10); glTranslated(0, 0-handleHeight,0); glCallList(slideHandleSupport); glTranslated(0,0,-2*(slideTopLength - handleRadius)); glCallList(slideHandleSupport); glTranslated(0,handleHeight,0); gluSphere(slideSphere, handleRadius, 10, 10); glTranslated(handleFrontLength,0,0); glCallList(slideHandleBack); glTranslated(handleFrontLength,0,0); gluSphere(slideSphere, handleRadius,10,10); glCallList(slideHandleStep); glTranslated(0,(0-stepSideHeight) * cos(angle), (0-stepSideHeight) * sin(angle)); glCallList(slideHandleBottom); glTranslated(0,(0-stepSideHeight) * cos(angle), (0-stepSideHeight) * sin(angle)); gluSphere(slideSphere, handleRadius, 10,10); glCallList(slideHandleBottom); glPopMatrix(); glEndList(); slideHandles = glGenLists(1); glNewList(slideHandles, GL_COMPILE); glPushMatrix(); glTranslated(0-slideTopWidth + handleRadius,0,0); glCallList(slideHandleSide); glTranslated(slideTopWidth - handleRadius,0,slideTopLength - handleRadius); glCallList(slideHandleTopFront); glTranslated(slideTopWidth - handleRadius,0, 0-slideTopLength + handleRadius); glMultMatrixd(reflect); glCallList(slideHandleSide); glPopMatrix(); glEndList(); double stepDif = 1; double stepDifz = stepDif * sin(angle); double stepDify = stepDif * cos(angle); int count; slideLadder = glGenLists(1); glNewList(slideLadder, GL_COMPILE); glColor3f(0,0,1); glPushMatrix(); glTranslated(slideWidth, 0, 0); glCallList(slideStepSide); glTranslated(-2*(slideWidth),0,0); glCallList(slideStepSide); glPopMatrix(); glPushMatrix(); for(count = 0; count < 9; count++) { glTranslated(0,0-stepDify, 0-stepDifz); glCallList(slideStep); } glPopMatrix(); glEndList(); slideFront = glGenLists(1); glNewList(slideFront, GL_COMPILE); glPushMatrix(); glColor3f(.5,.5,.5); glRotated(slideAngle,1,0,0); glCallList(slideInc); glTranslated(0,0,slideIncLength * 2); glCallList(slideCurve); glCallList(slideBottom); glPopMatrix(); glEndList(); }
//Функция рендера void GlutViewer::renderScene() { if (cloud == NULL) return; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glPushMatrix(); //Трансформации сцены glRotated(rotX, 1, 0, 0); glRotated(rotY, 0, 1, 0); glRotated(rotZ, 0, 0, 1); glTranslated(posX, posY, posZ); glScaled(scale, scale, scale); glBegin(GL_POINTS); glVertex3f(0, 0, 0); //Отрисовываем модель Color color(0, 0, 0); for (int i = 0; i < cloud->ChildrenCount(); i++) { BaseObject3D* object = cloud->GetChild(i); if (object != NULL) { if (object->GetType() == BaseObject3D::TYPE_OBJECT) { color.R += 0.5; if (color.R > 1) { color.R = 0; color.B += 0.5; } if (color.G > 1) { color.G = 0; color.B += 0.5; } if (color.B > 1) color.B = 0; renderObject(dynamic_cast<Object3D*>(object), color); } else if (object->GetType() == BaseObject3D::TYPE_POINT) { cv::Vec3f coord = object->GetCoord(); glVertex3f(coord[0]/5.0, coord[1]/5.0, coord[2]/5.0); } } } //glVertex3f(0.5, 0, 0); //glVertex3f(0, 0.5, 0); //glVertex3f(0, 0, 0.5); glEnd(); glutSwapBuffers(); }
void initDisplayLists() { double endCenter = monkeyLength - monkeyThick; int count; createCubeList(); createCylinderList(7); glMatrixMode(GL_MODELVIEW); createSlideParts(); // pliminary createSlideSections(); slide = glGenLists(1); glNewList(slide, GL_COMPILE); glPushMatrix(); glCallList(slideHandles); glTranslated(0,-2*handleHeight,0); glCallList(slideBase); glTranslated(0,0,0-slideTopLength); glCallList(slideLadder); glTranslated(0,0,2*slideTopLength); glCallList(slideFront); glPopMatrix(); glEndList(); createSwingParts(); //swing frames swingSupport = glGenLists(1); glNewList(swingSupport, GL_COMPILE); glPushMatrix(); glColor3f(1,0,1); glCallList(horizPole); glPushMatrix(); glRotated(30,0,0,1); glCallList(vertPole); glRotated(-60,0,0,1); glCallList(vertPole); glPopMatrix(); glCallList(swingEnd); glRotated(180,0,1,0); glCallList(swingEnd); glPopMatrix(); glEndList(); // swing seats, separate to allow animation swing = glGenLists(1); glNewList(swing, GL_COMPILE); glPushMatrix(); glColor3f(.5,.5,.5); glTranslated(0,-1*stringLength - poleRadius, seatLength); glCallList(swingSide); glTranslated(0,0,-2*seatLength); glCallList(swingSide); glColor3f(1,0,0); glTranslated(0,-1*stringLength, seatLength); glCallList(swingSeat); glPopMatrix(); glEndList(); // monkeybars createMonkeyBarsParts(); monkeyBars = glGenLists(1); glNewList(monkeyBars, GL_COMPILE); glColor3f(.65,.5,.4); glPushMatrix(); glTranslated(0,0-monkeyHeight,0); glPushMatrix(); glTranslated(0,0,monkeyWidth); glCallList(monkeySide); glTranslated(0,0,-2*monkeyWidth); glCallList(monkeySide); glPopMatrix(); glPushMatrix(); glTranslated(endCenter,0,0); glCallList(bar); for(count = 0; count < 3; count++) { glTranslated(0,-1,0); glCallList(bar); } // other side glTranslated(-2 *endCenter,0,0); glCallList(bar); for(count = 0; count < 3; count++) { glTranslated(0,1,0); glCallList(bar); } //top glTranslated(.5 - monkeyThick ,monkeyHeight - monkeyThick,0); glCallList(bar); for(count = 0; count < 9; count++) { glTranslated(1,0,0); glCallList(bar); } glPopMatrix(); glPopMatrix(); glEndList(); }
void NosuchGraphics::rotate(double degrees) { glRotated(-degrees,0.0f,0.0f,1.0f); }