#Molly Fane --- Fall, 2015 --- Math 198 #This program graphs a 3D surface using openGL, pygame, and python. #The input is a function with only real values on the specified domain of x and y, solved for z #the input must be python-readable, so you must use pow(x,2) instead of x^2. #You can also use the functions from math, such as math.sin() or math.pi import pygame from pygame.locals import * from OpenGL.GL import * from OpenGL.GLU import * vertices = [] edges = [] #----------------------------------------------- import math #define range for y yInit = -2 yRange = 4 yFin = yInit + yRange deltaY = .2 y = yInit #define range for x xInit = -2 xRange = 4 xFin = xInit + xRange deltaX = .1 x = xInit #defines z as in z = f(x,y) z = 0 funcIn = input("z = ")#asks for input function -- MUST USE math.sin, math.cosh... etc! #holds x constant, then draws that trace as y increases by deltaY until y goes from yInit to yFin #.. then repeats this process for x = x + deltaX until x goes from xInit to xFin while(x < deltaX + xFin): #loop to hold x contsant and then make a trace in zy-plane and then increase x y = yInit while(y < deltaY + yFin): # <--- loop that makes vertices for trace in zy-plane z = eval(funcIn) vertices.append([x,y,z]) y = y + deltaY x = x + deltaX #--------------------------------------------- def drawEdges(): vertex = 0 edge = 0 glBegin(GL_LINES) while(vertex < len(vertices)-1): if(vertices[vertex][0]== vertices[vertex+1][0]): glVertex3fv(vertices[vertex]) glVertex3fv(vertices[vertex+1]) vertex = vertex + 1 glEnd() def drawAxis(): glBegin(GL_LINES) glColor3f(1.0,0.0,0.0) glVertex3f(0-xRange/2,0,0)#sets x axis glVertex3f(xRange/2,0,0) glColor3f(0.0,1.0,0.0) glVertex3f(0,0-xRange/2,0)#sets y axis glVertex3f(0,xRange/2,0) glColor3f(0.0,0.0,1.0) glVertex3f(0,0,0-xRange/2)#sets z axis glVertex3f(0,0,xRange/2) glColor3f(1.0,1.0,1.0) glEnd() def main(): pygame.init() display = (1000,800) pygame.display.set_mode(display, DOUBLEBUF|OPENGL) gluPerspective(45, (display[0]/display[1]), 0.1, 50.0) glTranslatef(0.0,0.0, -10) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) drawEdges() drawAxis() pygame.display.flip() pygame.time.wait(10) #Keyboard rotate object controls pygame.key.get_focused()#checks to see which keys are pressed keyList = pygame.key.get_pressed()#assigns name to array of booleans each index is a key 1 = pressed #ROTATE ABOUT X AXIS if(keyList[274] == 1): #DOWN KEY glRotatef(.5, 1, 0, 0) if(keyList[273] == 1):#UP KEY glRotatef(-.5,1,0,0) #ROTATE ABOUT Y AXIS if(keyList[275] == 1):#RIGHT KEY glRotatef(-.5,0,1,0) if(keyList[276] == 1):#LEFT KEY glRotatef(.5,0,1,0) #ROTATE ABOUT Z AXIS if(keyList[44] == 1):#, KEY glRotatef(.5,0,0,1) if(keyList[46] == 1):#. KEY glRotatef(-.5,0,0,1) #TRANSLATE ALONG Z AXIS if(keyList[119] == 1):#W KEY glTranslatef(0.0,0.0, .1) if(keyList[115] == 1):#S KEY glTranslatef(0.0,0.0, -.1) #TRANSLATE ALONG Y AXIS if(keyList[97] == 1):#A KEY glTranslatef(0.0,.1,0.0) if(keyList[100] == 1):#D KEY glTranslatef(0.0,-.1, 0.0) #TRANSLATE ALONG X AXIS if(keyList[113] == 1):#Q KEY glTranslatef(.1,0.0,0.0) if(keyList[101] == 1):#E KEY glTranslatef(-.1,0.0, 0.0) main()