Related
I'm currently doing an assignment in OpenGL and what I'm supposed to do is make two disks and then as I move the cursor on the screen and click the two disks should move to that position where I click on the window but the trick is that both the disks should not move together. The first disk should move and then after a small delay the second disk should move. Now I have been able to make both the disks move together at the point I click. What I want to know is how am I supposed to add a delay in the movement of both the disks. Please help!
This is my current code. Thanks!
EDIT:
Here's my updated code after implementing exactly what you said and now none of the disks are moving. If you understand the problem, fine otherwise thanks for all the help :)
#include <math.h>
#include <cstdlib>
#include <glut.h>
typedef struct _Vector
{
double x,y,z;
} Vector;
const int W_SCREEN = 1366;
const int H_SCREEN = 768;
const double PI = 3.14159265;
GLUquadric *qobja;
double speed = 10;
double radian;
double rot;
Vector pos2;
Vector pos;
Vector vel;
Vector dis;
Vector dir;
Vector mousecoords;
void mouse(int button, int state, int x , int y)
{
mousecoords.x = x - W_SCREEN/2;
mousecoords.y = -y + H_SCREEN/2;
}
void move()
{
dis.x = mousecoords.x - pos.x;
dis.y = mousecoords.y - pos.y;
if(sqrt(dis.x*dis.x + dis.y*dis.y) < speed)
{
pos.x = mousecoords.x;
pos.y = mousecoords.y;
pos2.x = mousecoords.x;
pos2.y = mousecoords.y;
}
else
{
radian = atan2(dis.y,dis.x);
pos.x += cos(radian)*speed;
pos.y += sin(radian)*speed;
pos2.x += cos(radian)*speed;
pos2.y += sin(radian)*speed;
}
}
void moveSecondDisk(int value)
{
// Code that moves (updates coordinates) of second disk
move();
glutPostRedisplay();
}
void moveFirstDisk(int value)
{
// Code that moves (updates coordinates) of first disk
move();
glutPostRedisplay();
unsigned int secondDiskDelay = 5000;
glutTimerFunc(secondDiskDelay, moveSecondDisk, 0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glOrtho(-W_SCREEN/2,W_SCREEN/2,-H_SCREEN/2,H_SCREEN/2,-100,100);
qobja = gluNewQuadric();
gluQuadricNormals(qobja, GLU_SMOOTH);
glPushMatrix();
glColor3f(0,0,0);
glTranslated(pos.x,pos.y,0);
gluCylinder(qobja,25,50,0,100,100);
glPopMatrix();
glPushMatrix();
glColor3f(0,0,0);
glTranslated(pos2.x,pos2.y,0);
gluCylinder(qobja,50.5,65,0,100,100);
glPopMatrix();
/*glPushMatrix();
glColor3f(0,0,0);
glTranslated(pos.x,pos.y,0);
gluCylinder(qobja,65.5,80,0,100,100);
glPopMatrix();
glPushMatrix();
glColor3f(0,0,0);
glTranslated(pos.x,pos.y,0);
gluCylinder(qobja,80.5,90,0,100,100);
glPopMatrix();
glPushMatrix();
glColor3f(0,0,0);
glTranslated(pos.x,pos.y,0);
gluCylinder(qobja,90.5,100,0,100,100);
glPopMatrix();
glPushMatrix();
glColor3f(0,0,0);
glTranslated(pos.x,pos.y,0);
gluCylinder(qobja,100.5,110,0,100,100);
glPopMatrix();
glPushMatrix();
glColor3f(0,0,0);
glTranslated(pos.x,pos.y,0);
gluCylinder(qobja,110.5,120,0,100,100);
glPopMatrix();
glPushMatrix();
glColor3f(0,0,0);
glTranslated(pos.x,pos.y,0);
gluCylinder(qobja,120.5,130,0,100,100);
glPopMatrix();
glPushMatrix();
glColor3f(0,0,0);
glTranslated(pos.x,pos.y,0);
gluCylinder(qobja,130.5,140,0,100,100);
glPopMatrix();
glPushMatrix();
glColor3f(0,0,0);
glTranslated(pos.x,pos.y,0);
gluCylinder(qobja,140.5,150,0,100,100);
glPopMatrix();
glPushMatrix();
glColor3f(0,0,0);
glTranslated(pos.x,pos.y,0);
gluCylinder(qobja,150.5,160,0,100,100);
glPopMatrix();
glPushMatrix();
glColor3f(0,0,0);
glTranslated(pos.x,pos.y,0);
gluCylinder(qobja,160.5,170,0,100,100);
glPopMatrix();*/
glFlush();
glutSwapBuffers();
glutPostRedisplay();
}
void init(void)
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(W_SCREEN,H_SCREEN);
glutCreateWindow("ORCA WHALE SIMULATION");
glutDisplayFunc(display);
glutMouseFunc(mouse);
unsigned int firstDiskDelay = 2000;
glutTimerFunc(firstDiskDelay, moveFirstDisk, 0);
glClearColor(1,1,1,1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
init();
glutMainLoop();
return(0);
}
Use a timer to accomplish this. Create a function that moves the second disk with the following signature:
void moveSecondDisk(int value);
Then, execute the code that moves the first disk. Finally, call function glutTimerFunc, passing it the function moveSecondDisk as callback (second parameter). The first parameter will be delay between moving of two discs (expressed in miliseconds), and the third one will be value that is passed the callback function. Note that you can pass complex data to callback function by passing a pointer to a complex structure (or an object) as an integer, then casting it back to the desired type in the callback function. For more info, check the following links:
https://www.opengl.org/resources/libraries/glut/spec3/node64.html
https://www.opengl.org/discussion_boards/showthread.php/170990-glutTimerFunc
EDIT:
A more detailed explanation:
void movePos()
{
// Code that only modifies pos
...
}
void movePos2()
{
// Code that only modifies pos2
...
}
void moveSecondDisk(int value)
{
movePos2();
glutPostRedisplay();
}
void moveFirstDisk(int value)
{
movePos();
glutPostRedisplay();
unsigned int secondDiskDelay = 200;
glutTimerFunc(secondDiskDelay, moveSecondDisk, 0);
glutTimerFunc(300, moveFirstDisk, 0);
}
void init()
{
...
unsigned int firstDiskDelay = 50;
glutTimerFunc(firstDiskDelay, moveFirstDisk, 0);
...
}
So i'm making a game(something similar to a pong)using openGl in cpp , and when i try running the program , the image rendering is too slow ,i feel something is taking a huge toll on my GPU memory ,
(So far i have competed the drawing and movement of the paddle which works fine and the problem arises when the ball is set to motion on the press of a button , Everything slows down after this )
Here i have attached a portion of my code
Point out any deficiencies in my code
#include<stdlib.h>
#include<GL/glut.h>
#include<windows.h>
#include<iostream>
//#include<sstream>
#include<stdio.h>
#include<ctime>
#include<string.h>
typedef double point[3];
point a,b,c,d;
static float xmove=0.0;
static int release=0;
static float ii=0.00;
static int reac_top;
using namespace std;
//GLvoid player1_box();
GLfloat xangle=0.0;
float changeDir=1;// positive direction
double vv[4][3]={ {0.5,0.2,0.0},{ 0.5,0.25,0.0},{0.7,0.25,0.0},{0.7,0.2,0.0}};
void renderoutline()
{
glBegin(GL_LINE_LOOP);
glColor3f(1,0,0);
glVertex2f(0.0,0.0);
glColor3f(1,0,0);
glVertex2f(0,1.2);
glColor3f(1,0,0);
glVertex2f(1.2,1.2);
glColor3f(1,0,0);
glVertex2f(1.2,0);
glEnd();
}
void ball()
{
glBegin(GL_POLYGON);
glColor3f(0,0,1);
glVertex3f(0.6,0.25,0.0);
glColor3f(0,0,1);
glVertex3f(0.6,0.28,0.0);
glColor3f(0,0,1);
glVertex3f(0.62,0.28,0.0);
glColor3f(0,0,1);
glVertex3f(0.62,0.25,0.0);
glEnd();
}
void renderbox1( point a , point b , point c, point d)
{
glBegin(GL_POLYGON);
glColor3f(0,0,0);
glVertex3dv(a);
glColor3f(0,0,0);
glVertex3dv(b);
glColor3f(0,0,0);
glVertex3dv(c);
glColor3f(0,0,0);
glVertex3dv(d);
glEnd();
}
void keyboard(unsigned char key,int x,int y)
{
switch(key){
case 'a':xangle-=0.1;
++xmove;
while(xmove>5)
{
xmove=-5;
xangle=0.5;
continue;
}
break;
case 'd':xangle+=0.1;
--xmove;
while(xmove<-5)
{
xmove=5;
xangle =-0.5;
continue;
}
break;
case 'r':
release=1;
//printf("Inside switch r");
break;
}
glutPostRedisplay();
}
void display()
{
Sleep(2);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glLineWidth(3.0);
glLoadIdentity();
glTranslatef(xangle,0,0);
renderbox1(vv[0],vv[1],vv[2],vv[3]); // To draw the paddle
glPopMatrix();
//printf("x move in disp:%1f \n",xmove);
renderoutline();
glPushMatrix();
glTranslatef(0,ii,0);
ball(); //To draw the ball
glPopMatrix();
glFlush();
}
void moveBall(){ //glutidlefunction
if(release==1)
{ //Release becomes one when the key 'r' is pressed (glutkeyboardfunc)
Sleep(500);
if(ii>=0&&ii<=0.9 )
{
reac_top=false;
ii=ii+0.05;printf("1\n");//The ball which was intially on the paddle starts moving till it reaches the top edge of thescreen
}
else if(ii>=0.9)
{
//when the boll reaches top
ii=ii-0.05;
reac_top=3;
printf("2\n");
}
else if( (ii<0.9)&& (reac_top==3)&&(ii>0.05)) //The balls journey back to the paddle
{
printf("3\n");
ii=ii-0.05;
}
release=1;
}
}
void reshape(int w, int h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1.2, 0, 1.2,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0,0,w,h);
//glutPostRedisplay();
//Use the whole window for rendering
}
int main(int argc ,char **argv)
{
//clock_t begin= clock();
char n1,n2;
int ack;
/*printf("The object of Tic Tac Toe is to get three in a row. \n . Players alternate placing Xs and Os(Traingles and Boxes in this version) \n on the game board until either opponent has three \n in a row or all nine squares are filled(Which would be a drawObviously). \n");
printf("\n Press 1 if acknowledged \n");
scanf("%d",&ack);*/
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(1000,500);
glutInitWindowPosition(200,0);
glutCreateWindow("3d Gasket");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutReshapeFunc(reshape);
glutIdleFunc(moveBall);
glClearColor(1,1,1,1);
glutMainLoop();
//clock_t end=clock();
}
You're using c++, use C++-style (stdlib.h -> cstdlib) headers.
Apply your projection/modelview matrices each time through display() instead of a reshape callback, helps prevent annoying matrix stack issues.
Don't Sleep(), use a timer/idle callback to glutPostRedisplay(). Bonus points for framerate-independent movement (you can use glutGet(GLUT_ELAPSED_TIME) to calculate delta-t).
Without Sleep() there's no reason to drag windows.h into things.
All together:
#include <GL/glut.h>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <cstring>
typedef double point[3];
point a,b,c,d;
static float xmove=0.0;
static int release=0;
static float ii=0.00;
static int reac_top;
using namespace std;
GLfloat xangle=0.0;
float changeDir=1;// positive direction
double vv[4][3]={ {0.5,0.2,0.0},{ 0.5,0.25,0.0},{0.7,0.25,0.0},{0.7,0.2,0.0}};
void renderoutline()
{
glBegin(GL_LINE_LOOP);
glColor3f(1,0,0);
glVertex2f(0.0,0.0);
glColor3f(1,0,0);
glVertex2f(0,1.2);
glColor3f(1,0,0);
glVertex2f(1.2,1.2);
glColor3f(1,0,0);
glVertex2f(1.2,0);
glEnd();
}
void ball()
{
glBegin(GL_POLYGON);
glColor3f(0,0,1);
glVertex3f(0.6,0.25,0.0);
glColor3f(0,0,1);
glVertex3f(0.6,0.28,0.0);
glColor3f(0,0,1);
glVertex3f(0.62,0.28,0.0);
glColor3f(0,0,1);
glVertex3f(0.62,0.25,0.0);
glEnd();
}
void renderbox1( point a , point b , point c, point d)
{
glBegin(GL_POLYGON);
glColor3f(0,0,0);
glVertex3dv(a);
glColor3f(0,0,0);
glVertex3dv(b);
glColor3f(0,0,0);
glVertex3dv(c);
glColor3f(0,0,0);
glVertex3dv(d);
glEnd();
}
void keyboard(unsigned char key,int x,int y)
{
switch(key)
{
case 'a':xangle-=0.1;
++xmove;
while(xmove>5)
{
xmove=-5;
xangle=0.5;
continue;
}
break;
case 'd':xangle+=0.1;
--xmove;
while(xmove<-5)
{
xmove=5;
xangle =-0.5;
continue;
}
break;
case 'r':
release=1;
break;
}
}
void display()
{
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1.2, 0, 1.2,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glLineWidth(3.0);
glLoadIdentity();
glTranslatef(xangle,0,0);
renderbox1(vv[0],vv[1],vv[2],vv[3]); // To draw the paddle
glPopMatrix();
renderoutline();
glPushMatrix();
glTranslatef(0,ii,0);
ball();
glPopMatrix();
glFlush();
}
void moveBall()
{
if(release==1)
{
//Release becomes one when the key 'r' is pressed (glutkeyboardfunc)
if(ii>=0&&ii<=0.9 )
{
reac_top=false;
ii=ii+0.05;printf("1\n");//The ball which was intially on the paddle starts moving till it reaches the top edge of thescreen
}
else if(ii>=0.9)
{
//when the boll reaches top
ii=ii-0.05;
reac_top=3;
printf("2\n");
}
else if( (ii<0.9)&& (reac_top==3)&&(ii>0.05)) //The balls journey back to the paddle
{
printf("3\n");
ii=ii-0.05;
}
release=1;
}
}
void timer( int value )
{
moveBall();
glutTimerFunc( 16, timer, 0 );
glutPostRedisplay();
}
int main(int argc ,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(1000,500);
glutInitWindowPosition(200,0);
glutCreateWindow("3d Gasket");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutTimerFunc( 0, timer, 0 );
glutMainLoop();
}
Extra Object(Block,not OOP) is formed while playing Game in Opengl during each move.
I tried to made a Bloxerz game ,in which block is moved with keys on a board , The problem is every time, as i move the block, a new Image of block is formed under the board.
I can't find trace of this thing
I think ,there is nothing wrong with the code and logic except it is written nonprofessional like OpenGl programmers write.
There must be some thing missing,some method to stop this.
In order to make every thing visible,i have shared all my code.
Following is my code:
int s=-1;
int stage=0;
int arr[]={5,5,5,4,4,4,4,5,9,9,10,9,10,8,9,8,10,7,11,7,11,6,10,6};
int fallcount=0;
int fallset=0;
int rot[]={0,0,0};
int axis=0;
int a=0;
double x=0.8,y=1.3,z=1;
double move1=0;
int moved=0;
int pos=1;
int position;
void Init()
{
x=0.8;
y=1.3;
z=1;
move1=0;
moved=0;
pos=1;
position=1;
fallcount=0;
}
void wait ( int seconds )
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {}
}
void FallOn()
{
if(position==1||position==3)
{
if(x<=-0.7||y<=-0.7||x>=11.3||y>=11.3)
{
Init();
}
}
if(stage==1)
{
if(x>=2.8&&y<=5.3&&y>=3.8)
{
Init();
}
if((x>=1.8)&&(x<=5.8)&&(y>=8.8))
{
Init();
}
}
}
void checkpos(int arg)
{
if((position==1)&&(arg==1))
{
position=1;
x=x+1;
return;
}
else if((position==1)&&(arg==2))
{
position=2;
rot[1]=-1;
rot[2]=0;
rot[3]=0;
y=y+2;
x=x+0.5;
return;
}
else if((position==1)&&(arg==3))
{
position=1;
rot[1]=0;
rot[2]=-1;
rot[3]=0;
x=x-1;
return;
}
else if((position==1)&&(arg==4))
{
position=2;
rot[1]=0;
rot[2]=0;
rot[3]=1;
y=y-1;
x=x+0.5;
return;
}
if((position==2)&&(arg==1))
{
position=3;
rot[1]=0;
rot[2]=1;
rot[3]=0;
x=x+1;
y=y-0.5;
return;
}
else if((position==2)&&(arg==2))
{
position=1;
rot[1]=0;
rot[2]=-1;
rot[3]=0;
y=y+1;
x=x-0.5;
return;
}
else if((position==2)&&(arg==3))
{
position=3;
rot[1]=0;
rot[2]=-1;
rot[3]=0;
x=x-2;
y=y-0.5;
return;
}
else if((position==2)&&(arg==4))
{
position=1;
rot[1]=0;
rot[2]=0;
rot[3]=1;
y=y-2;
x=x-0.5;
return;
}
if((position==3)&&(arg==1))
{
position=2;
rot[1]=0;
rot[2]=1;
rot[3]=0;
x=x+2;
y=y+0.5;
return;
}
else if((position==3)&&(arg==2))
{
position=3;
rot[1]=0;
rot[2]=0;
rot[3]=-1;
y=y+1;
return;
}
else if((position==3)&&(arg==3))
{
position=2;
rot[1]=0;
rot[2]=-1;
rot[3]=0;
x=x-1;
y=y+0.5;
return;
}
else if((position==3)&&(arg==4))
{
position=3;
rot[1]=1;
rot[2]=0;
rot[3]=0;
y=y-1;
return;
}
}
void draw_string( int x, int y,int z ,const char * buffer ) {
// function to draw a string in glut
glRasterPos3i( x, y,z );
// i think its safe to assume a null terminator appears somewhere
while( *buffer )
glutBitmapCharacter( (void *)GLUT_BITMAP_TIMES_ROMAN_24, *(buffer++) );
}
void Display()
{
FallOn();
glLoadIdentity();
//glu look at
gluLookAt(-30,-30,30,2.5,4,0,0,0,1);
//***********************************************Board************************************************************************
if(stage==0)
{
glBegin(GL_POLYGON);
for(int i=0;i<=12;i++)
{
for(int j=0;j<=12;j++)
{
glColor3d(1,0,1);
glVertex3d(i,i,0);
glVertex3d(j,i,0);
glVertex3d(j,j,0);
glVertex3d(i,j,0);
}
}
glEnd();
glBegin(GL_POLYGON);
glColor3d(0,0,0);
glVertex3d(arr[s+1],arr[s+2],0);
glVertex3d(arr[s+3],arr[s+4],0);
glVertex3d(arr[s+5],arr[s+6],0);
glVertex3d(arr[s+7],arr[s+8],0);
glEnd();
glBegin(GL_LINES);
glColor3d(0,0,0);
for(int i=0;i<=12;i++)
{
glVertex3d(0,i,0);
glVertex3d(12,i,0);
glVertex3d(i,0,0);
glVertex3d(i,12,0);
}
glEnd();
}
else if(stage==1)
{
glFlush();
glClear(GL_CLIP_PLANE0);
glClear(GL_ACCUM_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
for(int i=0;i<=12;i++)
{
for(int j=0;j<=12;j++)
{
glColor3d(1,0,1);
glVertex3d(i,i,0);
glVertex3d(j,i,0);
glVertex3d(j,j,0);
glVertex3d(i,j,0);
}
}
glEnd();
glBegin(GL_POLYGON);
glColor3d(0,0,0);
glVertex3d(3,4,0);
glVertex3d(3,6,0);
glVertex3d(12,6,0);
glVertex3d(12,4,0);
glEnd();
glFlush();
glBegin(GL_POLYGON);
glColor3d(0,0,0);
glVertex3d(2,12,0);
glVertex3d(7,12,0);
glVertex3d(7,9,0);
glVertex3d(2,9,0);
glEnd();
glFlush();
s=7;
glBegin(GL_POLYGON);
glColor3d(0,0,0);
glVertex3d(arr[s+1],arr[s+2],0);
glVertex3d(arr[s+3],arr[s+4],0);
glVertex3d(arr[s+5],arr[s+6],0);
glVertex3d(arr[s+7],arr[s+8],0);
glEnd();
glBegin(GL_LINES);
glColor3d(0,0,0);
for(int i=0;i<=12;i++)
{
glVertex3d(0,i,0);
glVertex3d(12,i,0);
glVertex3d(i,0,0);
glVertex3d(i,12,0);
}
glEnd();
}
else if(stage==2)
{
glFlush();
glClear(GL_CLIP_PLANE0);
glClear(GL_ACCUM_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
for(int i=0;i<=12;i++)
{
for(int j=0;j<=12;j++)
{
glColor3d(1,0,1);
glVertex3d(i,i,0);
glVertex3d(j,i,0);
glVertex3d(j,j,0);
glVertex3d(i,j,0);
}
}
glEnd();
glBegin(GL_POLYGON);
glColor3d(0,0,0);
glVertex3d(3,11,0);
glVertex3d(9,11,0);
glVertex3d(9,0,0);
glVertex3d(3,0,0);
glEnd();
glFlush();
s=15;
glBegin(GL_POLYGON);
glColor3d(0,0,0);
glVertex3d(arr[s+1],arr[s+2],0);
glVertex3d(arr[s+3],arr[s+4],0);
glVertex3d(arr[s+5],arr[s+6],0);
glVertex3d(arr[s+7],arr[s+8],0);
glEnd();
glBegin(GL_LINES);
glColor3d(0,0,0);
for(int i=0;i<=12;i++)
{
glVertex3d(0,i,0);
glVertex3d(12,i,0);
glVertex3d(i,0,0);
glVertex3d(i,12,0);
}
glEnd();
}
if(moved==0)
{
glPushMatrix();
glColor3d(1,0,0);
glTranslated(x,y,z);
glScalef(1,2.1,1);
glutSolidCube(.9);
glPopMatrix();
}
if(moved==1)
{
if(position==1)
{
glPushMatrix();
glColor3d(1,0,0);
glTranslated(x,y,z);
glScalef(1,2.1,1);
glutSolidCube(.9);
glPopMatrix();
moved=0;
}
else if(position==2)
{
cout<<"2:- "<<"x="<<x<<"y="<<y<<"z="<<z<<"position="<<position<<endl;
if(((x==4.3&&y==4.3)&&stage==0)||((x==9.3&&y==8.3)&&stage==1))
{
cout<<"Event Happened"<<endl;
z=z-1;
cout<<"Stage Cleared"<<endl;
glPushMatrix();
glColor3d(1,0,0);
glTranslated(x,y,z);
glScalef(1,1,2.1);
glutSolidCube(.9);
glPopMatrix();
moved=0;
wait(1);
Init();
stage+=1;
glutPostRedisplay();
glFlush();
}
else if((x==10.3&&y==6.3)&&(stage==2))
{
cout<<"Event Happened"<<endl;
z=z-1;
cout<<"Stage Cleared"<<endl;
glPushMatrix();
glColor3d(1,0,0);
glTranslated(x,y,z);
glScalef(1,1,2.1);
glutSolidCube(.9);
glPopMatrix();
moved=0;
wait(1);
Init();
cout<<"Congratulations!!!!! You Won"<<endl;
stage=0;
glFlush();
}
else
{
glPushMatrix();
glColor3d(1,0,0);
glTranslated(x,y,z);
glScalef(1,1,2.1);
glutSolidCube(.9);
glPopMatrix();
moved=0;
}
}
else if(position==3)
{
glPushMatrix();
glColor3d(1,0,0);
glTranslated(x,y,z);
glScalef(2.1,1,1);
glutSolidCube(.9);
glPopMatrix();
moved=0;
}
moved=3;
}
glFlush();
glutSwapBuffers();
}
void Resize(int h,int w)
{
if(h==0)
{
h=1;
}
float ratio=w*1/h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,w,h);
gluPerspective(14.0f ,ratio,0.1f,100.f);
glMatrixMode(GL_MODELVIEW);
}
void Board(unsigned char key,int x,int y)
{
switch (key)
{
case 'w':
case 'W':
move1=1;
moved=1;
axis=1;
checkpos(1);
glFlush();
glutSwapBuffers();
glutPostRedisplay();
break;
case 'a':
case 'A':
move1=2;
moved=1;
axis=2;
checkpos(2);
glFlush();
glutSwapBuffers();
glutPostRedisplay();
break;
case 'S':
case 's':
move1=3;
moved=1;
axis=1;
checkpos(3);
glFlush();
glutSwapBuffers();
glutPostRedisplay();
break;
case 'd':
case 'D':
move1=4;
moved=1;
axis=2;
checkpos(4);
glFlush();
glutSwapBuffers();
glutPostRedisplay();
break;
default:
break;
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutInitWindowPosition(200, 200);
glutCreateWindow("3D Object in OpenGL");
Init();
glutDisplayFunc(Display);
glutReshapeFunc(Resize);
glutKeyboardFunc(Board);
glutMainLoop();
return 0;
}
Do openGl show anomalies?
Extra Object (Block,not OOP) is formed while playing Game in Opengl during each move.
What are those "objects" you're talking about? There are no objects in OpenGL. There are only points, lines an triangles, drawn to a pixel based framebuffer.
In OpenGL there is no scene, there are no models, there are no objects. Any notion of a scene or objects are your burden to implement. On the OpenGL side, whenever something changes the only correct way to go about this is to perform a full redraw: Clear the whole buffer and draw the scene from scratch.
I think ,there is nothing wrong with the code and logic except it is written nonprofessional like OpenGl programmers write.
I'm sorry to break it to you, but your code is full of misconceptions and logic errors.
There must be some thing missing, some method to stop this.
It's rather simple: Don't treat OpenGL like something it is not. OpenGL is not a scene graph. Treat it like a pencil you use to draw onto a piece of paper provided to you by the operating system. If you want to draw some flip page book animation you draw each frame from scratch, and that's how you do it in OpenGL as well.
BTW, there are tools like Uncrustify that make your code readble. The mess you posted is unberable.
Update
Okay, I just sent your code through a pretty formatter to make it readable, and it's quite full of WTFs.
What do you think glClear(GL_CLIP_PLANE0) does? (hint: It's raises an invalid value error)
Why are you calling SwapBuffers followed by a glutPostRedisplay in a input event handlers. You never put graphics code into a event handler. Never!
Why do you have several glutPostRedisplay calls in the drawing code, with the occasional SwapBuffers mixed in?
Clearly you have absolutely no understanding whatsoever what those functions do and how they interact. That's pure Cargo Cult Programming.
Please, please, please go back to an introductionionary tutorial on OpenGL and work through it step for step. Don't skip things, because you clearly demonstrated you lack even the most basic understanding of how things like a double buffer works (what does swapping mean), how calling glFinish is completely uncalled for because the buffer swap implies it.
I recommend you this one: http://arcsynthesis.org/gltut
Add glClear(GL_COLOR_BUFFER_BIT); to the top of your display function.
I am trying to learn the scan-line fill algorithm implemented in OpenGL/GLUT. I cannot wrap my mind around the concept. Could anybody explain to me the algorithm in a reasonably simple fashion? The algo is below:
#include<GL/glut.h>
float x1,x2,x3,x4,y1,y2,y3,y4;
void draw_pixel(int x,int y)
{
glColor3f(0.0,1.0,1.0);
glPointSize(1.0);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
void edgedetect(float x1,float y1,float x2,float y2,int *le,int *re)
{
float temp,x,mx;
int i;
if(y1>y2)
{
temp=x1,x1=x2,x2=temp;
temp=y1,y1=y2,y2=temp;
}
if(y1==y2)
mx=x2-x1;
else
mx=(x2-x1)/(y2-y1);
x=x1;
for(i=int(y1);i<=(int)y2;i++)
{
if(x<(float)le[i]) le[i]=(int)x;
if(x>(float)re[i]) re[i]=(int)x;
x+=mx;
}
}
void scanfill(float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4)
{
int le[500],re[500],i,j;
for(i=0;i<500;i++)
le[i]=500,re[i]=0;
edgedetect(x1,y1,x2,y2,le,re);
edgedetect(x2,y2,x3,y3,le,re);
edgedetect(x3,y3,x4,y4,le,re);
edgedetect(x4,y4,x1,y1,le,re);
for(j=0;j<500;j++)
{
if(le[j]<=re[j])
for(i=le[j];i<re[j];i++)
draw_pixel(i,j);
}
}
void display()
{
x1=250.0;y1=200.0;x2=150.0;y2=300.0;x3=250.0;
y3=400.0;x4=350.0;y4=300.0;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glVertex2f(x3,y3);
glVertex2f(x4,y4);
glEnd();
scanfill(x1,y1,x2,y2,x3,y3,x4,y4);
glFlush();
}
void init()
{
glClearColor(1.0,1.0,1.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,499.0,0.0,499.0);
}
void main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutCreateWindow("scanline");
glutDisplayFunc(display);
init();
glutMainLoop();
}
The scanfill function is filling the the pixels coverted by the 4-sided polygon described by the four x-y coordinates. To do this, it uses the edgedetect function, which simulates drawing the edges of the polygon and remembers the minimum and maximum x coordinate for each y coordinate. The scanfill function then goes through each y coordinate and draws a horizontal line of pixels across the remembered range.
Im stuck in this bunch of codes...i cant get the pixel to fill up the circle??...any help
#include<iostream>
#include<glut.h>
struct Color{
float red, green, blue;
};
Color getPixel(int x, int y){ // gets the color of the pixel at (x,y)
Color c;
float color[4];
glReadPixels(x,y,1,1,GL_RGBA, GL_FLOAT, color);
c.red = color[0];
c.green = color[1];
c.blue = color[2];
return c;
}
void setPixel(int x, int y, Color c){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushAttrib(GL_ALL_ATTRIB_BITS);
glColor3f(c.red, c.green, c.blue);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
glPopAttrib();
glFlush();
}
void init()
{
glClearColor(1.0,1.0,1.0,0.0);
gluOrtho2D(0.0,300.0,0.0,300.0);
}
void drawPixel(int x,int y)
{
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
glFlush();
}
void Boundary_fill(int x,int y,Color thisColor){
Color boundary_color;
boundary_color.red=0.0;
boundary_color.green=1.0;
boundary_color.blue=0.0;
Color nextpixel=getPixel(x,y);
if((nextpixel.red!=boundary_color.red)&&(nextpixel.blue!=boundary_color.blue)&&(nextpixel.green!=boundary_color.green) && (nextpixel.red!=thisColor.red)&& (nextpixel.blue!=thisColor.blue)&& (nextpixel.green!=thisColor.green)){
setPixel(x,y,thisColor);
Boundary_fill((x+1),y,thisColor);
Boundary_fill((x-1),y,thisColor);
Boundary_fill(x,(y+1),thisColor);
Boundary_fill(x,(y-1),thisColor);
}
}
void draw(int x1,int y1, int x, int y){
drawPixel(x1+x,y1+y);//quadrant1
drawPixel(x1+x,y1-y);//quadrant2
drawPixel(x1-x,y1+y);//quadrant3
drawPixel(x1-x,y1-y);//quadrant4
drawPixel(x1+y,y1+x);//quadrant5
drawPixel(x1+y,y1-x);//quadrant6
drawPixel(x1-y,y1+x);//quadrant7
drawPixel(x1-y,y1-x);//quadrant8
}
void circle(int px,int py,int r){
int a,b;
float p;
a=0;
b=r;
p=(5/4)-r;
while(a<=b){
draw(px,py,a,b);
if(p<0){
p=p+(2*a)+1;
}
else{
b=b-1;
p=p+(2*a)+1-(2*b);
}
a=a+1;
}
}
void Circle(void)
{
Color thisColor;
thisColor.red=1.0;
thisColor.blue=0.0;
thisColor.green=0.0;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,1.0,0.0);
glPointSize(2.0);
int x0 = 100;
int y0 = 150;
circle(x0,y0,50);
glColor3f(thisColor.red,thisColor.blue,thisColor.green);
Boundary_fill(x0,y0,thisColor);
}
void main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400,400);
glutInitWindowPosition(1,1);
glutCreateWindow("Boundary fill in a circle:Taaseen And Abhinav");
init();
glutDisplayFunc(Circle);
glutMainLoop();
}
Your setPixel() routine is broken. It clears the window to white, thereby erasing all previously drawn pixels. You should remove this line and put it in your Circle() routine instead:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Because of that line, getPixel() will always read white pixels (except for one) and will recurse to the right. If you're lucky glReadPixels() will return some random colors when reaching the outside of the frame buffer. Otherwise it will produce a stack overflow. You should therefore also check your pixel position in Boundary_fill().