textures & normals

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

textures & normals

Post by LuMo »

hi,
i am still working on my meshloader and currently i am trying to get textures on the meshes;
the structure i am using for a textured vertex is:

Code: Select all

class Vertex3dbV4
{
  public:

  float u, v;         // UV-texture
  unsigned int color; // AABBGGRR
  float nx, ny, nz;   // normals	
  float x, y, z;
};
so when i am drawing the model i pass the following line:

Code: Select all

            sceGumDrawArray(GU_TRIANGLES,
                            GU_INDEX_16BIT | GU_TEXTURE_32BITF | GU_NORMAL_32BITF | GU_COLOR_8888 | GU_VERTEX_32BITF | GU_TRANSFORM_3D,
                            (int)mesh.faceCount * 3,
                            mesh.faces,
                            mesh.verticesV4); 
note: i also tried to swap to
GU_INDEX_16BIT | GU_TEXTURE_32BITF | GU_COLOR_8888 | GU_NORMAL_32BITF |GU_VERTEX_32BITF | GU_TRANSFORM_3D
with the absolute same effect...
its still flat!
the normals are calculated correct, as i am using it in non-textured mode, which works out nice!
any ideas?
greets
lumo
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
jsgf
Posts: 254
Joined: Tue Jul 12, 2005 11:02 am
Contact:

Post by jsgf »

You're not calling

Code: Select all

sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGB);
? Without that, the vertex colours computed by lighting will have no effect on the texture. Also, you probably don't need per-vertex colours, since you're using lighting.
TheBrooster
Posts: 16
Joined: Wed Jun 01, 2005 8:55 am

Post by TheBrooster »

On a similar note. I am trying to modify the cube demo to use a 32 bpp tga, with alpha. The tga loads and renders, but the transparent portions are completely opaque, any ideas?

Here is the setup code

Code: Select all

void
InitDisplay(void)
{
    pspDebugScreenInit();
	setupCallbacks();

	// setup GU

	void* fbp0 = getStaticVramBuffer(BUF_WIDTH,SCR_HEIGHT,GU_PSM_8888);
	void* fbp1 = getStaticVramBuffer(BUF_WIDTH,SCR_HEIGHT,GU_PSM_8888);
	void* zbp = getStaticVramBuffer(BUF_WIDTH,SCR_HEIGHT,GU_PSM_4444);

	sceGuInit();

	sceGuStart(GU_DIRECT,list);
	sceGuDrawBuffer(GU_PSM_8888,fbp0,BUF_WIDTH);
	sceGuDispBuffer(SCR_WIDTH,SCR_HEIGHT,fbp1,BUF_WIDTH);
	sceGuDepthBuffer(zbp,BUF_WIDTH);
	sceGuOffset(2048 - (SCR_WIDTH/2),2048 - (SCR_HEIGHT/2));
	sceGuViewport(2048,2048,SCR_WIDTH,SCR_HEIGHT);
	sceGuDepthRange(65535,0);
	sceGuScissor(0,0,SCR_WIDTH,SCR_HEIGHT);
	sceGuEnable(GU_SCISSOR_TEST);
	sceGuFrontFace(GU_CW);
	sceGuShadeModel(GU_SMOOTH);
	sceGuDepthFunc(GU_GEQUAL);
	sceGuEnable(GU_DEPTH_TEST);
	sceGuEnable(GU_CULL_FACE);
	sceGuEnable(GU_TEXTURE_2D);
	sceGuEnable(GU_CLIP_PLANES);

	//sceGuAlphaFunc(GU_GREATER, 0, 0xff);
	//sceGuEnable(GU_ALPHA_TEST);

	// enable alpha channel
	sceGuEnable(GU_BLEND);
	sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);

	// setup projection matrix
	sceGumMatrixMode(GU_PROJECTION);
	sceGumLoadIdentity();
	sceGumPerspective(75.0f,16.0f/9.0f,0.5f,1000.0f);

	// setup view matrix
	sceGumMatrixMode(GU_VIEW);
	sceGumLoadIdentity();

	// setup model matrix
	sceGumMatrixMode(GU_MODEL);
	sceGumLoadIdentity();

	sceGuAmbientColor(0xffffffff);

	sceGuFinish();
	sceGuSync(0,0);

	sceDisplayWaitVblankStart();
	sceGuDisplay(GU_TRUE);
}
and my render code:

Code: Select all

void 
CRenderer::Render(void)
{
	if ( !pImage )
		pImage = g_TextureMan.LoadTGA( "Octane.tga");

	if ( !pImage )
		return;

	static float val = 0.f;

	sceGumMatrixMode(GU_MODEL);
	sceGumLoadIdentity();
	{
		ScePspFVector3 pos = { 0, 0, -2.5f };
		ScePspFVector3 rot = { val * 0.79f * (GU_PI/180.0f), val * 0.98f * (GU_PI/180.0f), val * 1.32f * (GU_PI/180.0f) };
		sceGumTranslate(&pos);
		sceGumRotateXYZ(&rot);
	}

	// setup texture
	sceGuTexMode( pImage->GetFormat(), pImage->GetNumMipmaps(), 0, pImage->IsSwizzled() );
	sceGuTexImage( 0, pImage->GetWidth(), pImage->GetHeight(), pImage->GetWidth(), pImage->GetImageData() );
	sceGuTexFunc(GU_TFX_BLEND, GU_TCC_RGBA);
	sceGuTexEnvColor(0x00000000);
	sceGuTexFilter(GU_LINEAR,GU_LINEAR);
	sceGuTexScale(1.0f,1.0f);
	sceGuTexOffset(0.0f,0.0f);

	// draw test case
	sceGumDrawArray(GU_TRIANGLES,GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_3D,12*3,0,vertices);

	val += 0.25f;
}
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Post by LuMo »

jsgf wrote:You're not calling

Code: Select all

sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGB);
? Without that, the vertex colours computed by lighting will have no effect on the texture. Also, you probably don't need per-vertex colours, since you're using lighting.
if i am using modulate the cube is black! (no more textures)
further, by using
GU_TFX_ADD - The texture is added on-top of the diffuse fragment
should do similar (and textures)

still no shading.

@thebrooster
try adding:


greets lumo

Code: Select all

sceGuTexFunc  	(GU_TFX_ADD,GU_TCC_RGBA);
should fix your problem
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Post by LuMo »

this is my full texture setup:

Code: Select all

  sceGuEnable(GU_TEXTURE_2D);
		sceGuTexMode(GU_PSM_8888,0,0,0);
		/* Set texture as current texture for the GU */
		sceGuTexImage(0,width, height, width, texture);
		sceGuTexFunc(GU_TFX_REPLACE,GU_TCC_RGBA); // adds to vertexpaint
		sceGuTexEnvColor(0xffff00);
		sceGuTexFilter(GU_LINEAR,GU_LINEAR);
		sceGuTexScale(1.0f,1.0f);
		sceGuTexOffset(0.0f,0.0f);  
and thats my gu setup: (above the texture setup...)

Code: Select all

  // setup GU
  sceGuInit();
  sceGuStart(GU_DIRECT, list);
  sceGuDrawBuffer(GU_PSM_8888, (void *)0, BUF_WIDTH);
  sceGuDispBuffer(SCR_WIDTH, SCR_HEIGHT, (void *)0x88000, BUF_WIDTH);
  sceGuDepthBuffer((void *)0x110000, BUF_WIDTH);
  sceGuOffset(2048 - (SCR_WIDTH / 2), 2048 - (SCR_HEIGHT / 2));
  sceGuViewport(2048, 2048, SCR_WIDTH, SCR_HEIGHT);
  sceGuDepthRange(0xc350, 0x2710);
  sceGuScissor(0, 0, SCR_WIDTH, SCR_HEIGHT);
  sceGuEnable(GU_SCISSOR_TEST);
  sceGuDepthFunc(GU_GEQUAL);
  sceGuEnable(GU_DEPTH_TEST);
  sceGuFrontFace(GU_CCW); //backfaceculling
  sceGuShadeModel(GU_SMOOTH);

  sceGuEnable(GU_CULL_FACE); //enable faceculling
  sceGuEnable(GU_CLIP_PLANES);

  //lightning
  sceGuEnable(GU_LIGHTING);
  sceGuEnable(GU_LIGHT0);

greets lumo
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Post by LuMo »

here a full sample of the modded version of the cube
note: normals are NOT correct, jzst random numbers, but that does not really matter, as at least something should be shaded...

its black using
sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGB);
and its textured using
sceGuTexFunc(GU_TFX_ADD, GU_TCC_RGB);

if someone gets THIS code running with shading, my problems are solved too :)

Code: Select all

/*
 * PSP Software Development Kit - http://www.pspdev.org
 * -----------------------------------------------------------------------
 * Licensed under the BSD license, see LICENSE in PSPSDK root for details.
 *
 * Copyright (c) 2005 Jesper Svennevid
 */

#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspdebug.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#include <pspgu.h>
#include <pspgum.h>

PSP_MODULE_INFO&#40;"Cube Sample", 0, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER&#41;;

#define printf	pspDebugScreenPrintf

static unsigned int __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; list&#91;262144&#93;;
extern unsigned char logo_start&#91;&#93;;

struct Vertex
&#123;
	float u, v;
	unsigned int color;
	float nx, ny, nz;   // normals	
	float x,y,z;
&#125;;

struct Vertex __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; vertices&#91;12*3&#93; =
&#123;
	&#123;0, 0, 0,0,1, 0xff7f0000,-1,-1, 1&#125;, // 0
	&#123;1, 0, 0,1,1, 0xff7f0000,-1, 1, 1&#125;, // 4
	&#123;1, 1, 1,1,1, 0xff7f0000, 1, 1, 1&#125;, // 5

	&#123;0, 0, 0,0,1, 0xff7f0000,-1,-1, 1&#125;, // 0
	&#123;1, 1, 1,1,1, 0xff7f0000, 1, 1, 1&#125;, // 5
	&#123;0, 1, 1,0,1, 0xff7f0000, 1,-1, 1&#125;, // 1

	&#123;0, 0, 0,0,0, 0xff7f0000,-1,-1,-1&#125;, // 3
	&#123;1, 0, 1,0,0, 0xff7f0000, 1,-1,-1&#125;, // 2
	&#123;1, 1, 1,1,0, 0xff7f0000, 1, 1,-1&#125;, // 6

	&#123;0, 0, 0,0,0, 0xff7f0000,-1,-1,-1&#125;, // 3
	&#123;1, 1, 1,1,0, 0xff7f0000, 1, 1,-1&#125;, // 6
	&#123;0, 1, 0,1,0, 0xff7f0000,-1, 1,-1&#125;, // 7

	&#123;0, 0, 1,0,0, 0xff007f00, 1,-1,-1&#125;, // 0
	&#123;1, 0, 1,0,1, 0xff007f00, 1,-1, 1&#125;, // 3
	&#123;1, 1, 1,1,1, 0xff007f00, 1, 1, 1&#125;, // 7

	&#123;0, 0, 1,0,0, 0xff007f00, 1,-1,-1&#125;, // 0
	&#123;1, 1, 1,1,1, 0xff007f00, 1, 1, 1&#125;, // 7
	&#123;0, 1, 1,1,0, 0xff007f00, 1, 1,-1&#125;, // 4

	&#123;0, 0, 0,0,0, 0xff007f00,-1,-1,-1&#125;, // 0
	&#123;1, 0, 0,1,0, 0xff007f00,-1, 1,-1&#125;, // 3
	&#123;1, 1, 0,1,1, 0xff007f00,-1, 1, 1&#125;, // 7

	&#123;0, 0, 0,0,0, 0xff007f00,-1,-1,-1&#125;, // 0
	&#123;1, 1, 0,1,1, 0xff007f00,-1, 1, 1&#125;, // 7
	&#123;0, 1, 0,0,1, 0xff007f00,-1,-1, 1&#125;, // 4

	&#123;0, 0, 0,1,0, 0xff00007f,-1, 1,-1&#125;, // 0
	&#123;1, 0, 1,1,0, 0xff00007f, 1, 1,-1&#125;, // 1
	&#123;1, 1, 1,1,1, 0xff00007f, 1, 1, 1&#125;, // 2

	&#123;0, 0, 0,1,0, 0xff00007f,-1, 1,-1&#125;, // 0
	&#123;1, 1, 1,1,1, 0xff00007f, 1, 1, 1&#125;, // 2
	&#123;0, 1, 0,1,1, 0xff00007f,-1, 1, 1&#125;, // 3

	&#123;0, 0, 0,0,0, 0xff00007f,-1,-1,-1&#125;, // 4
	&#123;1, 0, 0,0,1, 0xff00007f,-1,-1, 1&#125;, // 7
	&#123;1, 1, 1,0,1, 0xff00007f, 1,-1, 1&#125;, // 6

	&#123;0, 0, 0,0,0, 0xff00007f,-1,-1,-1&#125;, // 4
	&#123;1, 1, 1,0,1, 0xff00007f, 1,-1, 1&#125;, // 6
	&#123;0, 1, 1,0,0, 0xff00007f, 1,-1,-1&#125;, // 5
&#125;;

int SetupCallbacks&#40;&#41;;

#define BUF_WIDTH &#40;512&#41;
#define SCR_WIDTH &#40;480&#41;
#define SCR_HEIGHT &#40;272&#41;
#define PIXEL_SIZE &#40;4&#41; /* change this if you change to another screenmode */
#define FRAME_SIZE &#40;BUF_WIDTH * SCR_HEIGHT * PIXEL_SIZE&#41;
#define ZBUF_SIZE &#40;BUF_WIDTH SCR_HEIGHT * 2&#41; /* zbuffer seems to be 16-bit? */

int main&#40;int argc, char* argv&#91;&#93;&#41;
&#123;
	SetupCallbacks&#40;&#41;;

	// setup GU

	sceGuInit&#40;&#41;;

	sceGuStart&#40;GU_DIRECT,list&#41;;
	sceGuDrawBuffer&#40;GU_PSM_8888,&#40;void*&#41;0,BUF_WIDTH&#41;;
	sceGuDispBuffer&#40;SCR_WIDTH,SCR_HEIGHT,&#40;void*&#41;0x88000,BUF_WIDTH&#41;;
	sceGuDepthBuffer&#40;&#40;void*&#41;0x110000,BUF_WIDTH&#41;;
	sceGuOffset&#40;2048 - &#40;SCR_WIDTH/2&#41;,2048 - &#40;SCR_HEIGHT/2&#41;&#41;;
	sceGuViewport&#40;2048,2048,SCR_WIDTH,SCR_HEIGHT&#41;;
	sceGuDepthRange&#40;0xc350,0x2710&#41;;
	sceGuScissor&#40;0,0,SCR_WIDTH,SCR_HEIGHT&#41;;
	sceGuEnable&#40;GU_SCISSOR_TEST&#41;;
	sceGuDepthFunc&#40;GU_GEQUAL&#41;;
	sceGuEnable&#40;GU_DEPTH_TEST&#41;;
	sceGuFrontFace&#40;GU_CW&#41;;
	sceGuShadeModel&#40;GU_SMOOTH&#41;;
	sceGuEnable&#40;GU_CULL_FACE&#41;;
	sceGuEnable&#40;GU_TEXTURE_2D&#41;;
	sceGuEnable&#40;GU_CLIP_PLANES&#41;;
	sceGuFinish&#40;&#41;;
	sceGuSync&#40;0,0&#41;;

	sceDisplayWaitVblankStart&#40;&#41;;
	sceGuDisplay&#40;GU_TRUE&#41;;

	// run sample

	int val = 0;

	for&#40;;;&#41;
	&#123;
		sceGuStart&#40;GU_DIRECT,list&#41;;

		// clear screen

		sceGuClearColor&#40;0xff554433&#41;;
		sceGuClearDepth&#40;0&#41;;
		sceGuClear&#40;GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT&#41;;

		// setup matrices for cube

		sceGumMatrixMode&#40;GU_PROJECTION&#41;;
		sceGumLoadIdentity&#40;&#41;;
		sceGumPerspective&#40;75.0f,16.0f/9.0f,0.5f,1000.0f&#41;;

		sceGumMatrixMode&#40;GU_VIEW&#41;;
		sceGumLoadIdentity&#40;&#41;;

		sceGumMatrixMode&#40;GU_MODEL&#41;;
		sceGumLoadIdentity&#40;&#41;;
		&#123;
			ScePspFVector3 pos = &#123; 0, 0, -2.5f &#125;;
			ScePspFVector3 rot = &#123; val * 0.79f * &#40;GU_PI/180.0f&#41;, val * 0.98f * &#40;GU_PI/180.0f&#41;, val * 1.32f * &#40;GU_PI/180.0f&#41; &#125;;
			sceGumTranslate&#40;&pos&#41;;
			sceGumRotateXYZ&#40;&rot&#41;;
		&#125;

		// setup texture

		sceGuTexMode&#40;GU_PSM_4444,0,0,0&#41;;
		sceGuTexImage&#40;0,64,64,64,logo_start&#41;;
//sceGuTexFunc&#40;GU_TFX_ADD,GU_TCC_RGB&#41;;
sceGuTexFunc&#40;GU_TFX_MODULATE, GU_TCC_RGB&#41;;
		sceGuTexEnvColor&#40;0xffff00&#41;;
		sceGuTexFilter&#40;GU_LINEAR,GU_LINEAR&#41;;
		sceGuTexScale&#40;1.0f,1.0f&#41;;
		sceGuTexOffset&#40;0.0f,0.0f&#41;;
		sceGuAmbientColor&#40;0xffffffff&#41;;

		// draw cube

		sceGumDrawArray&#40;GU_TRIANGLES,GU_TEXTURE_32BITF | GU_COLOR_8888 | GU_NORMAL_32BITF |GU_VERTEX_32BITF|GU_TRANSFORM_3D,12*3,0,vertices&#41;;

		sceGuFinish&#40;&#41;;
		sceGuSync&#40;0,0&#41;;

		sceDisplayWaitVblankStart&#40;&#41;;
		sceGuSwapBuffers&#40;&#41;;

		val++;
	&#125;

	sceGuTerm&#40;&#41;;

	sceKernelExitGame&#40;&#41;;
	return 0;
&#125;

/* Exit callback */
int exit_callback&#40;int arg1, int arg2, void *common&#41;
&#123;
	sceKernelExitGame&#40;&#41;;
	return 0;
&#125;

/* Callback thread */
int CallbackThread&#40;SceSize args, void *argp&#41;
&#123;
	int cbid;

	cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
	sceKernelRegisterExitCallback&#40;cbid&#41;;

	sceKernelSleepThreadCB&#40;&#41;;

	return 0;
&#125;

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks&#40;void&#41;
&#123;
	int thid = 0;

	thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, 0, 0&#41;;
	if&#40;thid >= 0&#41;
	&#123;
		sceKernelStartThread&#40;thid, 0, 0&#41;;
	&#125;

	return thid;
&#125;
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
TheBrooster
Posts: 16
Joined: Wed Jun 01, 2005 8:55 am

Post by TheBrooster »

Cheers Lumo, I will give it a go when I get home from work.
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Post by LuMo »

ok i got it working
note: never use a white texture and white shading ;)

greets
lumo
PS: msg me if you need help
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
Post Reply