【问题标题】:Opengl 3.3 rendering nothingOpengl 3.3 无渲染
【发布时间】:2013-06-28 17:34:39
【问题描述】:

我正在尝试创建自己的可以简化代码的库,因此我正在尝试使用我的库编写我们可以在网络上找到的教程,但我遇到了一些麻烦,我不知道为什么会这样什么都没有。 所以这是我的主要文件

#include "../../lib/OpenGLControl.h"
#include "../../lib/Object.h"
#include <iostream>
using namespace std;
using namespace sgl;

int main(){
    OpenGLControl sglControl;
    sglControl.initOpenGL("02 - My First Triangle",1024,768,3,3);
    GLuint VertexArrayID;
    glGenVertexArrays(1, &VertexArrayID);
    glBindVertexArray(VertexArrayID);
    //trconfigurações do triangulo
    vector<glm::vec3> vertices;

    vertices.push_back(glm::vec3(-1.0f,-1.0f,0.0f));
    vertices.push_back(glm::vec3(1.0f,-1.0f,0.0f));
    vertices.push_back(glm::vec3(0.0f,1.0f,0.0f));
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    Object triangle(vertices);

    do{
        glClear(GL_COLOR_BUFFER_BIT); 
        triangle.render(GL_TRIANGLES);
            glfwSwapBuffers();

    }
    while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
    glfwGetWindowParam( GLFW_OPENED ) );
    glDeleteVertexArrays(1, &VertexArrayID);
    glfwTerminate();
    return 0;

}

这是我的 Object 类函数。

#include "../lib/Object.h"

sgl::Object::Object(){
    this->hasColor = false;

}

sgl::Object::Object(std::vector<glm::vec3> vertices){
    for(int i = 0; i < vertices.size(); i++)
        this->vertices.push_back(vertices[i]);

    glGenBuffers(1, &vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, this->vertices.size()*sizeof(glm::vec3),&vertices[0], GL_STATIC_DRAW);
}

sgl::Object::~Object(){
        glDisableVertexAttribArray(0);
        glDisableVertexAttribArray(1);
        glDeleteBuffers(1,&(this->vertexBuffer));
        glDeleteBuffers(1,&(this->colorBuffer));
}



void sgl::Object::render(GLenum mode){
    // 1rst attribute buffer : vertices
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
    glVertexAttribPointer(
        0, // The attribute we want to configure
        3,                  // size
        GL_FLOAT,           // type
        GL_FALSE,           // normalized?
        0,                  // stride
        (void*)0            // array buffer offset
    );

    cout<<vertices.size()<<endl;
    glDrawArrays(mode, 0, vertices.size());
    glDisableVertexAttribArray(0);


}

void sgl::Object::setColor(std::vector<glm::vec3> color){
    for(int i = 0; i < color.size(); i++)
        this->color.push_back(color[i]);
    glGenBuffers(1, &(this->colorBuffer));
    glBindBuffer(GL_ARRAY_BUFFER, this->colorBuffer);
    glBufferData(GL_ARRAY_BUFFER, color.size()*sizeof(glm::vec3),&color[0], GL_STATIC_DRAW);
    this->hasColor = true;
}

void sgl::Object::setVertices(std::vector<glm::vec3> vertices){
    for(int i = 0; i < vertices.size(); i++)
        this->vertices.push_back(vertices[i]);
    glGenBuffers(1, &vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(glm::vec3),&vertices[0], GL_STATIC_DRAW);
}

我重写的教程是:

/*

    Copyright 2010 Etay Meiri

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    Tutorial 03 - First triangle
*/

#include <stdio.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include "math_3d.h"

GLuint VBO;

static void RenderSceneCB()
{
    glClear(GL_COLOR_BUFFER_BIT);

    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glDrawArrays(GL_TRIANGLES, 0, 3);

    glDisableVertexAttribArray(0);

    glutSwapBuffers();
}


static void InitializeGlutCallbacks()
{
    glutDisplayFunc(RenderSceneCB);
}

static void CreateVertexBuffer()
{
    Vector3f Vertices[3];
    Vertices[0] = Vector3f(-1.0f, -1.0f, 0.0f);
    Vertices[1] = Vector3f(1.0f, -1.0f, 0.0f);
    Vertices[2] = Vector3f(0.0f, 1.0f, 0.0f);

    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
}


int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
    glutInitWindowSize(1024, 768);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Tutorial 03");

    InitializeGlutCallbacks();

    // Must be done after glut is initialized!
    GLenum res = glewInit();
    if (res != GLEW_OK) {
      fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
      return 1;
    }

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    CreateVertexBuffer();

    glutMainLoop();

    return 0;
}

如果有人能找到错误,请帮助我!

【问题讨论】:

    标签: opengl-3


    【解决方案1】:

    我发现了问题,在构造函数中,当我调用函数 glbufferdata 时,我发送了错误的数据,正确的做法是:

    sgl::Object::Object(std::vector<glm::vec3> vertices){
        for(int i = 0; i < vertices.size(); i++)
            this->vertices.push_back(vertices[i]);
    
        glGenBuffers(1, &vertexBuffer);
        glBindBuffer(GL_ARRAY_BUFFER, this->vertexBuffer);
        glBufferData(GL_ARRAY_BUFFER, this->vertices.size()*sizeof(glm::vec3),&(this->vertices[0]), GL_STATIC_DRAW);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多