Jump to content
  • Advertisement
Sign in to follow this  
baz333

OpenGL Can someone give me a hand please?

This topic is 4150 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I'm just learning Python and wondered if someone could give me a little help and provide me with some source code. I am using PyScripter as the console and pyOpenGL for the openGL bindings. I want to be able to: 1) Place 3 objects (e.g glutwireteapot) onto a blank screen 2) Make these objects automatically rotate when the program is run Any help would be greatly appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Right I have managed to create the 3d scene using the code below. I now want to be able to navigate around this scene using either the mouse or keyboard. Can some please help as I have hit a brick wall now and really dont know what to do next?


from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import sys

# Some api in the chain is translating the keystrokes to this octal string
# so instead of saying: ESCAPE = 27, we use the following.
ESCAPE = '\033'

# Number of the glut window.
window = 0

# Rotation angle for the triangle.
rtri = 0.0

# Rotation angle for the quadrilateral.
rquad = 0.0

# A general OpenGL initialization function. Sets all of the initial parameters.
def InitGL(Width, Height): # We call this right after our OpenGL window is created.
glClearColor(0.0, 0.0, 0.0, 0.0) # This Will Clear The Background Color To Black
glClearDepth(1.0) # Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LESS) # The Type Of Depth Test To Do
glEnable(GL_DEPTH_TEST) # Enables Depth Testing
glShadeModel(GL_SMOOTH) # Enables Smooth Color Shading

glMatrixMode(GL_PROJECTION)
glLoadIdentity() # Reset The Projection Matrix
# Calculate The Aspect Ratio Of The Window
gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)

glMatrixMode(GL_MODELVIEW)

# The function called when our window is resized (which shouldn't happen if you enable fullscreen, below)
def ReSizeGLScene(Width, Height):
if Height == 0: # Prevent A Divide By Zero If The Window Is Too Small
Height = 1

glViewport(0, 0, Width, Height) # Reset The Current Viewport And Perspective Transformation
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
glMatrixMode(GL_MODELVIEW)

# The main drawing function.
def DrawGLScene():
global rtri, rquad

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); # Clear The Screen And The Depth Buffer
glLoadIdentity(); # Reset The View
glTranslatef(0.0,0.0,-10.0) # Move Left And Into The Screen

glRotatef(rtri,0.0,1.0,0.0) #Rotate The Pyramid On It's Y Axis

glBegin(GL_TRIANGLES) # Start Drawing The Pyramid

glColor3f(1.0,0.0,0.0) # Red
glVertex3f( 0.0, 1.0, 0.0) # Top Of Triangle (Front)
glColor3f(0.0,1.0,0.0) # Green
glVertex3f(-1.0,-1.0, 1.0) # Left Of Triangle (Front)
glColor3f(0.0,0.0,1.0) # Blue
glVertex3f( 1.0,-1.0, 1.0)

glColor3f(1.0,0.0,0.0) # Red
glVertex3f( 0.0, 1.0, 0.0) # Top Of Triangle (Right)
glColor3f(0.0,0.0,1.0) # Blue
glVertex3f( 1.0,-1.0, 1.0) # Left Of Triangle (Right)
glColor3f(0.0,1.0,0.0) # Green
glVertex3f( 1.0,-1.0, -1.0) # Right

glColor3f(1.0,0.0,0.0) # Red
glVertex3f( 0.0, 1.0, 0.0) # Top Of Triangle (Back)
glColor3f(0.0,1.0,0.0) # Green
glVertex3f( 1.0,-1.0, -1.0) # Left Of Triangle (Back)
glColor3f(0.0,0.0,1.0) # Blue
glVertex3f(-1.0,-1.0, -1.0) # Right Of


glColor3f(1.0,0.0,0.0) # Red
glVertex3f( 0.0, 1.0, 0.0) # Top Of Triangle (Left)
glColor3f(0.0,0.0,1.0) # Blue
glVertex3f(-1.0,-1.0,-1.0) # Left Of Triangle (Left)
glColor3f(0.0,1.0,0.0) # Green
glVertex3f(-1.0,-1.0, 1.0) # Right Of Triangle (Left)
glEnd()


glLoadIdentity()
glTranslatef(3.5,0.0,-10.0) # Move Right And Into The Screen
glRotatef(rtri,0.0,1.0,0.0) # Rotate The Cube On X, Y & Z
glBegin(GL_QUADS) # Start Drawing The Cube


glColor3f(0.0,1.0,0.0) # Set The Color To Blue
glVertex3f( 1.0, 1.0,-1.0) # Top Right Of The Quad (Top)
glVertex3f(-1.0, 1.0,-1.0) # Top Left Of The Quad (Top)
glVertex3f(-1.0, 1.0, 1.0) # Bottom Left Of The Quad (Top)
glVertex3f( 1.0, 1.0, 1.0) # Bottom Right Of The Quad (Top)

glColor3f(1.0,0.5,0.0) # Set The Color To Orange
glVertex3f( 1.0,-1.0, 1.0) # Top Right Of The Quad (Bottom)
glVertex3f(-1.0,-1.0, 1.0) # Top Left Of The Quad (Bottom)
glVertex3f(-1.0,-1.0,-1.0) # Bottom Left Of The Quad (Bottom)
glVertex3f( 1.0,-1.0,-1.0) # Bottom Right Of The Quad (Bottom)

glColor3f(1.0,0.0,0.0) # Set The Color To Red
glVertex3f( 1.0, 1.0, 1.0) # Top Right Of The Quad (Front)
glVertex3f(-1.0, 1.0, 1.0) # Top Left Of The Quad (Front)
glVertex3f(-1.0,-1.0, 1.0) # Bottom Left Of The Quad (Front)
glVertex3f( 1.0,-1.0, 1.0) # Bottom Right Of The Quad (Front)

glColor3f(1.0,1.0,0.0) # Set The Color To Yellow
glVertex3f( 1.0,-1.0,-1.0) # Bottom Left Of The Quad (Back)
glVertex3f(-1.0,-1.0,-1.0) # Bottom Right Of The Quad (Back)
glVertex3f(-1.0, 1.0,-1.0) # Top Right Of The Quad (Back)
glVertex3f( 1.0, 1.0,-1.0) # Top Left Of The Quad (Back)

glColor3f(0.0,0.0,1.0) # Set The Color To Blue
glVertex3f(-1.0, 1.0, 1.0) # Top Right Of The Quad (Left)
glVertex3f(-1.0, 1.0,-1.0) # Top Left Of The Quad (Left)
glVertex3f(-1.0,-1.0,-1.0) # Bottom Left Of The Quad (Left)
glVertex3f(-1.0,-1.0, 1.0) # Bottom Right Of The Quad (Left)

glColor3f(1.0,0.0,1.0) # Set The Color To Violet
glVertex3f( 1.0, 1.0,-1.0) # Top Right Of The Quad (Right)
glVertex3f( 1.0, 1.0, 1.0) # Top Left Of The Quad (Right)
glVertex3f( 1.0,-1.0, 1.0) # Bottom Left Of The Quad (Right)
glVertex3f( 1.0,-1.0,-1.0) # Bottom Right Of The Quad (Right)
glEnd() # Done Drawing The Quad



glLoadIdentity()
glTranslatef(-3.5,0.0,-10.0) # Move Right And Into The Screen
glRotatef(rtri,0.0,1.0,0.0) # Rotate The Cube On X, Y & Z
glBegin(GL_QUADS) # Start Drawing The Cube


glColor3f(0.0,1.0,0.0) # Set The Color To Blue
glVertex3f( 1.0, 1.0,-1.0) # Top Right Of The Quad (Top)
glVertex3f(-1.0, 1.0,-1.0) # Top Left Of The Quad (Top)
glVertex3f(-1.0, 1.0, 1.0) # Bottom Left Of The Quad (Top)
glVertex3f( 1.0, 1.0, 1.0) # Bottom Right Of The Quad (Top)

glColor3f(1.0,0.5,0.0) # Set The Color To Orange
glVertex3f( 1.0,-1.0, 1.0) # Top Right Of The Quad (Bottom)
glVertex3f(-1.0,-1.0, 1.0) # Top Left Of The Quad (Bottom)
glVertex3f(-1.0,-1.0,-1.0) # Bottom Left Of The Quad (Bottom)
glVertex3f( 1.0,-1.0,-1.0) # Bottom Right Of The Quad (Bottom)

glColor3f(1.0,0.0,0.0) # Set The Color To Red
glVertex3f( 1.0, 1.0, 1.0) # Top Right Of The Quad (Front)
glVertex3f(-1.0, 1.0, 1.0) # Top Left Of The Quad (Front)
glVertex3f(-1.0,-1.0, 1.0) # Bottom Left Of The Quad (Front)
glVertex3f( 1.0,-1.0, 1.0) # Bottom Right Of The Quad (Front)

glColor3f(1.0,1.0,0.0) # Set The Color To Yellow
glVertex3f( 1.0,-1.0,-1.0) # Bottom Left Of The Quad (Back)
glVertex3f(-1.0,-1.0,-1.0) # Bottom Right Of The Quad (Back)
glVertex3f(-1.0, 1.0,-1.0) # Top Right Of The Quad (Back)
glVertex3f( 1.0, 1.0,-1.0) # Top Left Of The Quad (Back)

glColor3f(0.0,0.0,1.0) # Set The Color To Blue
glVertex3f(-1.0, 1.0, 1.0) # Top Right Of The Quad (Left)
glVertex3f(-1.0, 1.0,-1.0) # Top Left Of The Quad (Left)
glVertex3f(-1.0,-1.0,-1.0) # Bottom Left Of The Quad (Left)
glVertex3f(-1.0,-1.0, 1.0) # Bottom Right Of The Quad (Left)

glColor3f(1.0,0.0,1.0) # Set The Color To Violet
glVertex3f( 1.0, 1.0,-1.0) # Top Right Of The Quad (Right)
glVertex3f( 1.0, 1.0, 1.0) # Top Left Of The Quad (Right)
glVertex3f( 1.0,-1.0, 1.0) # Bottom Left Of The Quad (Right)
glVertex3f( 1.0,-1.0,-1.0) # Bottom Right Of The Quad (Right)
glEnd() # Done Drawing The Quad

# What values to use? Well, if you have a FAST machine and a FAST 3D Card, then
# large values make an unpleasant display with flickering and tearing. I found that
# smaller values work better, but this was based on my experience.
rtri = rtri + 0.2 # Increase The Rotation Variable For The Triangle
rquad = rquad - 0.15 # Decrease The Rotation Variable For The Quad


# since this is double buffered, swap the buffers to display what just got drawn.
glutSwapBuffers()

# The function called whenever a key is pressed. Note the use of Python tuples to pass in: (key, x, y)
def keyPressed(*args):
# If escape is pressed, kill everything.
if args[0] == ESCAPE:
glutDestroyWindow(window)
sys.exit()

def main():
global window

glutInit(sys.argv)

# Select type of Display mode:
# Double buffer
# RGBA color
# Alpha components supported
# Depth buffer
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)

# get a 640 x 480 window
glutInitWindowSize(640, 480)

# the window starts at the upper left corner of the screen
glutInitWindowPosition(0, 0)

# Okay, like the C version we retain the window id to use when closing, but for those of you new
# to Python (like myself), remember this assignment would make the variable local and not global
# if it weren't for the global declaration at the start of main.
window = glutCreateWindow("Jeff Molofee's GL Code Tutorial ... NeHe '99")

# Register the drawing function with glut, BUT in Python land, at least using PyOpenGL, we need to
# set the function pointer and invoke a function to actually register the callback, otherwise it
# would be very much like the C version of the code.
glutDisplayFunc(DrawGLScene)
#glutDisplayFunc()

# Uncomment this line to get full screen.
# glutFullScreen()

# When we are doing nothing, redraw the scene.
glutIdleFunc(DrawGLScene)

# Register the function called when our window is resized.
glutReshapeFunc(ReSizeGLScene)


# Register the function called when the keyboard is pressed.
glutKeyboardFunc(keyPressed)

# Initialize our window.
InitGL(640, 480)





# Print message to console, and kick off the main to get it rolling.
print "Hit ESC key to quit."

if __name__ == '__main__':
try:
GLU_VERSION_1_2
except:
print "Need GLU 1.2 to run this demo"
sys.exit(1)
main()
glutMainLoop()

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!