gpt4 book ai didi

c++ - 分离圆环中的三角带

转载 作者:行者123 更新时间:2023-11-30 02:14:13 25 4
gpt4 key购买 nike

鉴于问题描述:

problem_description

更具体地说,这个问题特别涉及装饰环。我遇到的问题是将浅色阴影三角形与深色阴影三角形分开。

我已经生成了生成圆形环的代码(基于前面的示例)。然而,三角形在一起而不是分开,并且产生相同的颜色。

这是我到目前为止生成的代码:

///////////////////////////////////////////////////////////////////////////////////////////        
// circularAnnuluses.cpp
//
// This program draws three identical-looking circular annuluses in three different ways -
// see comments below.
//
// Interaction:
// Press the space bar to toggle between wirefrime and filled for the lower annulus.
//
// Sumanta Guha.
///////////////////////////////////////////////////////////////////////////////////////////

#include <cstdlib>
#include <cmath>
#include <iostream>

#ifdef __APPLE__
# include <GL/glew.h>
# include <GL/freeglut.h>
# include <OpenGL/glext.h>
#else
# include <GL/glew.h>
# include <GL/freeglut.h>
//# include <GL/glext.h>
#pragma comment(lib, "glew32.lib")
#endif

#define PI 3.14159265
#define N 6.0 // Number of vertices on the boundary of the disc.

using namespace std;

// Globals.
static int isWire = 0; // Is wireframe?


// Drawing routine.
void drawScene(void)
{
float angle;
int i;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the buffers including
// the depth buffer.

glPolygonMode(GL_FRONT, GL_FILL);

// Lower circular annulus: with a true hole.
if (isWire) glPolygonMode(GL_FRONT, GL_LINE); else glPolygonMode(GL_FRONT, GL_FILL);
glBegin(GL_TRIANGLE_STRIP);
for (i = 0; i <= N; ++i)
{
angle = 2 * PI * i / N;
glColor3f(1.0, 0.0, 0.0);
glVertex3f(50 + cos(angle) * 10.0, 50 + sin(angle) * 10.0, 0.0);
glColor3f(0, 1, 0);
glVertex3f(50 + cos(angle) * 20.0, 50 + sin(angle) * 20.0, 0.0);
}
glEnd();

// Write labels.
glColor3f(0.0, 0.0, 0.0);

glFlush();
}

// Initialization routine.
void setup(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
}

// OpenGL window reshape routine.
void resize(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 100.0, 0.0, 100.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

// Keyboard input processing routine.
void keyInput(unsigned char key, int x, int y)
{
switch (key)
{
case ' ':
if (isWire == 0) isWire = 1;
else isWire = 0;
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}

// Routine to output interaction instructions to the C++ window.
void printInteraction(void)
{
cout << "Interaction:" << endl;
cout << "Press the space bar to toggle between wirefrime and filled for the lower annulus." << endl;
}

// Main routine.
int main(int argc, char** argv)
{
printInteraction();
glutInit(&argc, argv);

glutInitContextVersion(4, 3);
glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("DecoratedAnnulus.cpp");
glutDisplayFunc(drawScene);
glutReshapeFunc(resize);
glutKeyboardFunc(keyInput);

glewExperimental = GL_TRUE;
glewInit();

setup();

glutMainLoop();
}

问题所在的代码部分在“drawscene”函数中。我相信应该有两个不同的 for 循环来将三角形彼此分开,但每当我尝试拆分 for 循环时,它都会产生一个形状怪异的东西。

我不确定从哪里开始完成最后一个问题。

最佳答案

正如另一个答案中提到的,一种可能性是切换到 flat shading模式 glShadeModel .
但请注意,您还必须偏移外圆的顶点坐标:

glShadeModel( GL_FLAT );
glBegin(GL_TRIANGLE_STRIP);
for (i = 0; i <= N; ++i)
{
angle1 = 2 * PI * i / N;
angle2 = 2 * PI * (i+0.5) / N;

glColor3f(1.0, 0.0, 0.0);
glVertex3f(50 + cos(angle1) * 10.0, 50 + sin(angle1) * 10.0, 0.0);
glColor3f(0, 1, 0);
glVertex3f(50 + cos(angle2) * 20.0, 50 + sin(angle2) * 20.0, 0.0);
}
glEnd();

另一种可能性是用 primitive 绘制内部和外部三角形。在 2 个单独的循环中键入 GL_TRIANGLES:

glShadeModel( GL_SMOOTH );

glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
for (i = 0; i <= N; ++i)
{
angle1 = 2 * PI * i / N;
angle2 = 2 * PI * (i+0.5) / N;
angle3 = 2 * PI * (i+1) / N;

glVertex3f(50 + cos(angle1) * 10.0, 50 + sin(angle1) * 10.0, 0.0);
glVertex3f(50 + cos(angle2) * 20.0, 50 + sin(angle2) * 20.0, 0.0);
glVertex3f(50 + cos(angle3) * 10.0, 50 + sin(angle3) * 10.0, 0.0);
}
glEnd();

glBegin(GL_TRIANGLES);
glColor3f(0, 1, 0);
for (i = 0; i <= N; ++i)
{
angle1 = 2 * PI * (i-0.5) / N;
angle2 = 2 * PI * i / N;
angle3 = 2 * PI * (i+0.5) / N;

glVertex3f(50 + cos(angle1) * 20.0, 50 + sin(angle1) * 20.0, 0.0);
glVertex3f(50 + cos(angle2) * 10.0, 50 + sin(angle2) * 10.0, 0.0);
glVertex3f(50 + cos(angle3) * 20.0, 50 + sin(angle3) * 20.0, 0.0);
}
glEnd();

这两种方法都会生成以下图像:


如果您想要更“圆形”的外观,则必须沿着内圈或外圈 segmentation 线段。使用基元类型 GL_TRIANGLE_FAN(参见 Triangle primitives)绘制单个线段:

int N2 = 10;
glShadeModel( GL_SMOOTH );
# draw the red segments
glColor3f(1.0, 0.0, 0.0);
for (int i = 0; i <= N; ++i)
{
float angle1 = 2 * PI * i / N;
float angle2 = 2 * PI * (i+0.5) / N;
float angle3 = 2 * PI * (i+1) / N;

# draw a single red segment
glBegin(GL_TRIANGLE_FAN);
glVertex3f(50 + cos(angle2) * 20.0, 50 + sin(angle2) * 20.0, 0.0);
for (int j = 0; j <= N2; ++j)
{
float a = angle1 + (angle3 - angle1) * (float)j / (float)N2;
glVertex3f(50 + cos(a) * 10.0, 50 + sin(a) * 10.0, 0.0);
}
glEnd();
}

# draw the green sgements
glColor3f(0, 1, 0);
for (int i = 0; i <= N; ++i)
{
float angle1 = 2 * PI * (i-0.5) / N;
float angle2 = 2 * PI * i / N;
float angle3 = 2 * PI * (i+0.5) / N;

# draw a single green segment
glBegin(GL_TRIANGLE_FAN);
glVertex3f(50 + cos(angle2) * 10.0, 50 + sin(angle2) * 10.0, 0.0);
for (int j = 0; j <= N2; ++j)
{
float a = angle1 + (angle3 - angle1) * (float)j / (float)N2;
glVertex3f(50 + cos(a) * 20.0, 50 + sin(a) * 20.0, 0.0);
}
glEnd();
}

关于c++ - 分离圆环中的三角带,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58295066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com