Jump to content

  • Log In with Google      Sign In   
  • Create Account


JoryRFerrell

Member Since 14 Oct 2012
Offline Last Active Apr 24 2013 06:36 PM

Topics I've Started

File formats which export texture mappings from 3d CAD software?

01 April 2013 - 10:48 AM

I need to know of some formats which export texture maps so I can simply parse that and avoid having to hand map a texture to my models. I tried figuring out how .mtl files store their texture mapping. However, they just seem to reference the texture rather than recording the actual mapping. Any suggestions?


Determine if vert normal moves into or out of geometry by finding if verts cw/ccw?

06 March 2013 - 08:58 PM

<p>How do I determine if a the normal for a vertex has been created by ccw ordered verts or cw wound verts? I assign each vert's neighbors to a list in the order they wind around, but it doesn't assign them in a secific clockwise/ccw order consistently. I have just decided to calculate the normal then determine whether the verts are cw, or ccw, and then change the signs of the normal vector components accordingly.</p>
<p>&nbsp;</p>
<p>Some suggested using a secondary vector to compare the angle between the secondary and the generated normal. If greater than 90, or less than, that would tell me if the normal vector is facing the direction I need. But I don't think this works for concave (NOTE: I originally wrote &quot;CONVEX&quot;. I meant &quot;CONCAVE&quot;. :\) shapes (normals can face toward origin, allowing for angles less than 90 degrees when compared with a vector drawn from the origin of object.</p>

Why are my cubes randomly rotating a few seconds into animation?

15 February 2013 - 04:47 PM


Why are my cubes randomly rotating near the end of their animation? Also, why is the flat, stationary plane drawing with one edge"bending" into the origin?

 


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, 0.1, 10000.0)
    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.5), random.uniform(0.0,1.5) , random.uniform(0.0,1.5)])

    def render(self):
        glColor(0.0, 1.0, 1.0)



        glBegin(GL_QUADS)

        glNormal(0.0, 0.0, 1.0)
        glVertex3f(1.000000, 0.000000, 1.000000)
        glVertex3f(-1.000000, 0.000000, 1.000000)
        glVertex3f(1.000000, 0.000000, -1.000000)
        glVertex3f(-1.000000, 0.000000, -1.0000000)

        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])
            glRotate(30, 1, 1, 0)

            glBegin(GL_QUADS)

            glNormal3f(1.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(-1.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()

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


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, 2.0, 20.0)


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

    objects_Render = []
    ground_1 = ground()
    objects_Render.append(ground_1)

    mouse_pos = pygame.mouse.get_pos()

    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 = -5.0
        elif pressed[K_s]:
            movement_direction.z = +5.0


        if pygame.mouse.get_pressed()[0]:
            print pygame.mouse.get_pressed()[0]
            new_mouse_pos = pygame.mouse.get_pos()

            if new_mouse_pos != mouse_pos:

                # rotation_direction.z = (float(new_mouse_pos[0]) - (mouse_pos[0]))/2.0
                if new_mouse_pos > mouse_pos:
                    rotation_direction.z = ((1.0*new_mouse_pos[0]) - (1.0*mouse_pos[0]))/360

                elif new_mouse_pos < mouse_pos:
                    rotation_direction.z = -((1.0*mouse_pos[0]) - (1.0*new_mouse_pos[0]))/360

                if new_mouse_pos[0] > mouse_pos[0]:
                    rotation_direction.x = SCREEN_SIZE[1]/60.0 * .1

                elif new_mouse_pos[0] > mouse_pos[0]:
                    rotation_direction.x = SCREEN_SIZE[1]/60.0 * -.1

            mouse_pos = new_mouse_pos

        mouse_pos = (0, 1)


        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))


        print event.type



        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.70,0.50))
            tup.append(obj.position[1] + random.uniform(-0.70,0.50))
            tup.append(obj.position[2] + random.uniform(-0.70,0.50))
            obj.position = tuple(tup)

            obj.render()



        # Show the screen
        pygame.display.flip()

run()

 


Determining which order to add vert neighbors in prep for Newells Method/Normal Calcula...

10 February 2013 - 02:33 PM

I am attempting to find the neighbors for each vert in an object. By finding all the neighbors, adding them to a index in a consistent clockwise/counter-clockwise order, I can then calculate the normals via Newell's Method. But, I have a problem. When I run this with a cube as a test object, I get Half the normals moving in the correct direction (normals facing out of object), and the other half facing inwards. The problem is the order in which the verts are added. I can't figure out how to consistently find which verts to  add to the list of neighbors according to the neighbors position.

 

    for face in faces:

        if vert_ID+1 in face:

            if total_Neighbors == 3:

                if face.index(vert_ID+1) == 0:

                    # print "Located: 0"
                    if vertices[face[1]-1] not in neighbors:
                        neighbors[1] = vertices[face[1]-1]
                    if vertices[face[3]-1] not in neighbors:
                        neighbors[2] = vertices[face[3]-1]

                elif face.index(vert_ID+1) == 1:

                    # print "Located: 1"
                    if vertices[face[0]-1] not in neighbors:
                        neighbors[0] = vertices[face[0]-1]
                    if vertices[face[2]-1] not in neighbors:
                        neighbors[1] = vertices[face[2]-1]

                elif face.index(vert_ID+1) == 2:

                    # print "Located: 2"
                    if vertices[face[1]-1] not in neighbors:
                        neighbors[0] = vertices[face[1]-1]
                    if vertices[face[3]-1] not in neighbors:
                        neighbors[2] = vertices[face[3]-1]

                elif face.index(vert_ID+1) == 3:
                    # print "Located: 3"
                    if vertices[face[0]-1] not in neighbors:
                        neighbors[0] = vertices[face[0]-1]
                    if vertices[face[2]-1] not in neighbors:
                        neighbors[1] = vertices[face[2]-1]

For every vert in an object, I iterate through all the faces, checking to see if the vert is contained in the face. If so, I grab the address/index value of the verts position in the face. For every possible position the vert can be in (in a cube, there are obviously 4 possible places it can be...), I add the two verts, which would logically be connected to it, after checking to make sure each is not going to be written over a value already in the list of neighbors (If a value besides zero already occupies the spot, my algor. doesn't overwrite it. I ran into some issues when I did.).

 

I create the neighbors list and initialize it with values (so I can assign rather than append, which created problems with my partic. algorithm).

If the vert is in position 1 (or index value "0") I assign the two neighbors to specific spots in the neighbors list. The order and spot they occupy is important since newells method is non-commutative (the two possible results being complete opposite vectors), meaning I need to ensure all neighbors are added as either clockwise, or counter-clockwise.

 

The following is a line for each vertice, followed by the list of neighbors.

 Neighbors:
(-1.0, -1.0, 1.0)   [(-1.0, -1.0, -1.0), (1.0, -1.0, 1.0), (-1.0, 1.0, 1.0)]

(-1.0, -1.0, -1.0)   [(-1.0, -1.0, 1.0), (1.0, -1.0, -1.0), (-1.0, 1.0, -1.0)]

(1.0, -1.0, -1.0)   [(1.0, 1.0, -1.0), (1.0, -1.0, 1.0), (-1.0, -1.0, -1.0)]

(1.0, -1.0, 1.0)   [(-1.0, -1.0, 1.0), (1.0, 1.0, 1.0), (1.0, -1.0, -1.0)]

(-1.0, 1.0, 1.0)   [(-1.0, -1.0, 1.0), (1.0, 1.0, 1.0), (-1.0, 1.0, -1.0)]

(-1.0, 1.0, -1.0)   [(-1.0, -1.0, -1.0), (1.0, 1.0, -1.0), (-1.0, 1.0, 1.0)]

(1.0, 1.0, -1.0)   [(-1.0, 1.0, -1.0), (1.0, 1.0, 1.0), (1.0, -1.0, -1.0)]

(1.0, 1.0, 1.0)   [(1.0, 1.0, -1.0), (1.0, -1.0, 1.0), (-1.0, 1.0, 1.0)]


 



 

Then the output:

    Normals: 
(-2.0, -2.0,  2.0)
( 2.0,  2.0,  2.0)#<- reversed. Should be (-2.0, -2.0, -2.0)
( 2.0, -2.0, -2.0)
(-2.0,  2.0, -2.0)#<- reversed. Should be (2.0, -2.0, 2.0)
(-2.0,  2.0,  2.0)
( 2.0, -2.0,  2.0)#<- reversed. Should be (-2.0, 2.0, -2.0)
( 2.0,  2.0, -2.0)
(-2.0, -2.0, -2.0)#<- reversed. Should be (2.0, 2.0, 2.0)

And yes...I have not normalized these, so they are a length of 2. I already have a func for normalizing the values, I just didn't use it. tongue.png

Anyway's, has anyone seen examples of this problem or see any solutions for dealing with it? I have the bulk of this written, but it's useless if every other normal is pointing INTO the object.... tongue.png

 

I don't need the algorithm in any particular language or anything, you can even help with pseudocode. I'm not picky.

Thanks in advance. smile.png


Pyopengl Problem: Little confusion as to how to reset newly created objects position to...

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()

PARTNERS