Weird Translations

Started by
17 comments, last by adam_o 17 years, 4 months ago
Quote:Original post by jyk
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.


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;	}}
_______________________My computer stats:Xcode 3.1.2Mac OS 10.5.8---Visual C++ 2008 Express EditionWindows XP---NetBeansUbuntu 9.04---Help needed here!
Advertisement
Quote:Original post by adam_o
Quote:Original post by jyk
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.


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.
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();}
Quote:Original post by Zahlman
Cleaned 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?
_______________________My computer stats:Xcode 3.1.2Mac OS 10.5.8---Visual C++ 2008 Express EditionWindows XP---NetBeansUbuntu 9.04---Help needed here!
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.
Quote:Original post by Zahlman
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.


ookk...
_______________________My computer stats:Xcode 3.1.2Mac OS 10.5.8---Visual C++ 2008 Express EditionWindows XP---NetBeansUbuntu 9.04---Help needed here!
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?
_______________________My computer stats:Xcode 3.1.2Mac OS 10.5.8---Visual C++ 2008 Express EditionWindows XP---NetBeansUbuntu 9.04---Help needed here!
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.
Quote:Original post by Zahlman
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.


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?
_______________________My computer stats:Xcode 3.1.2Mac OS 10.5.8---Visual C++ 2008 Express EditionWindows XP---NetBeansUbuntu 9.04---Help needed here!

This topic is closed to new replies.

Advertisement