我想创建奥迪标志圈:
但是,我的代码的结果:
#define PI 3.14159265358979323846
#ifdef __APPLE_CC__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <cstdlib>
#include <iostream>
float zoom = 1.0f;
void init() {
// Set the background color to white
glClearColor(1.0, 1.0, 1.0, 1.0);
// Set up the viewport
glViewport(0, 0, 500, 500);
// Initialize the projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Initial zoom setup, centering at the origin
float range = 5.0 * zoom;
gluOrtho2D(-range, range, -range, range);
// Initialize the modelview matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void drawCircle(GLfloat x, GLfloat y, GLfloat radius, GLint numberOfSides, GLfloat r, GLfloat g, GLfloat b, GLfloat alpha) {
glColor4f(r, g, b, alpha);
glBegin(GL_POLYGON);
for (int i = 0; i < numberOfSides; i++) {
GLfloat angle = 2.0f * PI * i / numberOfSides;
glVertex2f(x + cos(angle) * radius, y + sin(angle) * radius);
}
glEnd();
}
void display() {
// Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT);
// Draw circles with specified fills
drawCircle(3.0, 0.0, 2.0, 100, 0.3921f, 0.4313f, 0.4470f, 1.0f); // outer circle 1
drawCircle(3.0, 0.0, 1.5, 100, 1.0f, 1.0f, 1.0f, 1.0f); // innermost circle 1
drawCircle(1.0, 0.0, 2.0, 100, 0.3921f, 0.4313f, 0.4470f, 1.0f); // outer circle 2
drawCircle(1.0, 0.0, 1.5, 100, 1.0f, 1.0f, 1.0f, 1.0f); // innermost circle 2
drawCircle(-1.0, 0.0, 2.0, 100, 0.3921f, 0.4313f, 0.4470f, 1.0f); // outer circle 2
drawCircle(-1.0, 0.0, 1.5, 100, 1.0f, 1.0f, 1.0f, 1.0f); // innermost circle 2
drawCircle(-3.0, 0.0, 2.0, 100, 0.3921f, 0.4313f, 0.4470f, 1.0f); // outer circle 2
drawCircle(-3.0, 0.0, 1.5, 100, 1.0f, 1.0f, 1.0f, 1.0f); // innermost circle 2
// Draw the star
glFlush();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 800);
glutInitWindowPosition(100, 100);
glutCreateWindow("Mercedes Logo with Circle");
init();
// Register display callback function
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
...不是我所期望的:
第二个圆圈隐藏了第一个圆圈的一部分。
我与chatGPT经过长时间讨论后找到了答案,答案如下:
#include <GL/glut.h>
#include <cmath>
#define PI 3.14159265358979323846
float zoom = 1.0f;
void init() {
// Set the background color to white
glClearColor(1.0, 1.0, 1.0, 1.0);
// Set up the viewport
glViewport(0, 0, 500, 500);
// Initialize the projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Initial zoom setup, centering at the origin
float range = 2.0 * zoom;
gluOrtho2D(-range, range, -range, range);
// Initialize the modelview matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void drawRingUsingQuads(float centerX, float centerY, float outerRadius, float innerRadius, int numSegments, float r, float g, float b, float alpha) {
float angleStep = (2.0f * PI) / numSegments;
glColor4f(r, g, b, alpha); // Set the color with alpha for transparency
glBegin(GL_QUADS);
for (int i = 0; i < numSegments; i++) {
float angle = i * angleStep;
float nextAngle = (i + 1) * angleStep;
// Outer circle vertices
float x1 = centerX + outerRadius * cos(angle);
float y1 = centerY + outerRadius * sin(angle);
float x2 = centerX + outerRadius * cos(nextAngle);
float y2 = centerY + outerRadius * sin(nextAngle);
// Inner circle vertices
float x3 = centerX + innerRadius * cos(nextAngle);
float y3 = centerY + innerRadius * sin(nextAngle);
float x4 = centerX + innerRadius * cos(angle);
float y4 = centerY + innerRadius * sin(angle);
// Define the quad connecting outer and inner circles
glVertex2f(x1, y1); // Outer vertex 1
glVertex2f(x2, y2); // Outer vertex 2
glVertex2f(x3, y3); // Inner vertex 2
glVertex2f(x4, y4); // Inner vertex 1
}
glEnd();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen
glEnable(GL_BLEND); // Enable blending
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Set blending function
// Draw a ring using quads
drawRingUsingQuads(0.0f, 0.0f, 0.5f, 0.3f, 100, 0.0f, 0.0f, 0.0f, 1.0f); // Black ring with full opacity
drawRingUsingQuads(0.5f, 0.0f, 0.5f, 0.3f, 100, 0.0f, 0.0f, 0.0f, 1.0f); // Black ring with full opacity
drawRingUsingQuads(1.0f, 0.0f, 0.5f, 0.3f, 100, 0.0f, 0.0f, 0.0f, 1.0f); // Black ring with full opacity
glDisable(GL_BLEND); // Disable blending
glFlush(); // Swap buffers to display drawing
}
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set display mode
glutInitWindowSize(500, 500); // Set window size
glutInitWindowPosition(100, 100); // Set window position
glutCreateWindow("Circle with Hole Using Quads"); // Create window with title
glutDisplayFunc(display); // Set display callback function
glClearColor(1.0, 1.0, 1.0, 1.0); // Set the background color to white
init();
glutMainLoop(); // Enter main loop for event handling and drawing
return 0;
}