Weird Translations

Recommended Posts

So I set up a program using C++ (cocoa) and OpenGL and told it to translate some stuff for me. I would press w to translate one way and x to translate another. I told it to increment these translations by 0.1 and whenever I get past 0.3 or -0.3, it will keep getting smaller or bigger until it goes off the screen, no matter which one I press. File is given below, stats in my signature. http://idisk.mac.com/adam_o/weird_translations.cpp

Share on other sites
Your source file is blocked. (SC_AUTHORIZATION_REQUIRED!!!)

Share on other sites
Quote:
 Original post by Omega147Your source file is blocked. (SC_AUTHORIZATION_REQUIRED!!!)

Hmm... I don't know what that is... it worked on my computer, but it might just be in my keychain... I'm still learning how to work this dotmac stuff... will post when it's updated...

Share on other sites
You can just paste your code directly into your post (use [source] tags to preserve formatting).

Share on other sites
Quote:
 Original post by jykYou can just paste your code directly into your post (use tags to preserve formatting).

Will that work for 100+ lines of code?

EDIT: Try this:

Share on other sites
same issue here, need auth...

Share on other sites
Quote:
Quote:
 Original post by jykYou can just paste your code directly into your post (use tags to preserve formatting).

Will that work for 100+ lines of code?

EDIT: Try this:

You could put your code in here, just be sure to use source tags like this:

Code goes here!
(edit my post to see the use for source tags)

Otherwise, your 100+ lines would stretch on for a while, cluttering the thread. Making the actual file available is better when you have that much source to show, so maybe just find a different host server to put it up somewhere or something.

Share on other sites
Here goes nothing!
#include <GLUT/glut.h>using namespace std;GLfloat ztrans;void draw();void init();void kybrd(unsigned char key, int x, int y);int main(int argc, char** argv){	glutInit(&argc, argv);	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);	glutInitWindowSize(500, 500);	glutInitWindowPosition(100, 100);	glutCreateWindow("Weird Translations");	init();	glutKeyboardFunc(kybrd);	glutDisplayFunc(draw);	glutMainLoop();	return 0;}void drawString(GLint x, GLint y, const char *string) {	int i;	glPushAttrib(GL_TRANSFORM_BIT | GL_CURRENT_BIT);	glMatrixMode(GL_PROJECTION);	glPushMatrix();	glLoadIdentity();	glOrtho(0, 640, 0, 480, -10.0, 10.0);	glMatrixMode(GL_MODELVIEW);	glPushMatrix();	glLoadIdentity();	glColor4f(1.0, 1.0, 1.0, 1.0);	glRasterPos2i(x, y);	int len = (int)strlen(string);	for ( i = 0; i < len; i++) 	{		glutBitmapCharacter(GLUT_BITMAP_8_BY_13, string[i]);	}	glPopMatrix();	glMatrixMode(GL_PROJECTION);	glPopMatrix();	glPopAttrib();}void draw(){	char infostring[1024];	glTranslatef(0.0, 0.0, ztrans);	glClear(GL_COLOR_BUFFER_BIT);	glBegin(GL_QUADS);	glVertex3f(0.0, 0.0, -5.0);	glVertex3f(1.0, 0.0, -5.0);	glVertex3f(1.0, 1.0, -5.0);	glVertex3f(0.0, 1.0, -5.0);	glEnd();	snprintf(infostring, 1023, "Keys are 'w', 'x' or ESC to exit, ztrans = %f", ztrans);	drawString(10, 10, infostring);	glFlush();}void init(){	glClearColor(0.0, 0.0, 0.0, 0.0);	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	gluPerspective(100.0, 10/10, 0.001, 10000.0);	glMatrixMode(GL_MODELVIEW);}void kybrd(unsigned char key, int x, int y){	switch (key)	{		case 'w':		{			ztrans += 0.1;			glutPostRedisplay();			break;		}		case 'x':		{			ztrans -= 0.1;			glutPostRedisplay();			break;		}		case 27:		{			exit(0);			return;		}		default: return;	}}

Share on other sites
You realize that in one second you can have over 60 frames and that would lead to moving at least 6 units per second. My advice would be to set a breaker after like 50 loops in the draw section and see what your ztrans value is. If it seems right, then its your translation, otherwise it might just be your actual ztrans, which is whats wrong most of the time with mine.

Share on other sites
There are a couple of things in your code I'm not sure about, but I would try adding a call to glLoadIdentity() before the line:
glTranslatef(0.0, 0.0, ztrans);
As it is now, you're accumulating the translation from frame to frame in the OpenGL modelview matrix, which is not what you want.

Share on other sites
Quote:
 Original post by jykThere are a couple of things in your code I'm not sure about, but I would try adding a call to glLoadIdentity() before the line:glTranslatef(0.0, 0.0, ztrans);As it is now, you're accumulating the translation from frame to frame in the OpenGL modelview matrix, which is not what you want.

So in the "kybrd" function I should put something like:

void kybrd(unsigned char key, int x, int y){	switch (key)	{		case 'w':		{			ztrans = 0.1;			glutPostRedisplay();			break;		}		case 'x':		{			ztrans = -0.1;			glutPostRedisplay();			break;		}		case 27:		{			exit(0);			return;		}		default: return;	}}

Share on other sites
Quote:
Quote:
 Original post by jykThere are a couple of things in your code I'm not sure about, but I would try adding a call to glLoadIdentity() before the line:glTranslatef(0.0, 0.0, ztrans);As it is now, you're accumulating the translation from frame to frame in the OpenGL modelview matrix, which is not what you want.

So in the "kybrd" function I should put something like:

*** Source Snippet Removed ***
That should have more or less the same effect, but in general it's better to manage incremental transforms on your end rather than having OpenGL do it.

As such, the function was fine as it was before. I would instead recommend adding glLoadIdentity() as described in my previous post.

Share on other sites
Cleaned up a bit:

#include <GLUT/glut.h>#include <string>#include <sstream>using namespace std;GLfloat ztrans;void drawString(GLint x, GLint y, const string& s) {	glPushAttrib(GL_TRANSFORM_BIT | GL_CURRENT_BIT);	glMatrixMode(GL_PROJECTION);	glPushMatrix();	glLoadIdentity();	glOrtho(0, 640, 0, 480, -10.0, 10.0);	glMatrixMode(GL_MODELVIEW);	glPushMatrix();	glLoadIdentity();	glColor4f(1.0, 1.0, 1.0, 1.0);	glRasterPos2i(x, y);	for (string::iterator it = s.begin(); it != s.end(); ++it) 	{		glutBitmapCharacter(GLUT_BITMAP_8_BY_13, *it);	}	glPopMatrix();	glMatrixMode(GL_PROJECTION);	glPopMatrix();	glPopAttrib();}void draw(){	glTranslatef(0.0, 0.0, ztrans);	glClear(GL_COLOR_BUFFER_BIT);	glBegin(GL_QUADS);	glVertex3f(0.0, 0.0, -5.0);	glVertex3f(1.0, 0.0, -5.0);	glVertex3f(1.0, 1.0, -5.0);	glVertex3f(0.0, 1.0, -5.0);	glEnd();	stringstream ss("Keys are 'w', 'x' or ESC to exit, ztrans = ");	ss << ztrans;	drawString(10, 10, ss.str());	glFlush();}void init(){	glClearColor(0.0, 0.0, 0.0, 0.0);	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	gluPerspective(100.0, 1.0, 0.001, 10000.0);	glMatrixMode(GL_MODELVIEW);}void handle_keyboard(unsigned char key, int, int){	switch (key)	{		case 'w':		{			ztrans += 0.1;			glutPostRedisplay();			break;		}		case 'x':		{			ztrans -= 0.1;			glutPostRedisplay();			break;		}		case 27:		{			exit(0);		}	}}int main(int argc, char** argv){	glutInit(&argc, argv);	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);	glutInitWindowSize(500, 500);	glutInitWindowPosition(100, 100);	glutCreateWindow("Weird Translations");	init();	glutKeyboardFunc(handle_keyboard);	glutDisplayFunc(draw);	glutMainLoop();}

Share on other sites
Quote:
 Original post by ZahlmanCleaned up a bit:*** Source Snippet Removed ***

Is there any way you could quickly comment to show what you fixed or just show where it is?

Share on other sites
Functions are reorganized to remove the need for prototypes, and std::string is used for representing text (along with std::stringstream to accumulate text). Beyond that, nothing major.

Share on other sites
Quote:
 Original post by ZahlmanFunctions are reorganized to remove the need for prototypes, and std::string is used for representing text (along with std::stringstream to accumulate text). Beyond that, nothing major.

ookk...

Share on other sites
Ok, new question. My code creates a stretched box, but I told it to make a square. I know that gluPerspective() and glutInitWindowSize() effect the way this is stretched, but what numbers should I put in them?? I want to create a 1024 by 768 window, and my computer is 1280 by 854. I created four variables, as you will see below, that control this, two for my window size, two for the resolution of my screen.

int xdimension = 1024;int ydimension = 768;int realxdimension = 1280;int realydimension = 854;//...glutInitWindowSize(xdimension, ydimension);//...gluPerspective(100.0, realxdimension/realydimension, 0.001, 10000.0);

I hoped that this would work, but it doesn't. What gives?

Share on other sites
Dividing two integers performs integer division (which discards the remainder). You are interested in the ratio as a floating-point value, so you must cast either or both to a floating-point type before performing the division.

Share on other sites
Quote:
 Original post by ZahlmanDividing two integers performs integer division (which discards the remainder). You are interested in the ratio as a floating-point value, so you must cast either or both to a floating-point type before performing the division.

So I nead to make them floating point numbers (so I guess since I'm using OpenGL then GLfloat) to get it to work nicely?

Create an account

Register a new account

• Partner Spotlight

• Forum Statistics

• Total Topics
627682
• Total Posts
2978615

• 9
• 13
• 12
• 10
• 12