【问题标题】:OpenGL simple VBO troubleOpenGL简单VBO麻烦
【发布时间】:2014-02-15 05:03:53
【问题描述】:

我无法使用 VBO 进行绘图。

我成功地使用此代码使用顶点数组进行绘制,但是当我尝试将其弹出到 vbo 中并绘制它时,我只是得到一个空白的白色屏幕。

我尝试将 vbo 的初始化添加到 glutinit 函数中,但这会导致中断。 Iv 还尝试仅在单独的 func 中调用 VBO 初始化一次,以查看是否多次制作 VBO 会导致问题,并且 iv 还将我的项目剥离为仅渲染它以查看是否可行;但唉,我仍然在白屏上胜过。

  • 请注意,我已经读到这可能与我的卡驱动程序不支持 > OGL 2.0 有关(知道我的运气,我的代码没有问题,就是这种情况)

    float f[] = {0, 2, -4, -2, -2, -4, 2, -2, -4};
    GLuint vbo;
    
    void DisplayGL()  // the update display callback?
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear out the stuff from the old window.     
    
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    
    //translate and rotate SWAP X AND Y DERP
    glRotatef(camera->rot.x, 0.0f, 1.0f, 0.0f); // deg, xflag, yflag, zflag
    glRotatef(camera->rot.y, 1.0f, 0.0f, 0.0f); // deg, xflag, yflag, zflag
    glTranslatef(camera->pos.x, camera->pos.y, camera->pos.z); // move us back a bit
    
    glGenBuffers(1, &vbo); // make a buffer
    glBindBuffer(GL_ARRAY_BUFFER, vbo); // bind the buffer - type, buffer add
    glBufferData(GL_ARRAY_BUFFER, sizeof(f), f, GL_STATIC_DRAW); //type, size of the data, the data, flag (GL_A_B)
    
    glBindBuffer(GL_ARRAY_BUFFER, vbo); // rebind?
    
    glEnableClientState(GL_VERTEX_ARRAY); // enable the client state 
    
    glVertexPointer(3, GL_FLOAT, sizeof(float), 0); // how may elemets per vertex, data type, how many bytes between vertex, starting point of data
    
    glDrawArrays(GL_TRIANGLES, 0, 3); // draw type, starting point (vertex), how many vertex
    
    glDisableClientState(GL_VERTEX_ARRAY); // disable the client state
    
    // render some text
    std::string text = "Mouse x, y: " + std::to_string(testx) + ", " + std::to_string(testy);
    drawText(text.data(), text.length(), 0, 575);
    
    text = "Cam x, y rotation: " + std::to_string(camera->rot.x) + ", " + std::to_string(camera->rot.y);
    drawText(text.data(), text.length(), 0, 560);
    
    glutSwapBuffers(); // swap our active buffer to our unactive buffer
    

    }

-另一个快速问题,我使用 GLM(opengl 数学库)。我想知道是否可以使用 glm::vec3 的向量来代替数组或浮点向量。如果可以的话,是否需要告诉 openGL 使用 glm::vec3 数据类型中的数据有什么特别之处?我只问,因为它的数据在一个结构中,并且在 for 循环中像这样访问 (*i).x ... 等不能像标准数组或浮点向量一样访问:(*i)

编辑-

以下是该问题的主要内容:

#include <iostream>
#include <vector>

#include "..\..\externals\glm\glm\glm.hpp" // openGL Math header.
#include <gl/glew.h> // include glew depend's
#define FREEGLUT_STATIC     // defign the lib for glut
#include <gl/freeglut.h> // include glut

int g_iWindowWidth = 800; // our global window width and height
int g_iWindowHeight = 600;

int g_hWindowHandle = 0;  // out handler for our main window

void initGLUT(int argc, char* argv[]);  // the initalise func for glut

//call back funcs
void DisplayGL();  // the update display func callback's
void IdleGL(); // idle function

GLuint vbo;
std::vector< glm::vec3 > verts;

void makeVbo();

int main(int argc, char* argv[])
{
    verts.push_back(glm::vec3(0, 2, -4));
    verts.push_back(glm::vec3(-2, -2, -4));
    verts.push_back(glm::vec3(2, -2, -4));

    initGLUT(argc, argv); // initalise glut
    makeVbo(); // ***no matter where this is placed, i get a break or a white screen?***
    glutMainLoop(); // run our grafix
}

void initGLUT(int argc, char* argv[])
{
    glutInit(&argc, argv); // initalise the widow with out global params

    glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION); // when we close the window return to the main func or previous func

    int iScreenWidth = glutGet(GLUT_SCREEN_WIDTH); // our window W and H
    int iScreenHeight = glutGet(GLUT_SCREEN_HEIGHT);

    glutInitDisplayMode(GLUT_RGBA | GLUT_ALPHA | GLUT_DOUBLE | GLUT_DEPTH); // rgb with alpha and a depth to our view

    glutInitWindowPosition((iScreenWidth - g_iWindowWidth) / 2, (iScreenHeight - g_iWindowHeight) / 2); // set the window pos on screen
    glutInitWindowSize(g_iWindowWidth, g_iWindowHeight); // windwow size

    g_hWindowHandle = glutCreateWindow("My OpenGL Template!"); // title & our window_H

    glutDisplayFunc(DisplayGL); // bind callback functions
    glutIdleFunc(IdleGL);

    glEnable(GL_DEPTH_TEST);
}

void DisplayGL()  // the update display func callback?
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear out the stuff from the old window.

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    //translate

    glTranslatef(0, 0,-6); // move us back a bit

    glEnableClientState(GL_VERTEX_ARRAY);

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glVertexPointer(3, GL_FLOAT, 0, 0);

    glDrawArrays(GL_TRIANGLES, 0, 3);

    glDisableClientState(GL_VERTEX_ARRAY);

    glutSwapBuffers(); // swap our active buffer to our unactive buffer
}
void IdleGL() // logic here
{
    glutPostRedisplay(); // redrawwindow
}
void makeVbo()
{
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData
        (
        GL_ARRAY_BUFFER,
        sizeof(glm::vec3) * verts.size(),
        &verts[0],
        GL_STATIC_DRAW
        );
}

编辑 2- 一整天都在这个地方四处张望和摆弄。似乎很多 cmets 都说要调用 glewinit();在制作 vbo 之前确保 gl 调用指针是正确的。

void makeVbo()
{

    glewInit();
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData
        (
        GL_ARRAY_BUFFER,
        sizeof(glm::vec3) * verts.size(),
        &verts[0],
        GL_STATIC_DRAW
        );
}

如果它在我之前的编辑中显示的 main 中调用,现在可以工作,但是屏幕是一个没有绘制对象的空白黑色窗口?在尝试使用我的“相机”移动但在任何地方的视口中都看不到它?

编辑 3- 我注意到我们的代码之间唯一不同的地方是我们看待世界的方式。你正在使用glortho(...); 而我正在使用gltranslatef(...); 在我改变它之后,我让它在屏幕上呈现:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5, 5, -5, 5, -5, 5);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
render...

所以,我现在的问题是为什么它会在数组循环中渲染得很好,但在使用 glTranslatef(...); 的 VBO 中却不行?

编辑 4 - 最终解决方案 似乎我没有使用透视图来正确设置一个,经过一点挖掘和玩关于 iv 交换 glOrtho(...);gluPerspective(45, g_iWindowWidth / g_iWindowHeight, 1.0, 500.0); 然后在模型视图中执行我的相对翻译,简而言之,这给了我想要的东西。 - 看到我还有很多阅读要做:)

【问题讨论】:

    标签: c++ opengl glut vbo glu


    【解决方案1】:
    glVertexPointer(3, GL_FLOAT, sizeof(float), 0);
                                 ^^^^^^^^^^^^^
    

    您的顶点数据非常紧凑,所以stride 应该是0

    drawText() 可能正在破坏您的状态;没办法知道。

    类似这样的:


    #include <GL/glew.h>
    #include <GL/glut.h>
    #include <glm/glm.hpp>
    #include <vector>
    
    GLuint vbo = 0;
    void init()
    {
        std::vector< glm::vec3 > verts;
        verts.push_back( glm::vec3( 0, 2, -4 ) );
        verts.push_back( glm::vec3( -2, -2, -4 ) );
        verts.push_back( glm::vec3( 2, -2, -4 ) );
    
        glGenBuffers( 1, &vbo );
        glBindBuffer( GL_ARRAY_BUFFER, vbo );
        glBufferData
            ( 
            GL_ARRAY_BUFFER, 
            sizeof( glm::vec3 ) * verts.size(), 
            &verts[0], 
            GL_STATIC_DRAW 
            ); 
    }
    
    void display()
    {
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        glOrtho( -5, 5, -5, 5, -5, 5 );
    
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();
    
        glEnableClientState( GL_VERTEX_ARRAY );
    
        glBindBuffer( GL_ARRAY_BUFFER, vbo );
        glVertexPointer( 3, GL_FLOAT, 0, 0 );
    
        glDrawArrays( GL_TRIANGLES, 0, 3 );
    
        glDisableClientState( GL_VERTEX_ARRAY );
    
        glutSwapBuffers();
    }
    
    int main( int argc, char **argv )
    {
        glutInitWindowSize( 600, 600 );
        glutInit( &argc, argv );
        glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
        glutCreateWindow( "GLUT" );
        glewInit();
    
        glutDisplayFunc( display );
        init();
        glutMainLoop();
        return 0;
    }
    

    我想知道是否可以使用 glm::vec3 的向量来代替数组或浮点向量。

    是的。见例子。

    如果可以的话,有什么特别的事情需要告诉 openGL 让它使用 glm::vec3 数据类型中的数据吗?

    没有。

    【讨论】:

    • 您好,genpfault!谢谢你的第一关。您的代码确实可以完美地将我的 main.cpp 替换为您的。但是,当我将您的代码合并到我自己的代码中时,如果它弹出到显示函数中,我会返回相同的白屏,如果它弹出到主函数或初始化函数中,则会出现相同的中断错误。这是它在glGenBuffers(1, &amp;vbo); Unhandled exception at 0x753DC9F5 in OpenGL Template.exe: 0xC0000005: Access violation executing location 0x00000000. 上中断的行
    • 解决了这个问题,但这一切都会导致相同的白屏/中断。所以似乎还有其他问题。我将编辑我的第一篇文章并添加我正在使用的 main.cpp。如果你有时间,你可以试一试,看看你得到了什么?亲切的问候,马特 - 再次感谢!
    • p.s 感谢您清理 gln::vec3 问题,您基本上采用 glm::vec3 的大小并将其乘以向量的大小 - 数据的数据。 sizeof(glm::vec3) * verts.size()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-31
    • 2022-01-24
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    • 1970-01-01
    相关资源
    最近更新 更多