Open GL Rotation - c++

i'm trying to draw 100 arrows in a grid and trying to rotate each arrow according to the size of the input i read. But here when i try to rotate one arrow, all the arrows rotate at once with it. whats wrong with it ?
void drawArrow(void) {
float y1 = 2.0;
float y2 = 2.15;
float y3 = 2.4;
int count2 = 0;
for (float col = 0; col < 10; col++) {
y1 -= 0.5;
y2 -= 0.5;
y3 -= 0.5;
float x1 = -2.25;
float x2 = -2.20;
float x3 = -2.15;
float x4 = -2.35;
float x5 = -2.30;
for (float rows = 0; rows < 10; rows++) {
glPushMatrix();
glTranslatef(-0.35 + 0.5 - 0.1, 0.4 - 0.01 + 0.1, 0.0);
float m = (windx[count2] * windx[count2]) + (windy[count2] * windy[count2]);
float rotator = sqrt(m);
glRotatef(rotator-50, 0.0, 0.0, 1.0);
glBegin(GL_LINE_STRIP);//start drawing a line loop
glColor3f(1.0, 0.0, 0.0);
glVertex3f(x1, y1, 0.0f);
glVertex3f(x2, y2, 0.0f);
glVertex3f(x3, y2, 0.0f); // drawing a arrow
glVertex3f(x1, y3, 0.0f);
glVertex3f(x4, y2, 0.0f);
glVertex3f(x5, y2, 0.0f);
glVertex3f(x1, y1, 0.0f);
x1 += .5;
x2 += .5;
x3 += .5;
x4 += .5;
x5 += .5;
count2 ++;
glEnd();//end drawing of line loop
glPopMatrix();
}
}
int main(int argc, char **argv) {
glMatrixMode(GL_PROJECTION);
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE); // Set up a basic display buffer (only single buffered for now)
glutInitWindowSize(1000, 800); // Set the width and height of the window
glutInitWindowPosition(100, 100); // Set the position of the window
glutCreateWindow("Weather Analysis"); // Set the title for the window
readDataFile(); //Read Data From File
glutKeyboardFunc(keyboard);
glutDisplayFunc(display); // Tell GLUT to use the method "display" for rendering
glutReshapeFunc(reshape); // Tell GLUT to use the method "reshape" for reshaping
glutMainLoop(); // Enter GLUT's main loop
void reshape(int width, int height) {
glViewport(0, 0, (GLsizei)width, (GLsizei)height); // Set our viewport to the size of our window
glMatrixMode(GL_PROJECTION); // Switch to the projection matrix so that we can manipulate how our scene is viewed
glLoadIdentity(); // Reset the projection matrix to the identity matrix so that we don't get any artifacts (cleaning up)
gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0); // Set the Field of view angle (in degrees), the aspect ratio of our window, and the new and far planes
glMatrixMode(GL_MODELVIEW); // Switch back to the model view matrix, so that we can start drawing shapes correctly
}
void display(void) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Clear the background of our window to red
glClear(GL_COLOR_BUFFER_BIT); //Clear the colour buffer (more buffers later on)
glLoadIdentity(); // Load the Identity Matrix to reset our drawing locations
glTranslatef(0.0f, 0.5f, -6.0f);
renderPrimitive(); // Render the primitive
drawArrow();
glFlush(); // Flush
}
and ALSO i dont know what to enter in the GLtranslatef method. Im really a beginner, sorry.

Try to draw your arrow in one rutine lets call it DrawArrow I suggest it size must be between [-1.0f; 1.0f] than place it at the proper position using affine transform first of all translation secondly rotation.
void DrawArrow(void)
{
float x1 = 0;
float x2 = -0.05;
float x3 = -0.1;
float x4 = 0.1;
float x5 = 0.05;
float y1 = -0.15;
float y2 = 0;
float y3 = 0.15;
glBegin(GL_LINE_STRIP);//start drawing a line loop
glColor3f(1.0, 0.0, 0.0);
glVertex3f(x1, y1, 0.0f);
glVertex3f(x2, y2, 0.0f);
glVertex3f(x3, y2, 0.0f); // drawing a arrow
glVertex3f(x1, y3, 0.0f);
glVertex3f(x4, y2, 0.0f);
glVertex3f(x5, y2, 0.0f);
glVertex3f(x1, y1, 0.0f);
glEnd();//
}
Than your array drawing function like this (you may correct inacurrances):
void drawArrows(void) {
float y1 = 2.0;
int count2 = 0;
for (float col = 0; col < 10; col++) {
y1 -= 0.5;
float x1 = -2.25;
for (float rows = 0; rows < 10; rows++) {
glPushMatrix();
glTranslatef(x1, y1, 0.0);
float m = (windx[count2] * windx[count2]) + (windy[count2] * windy[count2]);
float rotator = sqrt(m);
glRotatef(rotator-50, 0.0, 0.0, 1.0);
DrawArrow();
x1 += .5;
count2 ++;
glPopMatrix();
}
}
}
I have made some tests the result will be like this (with constant rotator).

Related

how can I add collision to my object and around the screen in my program?

I am trying to create a car game with along with obstacles. I want to add collision so every time my object hits one of the obstacles, obstacle changes color and we know there has been a collision. I also want there to be a collision around my screen so that my object can not leave the view port?
--void 'draw car' is my movable object
#include "include\freeglut.h" // OpenGL toolkit - in the local shared folder
#include <cmath>
#include <iostream>
//set up some constants
#define X_CENTRE 0.0 /* centre point of square */
#define Y_CENTRE 0.0
#define LENGTH 5.0 /* lengths of sides of square */
//forward declaration - best in the header
void drawStar(GLfloat radius, GLfloat x, GLfloat y);
void drawSquare(GLfloat length, GLfloat x, GLfloat y);
void drawRect(GLfloat lengthX, GLfloat lengthY, GLfloat x, GLfloat y);
void drawCar(GLfloat length, GLfloat x, GLfloat y);
GLfloat red = 1.0, green = 1.0, blue = 1.0;
GLint xmove = -12.0, ymove = -12.5, zmove = 0.0;
/* reshape callback function
executed when window is moved or resized. This function should be used in Tutorial 1 */
void reshape(int width, int height)
{
GLfloat aspect = (GLfloat)width / (GLfloat)height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix
glLoadIdentity();
// glOrtho(-10.0, 10.0, -10.0, 10.0, -1.0, 1.0); //sets the x,y,z plane from -1 to 1
if (width <= height) //if aspect is less or equal to 1
glOrtho(-10.0, 10.0, -10.0/aspect, 10.0/aspect, -1.0, 1.0);
else // aspect is greater than 1
glOrtho(-10.0 * aspect, 10.0* aspect, -10.0 , 10.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
/* display callback function
called whenever contents of window need to be re-displayed */
//this is the all important drawing method - all drawing code goes in here
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT); /* clear window */
/*glColor3f(1.0, 1.0, 1.0); /* white drawing objects */
glColor3f(0.0, 0.0, 1.0); /* blue drawing objects */
/* define object to be drawn as a square polygon */
glColor3f(0.3, -3.0, 4.0);
drawSquare(2, -3, 8);
glColor3f(0.3, -1.0, 2.0);
drawSquare(2, -3, 8);
glColor3f(1.0, 0.0, 0.0);
drawSquare(2, -5, -6);
glColor3f(0.0, 1.0, 0.0);
drawSquare(2, 0.0, 0.0);
glColor3f(2.0, 3.0, 1.1);
drawSquare(2, 6.0, 0.0);
glColor3f(0.3, -1.0, 2.0);
drawSquare(2, -6, 5);
glColor3f(0.7, -1.0, 2.0);
drawSquare(2, 5, -5);
glColor3f(0.0, 0.0, 1.0);
drawSquare(2, 5, 8);
glColor3f(0.75, 0.5, 0.25);
drawCar(1,0,0);
glFlush(); /* execute drawing commands in buffer */
}
void drawCar(GLfloat length, GLfloat x, GLfloat y) {
glPushMatrix();
glTranslatef(xmove, ymove, zmove);
//glBegin(GL_TRIANGLES);
////specify the vertices (points in 3D space) of the shape - note that these are 2D points
//glVertex2f(X_CENTRE - LENGTH / 3, Y_CENTRE - LENGTH / 3);
//glVertex2f(X_CENTRE - LENGTH / 3, Y_CENTRE + LENGTH / 3);
//glVertex2f(X_CENTRE + LENGTH / 3, Y_CENTRE + LENGTH / 3);
//glVertex2f(X_CENTRE + LENGTH / 3, Y_CENTRE - LENGTH / 3);
//glEnd();
glRectf(3, 4, 6, 2);
glPopMatrix();
glFlush();
}
void keyInput(unsigned char key, int x, int y)
{
switch (key)
{
case 'w':
ymove++;
if (ymove >= 10) ymove = 3.0;
break;
case 's':
ymove--;
// if (ymove <= -5) ymove = 0.0;
break;
case 'd':
xmove++;
// if (xmove >= 5) xmove = 0.0;
break;
case 'a':
xmove--;
// if (xmove <= -5) xmove = 0.0;
break;
}
glutPostRedisplay();
}
void drawSquare(GLfloat length, GLfloat x, GLfloat y)
{
//x1,y1 is the top left-hand corner coordinate
GLfloat x1, y1, x2, y2, x3, y3, x4, y4;
//glColor3f(0.1, 1.0, 0.2);
x1 = x - length / 2;
y1 = y + length / 2;
x2 = x + length / 2;
y2 = y + length / 2;
x3 = x + length / 2;
y3 = y - length / 2;
x4 = x - length / 2;
y4 = y - length / 2;
glBegin(GL_POLYGON);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glVertex2f(x3, y3);
glVertex2f(x4, y4);
glEnd();
glFlush();
}
void drawRect(GLfloat lengthX, GLfloat lengthY, GLfloat x, GLfloat y)
{
//x1,y1 is the top left-hand corner coordinate
GLfloat x1, y1, x2, y2, x3, y3, x4, y4;
//This example is for a rectangle
x1 = x - lengthX / 2;
y1 = y + lengthY / 2;
x2 = x + lengthX / 2;
y2 = y + lengthY / 2;
x3 = x + lengthX / 2;
y3 = y - lengthY / 2;
x4 = x - lengthX / 2;
y4 = y - lengthY / 2;
glBegin(GL_POLYGON);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glVertex2f(x3, y3);
glVertex2f(x4, y4);
glEnd();
glFlush();
}
//Draws a 5 pointed star using lines
void drawStar(GLfloat radius, GLfloat x, GLfloat y)
{
//x1,y1 is the top coordinate
//glColor3f(0.0, 1.0, 1.0);
GLfloat x1, y1, x2, y2, x3, y3, x4, y4, x5, y5;
x1 = x;
y1 = y + radius;
x2 = x + 0.90 * radius;
y2 = y + 0.40 * radius;
x3 = x + 0.65 * radius;
y3 = y - 0.55 * radius;
x4 = x - 0.65 * radius;
y4 = y - 0.55 * radius;
x5 = x - 0.90 * radius;
y5 = y + 0.40 * radius;
glLineWidth(1.0);
glBegin(GL_LINES);
glVertex2f(x1, y1);
glVertex2f(x3, y3);
glVertex2f(x1, y1);
glVertex2f(x4, y4);
glVertex2f(x2, y2);
glVertex2f(x4, y4);
glVertex2f(x2, y2);
glVertex2f(x5, y5);
glVertex2f(x3, y3);
glVertex2f(x5, y5);
glEnd();
glFlush();
}
/* graphics initialisation */
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0); //Setting up the background color
}
int main(int argc, char** argv)
{
/* window management code ... */
/* initialises GLUT and processes any command line arguments */
glutInit(&argc, argv);
/* use single-buffered window and RGBA colour model */
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
/* window width = 400 pixels, height = 400 pixels */
/* window width = 640 pixels, height = 480 pixels for a 4:3 ascpect ratio */
/* window width = 1024 pixels, height = 576 pixels for a 16:9 ascpect ratio */
glutInitWindowSize(750, 750);
/* window upper left corner at (100, 100) */
glutInitWindowPosition(100, 100);
/* creates an OpenGL window with command argument in its title bar */
glutCreateWindow("Example 1");
glutKeyboardFunc(keyInput);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
Keep track of your window size (750, 750), and your orthographic projection multiplier (10.0), then you should be able to tell when a certain world space coordinate is at the edge of the screen from these two.

How to fix zoom in & out with mouse scroll wheel?

I'm trying to make a program where I'm able to zoom in & out on the figures I've drawn, but is not doing it; it's switching the orientation of the figures and if I keep scrolling, it just disappears. The function that are suppose to be executed for this to work are the MouseFunc() along with the renderScene(). Any explanation for the problem or help is welcomed.
#include"glut.h"
#include<cmath>
#include<iostream>
using namespace std;
float xr = 0, yr = 0; //to control the object's movement from left to right
// XZ position of the camera
float x = 0.0f, z = 5.0f; //Module 4
float angleX = 0.0f; //Module 4
//Shift + ArrowKey rotation
float transX = 0.0f;
float transY = 0.0f;
float rotY = 0.0f;
//end
//Mouse Commands
//float ZoomFactor = 0.5;
GLfloat theta3 = 0;
GLfloat phi = 0;
GLfloat rho = 5;
GLfloat camX = 0;
GLfloat camY = 0;
GLfloat camZ = 0;
GLfloat upX = 0;
GLfloat upY = 0;
GLfloat upZ = 0;
//end
GLfloat angle = 0.0f;
int refreshmill = 1;
void timer(int value) { //to control the rotation of the object
glutTimerFunc(refreshmill, timer, 0);
}
void myDisplay(void) {
//Circle One
float theta;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 0, 0);
glPushMatrix();
glBegin(GL_POLYGON);
for (int x = 0; x < 360; x++) {
theta = x * 3.142 / 180;
glVertex2f(150 * cos(theta) + xr, 150 * sin(theta) + yr);
}
glEnd();
glPopMatrix();
//Circle Two
float theta2;
glPushMatrix();
glTranslatef(0.5f, 0.0f, 0.0f); // rotation
glRotatef(angle, 0.0f, 0.0f, -0.5f); // rotation
glBegin(GL_POLYGON);
glColor3f(0, 0, 1);
for (int x = 0; x < 360; x++) {
theta2 = x * 3.142 / 180;
glVertex2f(150 + 15 * cos(theta2) + xr, 15 * sin(theta2) + yr);
}
angle += 0.2; // rotation
glEnd();
glPopMatrix();
//Draw Star
glColor3ub(119, 193, 15);
glPushMatrix();
glBegin(GL_POLYGON);
glVertex2d(15 + xr, 60 + yr);
glVertex2d(75 + xr, 75 + yr); //right peak
glVertex2d(15 + xr, 90 + yr);
glVertex2d(0 + xr, 150 + yr); //Up-peak Changed
glVertex2d(-15 + xr, 90 + yr);
glVertex2d(-75 + xr, 75 + yr);
glVertex2d(-15 + xr, 60 + yr);
glVertex2d(0 + xr, 0 + yr);
glEnd();
glPopMatrix();
}
//Close code
void handleKeypress(unsigned char key, int x, int y){
switch (key){
case 27: //when the escape key is pressed the program will exit.
exit(0);
}
}
//Movement of drawing
void keyboard(int key, int x, int y) {
float fraction = 0.1f;
bool shift = false;
int mod = glutGetModifiers();
if (mod == GLUT_ACTIVE_SHIFT) {
shift = true;
}
if (!shift) {
switch (key) {
case GLUT_KEY_RIGHT: xr++; break;
case GLUT_KEY_LEFT: xr--; break;
case GLUT_KEY_UP: angleX -= 1.0f; break; //Module 4
case GLUT_KEY_DOWN: angleX += 1.0f; break; //Module 4
}
}
else {
switch (key) {
case GLUT_KEY_LEFT:// RotaciĆ³n del dibujo hacia la izquierda en el eje de Y
rotY -= 1.0f;
break;
case GLUT_KEY_RIGHT:// RotaciĆ³n del dibujo hacia la derecha en el eje de Y
rotY += 1.0f;
break;
}
}
}
//Mouse Function
void MouseFunc(int button, int state, int x, int y){
if (button > 4){
rho = rho + 3.0;
}
else (button < 3);{
rho = rho - 3.0;
}
}
void renderScene(void) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 1.0, 0.0);
glRotatef(rotY, 0.0, 1.0, 0.0); //Rotation with Shift + ArrowKey
GLfloat camX = rho * cos(theta3*3.1415926f / 180)*sin(phi*3.1415926f / 180);
GLfloat camY = rho * sin(theta3*3.1415926f / 180);
GLfloat camZ = rho * cos(theta3*3.1415926f / 180)*cos(phi*3.1415926f / 180);
// Reduce theta slightly to obtain another point on the same longitude line on the sphere.
GLfloat dt = 1;
GLfloat eyeXtemp = -rho * cos((theta3 - dt)*3.1415926f / 180)*sin(phi*3.1415926f / 180);
GLfloat eyeYtemp = -rho * sin((theta3 - dt)*3.1415926f / 180);
GLfloat eyeZtemp = -rho * cos((theta3 - dt)*3.1415926f / 180)*cos(phi*3.1415926f / 180);
// Connect these two points to obtain the camera's up vector.
GLfloat upX = eyeXtemp - camX;
GLfloat upY = eyeYtemp - camY;
GLfloat upZ = eyeZtemp - camZ;
// Clear Color and Depth Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Set the camera
gluLookAt(camX, camY, camZ, 0, 0, 0, upX, upY, upZ);
gluLookAt(x, 0.0f, z, x, 0.0f, z - 1.0f, 0.0f, 1.0f, 0.0f); //Module 4
glRotatef(angleX, 1, 0, 0); //Module 4
myDisplay();
glFlush();
glutPostRedisplay();
glutSwapBuffers();
}
void init() {
glClearColor(0, 0, 0, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(0.0f, 0.1f, 0.1f, 0.0f);
glOrtho(-250, 250, -250, 250, -250, 250); //IMPORTANT- Define from negative to positive
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv) {
// init GLUT and create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Homework: Circle");
// register callbacks
glutDisplayFunc(renderScene);
glutTimerFunc(0,timer,0);
glutKeyboardFunc(handleKeypress);
glutSpecialFunc(keyboard);
glutMouseFunc(MouseFunc);
// OpenGL init
init();
// enter GLUT event processing cycle
glutMainLoop();
}
Is wired to set a perspective and an orthographic projection. Delete the perspective projection gluPerspective:
void init() {
glClearColor(0, 0, 0, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//gluPerspective(0.0f, 0.1f, 0.1f, 0.0f); <---- delete
glOrtho(-250, 250, -250, 250, -250, 250);
glMatrixMode(GL_MODELVIEW);
}
If you want to zoom, the the orthographic projection has to be changed (glOrtho). The projection matrix describes the mapping from 3D points of a scene, to 2D points of the viewport.
Implement a mouse wheel event, which changes the zoom, in a restricted range:
e.g.
GLdouble zoom = 0.0f;
void MouseFunc(int button, int state, int x, int y){
GLdouble min_z = -100.0;
GLdouble max_z = 100.0;
if (button == 4 && zoom < max_z) {
zoom += 3.0;
}
else if (button == 3 && zoom > min_z) {
zoom -= 3.0;
}
}
Modify the orthographic projection, dependent on the zoom in the display loop renderScene:
void renderScene(void) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLdouble ortho = 250 + zoom;
glOrtho(-ortho, ortho, -ortho, ortho, -250, 250);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// [...]
}

No output for simple openGL line drawing program?

I am unable to understand why I am not getting any output for the given program. This problem has been occurring to me for my last 2 openGL programs, the previous one being DDA algorithm, again in which I didnt get any output. Is there a problem with the algorithm, or in my understanding how openGL works internally ?
#include <iostream>
#include <GL/glut.h>
using namespace std;
float X1 = 0, X2 = 100, Y1 = 0, Y2 = 400, X11, Y11, X22, Y22, slope, dely, delx, c, intercept, pi;
float absl(float x) {
if (x >= 0) return x;
return -1*x;
}
void drawScene() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 500, 0, 500);
glClearColor(1.0, 1.0, 1.0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
//start bresenham algo
glBegin(GL_POINTS);
glVertex2f(X1, Y1);
while(X1 <= X2) {
X1++;
if(pi < 0) {
glVertex2f(X1, Y1);
}
else {
Y1++;
glVertex2f(X1, Y1);
}
float flag = (pi >= 0);
pi = pi + 2*dely - 2*delx*flag;
}
glEnd();
//end DDA algo
glFlush();
}
int main(int argc, char **argv) {
//cin >> X1 >> Y1 >> X2 >> Y2;
// dely = Y2 - Y1;
// delx = X2 - X1;
// pi = 2*dely - delx;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0,0);
glutCreateWindow("DDA");
glutDisplayFunc(drawScene);
glutMainLoop();
return 0;
}
gluOrtho2D defines a 2D orthographic projection matrix. But it also multiplies the current matrix on the matrix stack with the orthographic projection matrix and replaces the current matrix with the product.
Note, in OpenGL fixed function pipeline all matrix operations work like this (except glPushMatrix, glPopMatrix, glLoadMatrix and glLoadIdentity).
This means you have to replace the current matrix with the identity matrix (glLoadIdentity), befor you apply the orthographic projection matrix:
glLoadIdentity();
gluOrtho2D(0, 500, 0, 500);
Or you do gluOrtho2D only one time before you start the main loop in the main function:
gluOrtho2D(0, 500, 0, 500);
glutMainLoop();
Since the display function is called (drawScene) continuously, you should use local control varibales and not modify X1 and Y1:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 500, 0, 500);
glClearColor(0.0, 0.0, 0.0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
//start bresenham algo
int x = X1, y = Y1;
glBegin(GL_LINES);
glVertex2f(100, 400);
glVertex2f(400, 100);
glEnd();
glBegin(GL_POINTS);
glVertex2f(x, Y1);
while(x <= X2) {
x ++;
if(pi < 0) {
glVertex2f(x, y);
}
else {
y ++
glVertex2f(x, y);
}
}
int flag = (pi >= 0);
pi = pi + 2*dely - 2*delx*flag;
glEnd();

OpenGL the direction of translation is not right after rotation

I'm writing a simple demo with the fixed pipeline mode in OpenGL
First I write some codes that will draw a rectangle, with top-left point (x1,y1), and buttom-right point (x2,y2)
// Drawing here
glPushMatrix();
// Perform rotation first
glTranslatef(x_cent, y_cent, 0.0f);
glRotatef(angle, 0.0f, 0.0f, 1.0f);
glScalef(scale, scale, 1.0f);
// Set origin back to the screen center with the rotation set above
glTranslatef(-x_cent, -y_cent, 0.0f);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glBegin(GL_QUADS);
{
glVertex2f(x1, y2);
glVertex2f(x2, y2);
glVertex2f(x2, y1);
glVertex2f(x1, y1);
}
glEnd();
// End of drawing
glPopMatrix();
After I tweak the value of the angle with arrow keys, say 45 degrees counter-clockwise, and now the rectangle will rotate 45 degrees counter-clockwise. Then I translate it with arrow keys, it moves in the direction of up, down, left, and right. Everything works fine.
So I write anoter part of code that will draw a general polygon. It's similar to the code above. I simply draw lines between each pair of points
for (int i = 0; i < p_size; i++)
{
float x1 = point[i].x, x2 = point[(i + 1) % p_size].x;
float y1 = point[i].y, y2 = point[(i + 1) % p_size].y;
x1 = ((x1 + 0.5f) / winWidth) * 2.0f - 1.0f;
x2 = ((x2 + 0.5f) / winWidth) * 2.0f - 1.0f;
y1 = ((-y1 + 0.5f) / winHeight) * 2.0f + 1.0f;
y2 = ((-y2 + 0.5f) / winHeight) * 2.0f + 1.0f;
// Set the curent coord origin to this poly's center
float x_cent = (left + right) / 2.0, y_cent = (top + down) / 2.0;
float ratio = winWidth / winHeight;
if (ratio > 1)
{
x1 *= ratio;
x2 *= ratio;
}
else //if (ratio < 1)
{
y1 /= ratio;
y2 /= ratio;
}
// Drawing here
glPushMatrix();
// Perform rotation first
glTranslatef(x_cent, y_cent, 0.0f);
glRotatef(angle, 0.0f, 0.0f, 1.0f);
glScalef(scale, scale, 1.0f);
// Set origin back to the screen center with the rotation set above
glTranslatef(-x_cent, -y_cent, 0.0f);
glBegin(GL_LINES);
{
glVertex2f(x1, y1);
glVertex2f(x2, y2);
}
glEnd();
// End of drawing
glPopMatrix();
}
I rotate the polygon with arrow keys, that works fine, say also 45 degrees counter-clockwise. But after the rotation, when I translate it with up key, it moves not up but in the direction of 45 degrees.
Bbut why can the rectangle translate normally in the 4 directions after rotation? These two part of codes are almost the same, why is this happening...

OpenGL Squares and Arrows

i'm trying to create a square and on top of it i'm creating an arrow. its like a 2D meter something similar to that. But i don't know how to create the arrow on top of the square.
int count = 1;
for (float y = 1; y < 11; y++) {
y1 = y1 - 0.51;
y2 = y2 - 0.51;
float x2 = -2.0;
for (float x1 = -2.5; x1 < 2.5; x1 = x1 + 0.51) {
glColor3f(windy[count], 1.0, 0.0);
glVertex2f(x1, y1);
glVertex2f(x1, y2);
glVertex2f(x2, y2);
glVertex2f(x2, y1);
count = count + 1;
x2 = x2 + 0.51;
}
}
glutSwapBuffers();
glEnd(); //End the glBegin Function
And this is what i want to be on top of it.
glBegin(GL_LINE_LOOP);//start drawing a line loop
glVertex3f(-1.0f, 0.0f, 0.0f);//left of window
glVertex3f(0.0f, -1.0f, 0.0f);//bottom of window
glVertex3f(1.0f, 0.0f, 0.0f);//right of window
glVertex3f(0.0f, 1.0f, 0.0f);//top of window
glEnd();//end drawing of line loop
Why did you swap buffers before glEnd() call:
int count = 1;
for (float y = 1; y < 11; y++) {
y1 = y1 - 0.51;
y2 = y2 - 0.51;
float x2 = -2.0;
for (float x1 = -2.5; x1 < 2.5; x1 = x1 + 0.51) {
glColor3f(windy[count], 1.0, 0.0);
glVertex2f(x1, y1);
glVertex2f(x1, y2);
glVertex2f(x2, y2);
glVertex2f(x2, y1);
count = count + 1;
x2 = x2 + 0.51;
}
}
//glutSwapBuffers(); // wrong place
glEnd(); //End the glBegin Function
glutSwapBuffers(); // Must be after draw operation
to draw arrow on the top of the shape must be even more:
int count = 1;
for (float y = 1; y < 11; y++) {
y1 = y1 - 0.51;
y2 = y2 - 0.51;
float x2 = -2.0;
for (float x1 = -2.5; x1 < 2.5; x1 = x1 + 0.51) {
glColor3f(windy[count], 1.0, 0.0);
glVertex2f(x1, y1);
glVertex2f(x1, y2);
glVertex2f(x2, y2);
glVertex2f(x2, y1);
count = count + 1;
x2 = x2 + 0.51;
}
}
//glutSwapBuffers(); // wrong place
glEnd(); //End the glBegin Function
// draw arrow
glBegin(GL_LINE_LOOP);//start drawing a line loop
glVertex3f(-1.0f, 0.0f, 0.0f);//left of window
glVertex3f(0.0f, -1.0f, 0.0f);//bottom of window
glVertex3f(1.0f, 0.0f, 0.0f);//right of window
glVertex3f(0.0f, 1.0f, 0.0f);//top of window
glEnd();//end drawing of line loo
// atlast we can swap our buffers
glutSwapBuffers(); // Must be after draw operation