Mapping opengl coordinates to pixel coordinates

Post Reply
EMUGOD
Posts: 1
Joined: Sun Feb 08, 2009 6:10 am

Mapping opengl coordinates to pixel coordinates

Post by EMUGOD » Sat Jun 20, 2009 10:21 pm

Code: Select all

int main()
{	
	//int triangle[3][3] = { {100,100,0}, {60,180,0}, {140,180,0} };
	//int triangle[3][3] = { {60,60,0}, {40,80,0}, {80,80,0} };
	int triangle[3][3] = { {0,0,0}, {10,0,0}, {0,10,0} };
	//int triangle[3][3] = { {0.0f, 1.0f, 0.0f}, {-1.0f,-1.0f, 0.0f}, {1.0f,-1.0f, 0.0f} };
	int tricolor[3][3] = { {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 1.0f} };
	
	touchPosition touchXY;
	
	//lcdMainOnTop();
	lcdMainOnBottom();
	
	//set mode 0, enable BG0 and set it to 3D
	videoSetMode(MODE_0_3D);
	consoleDemoInit();
	
	// initialize gl
	glInit();
	
        // map coords directly to screen
	glViewport(0, 0, 255, 191);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, 255, 0, 191, -1, 1);
	//gluPerspective(70, 256.0 / 192.0, 0.1, 100);
	glScalef(1, -1, 1);
	glTranslatef(0, -191, 0);
       // \map coords directly to screen

	//ds specific, several attributes can be set here
	glPolyFmt(POLY_ALPHA(31) | POLY_CULL_NONE);
	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
	gluLookAt (0, 0, -1, 0, 0, 1, 0, 1, 0);
	
	float zoom = 0;
	int nTexture = 0;
	int oldx0 = 0, oldy0 = 0;
	int oldx1 = 0, oldy1 = 0;
	int mx = 0, my = 0;
	int rx = 0, ry = 0;
	int screen = 0;
	int i = 0, j=0;
	
	while(1)		
	{
		glPushMatrix();
		
		scanKeys();
		touchRead(&touchXY);
		
		u16 pressed = keysDown();
		u16 keys = keysHeld();
		
		//reset x and y when user touches screen
		if(pressed & KEY_TOUCH)  
		{
			oldx1 = touchXY.px;
			oldy1 = touchXY.py;
		}
		
		//if user drags then grab the delta
		if(keys & KEY_TOUCH) 
		{
			mx += touchXY.px - oldx1; 
			my += touchXY.py - oldy1;
			oldx1 = touchXY.px;
			oldy1 = touchXY.py;
		}
		
		//glTranslatef(mx,my,zoom);
		
		
		glBegin(GL_TRIANGLES);
		//for (int i = 
		//{
			for (j=0; j<3; j++)
			{
				glColor3f (tricolor[j][0], tricolor[j][1], tricolor[j][2]);
				glVertex3f(triangle[j][0], triangle[j][1], triangle[j][2]);
			}
		//} inttov16(
		glEnd();
		
		/*
		glBegin(GL_TRIANGLES);
			glColor3f(0.0, 0.0, 1.0);  //blue
			glVertex3f(0, 0, 0);
			glColor3f(0.0, 1.0, 0.0);  //green 
			glVertex3f(200, 200, 0);
			glColor3f(1.0, 0.0, 0.0);  //red
			glVertex3f(20, 200, 0);
		glEnd();
		*/
		
		glPopMatrix(1);
		
		glFlush(0);
		swiWaitForVBlank();
	}

return 0;
}
i included the whole code just in case anyone was interested, but it's really the part between the two "map coords directly to screen" comments that is the trouble. this works in opengl but not libnds (assuming i haven't made some other mistake). anybody know how to do this in libnds, or else what my problem might be here?

melw
Posts: 13
Joined: Thu Mar 20, 2008 11:43 am

Re: Mapping opengl coordinates to pixel coordinates

Post by melw » Sun Jun 28, 2009 8:12 am

Perhaps this is a glOrtho() / value overflow related issue? See here for more information.

I haven't used 3d modes on DS for a while, but last time I checked (several libnds versions ago) drawing 2d quads in 3d mode worked also when initializing simply with:

Code: Select all

glInit();
glEnable(GL_TEXTURE_2D);
glViewport(0,0,255,191);

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Semrush [Bot] and 2 guests