Problem with texturing and algorithma

Started by
3 comments, last by cyam95 22 years, 3 months ago
I've a few problem with this program. 1) Funtion Compare_char There is one array that contain 3 char.I want to compare this char with the user input. User must input their key according to the array. What the problem is this function didn't work exactly. 2) Funtion Compare_char When this function play the sound, the color of the quad (in Function Display) did not return to it current state. When I disable the Function Compare_char the color of quad work perfectly. 3) Texturing Quad I've done texturing to the quad. It seem that the color that I assign to the font intefere with my texture quad. If I set the color to green then my texture color will change to the green. I did not place the function here because it will make this program too long. If anyone want to look at it I will post it later. I really appreciate if anyone can help me.
      
bool keyAdown;
bool keyBdown;
float red,blue,green,g_rot;
//----------------------------------------//


//----------------------------------------//


void render_stroke_string(void* font, const char* string)  //print stroke text

{
	char* p;
	float width = 0;

	// Center Our Text On The Screen

    glPushMatrix();
	
	// Render The Text

	p = (char*) string;
	while (*p != '\0') 
		
	glutStrokeCharacter(font, *p++);
	glPopMatrix();
}

//--------------------------------------//


void keysDown(unsigned char key,int x,int y)
{

	switch(key)
	{
	case'a':
		keyAdown = 1;break;
	case'b':
		keyBdown = 1;break;
	}

}

//--------------------------------------//


void keysUp(unsigned char key,int x,int y)
{
	switch(key)
	{
	case'a':
		keyAdown = 0;break;
	case'b':
		keyBdown = 0;break;
	}

}

//-------------------------------------//


void Display(void)
{
	char str[128];

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
	glLoadIdentity();
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60.0, 1, 0.1, 1000.0); 
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glPushMatrix();
	
		glTranslatef(0.0f,4.0f,-15.0f);	 // Move One Unit Into The Screen

		glRotatef(g_rot*1.5f,0.0f,1.0f,0.0f);	// Rotate On The Y Axis

		glScalef(0.005, 0.005, 0.0);
		glColor3f(1.0f,0.0f,1.0f);
		sprintf(str, "NeHe ",g_rot/50);		// Print GL Text To The Screen

		render_stroke_string(GLUT_STROKE_ROMAN, str);
		g_rot += 0.5f;
	
	glPopMatrix();
	
	glPushMatrix();							//draw first cube


		glTranslatef(0.0, 0.0, -5.0);


		if (keyAdown)
		{
	
			red=1.0f; green=0.0f; blue=0.0f;
		}

		else
		{
			red=0.0f; green=1.0f;blue=0.0f;
		}

		glColor3f(red,blue,green);

		glBegin(GL_QUADS);
		 
		glVertex3f(-1.0f, -1.0f,  1.0f);	// Bottom Left Of The Texture and Quad

		glVertex3f( 1.0f, -1.0f,  1.0f);	// Bottom Right Of The Texture and Quad

		glVertex3f( 1.0f,  1.0f,  1.0f);	// Top Right Of The Texture and Quad

		glVertex3f(-1.0f,  1.0f,  1.0f);
	
		glEnd();

	glPopMatrix();

	glPushMatrix();							//draw second cube


		glTranslatef(-3.0, 0.0, -5.0);

		if (keyBdown)
		{
	
			red=1.0f; green=1.0f; blue=0.0f;
		}

		else
		{
			red=0.0f; green=1.0f;blue=1.0f;
		}

		glColor3f(red,blue,green);
		
		glBegin(GL_QUADS);
	 
		glVertex3f(-1.0, -1.0, -2.0);
		glVertex3f(1.0, -1.0, -2.0);
		glVertex3f(1.0, 1.0, -2.0);
		glVertex3f(-1.0,1.0,-2.0);
		
		glEnd();
	
	glPopMatrix();


	glutPostRedisplay();

	glutSwapBuffers();
}

//----------------------------------------//


void Init(void)
{

	glClearColor(0.0, 0.0, 0.0, 0.0);
	

}

//----------------------------------------//


void Resize(int width, int height)
{
	glViewport(0, 0, width, height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60.0, width/height, 0.1, 1000.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

//----------------------------------------//


void Compare_char(unsigned char key,int x,int y) //comparing input from user

	{
		char array1[3] = {'a','b','c'};
		int i;

		bool same = false; 
		
		while (!same)
		{ 

			for(i=0;i<3 && !same;i++) 
	
			{ 
				if (key == array1[i]) 
				{	
					
					PlaySound("True.wav", NULL, SND_ASYNC);
					same = true; 
					
				} 
			}

			if (!same)
			{  PlaySound("False.wav", NULL, SND_ASYNC);
				
			}
			 

		}
		
	}


//----------------------------------------//


int main(int argc, char **argv)

{

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(600, 600);
	glutInitWindowPosition(200, 200);
	glutCreateWindow("First OpenGL Program");

	Init();

	glutDisplayFunc(Display);

	glutKeyboardFunc(keysDown);
	glutKeyboardUpFunc(keysUp);
	glutKeyboardUpFunc(Compare_char);

	glutReshapeFunc(Resize);

	glutMainLoop();

	return 0;
}      
Edited by - cyam95 on December 31, 2001 3:28:32 AM Edited by - cyam95 on December 31, 2001 3:30:38 AM Edited by - cyam95 on December 31, 2001 3:34:27 AM
Thanx~cyam95~
Advertisement
Looks like you are reading past of the char array ,
when you initialize the char str[128] try to set to ''0''
just my 2 cents, the code looks good although


quote:
There is one array that contain 3 char.I want to compare this char with the user input.
User must input their key according to the array. What the problem is this function didn''t work
exactly.


What do you mean by ''didn''t work exactly'' ?

quote:
When this function play the sound, the color of the quad (in Function Display) did not return
to it current state. When I disable the Function Compare_char the color of quad work perfectly.


How does it affect it ? Does it always turn the color to the same one or is it something quite random.

quote:Original post by cyam95
I''ve done texturing to the quad. It seem that the color that I assign to the font intefere with
my texture quad. If I set the color to green then my texture color will change to the green.


It''s a normal feature. With default settings, the texels computed by the texture unit are scaled by the current color before being put to the frame buffer. You can set the current color to white or set your texture environement to GL_REPLACE (instead of GL_MODULATE, the default one), this way the current color will be ignored when texturing is enabled.

Hint : when you post some piece of code, put it between [ source ] and [ /source ] (whithout the extra spaces), it looks much better.

Thanx everyone...


quote:There is one array that contain 3 char.I want to compare this char with the user input.
User must input their key according to the array. What the problem is this function didn't work
exactly.

First let me explain what this function should do.
This function should compare the user input with the array.
If user press any key then this function will compare the key with the first array item that is 'a'. If it true it will move to the second array and so on, if not it will keep comparing the key that user press.

When I run the program it didn't work exactly because if u press any key that contain in the array the function will return true no matter what key( that contain in the array) u press first.


quote:
When this function play the sound, the color of the quad (in Function Display) did not return to it current state.
When I disable the Function Compare_char the color of quad work perfectly.

Say that I assign red color to the quad then when the user press the key the color will change to the green.
When the key is up (Function keysUp) the color change back to the original state (red).
Right now when the sound is play the quad color will change to the green but did not change back to the original state(red), it remain in green color.

I hope u can understand what I'm talking about. Sorry about the way I post the source code, I didn't know how to post it correctly. Thanx 4 ur hint.






Edited by - cyam95 on December 31, 2001 3:25:05 AM

Edited by - cyam95 on December 31, 2001 3:41:04 AM
Thanx~cyam95~
quote:You can set the current color to white or set your texture environement to GL_REPLACE (instead of GL_MODULATE, the default one), this way the current color will be ignored when texturing is enabled.


I''ve try it but the color of the first texture will have an impact to my font.
If the texture have a green color then my font will change to green color.

It happen only with the first texture. What is wrong here ?
Thanx~cyam95~

This topic is closed to new replies.

Advertisement