OpenGL报错pixel format with necessary capabilities not found

发布于 2022-09-04 17:50:40 字数 3914 浏览 21 评论 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

×纯※雪 2022-09-11 17:50:40

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;
}

楼主数组使用不当啊

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文