📄 billiards.cpp
字号:
// Base glBindTexture(GL_TEXTURE_2D, theTexture[WOOD]); glPushMatrix(); glTranslatef(0.0, -1.4, 0.0); glScalef(1.5, 1.0, 2.9); glRotatef(45.0, 0.0, 1.0, 0.0); glRotatef(90.0, 1.0, 0.0, 0.0); gluCylinder(pillarCylinder, tableWidth/2, tableWidth/2 - 2, 5, 4, 2); glPopMatrix(); // Legs glBindTexture(GL_TEXTURE_2D, theTexture[DARK_WOOD]); for (int g=-1; g <= 1; g+=2) { for (int d=-1; d <= 1; d+=2) { glPushMatrix(); glTranslatef(tableWidth/(g*2.0) + g*(-5.0), -20.0, tableLength/(d*2.0) + d*(-5.0)); glCallList(displayList[4]); glPopMatrix(); } } glEndList();}// Computes the stairs using a ton of adjustable settings (will document their use maybe later)void renderStairs(GLint orientation, GLfloat stepCount, GLfloat stepWidth, GLfloat stepWidthDecrease, GLfloat totalHeight, GLuint &myFlatTexture, GLuint &myStepTexture) { glPushMatrix(); GLfloat t = 0.0, curve = 0.0, nextCurve = 0.0, groundHeight = 0, groundIncrease = totalHeight/stepCount, depth = 70.0, moveRight = 0.0; GLfloat spreadFactor = 0.2, minRange = -1.0*sqrt(depth/spreadFactor)+moveRight, maxRange = sqrt(depth/spreadFactor)+moveRight; vector3 originVector = vector3(0.0, 0.0, 0.0); // Half-flight of stairs climbing from right to left if (orientation == 0) { maxRange = 0.0; // Half-flight of stairs climbing from left to right } else if (orientation == 1) { minRange = 0.0; groundHeight = stepCount*groundIncrease + 2.0*groundIncrease; stepWidth = stepWidth - stepCount*stepWidthDecrease; stepWidthDecrease *= -1.0; groundIncrease *= -1.0; } GLfloat incr = (sqrt((maxRange - minRange)*(maxRange - minRange)))/(stepCount+1); if (orientation == 1) { maxRange = maxRange - incr; minRange = 0.0 - 2.0*incr; minRange = minRange + incr; } else if (orientation == 0) { } else if (orientation == 2) { groundHeight = stepCount*groundIncrease + 2.0*groundIncrease; stepWidth = stepWidth - stepCount*stepWidthDecrease; stepWidthDecrease *= -1.0; groundIncrease *= -1.0; } t = minRange; for (t; t < maxRange; t+= incr) { curve = spreadFactor*((-1.0*(t - moveRight)*(t-moveRight))) + depth; nextCurve = spreadFactor*((-1.0*(t + incr - moveRight)*(t + incr -moveRight))) + depth; vector3 stepNormalV = vector3(0.0,0.0,0.0); stepNormalV.x = curve - (spreadFactor*((-1.0*(t + incr - moveRight)*(t+incr-moveRight))) + depth); stepNormalV.y = 0.0; stepNormalV.z = incr; GLfloat length = sqrt( (stepNormalV.x*stepNormalV.x) + (stepNormalV.y*stepNormalV.y) + (stepNormalV.z*stepNormalV.z) ); stepNormalV = stepNormalV * (1/length); stepNormalV = stepNormalV * stepWidth; GLfloat stairs1[3] = {stepNormalV.x + originVector.x, groundHeight + groundIncrease, stepNormalV.z + originVector.z}; GLfloat stairs2[3] = {originVector.x, groundHeight + groundIncrease, originVector.z}; GLfloat stairs3[3] = {originVector.x, groundHeight, originVector.z}; GLfloat stairs4[3] = {stepNormalV.x + originVector.x, groundHeight, stepNormalV.z + originVector.z}; GLfloat stairs6[3] = {t+incr, groundHeight + groundIncrease, nextCurve}; GLfloat stairs7[3] = {t+incr, groundHeight, nextCurve}; originVector.x = stairs6[0]; originVector.y = stairs6[1]; originVector.z = stairs6[2]; curve = spreadFactor*((-1.0*(t+incr - moveRight)*(t+incr-moveRight))) + depth; nextCurve = spreadFactor*((-1.0*(t + incr+incr - moveRight)*(t +incr+ incr -moveRight))) + depth; stepWidth = stepWidth - stepWidthDecrease; stepNormalV = vector3(0.0,0.0,0.0); stepNormalV.x = curve - (spreadFactor*((-1.0*(t+incr + incr - moveRight)*(t+incr+incr-moveRight))) + depth); stepNormalV.y = 0.0; stepNormalV.z = incr; length = sqrt( (stepNormalV.x*stepNormalV.x) + (stepNormalV.y*stepNormalV.y) + (stepNormalV.z*stepNormalV.z) ); stepNormalV = stepNormalV * (1/length); stepNormalV = stepNormalV * stepWidth; GLfloat stairs5[3] = {stepNormalV.x + originVector.x, groundHeight + groundIncrease, stepNormalV.z + originVector.z}; groundHeight = groundHeight + groundIncrease; if (t != minRange) { glBindTexture(GL_TEXTURE_2D, myStepTexture); if (orientation == 0) { renderQuad(stairs1, stairs2, stairs3, stairs4, 1.0, 1.0, 1.0); } else { renderQuad(stairs1, stairs2, stairs3, stairs4, -1.0, 1.0, 1.0); } if ((orientation == 1) && (t > maxRange-incr)) { } else { glBindTexture(GL_TEXTURE_2D, myFlatTexture); renderQuad(stairs1, stairs2, stairs6, stairs5, -1.0, 1.0, 1.0); } } } glPopMatrix();}// Renders the balcony (stairs plus red screen) to screenvoid renderBalcony() { glNewList(displayList[3], GL_COMPILE); glPushMatrix(); glTranslatef(0.0,0.0,-100.0); glRotatef(145, 0.0, 1.0, 0.0); renderCurve(70.0, 90.0, 1.945, 16, -1, theTexture[RED_LIGHT]); glPopMatrix(); glPushMatrix(); glTranslatef(0.0,0.0,-100.0); glRotatef(180.0, 0.0, 1.0, 0.0); glPushMatrix(); glTranslatef(-45.0, -3.0, 10.0); renderStairs(0, 15.0, 60.0, 1.5, 60.0, theTexture[RED_CARPET], theTexture[BLACK]); glPopMatrix(); glPushMatrix(); glTranslatef(45.0, 0.0, 0.0); renderStairs(1, 15.0, 60.0, 1.5, 60.0, theTexture[RED_CARPET], theTexture[BLACK]); glPopMatrix(); glPopMatrix(); glEndList();}// Renders the rest of the room (minus balcony, floot and table) to screenvoid renderRoom() { glNewList(displayList[2], GL_COMPILE); GLfloat height= 90.0; // Roof glPushMatrix(); glTranslatef(0.0, 90.0 ,0.0); glRotatef(-90.0, 1.0, 0.0, 0.0); glRotatef(22.5, 0.0, 0.0, 1.0); glBindTexture(GL_TEXTURE_2D, theTexture[ROOF]); gluCylinder(pillarCylinder, 230.0, 0.0, 50, 8, 1); glPopMatrix(); // Windows glPushMatrix(); glRotatef(292.5, 0.0, 1.0, 0.0); renderCurve(230, height, (M_PI/4.0), 1, -1, theTexture[SEA_VIEW]); glRotatef(-45, 0.0, 1.0, 0.0); renderCurve(230, height, (M_PI/4.0), 1, -1, theTexture[SEA_VIEW2]); glPopMatrix(); glPushMatrix(); glTranslatef(165, 20.0, -25.0); glRotatef(22.5, 0.0, 1.0, 0.0); renderCurve(50, 50, (M_PI/4.0), 1, -1, theTexture[PAINTING2]); glTranslatef(-20, 0.0, 50.0); renderCurve(50, 50, (M_PI/4.0), 1, -1, theTexture[PAINTING1]); glPopMatrix(); glPushMatrix(); glRotatef(202.5, 0.0, 1.0, 0.0); // Walls renderCurve(230.0, height, ((2*M_PI) - (M_PI/4.0)), 7, -1, theTexture[BLUE_WALL]); // Ceiling glPushMatrix(); glTranslatef(0.0,90.0,0.0); renderCap(135.0, 230.0, M_PI*2.0, M_PI*2.0, 8, theTexture[BLACK]); renderCurve(135.0, 20.0, M_PI*2.0, 8, -1, theTexture[RED_MARBLE]); glPopMatrix(); glPushMatrix(); glTranslatef(0.0,0.5,0.0); renderCap(135.0, 230.0, M_PI*2.0, M_PI*2.0, 8, theTexture[BLACK]); glPopMatrix(); // Pillars for (GLfloat t=0.0; t < M_PI*2.0; t+= M_PI*2.0/8.0) { glBindTexture(GL_TEXTURE_2D, theTexture[DARKBLUE_MARBLE]); // Pillars (far) glPushMatrix(); glTranslatef(223.0*cos(t), height, 223.0*sin(t)); glRotatef(90.0, 1.0, 0.0, 0.0); glutSolidTorus(5.0, 6.0, 8, 12); gluCylinder(pillarCylinder, 7.0, 7.0, height, 16, 1); glPopMatrix(); glPushMatrix(); glTranslatef(223.0*cos(t), 1.0, 223.0*sin(t)); glRotatef(90.0, 1.0, 0.0, 0.0); glutSolidTorus(5.0, 6.0, 8, 12); glPopMatrix(); glBindTexture(GL_TEXTURE_2D, theTexture[BEIGE_WALL]); // Pillars (near) glPushMatrix(); glTranslatef(145.0*cos(t), height, 145.0*sin(t)); glRotatef(90.0, 1.0, 0.0, 0.0); glutSolidTorus(1.5, 2.5, 8, 12); gluCylinder(pillarCylinder, 3.0, 3.0, height, 16, 1); glPopMatrix(); glPushMatrix(); glTranslatef(145.0*cos(t), 1.0, 145.0*sin(t)); glRotatef(90.0, 1.0, 0.0, 0.0); glutSolidTorus(1.5, 2.5, 8, 12); glPopMatrix(); } glPopMatrix(); glEndList();}// Renders the cue stick to screenvoid renderCueStick() { glPushMatrix(); updateTarget(); updateCamera(); glTranslatef(camera[3], camera[4], camera[5]); glRotatef(theta*180.0/M_PI*(-1.0) + 90, 0.0, 1.0 ,0.0); glRotatef(-11.5, 1.0, 0.0 ,0.0); glTranslatef(0.0, 0.0, power); glBindTexture(GL_TEXTURE_2D, theTexture[CUE]); gluCylinder(ballQuadric, 0.35, 1.0, 50.0, 8, 1); glPopMatrix();}// Draws the guiding/aiming line to screenvoid drawGuideLine() { GLfloat guideLineLength = -1000; GLfloat targetX = guideLineLength*(cos(theta)) + camera[3]; GLfloat targetZ = guideLineLength*(sin(theta)) + camera[5]; if (targetX < minX) { targetX = minX; guideLineLength = (targetX - camera[3]) / cos(theta); targetZ = guideLineLength*(sin(theta)) + camera[5]; } if (targetX > maxX) { targetX = maxX; guideLineLength = (targetX - camera[3]) / cos(theta); targetZ = guideLineLength*(sin(theta)) + camera[5]; } if (targetZ < minZ) { targetZ = minZ; guideLineLength = (targetZ - camera[5]) / sin(theta); targetX = guideLineLength*(cos(theta)) + camera[3]; } if (targetZ > maxZ) { targetZ = maxZ; guideLineLength = (targetZ - camera[5]) / sin(theta); targetX = guideLineLength*(cos(theta)) + camera[3]; } glPushMatrix(); glDisable(GL_TEXTURE_2D); glDisable(GL_LIGHTING); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glColor4f(0.61, 0.61, 0.06, 0.75); glBegin(GL_LINES); glVertex3f(camera[3], camera[4], camera[5]); glVertex3f(targetX, camera[4], targetZ); glEnd(); glDisable(GL_BLEND); glEnable(GL_LIGHTING); glEnable(GL_TEXTURE_2D); glPopMatrix();}// Draws a text string on screenvoid renderBitmapString(float x, float y, void *font, char* c) { glRasterPos2f(x, y); for (c; *c != '\0'; c++) { glutBitmapCharacter(font, *c); }}// Draws the end-of-game screenvoid drawEndScreen() { switchToOrtho(); glDisable(GL_TEXTURE_2D); glDisable(GL_LIGHTING); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glColor4f(0.1, 0.1, 0.1, 0.7); glBegin(GL_POLYGON); glVertex2f(0, parentWindowHeight); glVertex2f(parentWindowWidth, parentWindowHeight); glVertex2f(parentWindowWidth, 0); glVertex2f(0, 0); glEnd(); glColor4f(1.0, 1.0, 1.0, 1.0); renderBitmapString(parentWindowWidth / 2 - 40, parentWindowHeight/2 - 75,(void *)largeFont, endGame); renderBitmapString(parentWindowWidth / 2 - 5, parentWindowHeight/2 - 60,(void *)font, "Game Over"); renderBitmapString(parentWindowWidth / 2 - 18,parentWindowHeight/2 - 45,(void *)font, "Play again? (Y/N)"); glDisable(GL_BLEND); glEnable(GL_LIGHTING); glEnable(GL_TEXTURE_2D); switchFromOrtho();}// Renders the user interface to screenvoid drawUI() { GLfloat displayOffset = parentWindowWidth - 5; char playerBuffer[100]; char shotBuffer[20]; sprintf(shotBuffer, "%d", players[currentPlayer].noOfShots); switchToOrtho(); glDisable(GL_LIGHTING); glDisable(GL_TEXTURE_2D); glColor3f(1.0,1.0,1.0); renderBitmapString(80, 15, (void *)font, "Player: "); renderBitmapString(80, 30, (void *)font, "Team: "); renderBitmapString(80, 45, (void *)font, "Shots: "); glColor3f(0.8,0.8,0.8); renderBitmapString(130, 15, (void *)font, players[currentPlayer].name); if (players[currentPlayer].side == 0) renderBitmapString(130, 30, (void *)font, "Solids"); else if (players[currentPlayer].side == 1) renderBitmapString(130, 30, (void *)font, "Stripes"); else renderBitmapString(130, 30, (void *)font, "Undetermined"); renderBitmapString(130, 45, (void *)font, shotBuffer); glColor3f(1.0,1.0,1.0); glEnable(GL_TEXTURE_2D); // Render portrait glBindTexture(GL_TEXTURE_2D, theTexture[players[currentPlayer].id + 32]); glBegin(GL_POLYGON); glTexCoord2f(0.0, 1.0); glVertex2f(5, 5); glTexCoord2f(1.0, 1.0); glVertex2f(69, 5); glTexCoord2f(1.0, 0.0); glVertex2f(69, 69); glTexCoord2f(0.0, 0.0); glVertex2f(5, 69); glEnd(); // Render power-indicator-bar if (shootMode == true) { glBindTexture(GL_TEXTURE_2D, theTexture[POWER_BAR]); glBegin(GL_POLYGON); glTexCoord2f(0.0, 1.0); glVertex2f(5, 75); glTexCoord2f(power/35, 1.0); glVertex2f(5+power*4, 75); glTexCoord2f(power/35, 0.88); glVertex2f(5+power*4, 90); glTexCoord2f(0.0, 0.88); glVertex2f(5, 90); glEnd(); glDisable(GL_TEXTURE_2D); glLineWidth(2.5f); glBegin(GL_LINE_LOOP); glVertex2f(5,75); glVertex2f(145,75); glVertex2f(145,90); glVertex2f(5,90); glEnd(); glLineWidth(1.0f); glEnable(GL_TEXTURE_2D); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -