problem with normals

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
superted
Posts: 1
Joined: Fri Apr 11, 2003 12:51 pm

problem with normals

Post by superted » Fri Apr 11, 2003 12:54 pm

Hello,

I use a python scipt to export 3d datas. Then I import thoses exported datas in my OpenGL/C++ program.
All works great, expect for normals : vertices normals seem to be ALWAYS interpolated between the adjacent faces.
If this is good for a sphere, it isn't for a cube (which is not 'smooth').
Would you know how have the normals such as defined in blender ?

Here is the way I access to the normals :
(for a cube it should print {0,1,-1}, but here it prints {-0.577349185944, 0.577349185944})

Code: Select all

import Blender
import sys

from Blender import Object, NMesh, Mesh, BGL, Draw

select=Object.get()
for ob in select :
	nomMesh=ob.data.name
	mesh=Mesh.get(nomMesh)
	if (mesh!=None) :
		nmesh=NMesh.GetRaw(nomMesh)

		numFace=0
		faces=nmesh.faces
		for face in faces :
			for ver in face.v :
				print ver.no[0]
				print ver.no[1]
				print ver.no[2]

eeshlo
Posts: 132
Joined: Wed Nov 06, 2002 10:02 pm

Post by eeshlo » Mon Apr 14, 2003 10:11 pm

You want face normals, not vertex normals, which you have to calculate yourself.

z3r0_d
Posts: 1522
Joined: Wed Oct 16, 2002 2:38 am
Contact:

Post by z3r0_d » Tue Apr 15, 2003 7:21 pm

... calculating them yourself

Code: Select all

def vec_dot(u, v):
	return u[0]*v[0] + u[1]*v[1] + u[2]*v[2]
def vec_cross(u, v):
	#	|	i	j	k	|
	#	|	u0	u1	u2	|
	#	|	v0	v1	v2	|
	# (u1*v2-u2*v1)i - (u0*v2-u2*v0)j + (u0*v1-u1*v0)k
	# (u1*v2-u2*v1)i + (u2*v0-u0*v2)j + (u0*v1-u1*v0)k
	return [u[1]*v[2]-u[2]*v[1], u[2]*v[0]-u[0]*v[2], u[0]*v[1]-u[1]*v[0]]
def vec_mag(u):
	return sqrt(u[0]*u[0] + u[1]*u[1] + u[2]*u[2])
def vec_norm(u):
	n = vec_mag(u)
	return [u[0]/n, u[1]/n, u[2]/n]
def vec_between_points(u, v):
	# returns the vector from point u to point v
	return [v[0]-u[0], v[1]-u[1], v[2]-u[2]]
# thisMesh is a NMesh object
i = 0
while i<len(thisMesh.faces):
	aFace = thisMesh.faces[i]
	norm = vec_norm(vec_cross(vec_between_points(aFace.v[0], aFace.v[1]), vec_between_points(aFace.v[1], aFace.v[2])))
(you may use this code, even though it is from two of my scripts (only one of which has ben released)

oh, I forgot to mention that this works best on triangles as they are planar. On quads one vertex is completely ignored. It may be prefered that you average your normals on a quad

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests