#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 = .4 y = yInit #define range for x xInit = -2 xRange = 4 xFin = xInit + xRange deltaX = .4 x = xInit #defines z as in z = f(x,y) z = 0 #following two lines define the index variables of our coordinates-- coor at (yinit,xinit) a = 0 and b = 0 a = 0 b = 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 b = 0 while(y < deltaY + yFin): # <--- loop that makes vertices for trace in zy-plane z = eval(funcIn) vertices.append([x,y,z,a,b]) y = y + deltaY b = b + 1 x = x + deltaX a = a + 1 #--------------------------------------------- #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 drawQuads(): quadsRendered = 0 #keeps track of the number of quadrilaterals you have mucking every thing up aLocal = 0 bLocal = 0 glBegin(GL_QUADS) glColor3f(0,0,10) while(aLocal < math.floor(xRange/deltaX)-1): bLocal = 0 while(bLocal < math.floor(yRange/deltaY)-1): #little color edit before the quad is entered lastVertex = searchForAandB(aLocal,bLocal) glColor3f(0,1/(abs(2*lastVertex[2])+1),0) #as z increases, the color darkens --> pretty sweet style. glVertex3fv(searchForAandB(aLocal,bLocal)) glVertex3fv(searchForAandB(aLocal+1,bLocal)) glVertex3fv(searchForAandB(aLocal+1,bLocal+1)) glVertex3fv(searchForAandB(aLocal,bLocal+1)) quadsRendered = quadsRendered +1 bLocal = bLocal+1 aLocal = aLocal+1 glEnd() def searchForAandB(aLocal,bLocal): counter = 0 while(counter < len(vertices)-1): if(vertices[counter][3] == aLocal and vertices[counter][4]== bLocal): return [vertices[counter][0],vertices[counter][1],vertices[counter][2]] counter = counter + 1 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 KeyBoardControls(): #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) def main(): pygame.init() display = (1000,800) pygame.display.set_mode(display, DOUBLEBUF|OPENGL) gluPerspective(45, (display[0]/display[1]), 0.1, 50.0) glEnable (GL_DEPTH_TEST) 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) drawQuads() #drawEdges() drawAxis() pygame.display.flip() pygame.time.wait(2) KeyBoardControls() main()