gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Help with OpenGL to draw 2D shapes

#59576 - LOst? - Wed Nov 02, 2005 7:34 pm

I don't know if this is possible to do in one day. I need to do a special type analog clock for a company in one day. And I have no experience with OpenGL. I want to do this analog clock on the DS. This is only an opportunity for me, and if I fail it is not a big idea.

The problem is I need to use special lines for the hands called "Geometric Pens" in Windows, are thick lines with flat edges. I thought maybe this can be done with 2 solid polygons, or maybe even a solid quad? Having a dynamic function to create a cosmetic line with an angle, thickness, start, and stop coordinates.
I also need to have a solid circle in the middle of the clock face.

I will give you a picture of how I want it to look (color is not important):
http://www.kund.hemmanet.se/~lost/ogl_cl.png

Is this possible to program in less than 12 hours? Is it easy? Someone want to do the basic for me? (heh, then I just mean the functions, and not the whole DS program).

I need 64 lines and one circle in the middle, and I hope the DS can handle that drawing in less than a second.


Do I have a chance to finish this? Give me your guess! I am so screwed if I have to guess.
_________________
Exceptions are fun

#59617 - tepples - Wed Nov 02, 2005 10:13 pm

Each hand is a quad. You can use GL's rotation commands to set up a matrix through which each quad will be rotated.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#59660 - LOst? - Thu Nov 03, 2005 3:53 am

tepples wrote:
Each hand is a quad. You can use GL's rotation commands to set up a matrix through which each quad will be rotated.

That would help if I knew OpenGL, but I don't. I need a few hints. Only which API's to call. I will figure out the rest hopefully.


EDIT: I figured out the circle, and it was supposed to be the hard part. Maybe just maybe I will manage to finish this. Help anyone with the quads, and how to rotate them?
_________________
Exceptions are fun

#59665 - LOst? - Thu Nov 03, 2005 5:22 am

I have done all shapes.

Now I just need to know how to rotate one shape.

I wonder if I need to do this with software. Hmm. Probably.
_________________
Exceptions are fun

#59666 - dovoto - Thu Nov 03, 2005 5:36 am

Looked like it might make a cool quick little demo so here you go:
This should do you:
Code:


#include<nds.h>

void DrawQuad(float x, float y, float width, float height)
{
   
   glBegin(GL_QUADS);
      glVertex3f(x - width / 2, y, 0);
      glVertex3f(x + width / 2, y, 0);
      glVertex3f(x  + width / 2, y  + height, 0);
      glVertex3f(x - width / 2, y  + height, 0);
   glEnd();
}
int main()
{   
   int hours, seconds, minutes;

   // Turn on everything
   powerON(POWER_ALL);

   //put 3D on top
   lcdMainOnTop();

   // Setup the Main screen for 3D
   videoSetMode(MODE_0_3D);

   // IRQ basic setup
   irqInit();
   irqSet(IRQ_VBLANK, 0);

   // Set our viewport to be the same size as the screen
   glViewPort(0,0,255,191);

   // Specify the Clear Color and Depth
   glClearColor(0,0,0);
   glClearDepth(0x7FFF);

   while (1)
   {
      hours = (IPC->rtc_hours < 12) ? IPC->rtc_hours : IPC->rtc_hours - 52;
      minutes = IPC->rtc_minutes;
      seconds = IPC->rtc_seconds;

      // Reset the screen and setup the view
      glReset();
      gluPerspective(35, 256.0 / 192.0, 0.1, 100);                        

      //ds specific, several attributes can be set here   
      glPolyFmt(POLY_ALPHA(31) | POLY_CULL_NONE);

      // Set the current matrix to be the model matrix
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      
      //move the camera back a little
      glTranslatef(0,0,-3);

      //Push our original Matrix onto the stack (save state)
      glPushMatrix();   
      
      //draw second hand
      glColor3f(0,0,1);
      glRotateZ(-seconds * 360 / 60);
      glTranslatef(0,1.9,0);
      DrawQuad(0,0,.2,.2);

      // Pop our Matrix from the stack (restore state)
      glPopMatrix(1);

      //Push our original Matrix onto the stack (save state)
      glPushMatrix();   

      //draw minute hand
      glColor3f(0,1,0);
      glRotateZ(-minutes * 360 / 60);
      DrawQuad(0,0,.2,2);

      // Pop our Matrix from the stack (restore state)
      glPopMatrix(1);

      //Push our original Matrix onto the stack (save state)
      glPushMatrix();   

      //draw hourhand
      glColor3f(1,0,0);
      glRotateZ(-hours * 360 / 12);
      DrawQuad(0,0,.3,1.8);

      // Pop our Matrix from the stack (restore state)
      glPopMatrix(1);

      //a handy little built in function to wait for a screen refresh
      swiWaitForVBlank();

      // flush to screen   
      glFlush();

   }

   return 0;
}

_________________
www.drunkencoders.com

#59668 - LOst? - Thu Nov 03, 2005 5:53 am

Thanks dovoto. This will help me a lot. Since i have never used OpenGL before, you have helped me to realize what some stuff actually do.

Your example helps me to understand glPushMatrix, and glRotateZ. Hopefully I will be able to handle everything now. I only have one problem: GL_QUADS

GL_QUADS doesn't exist in my libnds version. I am using GL_QUAD instead.
_________________
Exceptions are fun

#59675 - LOst? - Thu Nov 03, 2005 7:01 am

In the end everything worked except reading the time. All I get is zeros. IPC structure won't update no matter what tricks I try to pull off.
_________________
Exceptions are fun

#59721 - dovoto - Thu Nov 03, 2005 7:24 pm

On hardware? I tested the above code and it works fine on my DS..so perhaps your libnds is out of date?
_________________
www.drunkencoders.com

#59798 - LOst? - Fri Nov 04, 2005 4:25 am

dovoto wrote:
On hardware? I tested the above code and it works fine on my DS..so perhaps your libnds is out of date?

It works on hardware, with time and all.

I have another problem, and it is connected to the 3D stuff somehow. When I add or delete code in my main(), I will get a white screen on bootup, but only on real hardware. Maybe it is connected to my inline functions somehow. I will try to make them normal functions. But if it isn't, then the bugs are too big for me to handle. My libnds and GCC are 6 months old.

EDIT: Nah, I have to give up. I can't get any futher with this bug. I got an analog clock at least, and it will help me today for sure.
_________________
Exceptions are fun