Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ OpenGL
Okay well i just pulled out this code to test the movement of a square, but i have two problems and one question on structure. first two problems are every time the frame renders the square becomes smaller till it can't be seen in about 3 renders, secondly the movement function doesn't seem to work when the place i pulled it from says it apparently should work which brings me to my question about structure, What is the switch, case thing that i have written in movement, it there a tutorial on it because it seems interesting and useful.
 

 

#include <iostream>
#include <gl\glut.h>
using namespace std;
unsigned int Character(0);
float Posx(1);
float Posy(1);
int Test(0);
void DisplayListInit()
{
	Character = glGenLists(1);
	glNewList(Character, GL_COMPILE);
		glBegin(GL_QUADS);
			glColor3i(0, 0, 1);
			glVertex2f(-1, -1);
			glVertex2f(-1, 1 );
			glVertex2f( 1, 1 );
			glVertex2f( 1, -1);
		glEnd();
	glEndList();
	cout << "List Declared\n";
}
void Render()
{
	glClearColor(1, 1, 1, 1);
	glClearDepth(0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glOrtho(-10, 10, -10, 10, -1, 1);
	glPushMatrix();
		glTranslatef(Posx, Posy, 1);	
		glCallList(Character);
	glPopMatrix();
	glutSwapBuffers();
	cout << "Render Complete\n";
	if(Test < 1){
		glutPostRedisplay();
		Test ++;
	}
}
void Movement(unsigned char key, int x, int y)
 
	switch (key){
		case 'a':
			Posx -= 0.5;
			break;
		case 's':
			Posy -= 0.5;
			break;
		case 'd':
			Posx += 0.5;
			break;
		case 'w':
			Posy += 0.5;
			break;
		default:
			break;
	}
	glutPostRedisplay();
	cout << "****************Movement done\n";
}
int main (int argc, char **argv )
{
	glutInit(&argc, argv);
	glutInitWindowSize(800, 800);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
	glutCreateWindow("Boxs");
	DisplayListInit();
	glutDisplayFunc(Render);
	glutKeyboardFunc(Movement);
	//glutIdleFunc(Render);
	glutMainLoop();
}
Posted 18-Jun-11 22:44pm
Comments
CPallini at 19-Jun-11 8:19am
   
Adventuring in OpenGL development without knowing the basic constructs of the C/C++ programming languages makes you a very brave programmer.
Joshua Waring at 24-May-14 4:50am
   
Haha thank you, but I'd say after 3 years it's payed off!
CPallini at 24-May-14 4:58am
   
:thumbsup:
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I hate to say this but there seems little point in you trying to debug this code when you apparently do not even understand what a switch statement does. Get hold of a C++ book, search Google, or look in MSDN[^] for some useful information about coding in C++.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

First problem caused by improper usage of glOrtho.
The glOrtho function multiplies the current matrix by an orthographic matrix.
First time projection matrix is multipled for ortho projection.
Next renderings also multiplies previous matrix again and will cause issue.
 
This issue can be solved by calling glOrtho() once.
Or call glLoadIdendity() before calling glOrtho.
 
glClearColor(1, 1, 1, 1);
    glClearDepth(0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity(); // Reset your current matrix with idendity matrix.
    glOrtho(-10, 10, -10, 10, -1, 1);
    glPushMatrix();
    glTranslatef(Posx, Posy, 1);
    glCallList(Character);
    glPopMatrix();
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 150
2 Richard MacCutchan 135
3 PIEBALDconsult 110
4 Sergey Alexandrovich Kryukov 104
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 30 Jun 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100