0
#include<GLTools.h>
#include<GLShaderManager.h>

#include<GL/glut.h>

int g_width = 800;
int g_height = 600;

int nStep = 200;
const GLfloat a = 0.6f, b = 0.3f;
const GLfloat Pi = 3.1415926536f;
GLShaderManager shaderManager;
GLBatch pointBatch;
GLBatch lineBatch;
GLBatch lineStripBatch;
GLBatch lineLoopBatch;
GLBatch triangleBatch;
GLBatch triangleStripBatch;
GLBatch triangleFanBatch;

void initialize();
void uninitialize();
void display_func(void);
void reshape_func(int w, int h);
void key_func(unsigned char key, int x, int y);
void idle_func();
void initialize() 
{
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glClearDepth(1.0);

    shaderManager.InitializeStockShaders();

    GLfloat vCoast[24][3] = { {2.80,1.20,0.0},{2.0,1.20,0.0},
    {2.0,1.08,0.0},{2.0,1.08,0.0},
    {0.0,0.80,0.0},{-.40,0.40,0.0},
    {-.48,0.2,0.0},{-.40,0.0,0.0},
    {-.60,-.40,0.0},{-.80,-.80,0.0},
    {-.80,-1.4,0.0},{-.40,-1.60,0.0},
    {0.0,-1.20,0.0},{.2,-.80,0.0},
    {.48,-.40,0.0},{.2,-.80,0.0},
    {.48,.20,0.0},{.80,.40,0.0},
    {1.20,.80,0.0},{1.60,.60,0.0},
    {2.0,.60,0.0},{2.2,.80,0.0},
    {2.40,1.0,0.0},{2.80,1.0,0.0}
    };
    for (int i = 0; i < 24; i++)
    {
        vCoast[i][0] *= 0.3;
        vCoast[i][1] *= 0.3;
        vCoast[i][2] *= 0.3;
    }
    //Load point batch
    pointBatch.Begin(GL_POINT, 24);
    pointBatch.CopyVertexData3f(vCoast);
    pointBatch.End();

    //Load as a bunch of line segments
    lineBatch.Begin(GL_LINES, 24);
    lineBatch.CopyVertexData3f(vCoast);
    lineBatch.End();

    //Load as a single line segment
    lineStripBatch.Begin(GL_LINE_STRIP, 24);
    lineStripBatch.CopyVertexData3f(vCoast);
    lineStripBatch.End();

    //Single line,connect first and last points
    lineLoopBatch.Begin(GL_LINE_LOOP, 24);
    lineLoopBatch.CopyVertexData3f(vCoast);
    lineLoopBatch.End();


    GLfloat vPyramid[16][3] = {0};
    float angle = 0;
    int i = 0;
    for (; angle < (2.0f*Pi)&&i<16; angle += (Pi / 8.0f),i++)
    {
        int x = 50.0f*sin(angle);
        int y = 50.f*cos(angle);
        vPyramid[i][1] = x;
        vPyramid[i][2] = y;
        vPyramid[i][3] = 0.0f;
    }
    //Load as a triangle fan
    triangleFanBatch.Begin(GL_TRIANGLE_FAN,16);
    triangleFanBatch.CopyVertexData3f(vPyramid);
    triangleFanBatch.End();
}
void display_func(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutSwapBuffers();
}
void idle_func()
{
    glutPostRedisplay();
}
void reshape_func(int w, int h)
{
    glViewport(0, 0, w, h);
}
void key_func(unsigned char key, int x, int y)
{
    switch (key) {
    case 49:
    case 50:
        glLineWidth(2.0f);
        lineStripBatch.Draw();
        glLineWidth(1.0f);
        break;
    case 51:
        glLineWidth(2.0f);
        lineLoopBatch.Draw();
        glLineWidth(1.0f);
        break;
    case 52:
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
        triangleBatch.Draw();
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
        break;
    case 53:
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
        triangleFanBatch.Draw();
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
        break;
    case 54:
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
        triangleStripBatch.Draw();
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
        break;
    }
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitWindowSize(g_width, g_height);
    glutInitDisplayMode(GLUT_DEPTH || GLUT_DOUBLE || GLUT_RGB);
    glutCreateWindow("MyShapes");
    initialize();
    GLenum err = glewInit();
    if (GLEW_OK != err) {
        fprintf(stderr, "GLEW Error: $s\n", glewGetErrorString(err));
        return 1;
    }
    glutDisplayFunc(display_func);
    glutReshapeFunc(reshape_func);
    glutKeyboardFunc(key_func);
    glutIdleFunc(idle_func);
    glutMainLoop();
    return 0;
}
2017-03-18 提问
1 个回答
0

for (; angle < (2.0f*Pi)&&i<16; angle += (Pi / 8.0f),i++)

{
    int x = 50.0f*sin(angle);
    int y = 50.f*cos(angle);
    vPyramid[i][0] = x;
    vPyramid[i][1] = y;
    vPyramid[i][2] = 0.0f;
}

楼主数组使用不当啊

撰写答案

推广链接