This code is from the red book, example 2-15 (well, the code is not exactly the one in the book). Take care of the note I noted.
#include <fstream>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glut.h>
#pragma comment(lib,"glew32.lib")
using namespace std;
#define BUFFER_OFFSET(offset) ((GLubyte *)NULL+offset)
#define XStart -0.8
#define XEnd 0.8
#define YStart -0.8
#define YEnd 0.8
#define NumXPoints 11
#define NumYPoints 11
#define NumPoints (NumXPoints * NumYPoints)
#define NumPointsPerStrip (2*NumXPoints)
#define NumStrips (NumYPoints-1)
#define RestartIndex 0xffff
void display(void)
{
int i,start;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glDrawElements(GL_TRIANGLE_STRIP,NumStrips*(NumPointsPerStrip+1),GL_UNSIGNED_SHORT,BUFFER_OFFSET(0));
//glFlush();//it works,show a white square with black backgroud
glutSwapBuffers();///it doesn't work,show what tha area looked like before
}
void init (void)
{
GLuint vbo,ebo;
GLfloat *vertices;
GLushort *indices;
glewInit();
glGenBuffers(1,&vbo);
glBindBuffer(GL_ARRAY_BUFFER,vbo);
glBufferData(GL_ARRAY_BUFFER,2*NumPoints*sizeof(GLfloat),NULL,GL_STATIC_DRAW);
vertices=(GLfloat *)glMapBuffer(GL_ARRAY_BUFFER,GL_WRITE_ONLY);
if(vertices==NULL)
{
fprintf(stderr,"Unable to map vertex buffer\n");
exit(EXIT_FAILURE);
}
else
{
int i,j;
GLfloat dx=(XEnd-XStart)/(NumXPoints-1);
GLfloat dy=(YEnd-YStart)/(NumYPoints-1);
GLfloat *tmp=vertices;
int n=0;
for(j=0;j<NumYPoints;++j)
{
GLfloat y=YStart+j*dy;
for(i=0;i<NumXPoints;++i)
{
GLfloat x=XStart + i*dx;
*tmp++=x;
*tmp++=y;
}
}
glUnmapBuffer(GL_ARRAY_BUFFER);
glVertexPointer(2,GL_FLOAT,0,BUFFER_OFFSET(0));
glEnableClientState(GL_VERTEX_ARRAY);
}
glGenBuffers(1,&ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,NumStrips*(NumPointsPerStrip+1)*sizeof(GLushort),NULL,GL_STATIC_DRAW);
indices=(GLushort *)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER,GL_WRITE_ONLY);
if(indices==NULL)
{
fprintf(stderr,"Unable to map index buffer\n");
exit(EXIT_FAILURE);
}
else
{
int i,j;
GLushort *index=indices;
for(j=0;j<NumStrips;++j)
{
GLushort bottomRow=j*NumYPoints;
GLushort topRow=bottomRow+NumYPoints;
for(i=0;i<NumXPoints;++i)
{
*index++=topRow+i;
*index++=bottomRow+i;
}
*index++=RestartIndex;
}
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
}
glPrimitiveRestartIndex(RestartIndex);
glEnable(GL_PRIMITIVE_RESTART);
}
void reshape (int w, int h)
{
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D (-1,1,-1,1);
glViewport (0,0,w,h);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (200, 200);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc (keyboard);
glutMainLoop();
return 0;
}
as the http://www.opengl.org/resources/libraries/glut/spec3/node21.html says:
An implicit glFlush is done by glutSwapBuffers before it returns. Subsequent OpenGL commands can be issued immediately after calling glutSwapBuffers, but are not executed until the buffer exchange is completed.
If the layer in use is not double buffered, glutSwapBuffers has no effect.
How do I know whether the layer in use is double buffered?give an double buffered example.is this code I wrote is double buffered?
In your main function, you call
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
That needs to be GLUT_DOUBLE. See the documentation for glutInitDisplayMode().
Related
how do I get my C++ glut project to have smooth input?
the only way I could get input was with a slight delay, which is annoying. I am new to C++ glut so pls help me.
#include <windows.h>
#include <GL/glut.h>
#include <GL/glu.h>
#include <stdio.h>
#include <iostream>
GLboolean upPressed = false;
using namespace std;
void display();
void reshape(int,int);
void timer(int);
void keyboard_callback(int, int, int);
void SpecialKeysUp(int, int, int);
void update();
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowPosition(200, 100);
glutInitWindowSize(500, 500);
glutCreateWindow("window");
glutSpecialFunc(keyboard_callback);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(0,timer,0);
glutSpecialUpFunc(SpecialKeysUp);
glutIdleFunc(update);
init();
glutMainLoop();
}
// sy is the first squares y position. no x value needed since the x value is static.
// also note that the "s" in sy stands for square, squarey_position
float x_position = 0.0;
float sy_position = -4.0;
int state = 1;
char input;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glBegin(GL_QUADS);
glColor3f(1.0,0.0,0.0);
glVertex2f(x_position,1.0);
glVertex2f(x_position,-1.0);
glVertex2f(x_position+2.0,-1.0);
glVertex2f(x_position+2.0,1.0);
glColor3f(1.0,0.0,0.0);
glVertex2f(-8.0,sy_position);
glVertex2f(-9.0,sy_position);
glVertex2f(-9.0,sy_position+8.0);
glVertex2f(-8.0,sy_position+8.0);
glEnd();
glutSwapBuffers();
}
void reshape(int w,int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-10, 10, -10, 10);
glMatrixMode(GL_MODELVIEW);
}
void timer(int)
{
glutPostRedisplay();
glutTimerFunc(1000/60,timer,0);
switch(state)
{
case 1:
if(x_position<8)
x_position+=0.15;
else
state = -1;
break;
case -1:
if(x_position>-10)
x_position-=0.15;
else
state=1;
break;
}
}
void keyboard_callback(int key, int, int)
{
if(GLUT_KEY_UP == key)
upPressed = true;
}
void SpecialKeysUp(int key, int, int)
{
if(GLUT_KEY_UP == key)
upPressed = false;
}
void update()
{
// glutPostRedisplay();
// glutTimerFunc(1000/60,update,0);
if(upPressed)
sy_position+=0.15;
}
It is a ping pong game and currently the ball is working fine, however I am trying to get smooth input for the rectangle but the only way I could was with input delay.
I am new to glut C++ so pls help me
Actually I figured out how to do it. The glutTimerFunc updates and resets every 1000/60 milliseconds so valuing a bool at "true" whilst the up key is pressed(with GLUT_KEY_UP) and then valuing a bool at "false" when the key is not pressed(with the SpecialUpFunc) makes for smoother input since the update function resets must faster than just holding the key down.
here is my code
#include "stdafx.h"
#include <GL/glut.h>
#include <gl/GL.h>
#include <gl/GLU.h>
#include <math.h>
#define pi 3.14;
float angle=3;
void reshape(int w, int h)
{
if(h==0) h=1;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,w,0,h,-1,1);
}
void display(void)
{
glDepthMask(GL_TRUE);
glClear(GL_COLOR_BUFFER_BIT);
glRotatef(angle++,0,1.0,1.0);
glBegin(GL_POLYGON);
glVertex2f(10,10);
glVertex2f(30,20);
glVertex2f(30,30);
glVertex2f(10,30);
glEnd();
glRotatef(angle++,0,1.0,1.0);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(300,300);
int segments=20;
GLfloat angle=0;
for(int i=0;i<=segments; i++)
{
angle=i*2*pi; angle=angle / (segments);
glVertex2f(300+cos(angle)*30,300+sin(angle)*30);
}
glEnd();
glFlush();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc , argv);
glutInitDisplayMode(GLUT_DEPTH|GLUT_RGB|GLUT_DOUBLE);
glutInitWindowSize(640,480);
glutCreateWindow("first");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(display);
glutMainLoop();
return(0);
}
here the problems are
cannot see the rotation correctly just blinking the rectangle. Don't know is it rotating or not.
2.the circle is not rotating even there is code for rotation. why?
3.what change I made if I want only rectangle or circle is rotated?
1. cannot see the rotation correctly just blinking the rectangle.
You're probably running waaaaay too fast. Use a timer callback to issue glutPostRedisplay()s every 16ms or so.
2. the circle is not rotating even there is code for rotation. why?
glRotatef(angle++,0,1.0,1.0);
^^^
You're rotating outside the X/Y plane and exceeding the depth limits of your glOrtho() call resulting in clipped fragment. Bump your nearVal/farVal to accommodate:
glOrtho(0,w,0,h,-1000,1000);
3. what change I made if I want only rectangle or circle is rotated?
Remove one of the glRotatef()s.
All together:
#include <GL/glut.h>
#include <cmath>
using namespace std;
const double pi = 3.14159;
float angle=3;
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
glOrtho(-w,w,-h,h,-1000,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDepthMask(GL_TRUE);
//glRotatef(angle++,0,1.0,1.0);
glBegin(GL_POLYGON);
glVertex2f(10,10);
glVertex2f(30,20);
glVertex2f(30,30);
glVertex2f(10,30);
glEnd();
glRotatef(angle++,0,1.0,1.0);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(300,300);
const int segments=20;
for(int i=0;i<=segments; i++)
{
const GLfloat angle = ( i*2*pi ) / (float)segments;
glVertex2f(300+cos(angle)*30,300+sin(angle)*30);
}
glEnd();
glutSwapBuffers();
}
void timer( int value )
{
glutPostRedisplay();
glutTimerFunc( 16, timer, 0 );
}
int main(int argc, char** argv)
{
glutInit(&argc , argv);
glutInitDisplayMode(GLUT_DEPTH|GLUT_RGB|GLUT_DOUBLE);
glutInitWindowSize(640,480);
glutCreateWindow("first");
glutDisplayFunc(display);
glutTimerFunc( 0, timer, 0 );
glutMainLoop();
return(0);
}
There is a simple program which is actually from a bigger one,putting this aside,I tried to do a simple pixel read/write in Opengl.
The first section which involves reading a pixel color succeeds,But the second operation which involves writing a pixel and then reading it fails!.
Here is the program which demonstrates the problem :
#include <iostream>
#include <glut.h>
using namespace std;
void init(void)
{
glClearColor(1.0,0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,100.0,0.0,100.0);
}
void displayfnc()
{
glClear(GL_COLOR_BUFFER_BIT);
unsigned char current[3];
int r,g,b;
//x=0 to 6 is right
glBegin (GL_POINTS);
glColor3f (0.0,0.0,1.0);
glVertex2i (6,0);
glEnd();
//works well.
glReadPixels(6,0, 1, 1 , GL_RGB , GL_UNSIGNED_BYTE ,current);
r = current[0];
g = current[1];
b = current[2];
cout<<"read from x=6::::"<<"r:"<<r<<"g:"<<g<<"b:"<<b<<endl;
//x =7 and other isnt right
glBegin (GL_POINTS);
glColor3f (0.0,0.0,1.0);
glVertex2i (7,0);
glEnd();
//the problem is here in the reading.why this happen?
glReadPixels(7,0, 1, 1 , GL_RGB , GL_UNSIGNED_BYTE ,current);
r = current[0];
g = current[1];
b = current[2];
cout<<"Read from x=7::::"<<"r:"<<r<<"g:"<<g<<"b:"<<b<<endl;
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(100,100);
glutInitWindowPosition(0, 0);
glutCreateWindow("BoundaryFill");
init();
glutDisplayFunc(displayfnc);
glutMainLoop();
return 0;
}
Here is the output:
What is causing the second read procedure to fail?
At least on my system glutInitWindowSize(100,100) is just a suggestion. glutGet(GLUT_WINDOW_WIDTH) actually returns 104, not 100 as your gluOrtho2D() call is assuming.
Try using a dynamic projection instead:
#include <GL/glut.h>
#include <iostream>
using namespace std;
void displayfnc()
{
glClearColor(1.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
gluOrtho2D(0.0,w,0.0,h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
unsigned char current[3];
int r,g,b;
//x=0 to 6 is right
glBegin (GL_POINTS);
glColor3f (0.0,0.0,1.0);
glVertex2i (6,0);
glEnd();
//works well.
glReadPixels(6,0, 1, 1 , GL_RGB , GL_UNSIGNED_BYTE ,current);
r = current[0];
g = current[1];
b = current[2];
cout<<"read from x=6::::"<<"r:"<<r<<"g:"<<g<<"b:"<<b<<endl;
//x =7 and other isnt right
glBegin (GL_POINTS);
glColor3f (0.0,0.0,1.0);
glVertex2i (7,0);
glEnd();
//the problem is here in the reading.why this happen?
glReadPixels(7,0, 1, 1 , GL_RGB , GL_UNSIGNED_BYTE ,current);
r = current[0];
g = current[1];
b = current[2];
cout<<"Read from x=7::::"<<"r:"<<r<<"g:"<<g<<"b:"<<b<<endl;
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(100,100);
glutInitWindowPosition(0, 0);
glutCreateWindow("BoundaryFill");
glutDisplayFunc(displayfnc);
glutMainLoop();
return 0;
}
The program should simulate a planet rotating around another planet.
I use gltranslatef to let the planet move around the bigger planet, but the problem is that the planet should hide when is over the bigger planet, because dz is -0.5.
But if I test the program I always see the red planet over the blue one.
Another problem I have: the planet rotates too fast, how do I slow it?
#include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
#include "utility.h"
GLfloat dx=0.0;
GLfloat dz=-0.5;
bool plus=true;
void init()
{
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1, 1);
glEnable(GLUT_DEPTH);
}
void render()
{
glClearColor(BLACK);
glClear(GL_COLOR_BUFFER_BIT);
glColor4f(BLUE);
glutWireSphere(0.25, 100, 100);
glPushMatrix();
glLoadIdentity();
glTranslatef(-0.5+dx, 0.0, -dz);
glColor4f(RED);
glutWireSphere(0.05, 100, 100);
glPopMatrix();
glFlush();
}
void idle()
{
if(plus)
{
dx+=0.05;
}
else
{
dx-=0.05;
}
if(dx>=1.0)
{
dx=0.5;
plus=false;
}
else if(dx<=-0.0)
{
dx=0.0;
plus=true;
}
glutPostRedisplay();
}
int main(int argc, const char * argv[])
{
glutInit(&argc, (char**)argv);
glutInitWindowSize(500, 500);
glutInitWindowPosition(150, 150);
glutInitWindowPosition(0, 0);
glutCreateWindow("Simple");
glutIdleFunc(idle);
init();
glutDisplayFunc(render);
glutMainLoop();
return 0;
}
I haven't understood well how does the idle function work, why it gets called so many times? Can't I choose a time interval with which the idle function gets called?
More info: RED and BLUE are RGB floats, defined in the utility.h header file.
plus is a bool that is used to know if I have to decrease or increase dx.
Give this a shot:
#include <GL/glut.h>
double GetSeconds()
{
return glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
}
void render()
{
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glColor3ub(0,0,255);
glutWireSphere(0.25, 100, 100);
glPushMatrix();
glLoadIdentity();
static double prv = GetSeconds();
double cur = GetSeconds();
double delta = cur - prv;
prv = cur;
const float DEG_PER_SEC = 60.0f;
static float angle = 0.0f;
angle += DEG_PER_SEC * delta;
while( angle > 360 ) angle -= 360;
glPushMatrix();
glRotatef( angle, 0, 1, 0 );
glTranslatef( 0.5, 0, 0);
glColor3ub(255,0,0);
glutWireSphere(0.05, 100, 100);
glPopMatrix();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1, 1);
}
void timer(int extra)
{
glutPostRedisplay();
glutTimerFunc(16, timer, 0);
}
int main(int argc, const char * argv[])
{
glutInit(&argc, (char**)argv);
glutInitWindowSize(500, 500);
glutInitWindowPosition(150, 150);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Simple");
glutReshapeFunc(reshape);
glutTimerFunc(0, timer, 0);
glutDisplayFunc(render);
glEnable( GL_DEPTH_TEST );
glutMainLoop();
return 0;
}
Important parts:
Explicit glMatrixMode() calls
Calling glutInitDisplayMode() before glutCreateWindow()
Double-buffering requires glutSwapBuffers()
Clearing the depth buffer via GL_DEPTH_BUFFER_BIT
glEnable( GL_DEPTH_TEST )
glRotatef() for planet rotation
Timer-based animation
I am trying to draw a simple square wherever I press the left mouse button using opengl/glut. My program runs perfectly except for the part where it does not draw the square where I click the left mouse button :). Can someone point out what I am doing wrong?
#include <stdlib.h>
#include <glut.h>
GLsizei WIDTH = 1300, HEIGHT = 700;
GLsizei MOUSEx, MOUSEy;
GLfloat SIDE=1;
GLfloat RED[3] = {1,0,0};
GLfloat GREEN[3] = {0,1,0};
GLfloat BLUE[3] = {0,0,1};
GLfloat WHITE[3] = {1,1,1};
GLfloat BLACK[3] = {0,0,0};
GLfloat YELLOW[3] = {1,1,0};
GLfloat CYAN[3] = {0,1,1};
GLfloat MAGENTA[3] = {1,0,1};
void drawSquare(int x, int y)
{
glColor3fv(YELLOW);
glBegin(GL_POLYGON);
glVertex3f(x+SIDE, y+SIDE,0);
glVertex3f(x-SIDE, y+SIDE,0);
glVertex3f(x-SIDE, y-SIDE,0);
glVertex3f(x+SIDE, y-SIDE,0);
glEnd();
glFlush();
}
void drawSquare1()
{
int x=0,y=0;
glColor3fv(BLUE);
glBegin(GL_POLYGON);
glVertex3f(x+SIDE, y+SIDE,0);
glVertex3f(x-SIDE, y+SIDE,0);
glVertex3f(x-SIDE, y-SIDE,0);
glVertex3f(x+SIDE, y-SIDE,0);
glEnd();
glFlush();
}
void display (void) {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0,0,-5);
drawSquare1();
glFlush();
}
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, 100.0);
glMatrixMode (GL_MODELVIEW);
WIDTH=w;
HEIGHT=h;
}
void setX(int x)
{
MOUSEx=x;
}
void setY(int y)
{
MOUSEy=y;
}
void mouse(int btn, int state, int x, int y)
{
if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
{
setX(x);
setY(y);
drawSquare(MOUSEx,HEIGHT-MOUSEy);
glutPostRedisplay();
}
if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
{
exit(1);
}
}
int main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (WIDTH, HEIGHT);
glutInitWindowPosition (10, 10);
glutCreateWindow ("New Window");
glutDisplayFunc (display);
glutReshapeFunc (reshape);
glutMouseFunc(mouse);
//glutMotionFunc(drawSquare);
glutMainLoop ();
return 0;
}
In short words: OpenGL is not a scene graph. That means that those drawing commands issued in the mouse handler don't create some kind of "persistency".
Instead clicking the mouse you should store the position in a list/array and draw the squares from the values in that list in the display function.
I am fresh to this Coding But I done it.
Here is My Code It will Work.
#include <GL/glut.h>
GLsizei MOUSEx=0, MOUSEy=0;
GLfloat SIDE=50;
GLfloat BLUE[3] = {0,0,1};
void drawSquare1()
{
glColor3fv(BLUE);
glBegin(GL_POLYGON);
glVertex3f(MOUSEx, MOUSEy,0);
glVertex3f(MOUSEx+SIDE, MOUSEy,0);
glVertex3f(MOUSEx+SIDE, MOUSEy+SIDE,0);
glVertex3f(MOUSEx, MOUSEy+SIDE,0);
glEnd();
glFlush();
}
void display(void)
{
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
drawSquare1();
glFlush();
}
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, 100.0);
glOrtho(0.0,1368,768,0,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void spindisplay(void)
{
glutPostRedisplay();
}
void setX(int x)
{
MOUSEx=x;
}
void setY(int y)
{
MOUSEy=y;
}
void mouse(int btn, int state, int x, int y)
{
if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
{
setX(x);
setY(y);
//drawSquare(MOUSEx,HEIGHT-MOUSEy);
glutPostRedisplay();
}
if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
{
exit(1); // To Exit the Program
}
}
int main(int argc, char **argv)
{ glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(1366,768);
glutInitWindowPosition(0,0);
glutCreateWindow("Moving squares");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutIdleFunc(spindisplay);
glutMainLoop();
}
Below is the sample code taken from OpenGL Projects. Try to execute this and I think the problem with your drawing squares in OpenGL would be solve easily. If any question kindly ask.
#include<stdlib.h>
#include<glut.h>
GLsizei wh=500,ww=500;
GLfloat size=3.0;
void display()
{
glFlush();
}
void myInit()
{
glViewport(0,0,ww,wh);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,(GLdouble)ww,0.0,(GLdouble)wh);
glMatrixMode(GL_MODELVIEW);
glClearColor(0.0,0.0,0.0,1.0);
glColor3f(1.0,0.0,0.0);
}
void myReshape(GLsizei w,GLsizei h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,(GLdouble)w,0.0,(GLdouble)h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0,0,w,h);
ww=w;
wh=h;
}
void drawSquare(int x,int y)
{
y=wh-y;
glBegin(GL_POLYGON);
glVertex2f(x+size,y+size);
glVertex2f(x-size,y+size);
glVertex2f(x-size,y-size);
glVertex2f(x+size,y-size);
glEnd();
glFlush();
}
void size_menu(int id)
{
switch(id)
{
case 2: size=size*2;
break;
case 3:if(size>1) size=size/2;
break;
}
glutPostRedisplay();
}
void top_menu(int id)
{
switch(id)
{
case 1:exit(0);break;
}
glutPostRedisplay();
}
void myMouse(int button,int state,int x,int y)
{
if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
drawSquare(x,y);
if(button==GLUT_RIGHT_BUTTON&&state==GLUT_DOWN)
exit(0);
}
int main(int argc,char **argv)
{
int sub_menu;
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutCreateWindow("Hierarchical Menus to Draw Squares");
glutDisplayFunc(display);
myInit();
glutMouseFunc(myMouse);
sub_menu=glutCreateMenu(size_menu);
glutAddMenuEntry("Increase Square Size",2);
glutAddMenuEntry("Decrease Square Size",3);
glutCreateMenu(top_menu);
glutAddMenuEntry("Quit",1);
glutAddSubMenu("Resize",sub_menu);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutReshapeFunc(myReshape);
glClear(GL_COLOR_BUFFER_BIT);
glutMainLoop();
return 0;
}