Jump to content

  • Log In with Google      Sign In   
  • Create Account


Pyopengl Problem: Little confusion as to how to reset newly created objects position to default (0.0,0.0,0.0).


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 JoryRFerrell   Members   -  Reputation: 143

Like
0Likes
Like

Posted 13 January 2013 - 03:23 PM

When I start my program and press "N" to create a new cube, the cube is added to a list and rendered. However, with the addition of each new cube, it seems to save the position of the previous one and create any new cube at that spot. Feeling a little frustrated. I don't see how it's

overwriting the initialized position that I give it :

 

objects_Render.append(

                                       Cube_Projectile((0.0,0.0,0.0))

                                       )

                                                                                                              

  angry.png   tongue.png

 

What am I doing wrong?

 

 


SCREEN_SIZE = (800, 600)

import sys
from math import radians

from OpenGL.GL import *
from OpenGL.GLU import *

import pygame
from pygame.locals import *

from gameobjects.matrix44 import *
from gameobjects.vector3 import *
import random

def resize(width, height):

    glViewport(0, 0, width, height)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(90.0, float(width)/height, .1, 10000.)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()


def init():

    glEnable(GL_DEPTH_TEST)

    glShadeModel(GL_FLAT)
    glClearColor(1.0, 1.0, 1.0, 1.0)

    glEnable(GL_COLOR_MATERIAL)

    glEnable(GL_LIGHTING)
    glEnable(GL_LIGHT0)
    glLight(GL_LIGHT0, GL_POSITION,  (10, 10, 10, 0))


class Ground(object):
    def __init__(self):
        self.position= (0.0, 0.0, 0.0)
        self.color = tuple([random.uniform(0.0,1.0), random.uniform(0.0,1.0) , random.uniform(0.0,1.0)])

    def render(self):
        glColor(self.color[0], self.color[1], self.color[2])

        glTranslatef(self.position[0], self.position[1], self.position[2])

        glBegin(GL_QUADS)

        glNormal3f(0.000000, 1.000000, 0.000000)
        glVertex3f(-2000.000000, 0.000000, -200.000000)
        glVertex3f(-2000.000000, 0.000000, 200.000000)
        glVertex3f(-2000.000000, 0.000000, 200.000000)
        glVertex3f(-2000.000000, 0.000000, -200.000000)

        glEnd()




class Cube_Projectile(object):


    def __init__(self, position):

        self.position = (0.0, 0.0, 0.0)
        self.color = tuple([random.uniform(0.0,1.0), random.uniform(0.0,1.0) , random.uniform(0.0,1.0)])
        self.life = 1000

    def render(self):

        if self.life > 0:

            glColor(self.color)

            # Adjust all the vertices so that the cube is at self.position
            # vertices = [tuple(Vector3(v) + self.position) for v in self.vertices]

            # Draw all 6 faces of the cube



            self.life -= 1


            glTranslatef(self.position[0], self.position[1], self.position[2])

            glBegin(GL_QUADS)

            glNormal3f(2.000000, 0.000000, 0.000000)
            glVertex3f(-2.000000, -2.000000, -2.000000)
            glVertex3f(-2.000000, -2.000000, 2.000000)
            glVertex3f(-2.000000, 2.000000, 2.000000)
            glVertex3f(-2.000000, 2.000000, -2.000000)

            glNormal3f(0.000000, -0.000000, 2.000000)
            glVertex3f(-2.000000, 2.000000, -2.000000)
            glVertex3f(2.000000, 2.000000, -2.000000)
            glVertex3f(2.000000, -2.000000, -2.000000)
            glVertex3f(-2.000000, -2.000000, -2.000000)

            glNormal3f(-2.000000, 0.000000, -0.000000)
            glVertex3f(2.000000, 2.000000, -2.000000)
            glVertex3f(2.000000, 2.000000, 2.000000)
            glVertex3f(2.000000, -2.000000, 2.000000)
            glVertex3f(2.000000, -2.000000, -2.000000)

            glNormal3f(0.000000, -0.000000, -1.000000)
            glVertex3f(-2.000000, -2.000000, 2.000000)
            glVertex3f(2.000000, -2.000000, 2.000000)
            glVertex3f(2.000000, 2.000000, 2.000000)
            glVertex3f(-2.000000, 2.000000, 2.000000)

            glNormal3f(0.000000, -1.000000, 0.000000)
            glVertex3f(-2.000000, -2.000000, 2.000000)
            glVertex3f(-2.000000, -2.000000, -2.000000)
            glVertex3f(2.000000, -2.000000, -2.000000)
            glVertex3f(2.000000, -2.000000, 2.000000)

            glNormal3f(0.000000, 1.000000, 0.000000)
            glVertex3f(2.000000, 2.000000, 2.000000)
            glVertex3f(2.000000, 2.000000, -2.000000)
            glVertex3f(-2.000000, 2.000000, -2.000000)
            glVertex3f(-2.000000, 2.000000, 2.000000)

            glEnd()



def run():

    pygame.init()
    screen = pygame.display.set_mode(SCREEN_SIZE, HWSURFACE|OPENGL|DOUBLEBUF)

    resize(*SCREEN_SIZE)
    init()

    clock = pygame.time.Clock()

    glMaterial(GL_FRONT, GL_AMBIENT, (0.1, 0.1, 0.1, 1.0))
    glMaterial(GL_FRONT, GL_DIFFUSE, (1.0, 1.0, 1.0, 1.0))


    # Camera transform matrix
    camera_matrix = Matrix44()
    camera_matrix.translate = (10.0, 1.0, 20.0)
    print camera_matrix.translate


    # Initialize speeds and directions
    rotation_direction = Vector3()
    rotation_speed = radians(90.0)
    movement_direction = Vector3()
    movement_speed = 5.0

    objects_Render = []
    ground = Ground()

    while True:

        for event in pygame.event.get():
            if event.type == QUIT:
                return
            if event.type == KEYUP and event.key == K_ESCAPE:
                return


        # Clear the screen, and z-buffer

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        time_passed = clock.tick()

        time_passed_seconds = time_passed / 1000.

        pressed = pygame.key.get_pressed()

        # Reset rotation and movement directions

        rotation_direction.set(0.0, 0.0, 0.0)

        movement_direction.set(0.0, 0.0, 0.0)

        # Modify direction vectors for key presses
        if pressed[K_LEFT]:
            rotation_direction.y = +1.0
        elif pressed[K_RIGHT]:
            rotation_direction.y = -1.0
        if pressed[K_UP]:
            rotation_direction.x = +1.0
        elif pressed[K_DOWN]:
            rotation_direction.x = -1.0
        if pressed[K_d]:
            rotation_direction.z = -1.0
        elif pressed[K_a]:
            rotation_direction.z = +1.0
        if pressed[K_w]:
            movement_direction.z = -1.0
        elif pressed[K_s]:
            movement_direction.z = +1.0
        if pressed[K_n]:

            # Beginning of objects_Render.append()
            objects_Render.append(Cube_Projectile((0.0,0.0,0.0))) # End of objects_Render.append()






        # Calculate rotation matrix and multiply by camera matrix
        rotation = rotation_direction * rotation_speed * time_passed_seconds
        rotation_matrix = Matrix44.xyz_rotation(*rotation)
        camera_matrix *= rotation_matrix

        # Calcluate movment and add it to camera matrix translate
        heading = Vector3(camera_matrix.forward)
        movement = heading * movement_direction.z * movement_speed
        camera_matrix.translate += movement * time_passed_seconds

        # Upload the inverse camera matrix to OpenGL
        glLoadMatrixd(camera_matrix.get_inverse().to_opengl())

        # Light must be transformed as well
        glLight(GL_LIGHT0, GL_POSITION,  (0, 1.5, 1, 7))



        ground.render()

        for obj in objects_Render:

            print "Object: ", objects_Render.index(obj)
            print "Object Position: ", obj.position, "\n"

            tup = []
            tup.append(obj.position[0] + random.uniform(-0.50,0.50))
            tup.append(obj.position[1] + random.uniform(-0.50,0.50))
            tup.append(obj.position[2] + random.uniform(-0.50,0.50))
            obj.position = tuple(tup)

            obj.render()



        # Show the screen
        pygame.display.flip()

run()

Edited by JoryRFerrell, 13 January 2013 - 03:26 PM.


Sponsor:

#2 slicer4ever   Crossbones+   -  Reputation: 3226

Like
0Likes
Like

Posted 14 January 2013 - 10:59 AM

When I start my program and press "N" to create a new cube, the cube is added to a list and rendered. However, with the addition of each new cube, it seems to save the position of the previous one and create any new cube at that spot. Feeling a little frustrated. I don't see how it's

overwriting the initialized position that I give it :

 

objects_Render.append(

                                       Cube_Projectile((0.0,0.0,0.0))

                                       )

                                                                                                              

  angry.png   tongue.png

 

What am I doing wrong?

 

 


SCREEN_SIZE = (800, 600)

import sys
from math import radians

from OpenGL.GL import *
from OpenGL.GLU import *

import pygame
from pygame.locals import *

from gameobjects.matrix44 import *
from gameobjects.vector3 import *
import random

def resize(width, height):

    glViewport(0, 0, width, height)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(90.0, float(width)/height, .1, 10000.)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()


def init():

    glEnable(GL_DEPTH_TEST)

    glShadeModel(GL_FLAT)
    glClearColor(1.0, 1.0, 1.0, 1.0)

    glEnable(GL_COLOR_MATERIAL)

    glEnable(GL_LIGHTING)
    glEnable(GL_LIGHT0)
    glLight(GL_LIGHT0, GL_POSITION,  (10, 10, 10, 0))


class Ground(object):
    def __init__(self):
        self.position= (0.0, 0.0, 0.0)
        self.color = tuple([random.uniform(0.0,1.0), random.uniform(0.0,1.0) , random.uniform(0.0,1.0)])

    def render(self):
        glColor(self.color[0], self.color[1], self.color[2])

        glTranslatef(self.position[0], self.position[1], self.position[2])

        glBegin(GL_QUADS)

        glNormal3f(0.000000, 1.000000, 0.000000)
        glVertex3f(-2000.000000, 0.000000, -200.000000)
        glVertex3f(-2000.000000, 0.000000, 200.000000)
        glVertex3f(-2000.000000, 0.000000, 200.000000)
        glVertex3f(-2000.000000, 0.000000, -200.000000)

        glEnd()




class Cube_Projectile(object):


    def __init__(self, position):

        self.position = (0.0, 0.0, 0.0)
        self.color = tuple([random.uniform(0.0,1.0), random.uniform(0.0,1.0) , random.uniform(0.0,1.0)])
        self.life = 1000

    def render(self):

        if self.life > 0:

            glColor(self.color)

            # Adjust all the vertices so that the cube is at self.position
            # vertices = [tuple(Vector3(v) + self.position) for v in self.vertices]

            # Draw all 6 faces of the cube



            self.life -= 1


            glTranslatef(self.position[0], self.position[1], self.position[2])

            glBegin(GL_QUADS)

            glNormal3f(2.000000, 0.000000, 0.000000)
            glVertex3f(-2.000000, -2.000000, -2.000000)
            glVertex3f(-2.000000, -2.000000, 2.000000)
            glVertex3f(-2.000000, 2.000000, 2.000000)
            glVertex3f(-2.000000, 2.000000, -2.000000)

            glNormal3f(0.000000, -0.000000, 2.000000)
            glVertex3f(-2.000000, 2.000000, -2.000000)
            glVertex3f(2.000000, 2.000000, -2.000000)
            glVertex3f(2.000000, -2.000000, -2.000000)
            glVertex3f(-2.000000, -2.000000, -2.000000)

            glNormal3f(-2.000000, 0.000000, -0.000000)
            glVertex3f(2.000000, 2.000000, -2.000000)
            glVertex3f(2.000000, 2.000000, 2.000000)
            glVertex3f(2.000000, -2.000000, 2.000000)
            glVertex3f(2.000000, -2.000000, -2.000000)

            glNormal3f(0.000000, -0.000000, -1.000000)
            glVertex3f(-2.000000, -2.000000, 2.000000)
            glVertex3f(2.000000, -2.000000, 2.000000)
            glVertex3f(2.000000, 2.000000, 2.000000)
            glVertex3f(-2.000000, 2.000000, 2.000000)

            glNormal3f(0.000000, -1.000000, 0.000000)
            glVertex3f(-2.000000, -2.000000, 2.000000)
            glVertex3f(-2.000000, -2.000000, -2.000000)
            glVertex3f(2.000000, -2.000000, -2.000000)
            glVertex3f(2.000000, -2.000000, 2.000000)

            glNormal3f(0.000000, 1.000000, 0.000000)
            glVertex3f(2.000000, 2.000000, 2.000000)
            glVertex3f(2.000000, 2.000000, -2.000000)
            glVertex3f(-2.000000, 2.000000, -2.000000)
            glVertex3f(-2.000000, 2.000000, 2.000000)

            glEnd()



def run():

    pygame.init()
    screen = pygame.display.set_mode(SCREEN_SIZE, HWSURFACE|OPENGL|DOUBLEBUF)

    resize(*SCREEN_SIZE)
    init()

    clock = pygame.time.Clock()

    glMaterial(GL_FRONT, GL_AMBIENT, (0.1, 0.1, 0.1, 1.0))
    glMaterial(GL_FRONT, GL_DIFFUSE, (1.0, 1.0, 1.0, 1.0))


    # Camera transform matrix
    camera_matrix = Matrix44()
    camera_matrix.translate = (10.0, 1.0, 20.0)
    print camera_matrix.translate


    # Initialize speeds and directions
    rotation_direction = Vector3()
    rotation_speed = radians(90.0)
    movement_direction = Vector3()
    movement_speed = 5.0

    objects_Render = []
    ground = Ground()

    while True:

        for event in pygame.event.get():
            if event.type == QUIT:
                return
            if event.type == KEYUP and event.key == K_ESCAPE:
                return


        # Clear the screen, and z-buffer

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        time_passed = clock.tick()

        time_passed_seconds = time_passed / 1000.

        pressed = pygame.key.get_pressed()

        # Reset rotation and movement directions

        rotation_direction.set(0.0, 0.0, 0.0)

        movement_direction.set(0.0, 0.0, 0.0)

        # Modify direction vectors for key presses
        if pressed[K_LEFT]:
            rotation_direction.y = +1.0
        elif pressed[K_RIGHT]:
            rotation_direction.y = -1.0
        if pressed[K_UP]:
            rotation_direction.x = +1.0
        elif pressed[K_DOWN]:
            rotation_direction.x = -1.0
        if pressed[K_d]:
            rotation_direction.z = -1.0
        elif pressed[K_a]:
            rotation_direction.z = +1.0
        if pressed[K_w]:
            movement_direction.z = -1.0
        elif pressed[K_s]:
            movement_direction.z = +1.0
        if pressed[K_n]:

            # Beginning of objects_Render.append()
            objects_Render.append(Cube_Projectile((0.0,0.0,0.0))) # End of objects_Render.append()






        # Calculate rotation matrix and multiply by camera matrix
        rotation = rotation_direction * rotation_speed * time_passed_seconds
        rotation_matrix = Matrix44.xyz_rotation(*rotation)
        camera_matrix *= rotation_matrix

        # Calcluate movment and add it to camera matrix translate
        heading = Vector3(camera_matrix.forward)
        movement = heading * movement_direction.z * movement_speed
        camera_matrix.translate += movement * time_passed_seconds

        # Upload the inverse camera matrix to OpenGL
        glLoadMatrixd(camera_matrix.get_inverse().to_opengl())

        # Light must be transformed as well
        glLight(GL_LIGHT0, GL_POSITION,  (0, 1.5, 1, 7))



        ground.render()

        for obj in objects_Render:

            print "Object: ", objects_Render.index(obj)
            print "Object Position: ", obj.position, "\n"

            tup = []
            tup.append(obj.position[0] + random.uniform(-0.50,0.50))
            tup.append(obj.position[1] + random.uniform(-0.50,0.50))
            tup.append(obj.position[2] + random.uniform(-0.50,0.50))
            obj.position = tuple(tup)

            obj.render()



        # Show the screen
        pygame.display.flip()

run()

you translate to the cube's position in the render call, and don't translate back, so now 0.0, 0.0, 0.0 is in relative to the last cube's position, which is relative to the one before it, and that to the one before it, and so on.


Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#3 JoryRFerrell   Members   -  Reputation: 143

Like
0Likes
Like

Posted 14 January 2013 - 11:45 AM

you translate to the cube's position in the render call, and don't translate back, so now 0.0, 0.0, 0.0 is in relative to the last cube's position, which is relative to the one before it, and that to the one before it, and so on.

 

 

Soooo...when I use the translate method, I am actually setting the origin for ALL objects to be drawn at that point....not just that specific object? Weird. blink.png

TY. It's working now obviously. Oh, and any idea what I am doing wrong to cause the "ground" to not render?



#4 slicer4ever   Crossbones+   -  Reputation: 3226

Like
0Likes
Like

Posted 14 January 2013 - 07:00 PM

yes, it's called frame of reference, it works nicely if you are rendering a tree of objects(where the child is relative to the parent), but not so well if each object is suppose to already be in world coordinates(thus why you should learn about glPushMatrix/glPopMatrix for this sort of thing.)

 

note that newer openGL models with shaders no longer support these matrix operations, and it can be easier to work with only world-coordinates, because you have to manually pass the matrix.

 

as for your ground, it suffers from two problems:

1. if you ever move the ground, your going to have a similar issue you experiance with your cubes.

2. you do not change the x-coordinate, so you are essentially drawing a long line at -2000 x.

 

one last thing, you should recognize that the cube's/ground are relative to your camera, this means that when you fire your cube, and move, that cube well move with your camera, it'd probably be better to set the cube at the camera's position in world space, and make sure you pop the camera's matrix before drawing the cubes.

 

on a side note, that is generally how skybox's are handled, so keep that in mind for one day down the road.


Edited by slicer4ever, 14 January 2013 - 07:03 PM.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#5 JoryRFerrell   Members   -  Reputation: 143

Like
0Likes
Like

Posted 20 February 2013 - 11:46 AM

yes, it's called frame of reference, it works nicely if you are rendering a tree of objects(where the child is relative to the parent), but not so well if each object is suppose to already be in world coordinates(thus why you should learn about glPushMatrix/glPopMatrix for this sort of thing.)

 

note that newer openGL models with shaders no longer support these matrix operations, and it can be easier to work with only world-coordinates, because you have to manually pass the matrix.

 

as for your ground, it suffers from two problems:

1. if you ever move the ground, your going to have a similar issue you experiance with your cubes.

2. you do not change the x-coordinate, so you are essentially drawing a long line at -2000 x.

 

one last thing, you should recognize that the cube's/ground are relative to your camera, this means that when you fire your cube, and move, that cube well move with your camera, it'd probably be better to set the cube at the camera's position in world space, and make sure you pop the camera's matrix before drawing the cubes.

 

on a side note, that is generally how skybox's are handled, so keep that in mind for one day down the road.

 

Bit late...but ty. I ended up getting the answer before you replied. TY for notes though, as you did help with a few issues.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS