glRasterPos*() / glWindowPos*()

Hello all!

Sincere apologies but I’m completely new to C++ AND OpenGL…

After having completed my sprite engine (read, “finally managed to load and display a bitmap as a texture…”), I needed to conquer new ground. So I decided to return to my implementation of the “robot arm” from the red book. The arm is animated with time/glutIdleFunc as opposed to with keyboard input.

This, er, ‘program’, when run without some method of capping the framerate, shows a noticable drop in framerate when switched to fullscreen, or when the cursor is held over the window icons (min, max, close). I thought it would be nice to display the framerate to see the actual differences.

I decided the simplest way to do this would be to use the red books bitmap font and glRasterPos or glWindowPos.

If I use glRasterPos, (after all the 3D drawing), the best I can get is the font displayed as per, but the animated arm is only visible for a split second when the window is resized.

If I use glWindowPos I get an undeclared error! Glut is included, and so is glext, which appears to have ‘declarations’ for glWindowPosARB() and glWindowPosMESA(), although I should say the meaning of those is a bit beyond me…

Does anyone have any suggestions on how to progress with either method?

Thanks in advance.

Are you doing a glClear or something before you draw your fps counter - you shouldn’t be.

You should:
0) Clear Scene.

  1. Render 3D Scene.
  2. set projection to glOrtho.
  3. Render fps counter.
  4. Repeat.

it’s just about that simple.

Thanks for that, it makes an appreciable difference when someone tells you it can be done!

I had another look at it and eventually got it to work, but the code just doesn’t seem right. I’ve included it all here (for convenience) but it’s just the projection and modelview loadIdentity() that seem to cause a problem. It all works with the code as is, but it doesn’t seem right and I don’t know why it works!

As far as I’m aware it’s just the display() function that needs playing with…

Could anyone tell me how it should be done?

cheers.

 
//Robot Arm
//Various transformations, modelview, perspective, and projection

#include <windows.h>
#include <gl/glut.h>
#include <gl/glext.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>



GLubyte space[] = 
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

GLubyte letters[][13] = {
{0x00, 0x00, 0xc3, 0xc3, 0xc3, 0xc3, 0xff, 0xc3, 0xc3, 0xc3, 0x66, 0x3c, 0x18}, 
{0x00, 0x00, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0x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e7, 0x7e}, 
{0x00, 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe}, 
{0x00, 0x00, 0x3f, 0x6e, 0xdf, 0xdb, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0x66, 0x3c}, 
{0x00, 0x00, 0xc3, 0xc6, 0xcc, 0xd8, 0xf0, 0xfe, 0xc7, 0xc3, 0xc3, 0xc7, 0xfe}, 
{0x00, 0x00, 0x7e, 0xe7, 0x03, 0x03, 0x07, 0x7e, 0xe0, 0xc0, 0xc0, 0xe7, 0x7e}, 
{0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0xff}, 
{0x00, 0x00, 0x7e, 0xe7, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3}, 
{0x00, 0x00, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3}, 
{0x00, 0x00, 0xc3, 0xe7, 0xff, 0xff, 0xdb, 0xdb, 0xc3, 0xc3, 0xc3, 0xc3, 0xc3}, 
{0x00, 0x00, 0xc3, 0x66, 0x66, 0x3c, 0x3c, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3}, 
{0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3c, 0x3c, 0x66, 0x66, 0xc3}, 
{0x00, 0x00, 0xff, 0xc0, 0xc0, 0x60, 0x30, 0x7e, 0x0c, 0x06, 0x03, 0x03, 0xff}
};

GLuint fontOffset;

void makeRasterFont(void)
{
   GLuint i, j;
   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

   fontOffset = glGenLists (128);
   for (i = 0,j = 'A'; i < 26; i++,j++) {
      glNewList(fontOffset + j, GL_COMPILE);
      glBitmap(8, 13, 0.0, 2.0, 10.0, 0.0, letters[i]);
      glEndList();
   }
   glNewList(fontOffset + ' ', GL_COMPILE);
   glBitmap(8, 13, 0.0, 2.0, 10.0, 0.0, space);
   glEndList();
}

void printString(char *s)
{
   glPushAttrib (GL_LIST_BIT);
   glListBase(fontOffset);
   glCallLists(strlen(s), GL_UNSIGNED_BYTE, (GLubyte *) s);
   glPopAttrib ();
}

//////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

float inci = .025;
float i = inci;
int fps = 0;
int scrW = 500, scrH;

float s2e = 315;

void init(void)
{
	GLfloat mat_specular[] = {1,1,1,1};
	GLfloat mat_shininess[] = {75};
	GLfloat light_position[] = {10,10,10,0};
	GLfloat white_light[] = {1,1,1,1};
	GLfloat red_light[] = {1,0,0,1};
	GLfloat light_position1[] = {-10,-10,-10,0};
	GLfloat lmodel_ambient[] = {.1,.1,.1,1};

	glClearColor(0.0, 0.0, 0.0, 0.0);

	glShadeModel(GL_SMOOTH);
	glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
	glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
	glLightfv(GL_LIGHT0, GL_POSITION, light_position);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
	glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
	glLightfv(GL_LIGHT1, GL_POSITION, light_position1);
	glLightfv(GL_LIGHT1, GL_DIFFUSE, red_light);
	glLightfv(GL_LIGHT1, GL_SPECULAR, red_light);
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);

	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glEnable(GL_LIGHT1);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_NORMALIZE);

    makeRasterFont();
}

void movement(void)
{
	if(s2e>360){i = -i;}
	if(s2e<270){i = inci;}
	s2e = s2e + i;
	glutPostRedisplay();
}

void mouse(int button, int state, int x, int y)
{
	switch(button){
	case GLUT_LEFT_BUTTON:
		if(state==GLUT_DOWN)
			glutIdleFunc(NULL);
		else
			glutIdleFunc(movement);
		break;
	}
}

void drawLimb(void)
{
	glColor3f(1,1,1);
	glutSolidSphere(.5,30,30);
	glTranslatef(2,0,0);
	glPushMatrix();
	glScalef(3,1,1);
	glutSolidCube(1);
	glPopMatrix();
}

void drawHand(void)
{
	glColor3f(1,1,1);
	glutSolidSphere(.5,30,30);
	glTranslatef(.75,0,0);
	glPushMatrix();
	glScalef(.5,.5,1);
	glutSolidCube(1);
	glPopMatrix();
}

void drawDigit(void)
{
	glColor3f(1,1,1);
	glPushMatrix();
	//glScalef(.25,.25,.25);
	glutSolidSphere(.125,14,10);
	glTranslatef(.375,0,0);
	glScalef(2,1,1);
	glutSolidCube(.25);
	glPopMatrix();
}

void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(10,5,10,2,-7,-5,0,1,0);
	glPushMatrix();
		glRotatef(s2e,0,0,1);
		drawLimb();
		glTranslatef(2,0,0);
		glRotatef(s2e,0,1,0);
		drawLimb();
		glTranslatef(2,0,0);
		glRotatef(s2e,1,0,0);
		drawHand();
		glPushMatrix();
			glTranslatef(.375,.125,.375);
			glRotatef(s2e,0,0,1);
			drawDigit();
			glTranslatef(0,0,-.25);
			drawDigit();
			glTranslatef(0,0,-.25);
			drawDigit();
			glTranslatef(0,0,-.25);
			drawDigit();
			glTranslatef(.75,0,.75);
			glRotatef(s2e,0,0,1);
			drawDigit();
			glTranslatef(0,0,-.25);
			drawDigit();
			glTranslatef(0,0,-.25);
			drawDigit();
			glTranslatef(0,0,-.25);
			drawDigit();
		glPopMatrix();
		glTranslatef(0,0,.625);
		glRotatef(s2e,0,0,1);
		drawDigit();
		glTranslatef(.75,0,0);
		glRotatef(-s2e,0,1,0);
		drawDigit();
    	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
    glPopMatrix();


    glPushMatrix();
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,scrW,scrH,0.0);
    glColor3f(1.0, 1.0, 1.0);
    glRasterPos2i(10, 20);
//    glWindowPos2iARB(10, 20);
    printString("FPS TIMER GOES HERE");
    glPopMatrix();

	glutSwapBuffers();
}

void reshape(int w, int h)
{
    scrW = w;
    scrH = h;
	glViewport(0, 0, (GLsizei)w, (GLsizei)h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60.0,(GLfloat)w/(GLfloat)h,1.0,20.0);
}

int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(500, 500);
	glutInitWindowPosition(100,100);
	glutCreateWindow(argv[0]);
	init();
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutIdleFunc(movement);
	glutMouseFunc(mouse);
	glutMainLoop();
	return 0;
}