OpenGL reverse movement - c++

I'm trying to decrease the values of 'left_eye_b' and 'left_eye_b'and then increase the values again to simulate wink on a human I made on OpenGL.
Here I created the eye:
left_eye_b=10.0;
left_eye_s=10.0;
glColor3f(1.0f, 1.0f, 0.0f);
glBegin(GL_POLYGON);
for (float i = 0; i <= (2 * p); i += 0.001) { // Left Eye Inside
x = 10.0 * cos(i) - 24.56;
y = left_eye_s * sin(i) + 56.45;
glVertex2f(x, y);
}
glEnd();
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(2);
glBegin(GL_POINTS);
for (float i = 0; i <= (2 * p); i += 0.001) { // Left Eye Border
x = 10.0 * cos(i) - 24.56;
y = left_eye_b * sin(i) + 56.45;
glVertex2f(x, y);
}
glEnd();
Here is where I tried to make the eye wink:
void timer(int a) {
/// Simulating Wink
if (left_eye_b >= 1.12)
left_eye_b -= 0.1; // Eye Close
if (left_eye_s >= 1.12)
left_eye_s -= 0.1;
glutTimerFunc(10, timer, 1);
if (left_eye_b <= 10.0)
left_eye_b += 0.1; // Eye Open
if (left_eye_s <= 10.0)
left_eye_s += 0.1;
glutPostRedisplay();
glutTimerFunc(10, timer, 1);
}
Problem statement:
If I remove the 'eye open' part, I works perfectly and the eye closes.
However, I want to use the 'Eye Open' part to make increase the values back and
open the eye again after a few seconds.
Any suggestion about how that can be achieved?

Add a variable that indicates whether the eye will be opened or closed. The value of the variable is 1.0 to open and -1.0 to close:
double eye_change = -1.0;
Change the sign of the variable when the exe has opened or closed:
void timer(int a) {
left_eye_b += 0.1 * eye_change;
left_eye_s += 0.1 * eye_change;
if (left_eye_b <= 1.12)
eye_change = 1.0;
if (left_eye_b >= 10.0)
eye_change = -1.0;
glutPostRedisplay();
glutTimerFunc(10, timer, 1);
}

Related

OpenGL independent rotation

I'm trying to draw these shaped bellow this this:
What I want
Tried this code:
glLoadIdentity();
glColor3f(0.98f, 0.83f, 0.73f);
glBegin(GL_POLYGON);
for (float i = 0; i <= (2 * p); i += 0.001) {
x = 100 * cos(i)-10;
y = 115 * sin(i)+270;
glVertex2f(x, y);
}
glEnd();
glRotatef(-135.0f, 0.0f, 0.0f, 1.0f);
glColor3f(1.0f, 0.83f, 0.0f);
glBegin(GL_POLYGON);
for (float i = p; i <= (2 * p); i += 0.001) {
x = 100 * cos(i) - 10;
y = 115 * sin(i) + 270;
glVertex2f(x, y);
}
But this is what I get:
What I get
If I want to only use the glLoadIdentity and glRotatef for rotation, do you have any idea about how to fix it?
Note:
I don't want to use push/pop or translation
You have to rotate the object around its center and move the rotated object to its position in the world. glRotatef rotates the vertices around (0, 0). Draw the object around (0, 0) and glTranslate to move the object to its position in the world:
glTranslate(-10.0f, 270.0f, 0.0f);
glRotatef(-135.0f, 0.0f, 0.0f, 1.0f);
glColor3f(1.0f, 0.83f, 0.0f);
glBegin(GL_POLYGON);
for (float i = p; i <= (2 * p); i += 0.001) {
x = 100 * cos(i);
y = 115 * sin(i);
glVertex2f(x, y);
}
Note, the matrix operations like glRotate and glTranslate specify a new matrix and multiply the current matrix by the new matrix.
If you are not allowed to use glTranslate, you have to rotate the translation vector (-10, 270) in the opposite direction. Use the trigonometric functions sin an cos to rotate the vector (see Rotation matrix). You need to invert the angle and convert it to Radians since the unit of sin and cos is Radian.
float tx = -10.0f;
float ty = 270.0f;
float angle = -135.0f;
float inv_angle_rad = -angle * M_PI / 180.0f;
float tx_rot = tx * cos(inv_angle_rad) - ty * sin(inv_angle_rad);
float ty_rot = tx * sin(inv_angle_rad) + ty * cos(inv_angle_rad);
glRotatef(angle, 0.0f, 0.0f, 1.0f);
glColor3f(1.0f, 0.83f, 0.0f);
glBegin(GL_POLYGON);
for (float i = p; i <= (2 * p); i += 0.001) {
x = 100 * cos(i) + tx_rot;
y = 115 * sin(i) + ty_rot;
glVertex2f(x, y);
}

Error: expected primary-expression before '/' token

I have a problem with this :
"expected primary-expression before ‘/’ token"
Help me please
Thanks
the error shows on : angle = i * 2.0f * PI / numSegments;
Here is my code:
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear colour buffer
glMatrixMode(GL_MODELVIEW); // To operateon the model-view matrix
glLoadIdentity(); // Reset model-view matrix
glTranslatef(ballX, ballY, 0.0f); //translate to (xPos, yPos)
// use triangular segments to form a circle
glBegin(GL_TRIANGLE_FAN);
glColor3f(0.0f, 0.0f, 1.0f); // Biru
glVertex2f(0.0f, 0.0f); // center of circle
int numSegments = 100;
GLfloat angle;
for (int i = 0; i <= numSegments; i++) { // last vertex same as first vertex
angle = i * 2.0f * PI / numSegments;
glVertex2f(cos(angle) * ballRadius, sin(angle) * ballRadius);
}
glEnd();
glutSwapBuffers(); // Swap front and back buffers (of double buffered mode)
// animation control - compute the location for the next refresh
ballX += xSpeed;
ballY += ySpeed;
//Check if the ball exceeds the edges
if (ballX > ballXMax) {
ballX = ballXMax;
xSpeed = -xSpeed;
}
else if(ballX < ballXMin) {
ballX = ballXMin;
xSpeed = -xSpeed;
}
if (ballY > ballYMax) {
ballY = ballYMax;
ySpeed = -ySpeed;
}
else if (ballY < ballYMin) {
ballY = ballYMin;
ySpeed = -ySpeed;
}
}
Maddeningly, neither the C nor the C++ Standards define a value for pi, and the compiler error you're getting is entirely consistent with PI not being defined.
If you're not using Boost with C++, the easiest thing to do is to define PI yourself, to a very large number of decimal places in order to guard yourself against future precision changes in your types. In POSIX you could use M_PI, but then your code is not strictly portable.

in opengl, how to change mouse follow to mouse click, drag and move on release?

I have program in which the object (a triangle) follows the mouse as it moves around and rotates to the direction its moving. What do I have to do to make it so the object stays still, until I click it, drag it to a position and once I release the mouse, it starts to move to that position?
#include <GL/glut.h>
#include <math.h>
# define ANIMATION_STEP (1000/300)
# define PI 3.1415926535897932
struct Globals {
centre_x, centre_y, rotate;
float length;
float mouse_x, mouse_y, speed;
int animating;
} globals;
void init(void){
// Starting position of the triangle
globals.centre_x = 100;
globals.centre_y = 100;
globals.rotate = 0.0;
globals.mouse_x = 300.0;
globals.mouse_y = 300.0;
// Animation speed in pixels per second
globals.speed = 300.0;
// size of the triangle
globals.length = 27;
globals.animating = 1;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 1000.0, 0.0, 700.0);
}
void triangle(void){
glBegin(GL_POLYGON);
glVertex2f(0.5, 0.0);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glEnd();
}
void display(void){
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(globals.mouse_x, globals.mouse_y, 0.0);
glRotatef(globals.rotate, 0.0, 0.0, 1.0);
glScalef(globals.length, globals.length, 1.0);
triangle();
glFlush();
glutSwapBuffers();
}
float limit(float x, float min, float max){
if (x < min) {
x = min;
}
if (x > max) {
x = max;
}
return x;
}
void timer(int v){
// Computing elapsed time for smooth animation.
int time = glutGet(GLUT_ELAPSED_TIME);
float angle;
glutTimerFunc(ANIMATION_STEP, timer, time);
if (globals.animating) {
int delta_t = time - v;
float delta_x, delta_y, length, step_size;
// Compute vector from current location to mouse
delta_x = globals.mouse_x - globals.centre_x;
delta_y = globals.mouse_y - globals.centre_y;
// Compute length of the vector
length = sqrt (delta_x*delta_x + delta_y*delta_y);
// If the triangle is close to the mouse, then no motion is required.
step_size = globals.speed * delta_t / 1000.0;
if (length > step_size * 0.55) {
delta_x = delta_x / length;
delta_y = delta_y / length;
globals.centre_x += delta_x * step_size;
globals.centre_y += delta_y * step_size;
angle = atan2(delta_y, delta_x);
globals.rotate = angle * 180.0 / PI;
// Keep the triangle inside the world window.
globals.centre_x = limit(globals.centre_x, 0.0 + globals.length/2, 1000.0 - globals.length/2);
globals.centre_y = limit(globals.centre_y, 0.0 + globals.length/2, 700.0 - globals.length/2);
}
glutPostRedisplay();
}
}
void mousemotion(int x, int yc){
globals.mouse_x = x;
globals.mouse_y = 700 - yc;
glutPostRedisplay();
}
main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(1000, 700);
glutInitWindowPosition(10, 10);
glutDisplayFunc(display);
glutTimerFunc(ANIMATION_STEP, timer, 0);
glutPassiveMotionFunc(mousemotion);
init();
glutMainLoop();
return 0;
}
I have investigated processMouse() where if state == GLUT_DOWN then it records the current position and mouse press cordinates, but the best ive been able to get is that it immediately teleports to the mouse click. Can someone please explain what I would need to do to click on it, drag, release, then move to position?
in opengl, how to change mouse...
You don't. Mouse does not exist in OpenGL. Maybe you should change the question to "How to process mouse events in windows/GLUT/SDL/whatever-framework?" for which there are dozens of duplicates.

Terrain for Billboard effect in OpenGL 3.3 not working

I'm trying to run the "Cacti in the Desert" Billboard example from Chapter 15 of the OpenGL Game Programming book (Book source code available here). I'm having difficulty getting the desert terrain to be visible on my screen. I'm using GLFW for my window and the example code created it's own window, could this be the issue?
Or, could the issue be with the DisplayScene() function below. In this function, do I have to somehow set the following matrices from my camera class?
ViewMatrix = camera[currentCamera]->GetViewMatrix();
ProjectionMatrix = camera[currentCamera]->GetViewProjectionMatrix();
Here's the DisplayScene() function from the Cacti demo:
BOOL DisplayScene()
{
// used to track the orientation of the viewer
static GLfloat s_eye[] = { MAP_X * MAP_SCALE * 0.5, 8.0, -MAP_Z * MAP_SCALE * 0.5};
static GLfloat s_at[] = { 0.0, 0.0, 0.0 };
static GLfloat s_angle = -90.0;
float speed = 0.3f;
// check for rotation
if (g_keys[VK_LEFT])
{
s_angle -= 2.0;
}
if (g_keys[VK_RIGHT])
{
s_angle += 2.0;
}
// run if the shift key is pressed
if (KEY_DOWN(VK_SHIFT))
speed = speed * 2;
float rad = float(PI*s_angle/180.0f);
// check for forward and backward motion
if (g_keys[VK_UP])
{
s_eye[2] += (float)sin(rad) * speed;
s_eye[0] += (float)cos(rad) * speed;
}
if (g_keys[VK_DOWN])
{
s_eye[2] -= (float)sin(rad) * speed;
s_eye[0] -= (float)cos(rad) * speed;
}
// do bound's checking to make sure they don't leave the map
if (s_eye[0] < MAP_SCALE)
s_eye[0] = MAP_SCALE;
if (s_eye[0] > (MAP_X - 2) * MAP_SCALE)
s_eye[0] = (MAP_X - 2) * MAP_SCALE;
if (s_eye[2] < -(MAP_Z - 2) * MAP_SCALE)
s_eye[2] = -(MAP_Z - 2) * MAP_SCALE;
if (s_eye[2] > - MAP_SCALE)
s_eye[2] = -MAP_SCALE;
// set the eye position in relation to the ground
s_eye[1] = GetHeight(s_eye[0], s_eye[2]) + 2.0f;
//set the look at point to be at eye level in the direction the viewer is headed
s_at[0] = float(s_eye[0] + 100*cos(rad));
s_at[2] = float(s_eye[2] + 100*sin(rad));
s_at[1] = s_eye[1];
// set up the modelview matrix according to this viewer orientation
glLoadIdentity();
gluLookAt(s_eye[0], s_eye[1], s_eye[2],
s_at[0], s_at[1], s_at[2],
0.0, 1.0, 0.0
);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
DrawSand();
DrawCacti();
return TRUE;
} // end DisplayScene()
Here is the DrawSand() function to draw the terrain:
void DrawSand()
{
// select the sand texture
glBindTexture(GL_TEXTURE_2D, g_sand);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
// loop through all the triangle strips
for (int z = 0; z < MAP_Z-1; z++)
{
// draw the triangles in this strip
glDrawElements(GL_TRIANGLE_STRIP, MAP_X * 2, GL_UNSIGNED_INT, &g_indexArray[z * MAP_X * 2]);
}
} // end DrawSand()

Why doesn't my OpenGL Radar implementation render on screen?

I want to implement radar for my 3D game using openGL. I have been trying to accomplish it in many ways, but none of them approved to be correct. Here is the snippet of my code below:
int xi, yi;
GLfloat x,z;
glPushMatrix();
{
glTranslatef(-0.8f, 0.2f, -3.0f);
glColor3f(0.0f, 0.0f, 1.0f);
x = playerTank->givePosX();
z = playerTank->givePosZ();
xi = (int)((x + 1000) / 20) + 5;
yi = (int)(((z + 1000) / 20) + screenHeight - 105);
glPushMatrix();
{
glScalef(xi,yi,1.0f);
glBegin(GL_QUADS);
glVertex2i(xi-5, yi-5);
glVertex2i(xi+5, yi-5);
glVertex2i(xi+5, yi+5);
glVertex2i(xi-5, yi+5);
glEnd();
}
glPopMatrix();
glColor3f(1.0f, 0.0f, 0.0f);
glPushMatrix();
{
glScalef(xi,yi,1.0f);
for (int i = 0; i < tanks.size(); i++)
{
x = tanks[i]->givePosX();
z = tanks[i]->givePosZ();
xi = (int)((x + 1000) / 20) + 5;
yi = (int)(((z + 1000) / 20) + screenHeight - 105);
if (xi > 0 && xi < 110 && yi > (screenHeight - 110) && yi < screenHeight)
{
if (tanks[i] != playerTank)
{
glBegin(GL_TRIANGLES);
glVertex2i(xi, yi-5);
glVertex2i(xi+5, yi+5);
glVertex2i(xi-5, yi+5);
glEnd();
}
}
}
}
glPopMatrix();
glColor3f(0.0f, 1.0f, 0.0f);
glPushMatrix();
{
glScalef(xi,yi,1.0f);
for (int i = 0; i < obstacles.size(); i++)
{
x = obstacles[i]->givePosX();
z = obstacles[i]->givePosZ();
xi = (int)((x + 1000) / 20) + 5;
yi = (int)(((z + 1000) / 20) + screenHeight - 105);
glBegin(GL_LINE_LOOP);
glVertex2i(xi-3, yi-3);
glVertex2i(xi+3, yi-3);
glVertex2i(xi+3, yi+3);
glVertex2i(xi-3, yi+3);
glEnd();
}
}
glPopMatrix();
}
glPopMatrix();
Why doesn't anything appear on the screen?
Some things to try:
Make sure you've set up the renderer correctly - what calls are you making to set up the scene? Take a look at some of the lessons on nehe for some hints.
Make a simple square the size of the screen. You're doing something along those lines in the top section (blue quad right?) but make sure it's centred - looks like you might be pushing it up and right by 1000 units.
Check you're winding the correct way. Looks like you are from my somewhat rusty OGL memories.
Try translating in different directions. Looks to me like you might be translating the wrong way with your first translatef.