Related
I'm having trouble getting my yoyo program to work; I have a growing and shrinking string to go along with the yoyo moving and spinning, but I've got the string moving along with the yoyo. I need to have the string staying stationary while the yoyo spins up and down.I have to have the string and yoyo's movements allocated onto one button as well. Mind helping me with my code? (also please excuse the lackluster yoyo; I wanted to finish the programming before designing it)
#include "stdafx.h"
#include <GL/glut.h>
#include <GL/gl.h>
#include <math.h>
void firstDisplay();
void secondDisplay();
void movement();
void init();
void drawButton();
void handleButton(int button, int state, int x, int y);
const int Width=720;
const int Height = 480;
static float PI = 3.14159;
static float radius = 25;
static float INC= (PI/30);
static GLfloat spin = 0.0;
static GLfloat deltaz = .001;
static GLfloat deltax = 0.0;
static GLfloat deltay = 0.0;
//######################################################### Main #########################################################
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_MULTISAMPLE | GLUT_DEPTH);
glutInitWindowSize(Width,Height);
glutInitWindowPosition(0,0);
glutCreateWindow("Yoyo");
glutDisplayFunc(firstDisplay);
glutMouseFunc(handleButton);
init();
glutMainLoop();
return;
}
//######################################################### Draw Button #########################################################
void drawButton()
{
glBegin(GL_POLYGON);
float theta;
for (theta = 0.0; theta <= 2 * PI; theta += INC)
{
glColor3f(1,0,1);
glVertex2f(40 + radius * cos(theta), 40 + radius*sin(theta));
}
glEnd();
}
//######################################################### Second Display #########################################################
void secondDisplay()
{
float theta;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON); //Here is the yoyo
for (theta = 0.0; theta <= 2 * PI; theta += INC)
{
glColor3f(1,1,1);
float radius1 = 75;
glVertex2f(360 + radius1 * cos(theta), 80 + radius1*sin(theta));
}
glEnd();
glBegin(GL_POLYGON); //Here is the yoyo design
for (theta = 0.0; theta <= 2 * PI; theta += INC)
{
glColor3f(0,1,1);
float radius1 = 55;
glVertex2f(350 + radius1 * cos(theta), 90 + radius1*sin(theta));
}
glEnd();
glFlush();
return;
}
//######################################################### First Display #########################################################
void firstDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
float theta;
glBegin(GL_POLYGON); //Here is the yoyo
for (theta = 0.0; theta <= 2 * PI; theta += INC)
{
glColor3f(1,1,1);
float radius1 = 75;
glVertex2f(360 + radius1 * cos(theta), 80 + radius1*sin(theta));
}
glEnd();
glBegin(GL_POLYGON); //Here is the yoyo
for (theta = 0.0; theta <= 2 * PI; theta += INC)
{
glColor3f(0,1,1);
float radius1 = 55;
glVertex2f(350 + radius1 * cos(theta), 90 + radius1*sin(theta));
}
glEnd();
drawButton();
glutSwapBuffers();
glFlush();
return;
}
//######################################################### Draw String #########################################################
void draw_string()
{
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex3f(363, 8.25 - deltay, 0.0);
glVertex3f(357, 8.25 - deltay, 0);
glVertex3f(357, 6, 0);
glVertex3f(363, 6, 0);
glEnd();
}
//######################################################### Button Controls #########################################################
void handleButton(int button, int state, int x, int y)
{
static int index =-1;
if(button == GLUT_LEFT_BUTTON)
{
if(y>= 10 && y<= 100)
{
if (state == GLUT_DOWN)
{
glutIdleFunc(movement);
}
if (state == GLUT_UP)
{
glutIdleFunc(NULL);
}
}
glutSwapBuffers();
}
}
//######################################################### Yoyo Movement #########################################################
void movement()
{
static int goingup = 0;
glClear(GL_COLOR_BUFFER_BIT);
if(goingup==1)
{
deltay -= 6;
if(deltay <= 0)
{
goingup = 0;
}
spin = spin -5;
if(spin < 360)
{
spin = spin + 360;
}
}
if(goingup == 0)
{
deltay += 6;
if (deltay >= 315)
{
goingup = 1;
}
spin = spin +5;
if(spin < 360)
{
spin = spin + 360;
}
}
glPushMatrix();
glTranslatef(360+deltax, 80+deltay, 0.0);
glRotatef(spin,0.0,0.0,1.0);
glTranslatef(-360,-80, 0.0);
secondDisplay();
draw_string();
glPopMatrix();
drawButton();
glutSwapBuffers();
}
//######################################################### Init #########################################################
void init()
{
glClearColor(0, 0, 0, 0.0);
glColor3f(1, 1, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,Width,Height,0);
return;
}
I solved my problem. I went separated the draw_string() and seconddisplay() functions within the movement function like so:
}
glPushMatrix();
glTranslatef(360+deltax, 80+deltay, 0.0);
glRotatef(spin,0.0,0.0,1.0);
glTranslatef(-360,-80, 0.0);
secondDisplay();
glPopMatrix();
glPushMatrix();
glTranslatef(360+deltax, 80+deltay, 0.0);
glRotatef(0,0.0,0.0,1.0);
glTranslatef(-360,-80, 0.0);
draw_string();
glPopMatrix();
drawButton();
glutSwapBuffers();
}
I removed the spin from draw_string()'s glRotatef.
I am new to opengl. I am doing a simple 2D shooting game and using AABB collision to do collision between objects and bullet. I do the collision for my plane and the square but it doesn't seems to work. Can help me check what's my problem?
#include <iostream>
#include <time.h>
#include <windows.h>
#include <string.h>
#include <math.h>
#include <GL/glut.h>
using namespace std;
#define SPACEBAR 32
class Plane {
public:
GLfloat x = 0.05f;
GLfloat y = 0.95f;
GLfloat width = 0.05f;
GLfloat height = 0.10f;
GLfloat moveX = 0.0f;
GLfloat moveY = 0.0f;
void drawPlane(GLfloat, GLfloat, GLfloat, GLfloat);
};
class Enemy {
public:
GLfloat x, y;
GLfloat width, height;
GLfloat sqrMoveX, sqrMoveY;
void drawEnemySqr(GLfloat, GLfloat, GLfloat, GLfloat);
};
Plane plane;
Enemy enemy;
GLfloat XMax, XMin, YMax, YMin;
GLdouble clipAreaXLeft, clipAreaXRight, clipAreaYBottom, clipAreaYTop;
int refreshMills = 30;
float RandomNumberX() {
float Min = -1.0f;
float Max = 1.0f;
return ((float(rand()) / float(RAND_MAX)) * (Max - Min)) + Min;
}
float RandomNumberY() {
float Min = 0.7f;
float Max = 1.0f;
return ((float(rand()) / float(RAND_MAX)) * (Max - Min)) + Min;
}
void Timer(int value) {
glutPostRedisplay();
glutTimerFunc(refreshMills, Timer, 0);
}
bool collision(GLfloat x1, GLfloat y1, GLfloat w1, GLfloat h1,
GLfloat x2, GLfloat y2, GLfloat w2, GLfloat h2) {
if ((x1 < (x2 + w2)) &&
((x1 + w1) > x2) &&
(y1 < (y2 + h2)) &&
((h1 + y1) > y2)) {
return true;
}
else {
return false;
}
}
void initGL() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
void Plane :: drawPlane(GLfloat planeX, GLfloat planeY, GLfloat planeWidth, GLfloat planeHeight) {
glTranslatef(moveX, moveY, 0.0f);
glBegin(GL_POLYGON);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(-(planeX), -(planeY));
glVertex2f(planeX, -(planeY));
glVertex2f(planeX + planeWidth, -(planeY - planeHeight));
glVertex2f(planeX - (planeWidth), -(planeY - (planeHeight + 0.20f)));
glVertex2f(-(planeX + planeWidth), -(planeY - planeHeight));
glEnd();
if (moveX > XMax) {
moveX = XMax;
}
else if (moveX < XMin) {
moveX = XMin;
}
if (moveY > YMax) {
moveY = YMax;
}
else if (moveY < YMin) {
moveY = YMin;
}
}
void Enemy :: drawEnemySqr(GLfloat enemyX, GLfloat enemyY, GLfloat enemyWidth, GLfloat enemyHeight) {
glTranslatef(sqrMoveX, sqrMoveY, 0.0f);
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 1.0f);
glVertex2f(enemyX, enemyY);
glVertex2f(enemyX + enemyWidth, enemyY);
glVertex2f(enemyX + enemyWidth, enemyY + enemyHeight);
glVertex2f(enemyX, enemyWidth + enemyY);
glEnd();
sqrMoveY -= 0.0005f;
if (sqrMoveY < -1.8f) {
sqrMoveX = RandomNumberX();
sqrMoveY = RandomNumberY();
}
glutPostRedisplay();
}
void display() {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glPushMatrix();
plane.drawPlane(plane.x, plane.y, plane.width, plane.height);
glPopMatrix();
glPushMatrix();
enemy.drawEnemySqr(0.0f, 0.0f, 0.3f, 0.3f);
glPopMatrix();
if (collision(plane.moveX, plane.moveY, plane.width, plane.height,
enemy.sqrMoveX, enemy.sqrMoveY, enemy.width, enemy.height) == true) {
enemy.sqrMoveY = -2.0f;
}
glutSwapBuffers();
}
void keyButton(int key, int x, int y) {
switch (key) {
case GLUT_KEY_RIGHT:
plane.moveX += 0.05f;
break;
case GLUT_KEY_LEFT:
plane.moveX += -0.05f;
break;
case GLUT_KEY_UP:
plane.moveY += 0.05f;
break;
case GLUT_KEY_DOWN:
plane.moveY += -0.05f;
break;
default:
break;
}
glutPostRedisplay();
}
int main(int argc, char** argv) {
srand(time(NULL));
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(480, 640);
glutInitWindowPosition(50, 50);
glutCreateWindow("Plane Shooting Game");
glutDisplayFunc(display);
glutIdleFunc(idle);
glutReshapeFunc(reshape);
glutTimerFunc(25, Timer, 0);
glutSpecialFunc(keyButton);
initGL();
glutMainLoop();
return 0;
}
You have a problem in this line:
if (collision(plane.moveX, plane.moveY, plane.width, plane.height,
enemy.sqrMoveX, enemy.sqrMoveY, enemy.width, enemy.height) == true)
You must pass plane.x, plane.y, enemy.x, enemy.y to collision function instead of plane.moveX, plane.moveY, enemy.sqrMoveX, enemy.sqrMoveY.
This question already has an answer here:
C : display every x milliseconds (OpenGL)
(1 answer)
Closed 7 years ago.
I have successfully drawn a square with a line through it, a trapezoid with a line through it, and a square on top of a trapezoid with a line through it. At this point I'm just attempting to make all of the icons rotate slowly on the screen, but can't figure out where I'm going wrong.
I've modeled my program after a previous one that I wrote that makes a polygon walk across the screen and jump up and do a flip, but for some reason I can't get any movement out of any of my icons. Is my TimerFunction not behaving as I would expect?
I know it's tedious to read, but I included my whole code because I'm not sure where the issue might be...
void init(void); //function that initializes the window clear color
void DrawsAllIcons(float x[], float y[], int ndraws, int pointsperdraw [], int drawtype[], float colorr[], float colorg[], float colorb[], float rotate, float scalex, float scaley, float transx, float transy); //function to draw the functions in the opened window
void SetupRC(void);
void RenderScene(void);
void settrans2 (float rotate, float scalex, float scaley, float transx, float transy);//function that sets the clear color used to set the state of the OpenGL system
void TimerFunction(int );
//square
float rotate = 00.0;
float xCoords [7] = {1.0, 1.0, -1.0, -1.0, 1.0, 0.0, 0.0};
float yCoords [7] = {1.0,-1.0,-1.0,1.0,1.0,2.0,-2.0};
int numberofDraws = 2;
int pointsPerDraw[2] = {5, 2};
int typeOfDraw[2] = {2,1};
float colorR[3] = {1.0,0.0,0.0};
float colorg[3] = {0.0,1.0,0.0};
float colorb[3] = {0.0,0.0,1.0};
float transx = 5.0;
float transy = 5.0;
float scalex = 2.0;
float scaley = 2.0;
//trapezoid
float rotate2 = 00.0;
float xCoords2 [6] = {1.0, 1.5, -1.5, -1.0, 0.0, 0.0};
float yCoords2 [6] = {1.0,-1.0,-1.0,1.0,2.0,-2.0};
int numberofDraws2 = 2;
int pointsPerDraw2[2] = {4, 2};
int typeOfDraw2[2] = {3,1};
float colorR2[3] = {0.0,0.0,0.0};
float colorg2[3] = {1.0,1.0,0.0};
float colorb2[3] = {0.0,0.0,1.0};
float transx2 = -5.0;
float transy2 = -5.0;
float scalex2 = 2.0;
float scaley2 = 2.0;
//Square on trapezoid
float rotate3 = 00.0;
float xCoords3 [6] = {1.0, 1.0, -1.0, -1.0, 0.0,0.0};
float yCoords3 [6] = {1.0,0.0,0.0,1.0,3.0,-3.0};
int numberofDraws3 = 2;
int pointsPerDraw3[2] = {4, 2};
int typeOfDraw3[2] = {3,1};
float colorR3[3] = {1.0,0.0,0.0};
float colorg3[3] = {0.0,1.0,0.0}; //go down dont overthink
float colorb3[3] = {0.0,0.0,0.0};
float transx3 = 0.0;
float transy3 = 0.0;
float scalex3 = 1.0;
float scaley3 = 1.0;
//trapezoid under square
float rotate4 = 00.0;
float xCoords4 [6] = {1.5, 2.0, -2.0, -1.5, 0.0, 0.0};
float yCoords4 [6] = {0.0,-1.0,-1.0,0.0,3.0,-3.0};
int numberofDraws4 = 2;
int pointsPerDraw4[2] = {4, 2};
int typeOfDraw4[2] = {3,1};
float colorR4[3] = {0.0,0.0,0.0};
float colorg4[3] = {1.0,1.0,0.0};
float colorb4[3] = {0.0,0.0,1.0};
float transx4 = 0.0;
float transy4 = 0.0;
float scalex4 = 1.0;
float scaley4 = 1.0;
int main(int argc, char* *argv)
{
char header[]="This Bad Boy'll Draw any Icon you can think of"; //set up window title
glutInit(&argc, argv); // initialize glopen utility toolkit
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); // Set up the display mode with a buffer and colors **
glutInitWindowSize(560,440); //window size and position
glutInitWindowPosition(140,20);
SetupRC();
glutCreateWindow(header); // Open and label the window
glutDisplayFunc(RenderScene); //points to the function that will be drawing the item // Set the state of the rendering machine
glutTimerFunc(30, TimerFunction, 1);
glutMainLoop(); // Call and activate the main
return 0;
}
void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);//note clear color was set in SetupRC
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,540,440); //set viewpoint to dimensions
glOrtho(-20.0,20.0,-20.0,20.0,1.0,-1.0);
glClear(GL_COLOR_BUFFER_BIT);
DrawsAllIcons(xCoords, yCoords, numberofDraws, pointsPerDraw,typeOfDraw,colorR,colorg,colorb, rotate, transx,transy,scalex,scaley); //used GL_LINE_STRIP for the square to show that that case worked
DrawsAllIcons(xCoords2, yCoords2, numberofDraws2, pointsPerDraw2,typeOfDraw2,colorR2,colorg2,colorb2, rotate2, transx2,transy2,scalex2,scaley2); //used GL_POLYGON and GL_LINE for rest
DrawsAllIcons(xCoords3, yCoords3, numberofDraws3, pointsPerDraw3,typeOfDraw3,colorR3,colorg3,colorb3, rotate3, transx3,transy3,scalex3,scaley3);
DrawsAllIcons(xCoords4, yCoords4, numberofDraws4, pointsPerDraw4,typeOfDraw4,colorR4,colorg4,colorb4, rotate4, transx4,transy4,scalex4,scaley4);
glEnd();
glutSwapBuffers(); //
}
void DrawsAllIcons (float x[], float y[], int ndraws, int pointsperdraw [], int drawtype[], float colorr[], float colorg[], float colorb[], float rotateD, float transxD, float transyD, float scalexD, float scaleyD)
{
settrans2(rotateD,scalexD,scaleyD,transxD, transyD);
int k=0; //index for arrays
int drawTooIndex = 0;
ndraws=ndraws-1;
for (int j=0; j<=ndraws; j++) //runs through
{
int whatCase = drawtype[j]; //sees what type of draw
drawTooIndex +=pointsperdraw[j];
switch (whatCase)
{
case 1:
{
glColor3f(colorr[j],colorg[j],colorb[j]);
glBegin(GL_LINES);
glVertex2f(x[k], y[k]); //sets vertex at the first point at k in the point arrays
int i = k+1;
k++;
for (i; i <drawTooIndex; i++)
{
glVertex2f(x[i], y[i]);
k++;
}
glEnd();
glFlush();
}
break;
case 2:
{
glColor3f(colorr[j], colorg[j], colorb[j]);
glBegin(GL_LINE_STRIP);
glVertex2f(x[k], y[k]);
int m = k+1;
k++;
for (m; m <drawTooIndex; m++)
{
glVertex2f(x[m], y[m]);
k++;
}
glEnd();
glFlush();
}
break;
case 3:
{
glColor3f(colorr[j], colorg[j], colorb[j]);
glShadeModel(GL_FLAT);
glBegin(GL_POLYGON);
glVertex2f(x[k], y[k]);
int n = k+1; //gets index of where to start drawing in the x and y arrays
k++;
for (n; n <drawTooIndex; n++)
{
glVertex2f(x[n], y[n]);
k++;
}
glEnd();
glFlush();
}
break;
}
}
}
void SetupRC(void)
{ // function sets the clear color of an open window, and then clears the open window
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set clear color to pale green
return;
}//end of SetupRC
void settrans2(float rotateDD, float scalexDD, float scaleyDD, float transxDD, float transyDD)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(transxDD,transyDD,0.0);
glRotatef(rotateDD, 0.0, 0.0, 1.0); // where to put this in the program?
glScalef(scalexDD, scaleyDD, 1.0);
return;
}
void TimerFunction(int value)
{
rotate+=5.0;
rotate2+=5.0;
rotate3+=5.0;
rotate4+=5.0;
}
glutTimerFunc()s only fire once, you need to re-arm them in your callback to get another, as well as poking GLUT for a redraw with the new values:
void TimerFunction(int value)
{
rotate+=5.0;
rotate2+=5.0;
rotate3+=5.0;
rotate4+=5.0;
glutPostRedisplay();
glutTimerFunc( 30, TimerFunction, 1 );
}
All together:
#include <GL/glut.h>
//square
float rotate = 00.0;
float xCoords [7] = {1.0, 1.0, -1.0, -1.0, 1.0, 0.0, 0.0};
float yCoords [7] = {1.0,-1.0,-1.0,1.0,1.0,2.0,-2.0};
int numberofDraws = 2;
int pointsPerDraw[2] = {5, 2};
int typeOfDraw[2] = {2,1};
float colorR[3] = {1.0,0.0,0.0};
float colorg[3] = {0.0,1.0,0.0};
float colorb[3] = {0.0,0.0,1.0};
float transx = 5.0;
float transy = 5.0;
float scalex = 2.0;
float scaley = 2.0;
//trapezoid
float rotate2 = 00.0;
float xCoords2 [6] = {1.0, 1.5, -1.5, -1.0, 0.0, 0.0};
float yCoords2 [6] = {1.0,-1.0,-1.0,1.0,2.0,-2.0};
int numberofDraws2 = 2;
int pointsPerDraw2[2] = {4, 2};
int typeOfDraw2[2] = {3,1};
float colorR2[3] = {0.0,0.0,0.0};
float colorg2[3] = {1.0,1.0,0.0};
float colorb2[3] = {0.0,0.0,1.0};
float transx2 = -5.0;
float transy2 = -5.0;
float scalex2 = 2.0;
float scaley2 = 2.0;
//Square on trapezoid
float rotate3 = 00.0;
float xCoords3 [6] = {1.0, 1.0, -1.0, -1.0, 0.0,0.0};
float yCoords3 [6] = {1.0,0.0,0.0,1.0,3.0,-3.0};
int numberofDraws3 = 2;
int pointsPerDraw3[2] = {4, 2};
int typeOfDraw3[2] = {3,1};
float colorR3[3] = {1.0,0.0,0.0};
float colorg3[3] = {0.0,1.0,0.0}; //go down dont overthink
float colorb3[3] = {0.0,0.0,0.0};
float transx3 = 0.0;
float transy3 = 0.0;
float scalex3 = 1.0;
float scaley3 = 1.0;
//trapezoid under square
float rotate4 = 00.0;
float xCoords4 [6] = {1.5, 2.0, -2.0, -1.5, 0.0, 0.0};
float yCoords4 [6] = {0.0,-1.0,-1.0,0.0,3.0,-3.0};
int numberofDraws4 = 2;
int pointsPerDraw4[2] = {4, 2};
int typeOfDraw4[2] = {3,1};
float colorR4[3] = {0.0,0.0,0.0};
float colorg4[3] = {1.0,1.0,0.0};
float colorb4[3] = {0.0,0.0,1.0};
float transx4 = 0.0;
float transy4 = 0.0;
float scalex4 = 1.0;
float scaley4 = 1.0;
void settrans2(float rotateDD, float scalexDD, float scaleyDD, float transxDD, float transyDD)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(transxDD,transyDD,0.0);
glRotatef(rotateDD, 0.0, 0.0, 1.0); // where to put this in the program?
glScalef(scalexDD, scaleyDD, 1.0);
return;
}
void DrawsAllIcons( float x[], float y[], int ndraws, int pointsperdraw [], int drawtype[], float colorr[], float colorg[], float colorb[], float rotateD, float transxD, float transyD, float scalexD, float scaleyD)
{
settrans2(rotateD,scalexD,scaleyD,transxD, transyD);
int k=0; //index for arrays
int drawTooIndex = 0;
ndraws=ndraws-1;
for (int j=0; j<=ndraws; j++) //runs through
{
int whatCase = drawtype[j]; //sees what type of draw
drawTooIndex +=pointsperdraw[j];
switch (whatCase)
{
case 1:
{
glColor3f(colorr[j],colorg[j],colorb[j]);
glBegin(GL_LINES);
glVertex2f(x[k], y[k]); //sets vertex at the first point at k in the point arrays
int i = k+1;
k++;
for (i; i <drawTooIndex; i++)
{
glVertex2f(x[i], y[i]);
k++;
}
glEnd();
glFlush();
}
break;
case 2:
{
glColor3f(colorr[j], colorg[j], colorb[j]);
glBegin(GL_LINE_STRIP);
glVertex2f(x[k], y[k]);
int m = k+1;
k++;
for (m; m <drawTooIndex; m++)
{
glVertex2f(x[m], y[m]);
k++;
}
glEnd();
glFlush();
}
break;
case 3:
{
glColor3f(colorr[j], colorg[j], colorb[j]);
glShadeModel(GL_FLAT);
glBegin(GL_POLYGON);
glVertex2f(x[k], y[k]);
int n = k+1; //gets index of where to start drawing in the x and y arrays
k++;
for (n; n <drawTooIndex; n++)
{
glVertex2f(x[n], y[n]);
k++;
}
glEnd();
glFlush();
}
break;
}
}
}
void RenderScene(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set clear color to pale green
glClear(GL_COLOR_BUFFER_BIT);//note clear color was set in SetupRC
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,540,440); //set viewpoint to demensions
glOrtho(-20.0,20.0,-20.0,20.0,1.0,-1.0);
glClear(GL_COLOR_BUFFER_BIT);
DrawsAllIcons(xCoords, yCoords, numberofDraws, pointsPerDraw,typeOfDraw,colorR,colorg,colorb, rotate, transx,transy,scalex,scaley); //used GL_LINE_STRIP for the square to show that that case worked
DrawsAllIcons(xCoords2, yCoords2, numberofDraws2, pointsPerDraw2,typeOfDraw2,colorR2,colorg2,colorb2, rotate2, transx2,transy2,scalex2,scaley2); //used GL_POLYGON and GL_LINE for rest
DrawsAllIcons(xCoords3, yCoords3, numberofDraws3, pointsPerDraw3,typeOfDraw3,colorR3,colorg3,colorb3, rotate3, transx3,transy3,scalex3,scaley3);
DrawsAllIcons(xCoords4, yCoords4, numberofDraws4, pointsPerDraw4,typeOfDraw4,colorR4,colorg4,colorb4, rotate4, transx4,transy4,scalex4,scaley4);
glEnd();
glutSwapBuffers(); //
}
void TimerFunction(int value)
{
rotate+=5.0;
rotate2+=5.0;
rotate3+=5.0;
rotate4+=5.0;
glutPostRedisplay();
glutTimerFunc( 30, TimerFunction, 1 );
}
int main(int argc, char* *argv)
{
char header[]="This Bad Boy'll Draw any Icon you can think of"; //set up window title
glutInit(&argc, argv); // initialize glopen utility toolkit
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); // Set up the display mode with a buffer and colors **
glutInitWindowSize(560,440); //window size and positoin
glutInitWindowPosition(140,20);
glutCreateWindow(header); // Open and label the window
glutDisplayFunc(RenderScene); //points to the fucntion that will be drawing the item // Set the state of the rendering machine
glutTimerFunc(30, TimerFunction, 1);
glutMainLoop(); // Call and activate the main
return 0;
}
I've got some code but the matrix orientation does not appeal to my purposes, can someone teach me how to convert it's orientation, it's currently set up as X Z Y, but i would like it to reflect X Y Z, can someone please highlight what must be done?
when i do vertex3f(100, 100, 10); forexample, the 10 value should reflect the Z value on my grid.
Here is my code:
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <Windows.h>
#include <glut.h>
#include <iostream>
using namespace std;
const float sensitivity = 0.005;
const float walk_speed = 0.5;
float cam_pos[3] = { 100.5, 10.0f, 50 };
float cam_view[3] = { -1.0f, 0.0f, 1.0f };
static int old_x, old_y, half_width, half_height;
int width = 1024, height = 768;
void updateKeys()
{
if (GetAsyncKeyState('W')){
cam_pos[0] += cam_view[0] * walk_speed;
cam_pos[1] += cam_view[1] * walk_speed;
cam_pos[2] += cam_view[2] * walk_speed;
}
if (GetAsyncKeyState('S')){
cam_pos[0] -= cam_view[0] * walk_speed;
cam_pos[1] -= cam_view[1] * walk_speed;
cam_pos[2] -= cam_view[2] * walk_speed;
}
if (GetAsyncKeyState('A')){
cam_pos[0] += cam_view[2] * walk_speed;
cam_pos[2] -= cam_view[0] * walk_speed;
}
if (GetAsyncKeyState('D')){
cam_pos[0] -= cam_view[2] * walk_speed;
cam_pos[2] += cam_view[0] * walk_speed;
}
if (GetAsyncKeyState(VK_SPACE)){
cam_pos[1] += walk_speed;
}
}
void renderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//3d camera
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(
cam_pos[0], cam_pos[1], cam_pos[2],
cam_pos[0] + cam_view[0], cam_pos[1] + cam_view[1], cam_pos[2] + cam_view[2],
0.0f, 1.0f, 0.0f);
//render grid
glBegin(GL_LINES);
for (int i = 0; i <= 100; i++) {
if (i == 0) { glColor3f(.6, .3, .3); }
else { glColor3f(.25, .25, .25); };
glVertex3f(i, 0, 0);
glVertex3f(i, 0, 100);
if (i == 0) { glColor3f(.3, .3, .6); }
else { glColor3f(.25, .25, .25); };
glVertex3f(0, 0, i);
glVertex3f(100, 0, i);
};
glEnd();
glEnable(GL_POINT_SMOOTH);
glPointSize(50.0f);
glColor3f(1, 0, 0);
glBegin(GL_POINTS);
glVertex3f(0, 0, 0);
//X, Z, Y
glVertex3f(10, -10, 10);
glEnd();
updateKeys();
glutSwapBuffers();
}
void normalize(float *v)
{
float magnitude = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
v[0] /= magnitude;
v[1] /= magnitude;
v[2] /= magnitude;
}
void rotate_view(float *view, float angle, float x, float y, float z)
{
float new_x;
float new_y;
float new_z;
float c = cos(angle);
float s = sin(angle);
new_x = (x*x*(1 - c) + c) * view[0];
new_x += (x*y*(1 - c) - z*s) * view[1];
new_x += (x*z*(1 - c) + y*s) * view[2];
new_y = (y*x*(1 - c) + z*s) * view[0];
new_y += (y*y*(1 - c) + c) * view[1];
new_y += (y*z*(1 - c) - x*s) * view[2];
new_z = (x*z*(1 - c) - y*s) * view[0];
new_z += (y*z*(1 - c) + x*s) * view[1];
new_z += (z*z*(1 - c) + c) * view[2];
view[0] = new_x;
view[1] = new_y;
view[2] = new_z;
normalize(view);
}
void motion(int x, int y)
{
float rot_x, rot_y;
float rot_axis[3];
x -= half_width;
y -= half_height;
rot_x = -(float)(x - old_x) * sensitivity;
rot_y = -(float)(y - old_y) * sensitivity;
old_x = x;
old_y = y;
rotate_view(cam_view, rot_x, 0.0f, 1.0f, 0.0f);
rot_axis[0] = -cam_view[2];
rot_axis[1] = 0.0f;
rot_axis[2] = cam_view[0];
normalize(rot_axis);
rotate_view(cam_view, rot_y, rot_axis[0], rot_axis[1], rot_axis[2]);
}
void mouse(int button, int state, int x, int y)
{
old_x = x - half_width;
old_y = y - half_height;
glutPostRedisplay();
}
void idle()
{
glutPostRedisplay();
}
void keys(unsigned char c, int x, int y)
{
glutPostRedisplay();
cout << "camera view: :" << cam_view[0] << "," << cam_view[1] << "," << cam_view[2] << endl;
}
void reshape(int w, int h)
{
width = w;
height = h;
half_height = w / 2;
half_width = h / 2;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double)w / (double)h, 1.0, 10000.0);
glViewport(0, 0, w, h);
}
//----------------------------------------------------------------------
// Main program
//----------------------------------------------------------------------
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(width, height);
glutCreateWindow("OpenGL");
glutDisplayFunc(renderScene);
glutKeyboardFunc(keys);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutIdleFunc(idle);
// OpenGL init
glEnable(GL_DEPTH_TEST);
// enter GLUT event processing cycle
glutMainLoop();
return 0; // this is just to keep the compiler happy
}
Use a transformation matrix that "remaps" the values. You can push that matrix on your modelview as usual.
The identity matrix is:
(1, 0, 0; 0, 1, 0; 0, 0, 1)
Your matrix would be:
(1, 0, 0; 0, 0, 1; 0, 1, 0)
I guess you can spot the difference. You can extend to a 4D matrix for homogeneous coordinates accordingly.
I've just began learning OpenGL and this program is a mixture of stuff I've put together myself and some stuff straight copied from tutorials to speed up the process (like the whole OBJ loader you will see soon).
I'm having an error on
glNormal3f(normals[faces[i]->facenum-1]->x,normals[faces[i]->facenum-1]->y,normals[faces[i]->facenum-1]->z);
//draw the faces
glVertex3f(vertex[faces[i]->faces[0]-1]->x,vertex[faces[i]->faces[0]-1]->y,vertex[faces[i]->faces[0]-1]->z);
glVertex3f(vertex[faces[i]->faces[1]-1]->x,vertex[faces[i]->faces[1]-1]->y,vertex[faces[i]->faces[1]-1]->z);
glVertex3f(vertex[faces[i]->faces[2]-1]->x,vertex[faces[i]->faces[2]-1]->y,vertex[faces[i]->faces[2]-1]->z);
glVertex3f(vertex[faces[i]->faces[3]-1]->x,vertex[faces[i]->faces[3]-1]->y,vertex[faces[i]->faces[3]-1]->z);
and that error is EXC_BAD_ACCESS code = 1. I'm completely lost for answers to this issue and I've searched around but couldn't find anything unfortunately. I also know for a fact that it is loading the file as well because that was the first issue I faced but resolved. Below is all my code (which is in one file. I'll be making a whole new project from scratch soon).
Main.cpp
#include <GLUT/GLUT.h>
#include <math.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <cmath>
void init();
void keyboard(unsigned char key, int x, int y);
void reshape(int w, int h);
void display();
void camera();
void cubePositions();
void drawCube();
void enable();
void plane();
// Roation angles.
float xPos = 0;
float yPos = 0;
float zPos = 0;
float xRot = 0;
float yRot = 0;
float angle = 0.0;
float lastx, lasty;
// Cubes position arrays.
float cubePosZ[10];
float cubePozX[10];
// Fog variables.
GLfloat fogAngle = 0.0;
GLfloat density = 0.1;
GLfloat fogColor[4] = {0.5, 0.5, 0.5, 1.0};
#define checkImageWidth 64
#define checkImageHeight 64
static GLubyte checkImage[checkImageHeight][checkImageWidth][4];
static GLuint texName;
void makeCheckImage(void)
{
int i, j, c;
for (i = 0; i < checkImageHeight; i++) {
for (j = 0; j < checkImageWidth; j++) {
c = ((((i&0x8)==0)^((j&0x8))==0))*255;
checkImage[i][j][0] = (GLubyte) c;
checkImage[i][j][1] = (GLubyte) c;
checkImage[i][j][2] = (GLubyte) c;
checkImage[i][j][3] = (GLubyte) 255;
}
}
}
struct coordinate{
float x,y,z;
coordinate(float a,float b,float c) : x(a),y(b),z(c) {};
};
//for faces, it can contain triangles and quads as well, the four variable contain which is that
struct face{
int facenum;
bool four;
int faces[4];
face(int facen,int f1,int f2,int f3) : facenum(facen){ //constructor for triangle
faces[0]=f1;
faces[1]=f2;
faces[2]=f3;
four=false;
}
face(int facen,int f1,int f2,int f3,int f4) : facenum(facen){ //overloaded constructor for quad
faces[0]=f1;
faces[1]=f2;
faces[2]=f3;
faces[3]=f4;
four=true;
}
};
//we rotate or object with angle degrees
int loadObject(const char* filename)
{
std::vector<std::string*> coord; //read every single line of the obj file as a string
std::vector<coordinate*> vertex;
std::vector<face*> faces;
std::vector<coordinate*> normals; //normal vectors for every face
std::ifstream in(filename); //open the .obj file
if(!in.is_open()) //if not opened, exit with -1
{
std::cout << "Nor oepened" << std::endl;
return -1;
}
char buf[256];
//read in every line to coord
while(!in.eof())
{
in.getline(buf,256);
coord.push_back(new std::string(buf));
}
//go through all of the elements of coord, and decide what kind of element is that
for(int i=0;i<coord.size();i++)
{
if(coord[i]->c_str()[0]=='#') //if it is a comment (the first character is #)
continue; //we don't care about that
else if(coord[i]->c_str()[0]=='v' && coord[i]->c_str()[1]==' ') //if vector
{
float tmpx,tmpy,tmpz;
sscanf(coord[i]->c_str(),"v %f %f %f",&tmpx,&tmpy,&tmpz); //read in the 3 float coordinate to tmpx,tmpy,tmpz
vertex.push_back(new coordinate(tmpx,tmpy,tmpz)); //and then add it to the end of our vertex list
}else if(coord[i]->c_str()[0]=='v' && coord[i]->c_str()[1]=='n') //if normal vector
{
float tmpx,tmpy,tmpz; //do the same thing
sscanf(coord[i]->c_str(),"vn %f %f %f",&tmpx,&tmpy,&tmpz);
normals.push_back(new coordinate(tmpx,tmpy,tmpz));
}else if(coord[i]->c_str()[0]=='f') //if face
{
int a,b,c,d,e;
if(count(coord[i]->begin(),coord[i]->end(),' ')==3) //if it is a triangle (it has 3 space in it)
{
sscanf(coord[i]->c_str(),"f %d//%d %d//%d %d//%d",&a,&b,&c,&b,&d,&b);
faces.push_back(new face(b,a,c,d)); //read in, and add to the end of the face list
}else{
sscanf(coord[i]->c_str(),"f %d//%d %d//%d %d//%d %d//%d",&a,&b,&c,&b,&d,&b,&e,&b);
faces.push_back(new face(b,a,c,d,e)); //do the same, except we call another constructor, and we use different pattern
}
}
}
//raw
int num; //the id for the list
num=glGenLists(1); //generate a uniqe
glNewList(num,GL_COMPILE); //and create it
for(int i=0;i<faces.size();i++)
{
if(faces[i]->four) //if it's a quad draw a quad
{
glBegin(GL_QUADS);
//basically all I do here, is use the facenum (so the number of the face) as an index for the normal, so the 1st normal owe to the first face
//I subtract 1 because the index start from 0 in C++
glNormal3f(normals[faces[i]->facenum-1]->x,normals[faces[i]->facenum-1]->y,normals[faces[i]->facenum-1]->z);
//draw the faces
glVertex3f(vertex[faces[i]->faces[0]-1]->x,vertex[faces[i]->faces[0]-1]->y,vertex[faces[i]->faces[0]-1]->z);
glVertex3f(vertex[faces[i]->faces[1]-1]->x,vertex[faces[i]->faces[1]-1]->y,vertex[faces[i]->faces[1]-1]->z);
glVertex3f(vertex[faces[i]->faces[2]-1]->x,vertex[faces[i]->faces[2]-1]->y,vertex[faces[i]->faces[2]-1]->z);
glVertex3f(vertex[faces[i]->faces[3]-1]->x,vertex[faces[i]->faces[3]-1]->y,vertex[faces[i]->faces[3]-1]->z);
glEnd();
}else{
glBegin(GL_TRIANGLES);
glNormal3f(normals[faces[i]->facenum-1]->x,normals[faces[i]->facenum-1]->y,normals[faces[i]->facenum-1]->z);
glVertex3f(vertex[faces[i]->faces[0]-1]->x,vertex[faces[i]->faces[0]-1]->y,vertex[faces[i]->faces[0]-1]->z);
glVertex3f(vertex[faces[i]->faces[1]-1]->x,vertex[faces[i]->faces[1]-1]->y,vertex[faces[i]->faces[1]-1]->z);
glVertex3f(vertex[faces[i]->faces[2]-1]->x,vertex[faces[i]->faces[2]-1]->y,vertex[faces[i]->faces[2]-1]->z);
glEnd();
}
}
glEndList();
//delete everything to avoid memory leaks
for(int i=0;i<coord.size();i++)
delete coord[i];
for(int i=0;i<faces.size();i++)
delete faces[i];
for(int i=0;i<normals.size();i++)
delete normals[i];
for(int i=0;i<vertex.size();i++)
delete vertex[i];
return num; //return with the id
}
// Sets the position of the cubes randomly. Can later be used for more purposeful placements.
void cubePositions()
{
for (int i = 0; i < 10; i++)
{
cubePosZ[i] = rand() % 5 + 5;
cubePozX[i] = rand() % 5 + 5;
}
}
void drawCube()
{
for (int i = 0; i < 10; i++)
{
glPushMatrix();
//glTranslated(-cubePozX[i + 1] * 10, -1, -cubePosZ[i + 1] * 10);
glTranslated((i + 4), 0, (i - 5));
glTranslatef(i, 0, 5 * i);
glScalef(2, 2, 2);
glBegin(GL_QUADS);
glTexCoord2f(1, 0);
glVertex3f(-1, -1, 0);
glNormal3f(1, 1, 1);
glTexCoord2f(1, 1);
glVertex3f(-1, 1, 0);
glTexCoord2f(0, 1);
glVertex3f(1, 1, 0);
glTexCoord2f(0, 0);
glVertex3f(1, -1, 0);
glVertex3f(-1, -1, -1);
glVertex3f(-1, 1, -1);
glEnd();
glPopMatrix();
}
}
void plane()
{
glPushMatrix();
glColor4f(1.0, 0.0, 1.0, 1.0);
glTranslatef(0, -2.5, 0.0);
glScalef(100, 2, 100);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3f(-1, 0, 1);
glTexCoord2f(1.0, 0.0);
glVertex3f(-1, 0, -0.5);
glTexCoord2f(1.0, 1.0);
glVertex3f(1, 0, -0.5);
glTexCoord2f(0.0, 1.0);
glVertex3f(1, 0, 1);
glEnd();
glPopMatrix();
}
int cube;
void init()
{
cubePositions();
cube = loadObject("/Users/Admin/test.obj");
}
void enable()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_TEXTURE_2D);
float col[] = {1.0, 1.0, 1.0, 1.0};
glLightfv(GL_LIGHT0, GL_DIFFUSE, col);
}
void camera()
{
glRotatef(xRot, 1.0, 0.0, 0.0);
glRotatef(yRot, 0.0, 1.0, 0.0);
glTranslated(-xPos, -yPos, -zPos);
}
void display()
{
glClearColor(0.8, 0.8, 0.8, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
camera();
enable();
drawCube();
plane();
glCallList(cube);
glTranslated(-2, 0, 0);
glutSwapBuffers();
angle++;
}
void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 1000.0);
glMatrixMode (GL_MODELVIEW);
}
void keyboard(unsigned char key, int x, int y)
{
if (key == 27)
{
exit(0);
}
if (key == 'q')
{
xRot += 0.5;
if (xRot > 360)
{
xRot -= 360;
}
}
if (key == 'z')
{
xRot -= 0.5;
if (xRot < - 360)
{
xRot += 360;
}
}
if (key == 'w')
{
float xRotRad;
float yRotRad;
xRotRad = (xRot / 180 * 3.14159f);
yRotRad = (yRot / 180 * 3.14159f);
xPos += float(sin(yRotRad));
zPos -= float(cos(yRotRad));
yPos -= float(sin(xRotRad));
}
if (key == 's')
{
float xRotRad;
float yRotRad;
xRotRad = (xRot / 180 * 3.14159f);
yRotRad = (yRot / 180 * 3.14159f);
xPos -= float(sin(yRotRad));
zPos += float(cos(yRotRad));
yPos += float(sin(xRotRad));
}
if (key == 'd')
{
yRot += 1;
if (yRot > 360)
{
yRot -= 360;
}
}
if (key == 'a')
{
yRot -= 1;
if (yRot < -360)
{
yRot += 360;
}
}
if (key == 'x')
{
yPos -= 1;
}
if (key == 'c')
{
yPos += 1;
}
}
void mouseMovement(int x, int y)
{
int diffX = x - lastx;
int diffY = y - lasty;
lastx = x;
lasty = y;
xRot += (float)diffY;
yRot += (float)diffX;
}
int main(int argc, char * argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(1000, 1000);
glutInitWindowPosition(100, 100);
glutCreateWindow("OpenGL GLUT Computing - Taylor Moore");
init();
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutPassiveMotionFunc(mouseMovement);
glutMainLoop();
return 0;
}
EXC_BAD_ACCESS usually means you are trying to access a chunk of memory that has been released or dealloc'ed. Here's the first response to a Google search on the term EXC_BAD_ACCESS that explains two strategies for tracking down the cause.