how to fill ellipse in openGL?

mc 4,476 Reputation points
2024-09-25T01:24:27.7433333+00:00

and in openGL the width of the window is1.0f?

I want to use openGL to fill ellipse how to do it?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,723 questions
{count} votes

Accepted answer
  1. Castorix31 85,131 Reputation points
    2024-09-25T06:53:13.16+00:00

    You can see https://www.codeproject.com/Articles/67357/OpenGL-Oval

    or from ChatGPT :

    
    void DrawEllipse(float xCenter, float yCenter, float xRadius, float yRadius) {
    	int numSegments = 100;  // The number of segments for the circle approximation
    	float angleStep = (2.0f * 3.14159f) / numSegments;
    
    	// Draw filled ellipse
    	glBegin(GL_TRIANGLE_FAN);
    	glVertex2f(xCenter, yCenter);  // Center of the ellipse
    	for (int i = 0; i <= numSegments; i++) {
    		float angle = i * angleStep;
    		float x = xCenter + cosf(angle) * xRadius;
    		float y = yCenter + sinf(angle) * yRadius;
    		glVertex2f(x, y);
    	}
    	glEnd();
    
    	// Draw ellipse outline
    	glBegin(GL_LINE_LOOP);
    	for (int i = 0; i <= numSegments; i++) {
    		float angle = i * angleStep;
    		float x = xCenter + cosf(angle) * xRadius;
    		float y = yCenter + sinf(angle) * yRadius;
    		glVertex2f(x, y);
    	}
    	glEnd();
    }
    
    
    

    By testing in Nehe's Lesson 5 tutorial:

    gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
    

    replaced by

    glOrtho(0.0f, width, height, 0.0f, -1.0f, 1.0f);
    

    in DrawGLScene :

    glColor3f(1.0f, 0.0f, 0.0f);
    DrawEllipse(200.0f, 200.0f, 100.0f, 50.0f); 
    

    =>

    User's image

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.