PDA

View Full Version : Cannot Draw Ellipse



Stefano M.
08-26-2011, 06:57 AM
I'm trying to draw an ellipse and tried google and I have failed to make any useful progress. Here Are the codes I took on the internet and did a lot of modification, adding and removing to suit my needs and it does not work.
Please help me point what is wrong
Thanks
main.h


#include <GL/glut.h>
#include <cmath>
using namespace std;

//initiate display
void glutIniStuffs(int argc, char** argv) ;

//Create the window
int glutCreateWindow(char *title);

//Render our scene
void drawScene();

void changeSize(int w, int h);

//Register all call backs here
void registerCallBacks();

void drawEllipse(float radiusX, float radiusY);

main.cpp

#include "main.h"

void glutIniStuffs(int argc, char** argv) {
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(500, 500); // window size
glutInitWindowPosition((glutGet(GLUT_SCREEN_WIDTH)-640)/2, (glutGet(GLUT_SCREEN_HEIGHT)-480)/2); // window location
}

#define DEG2RAD 3.14159/180.0

void drawEllipse(float radiusX, float radiusY) {
int i;

glBegin(GL_LINE_LOOP);

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

glEnd();
}

void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.9f, 0.0f, 0.9f);
GLfloat axisZ = -15;
//glTranslated(-1.25, -1, 1);

drawEllipse(2.0, 2.8);

glutSwapBuffers();
}



void registerCallBacks() {
// register callbacks

//Draw function
glutDisplayFunc(drawScene);

//redraw function
glutReshapeFunc(changeSize);
}

void changeSize(int w, int h) {

// Prevent a divide by zero, when window is too short -- (you cant make a window of zero width).
if(h == 0) {
h = 1;
}

float winRatio = 1.0* w / h;

// Set the viewport to be the entire window
glViewport(0, 0, w, h);

// Use the Projection Matrix
glMatrixMode(GL_PROJECTION);

// Reset Matrix
glLoadIdentity();

// Set the correct perspective.
// Set the Field of view angle (in degrees), the aspect ratio of our window, and the new and far planes
gluPerspective(60, winRatio, 1.0, 100.0);

// Get Back to the Modelview
glMatrixMode(GL_MODELVIEW);


}


int main(int argc, char** argv) {
//initalize glut
glutIniStuffs(argc, argv);

//create window
glutCreateWindow("House Made with OpenGL");

//register callbacks
registerCallBacks();
glutMainLoop();
return 0;
}

mobeen
08-26-2011, 07:17 AM
Hi, I think u should try adding in orthographic projection first to make sure that u get something on screen. Replace the gluPerspective call with this and see if u get something on screen.

gluOrtho2D(-10,10,-10,10);

Stefano M.
08-26-2011, 08:04 AM
I Fixed it with codes below. Let me close this and open another thread because I have another problem

main.h

#include <GL/glut.h>
#include <cmath>
using namespace std;

//initiate display
void glutIniStuffs(int argc, char** argv) ;

//Create the window
int glutCreateWindow(char *title);

//Render our scene
void drawScene();

void changeSize(int w, int h);

//Register all call backs here
void registerCallBacks();

void drawEye(float radiusX, float radiusY);
void drawCircle(float x, float y, float r);

main.cpp

#include "main.h"

void glutIniStuffs(int argc, char** argv) {
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(500, 500); // window size
glutInitWindowPosition((glutGet(GLUT_SCREEN_WIDTH)-640)/2, (glutGet(GLUT_SCREEN_HEIGHT)-480)/2); // window location
}

#define DEG2RAD 3.14159/180.0

void drawEye(float radiusX, float radiusY) {
int i;

glBegin(GL_LINE_LOOP);

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

glEnd();
}

void drawCircle(float x, float y, float r) {

}


void drawScene() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);

drawEye(22.8, 12.0);
drawCircle(2, 4, 10);

glutSwapBuffers();
}



void registerCallBacks() {
// register callbacks

//Draw function
glutDisplayFunc(drawScene);

//redraw function
glutReshapeFunc(changeSize);
}

void changeSize(int w, int h) {
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}


int main(int argc, char** argv) {
//initalize glut
glutIniStuffs(argc, argv);

//create window
glutCreateWindow("House Made with OpenGL");

//register callbacks
registerCallBacks();
glutMainLoop();
return 0;
}

Stefano M.
08-26-2011, 08:06 AM
Please mod, help me mark it solved!