Opengl 3.3渲染没什么

时间:2013-06-28 17:34:40

标签: opengl-3

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

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

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

1 个答案:

答案 0 :(得分:0)

我发现问题,在构造函数中,当我调用de函数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);
}