I’ve a few problem with this program.
- 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.
- 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.
- 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.
#include <stdio.h>
#include <gl/glut.h>
#include <gl/glaux.h>
#include <gl/glu.h>
#include <windows.h>//----------------------------------------//
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;
}
[This message has been edited by cyam95 (edited 12-31-2001).]
[This message has been edited by cyam95 (edited 12-31-2001).]
[This message has been edited by cyam95 (edited 12-31-2001).]