Advertising (This ad goes away for registered users. You can Login or Register)

VITA SDK: 3D texture and light

Open discussions on programming specifically for the PS Vita.
Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
Locked
Mills
Posts: 36
Joined: Sat Aug 02, 2014 10:09 am

VITA SDK: 3D texture and light

Post by Mills »

Hi.

I wanted to create a 3D object loader for PSVITA.

The loader itself is working, I loaded ply models to test, and no problem.
But i could not find any sample code that includes both texturing and lights at the same time.
I can load a textured model without lighting... Or a lighted model without textures.

I first tried to implement vertex lighting on top of the textured cube sample included in the SDKhttps://github.com/vitasdk , and I just got a static light that won't move at all.

Then I tried to implement lighting from gxmfun https://github.com/xerpi/gxmfun on top of the textured cube. And It just did not work at all.

Vita's 3d is so complicated, I could not mix the texture sample with the light sample, so I was wondering if somebody could help.

Thanks a lot!
Advertising
romain337
Posts: 219
Joined: Wed Feb 29, 2012 10:42 am
Location: France
Contact:

Re: VITA SDK: 3D texture and light

Post by romain337 »

Hi

I think you have to use a texture coord buffer binded to the shader (with a texture of course) to apply the texture as usual, and maybe some uniforms to send light information to the shader.

Somebody can maybe explain better.
Maybe: http://www.ogre3d.org/tikiwiki/tiki-ind ... kbook#Code

You can try to search for "cg shader lighting" or something like that.
Good luck :)
Advertising
Mills
Posts: 36
Joined: Sat Aug 02, 2014 10:09 am

Re: VITA SDK: 3D texture and light

Post by Mills »

Thanks.

It looks like I got a very similar code, because i just copied it from the sample, so that does not help me a lot, the problem is something else i'm missing.

this is the vertex shader:

Code: Select all

float4 main(
	float3 position,
	float3 normal,
	float2 texcoord,
	uniform float4x4 u_mvp_matrix,
	out float3 out_position : TEXCOORD2,
	out float3 out_normal: TEXCOORD1,
	out float2 out_texcoord: TEXCOORD0) : POSITION
{
	out_position = position;
	out_texcoord = texcoord;
	out_normal = normal;
	
	return mul(u_mvp_matrix, float4(position, 1.0f));
}
I assume this works well because the 3D model looks perfect, so the vertex position is being exported.

Then the fragment shader:

Code: Select all

struct phong_material {
	float3 ambient;
	float3 diffuse;
	float3 specular;
	float shininess;
};

struct light {
	float3 position;
	float3 color;
};

uniform float4x4 u_modelview_matrix;
uniform float3x3 u_normal_matrix;
uniform phong_material u_material;
uniform light u_light;

float3 phong_lighting(float3 normal, float3 L, float3 position)
{
	/* Ambient */
	float3 ambient = u_material.ambient;

	/* Diffuse */
	float3 diffuse = max(0.0f, dot(L, normal))  * (u_material.diffuse);

	/* Specular */
	float3 R = reflect(-L, normal);
	float3 V = normalize(-position);
	float specular_component = pow(max(0.0f, dot(R, V)), u_material.shininess);
	float3 specular = specular_component * (u_material.specular);

	return (ambient + diffuse + specular) * (u_light.color);
}

void main(
	float2 texcoord : TEXCOORD0,
	float3 normal : TEXCOORD1,
	float3 position : TEXCOORD2,
	uniform sampler2D u_texture : TEXUNIT0,
	out float4 out_color : COLOR)
{	
	float3 position_eyespace = mul(u_modelview_matrix, float4(position, 1.0f)).xyz;
	float3 L = normalize(u_light.position - position_eyespace);
	float3 normal_eyespace = normalize(mul(u_normal_matrix, normal));
	
	float4 texcolor = tex2D(u_texture, texcoord);

	out_color = float4(phong_lighting(normal_eyespace, L, position_eyespace), 1.0f) * texcolor;
}
It just shows a black model, If i change the code to export "texcolor" to COLOR, the psvita will show the textured model without the light, so the texture part is working.

All the uniforms are declared in main code, and the vita is not crashing (it will crash a lot when editing the shaders :)).
Locked

Return to “Programming and Security”