将环形带分隔成三角形

问题描述 投票:1回答:2

给出问题描述:

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循环时,都会产生一个形状怪兽。

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

c++ opengl glut opengl-compat
2个回答
1
投票

如另一个答案中所述,可能是通过flat shading切换到glShadeModel模式。但是请注意,您还需要偏移外圆的顶点坐标:

glShadeModel

另一种可能性是在2个单独的循环中以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绘制内部和外部三角形:

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();


如果要获得更“圆形”的外观,则必须沿内圆或外圆细分这些细分。使用基本类型(请参见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(); }


0
投票

有比这更简单的解决方案。您可以使用阻止三角带在不同颜色之间进行插值。

© www.soinside.com 2019 - 2024. All rights reserved.