PDA

View Full Version : How to resize and position a circle



KSM888
05-26-2014, 09:25 AM
Hi new to OpenGL, my question is how to resize and position a circle object, ive created a door(a rectangle) and need a circle to be a door knob. Below is the code i found online to make a circle and im not familiar to what the code is doing can someone please assist me to goal is the have to knob place in the middle far left side. thank you!

//DOOR KNOB
glBegin(GL_TRIANGLE_FAN);
glColor3f(SkyBlue);
for (int i = 0; i <= 180; i++){
double angle = 2* M_PI * i / 180;
double x = cos(angle);
double y = sin(angle);
glVertex3d(x, y, -0.5);
}
glEnd();

//DOOR side - FRONT Rectangle
glBegin(GL_POLYGON);
glColor3f(White);
glVertex3f(0.1, -0.5, -0.5); // P1 Bottom Right
glVertex3f(0.1, -0.1, -0.5); // P2 Top Right
glVertex3f(-0.1, -0.1, -0.5); // P3 Top left
glVertex3f(-0.1, -0.5, -0.5); // P4 Bottom Left
glEnd();

Carmine
05-26-2014, 05:27 PM
You say you've 'created a door'. Does that mean you see the rectangle when you run the code? Do you seen any part of the circle? Generally speaking, use glScalef to resize things and glTranslatef to move things.

KSM888
05-26-2014, 08:07 PM
You say you've 'created a door'. Does that mean you see the rectangle when you run the code? Do you seen any part of the circle? Generally speaking, use glScalef to resize things and glTranslatef to move things.

I tried adding the following code but its doesn't really work. I see the white door and then i see the circle ahead of the door.

//DOOR KNOB
glBegin(GL_TRIANGLE_FAN);
glColor3f(SkyBlue);
for (int i = 0; i <= 300; i++){
double angle = 2* M_PI * i / 300;
double x = cos(angle);
double y = sin(angle);
glVertex3d(x, y, -0.5);
}
glTranslatef(-0.2, -0.2, -0.5);
glScalef(0.5, 0.5, 0.5);
glEnd();

Carmine
05-27-2014, 10:37 AM
Translate and Scale must come BEFORE glBegin.

dewdrops39
07-12-2014, 10:53 PM
#include <gl/gl.h>
#include <gl/glu.h>
#include <math.h>
#define DEG2RAD 3.14159/180.0
void circl(float radiusX,float radiusY);

void init()
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(1.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10.0,10.0,-10.0,10.0,-10.0,10.0);//change diff values here to position
//glViewport(0, 0,100,100);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);

circl(2.0,2.0);
glEnd();
glFlush();
}
void circl(float radiusX,float radiusY)
{
int i;
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINE_LOOP);

for(i=0;i<360;i++)
{
float rad = i*DEG2RAD;
glVertex2f(cos(rad)*radiusX,sin(rad)*radiusY);
}

glEnd();
}
void main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize( 800, 800 );
glutInitWindowPosition(100,100);

glutCreateWindow("cool");
glutDisplayFunc(display);
init();
glutMainLoop();

} //Anuroop,kongad,pkd