如何在 C++ Opengl 中绘制索引
How to draw indices in c++ Opengl
大家好,我开始将 opengl 与 C++ 一起使用,我发现这些函数与 java lwjgl.So 中的函数不一样,让我直说吧。
我想绘制指数 我看了一个关于绘制 ibo 的教程,但它不会 work.I 只得到黑屏。
Window.cpp
#include "garbagecollector.h"
#include "window.h"
Window::Window(int width, int height, const std::string& title)
{
if(!glfwInit())
{
GarbageCollector::collectGarbage();
exit(EXIT_FAILURE);
}
/*glfwWindowHint(GLFW_SAMPLES, 32);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL
*/
window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL);
if(!window){
std::cerr << "Cannot create window!" << std::endl;
GarbageCollector::collectGarbage();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
if(glewInit() != GLEW_OK)
{
std::cerr << "Failed to init GLEW!" << std::endl;
GarbageCollector::collectGarbage();
exit(EXIT_FAILURE);
}
}
void Window::clear(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void Window::display(){
glfwSwapBuffers(window);
glfwPollEvents();
}
Window::~Window()
{
std::cout << "window" << std::endl;
glfwTerminate();
}
Mesh.h
#ifndef MESH_H
#define MESH_H
#include "object.h"
#include <GL/glew.h>
#include <vector>
struct Vertex{
float position[3];
};
class Mesh : public Object
{
public:
Mesh();
Mesh(Vertex vertecies[], int indices[]){
glGenBuffers(1, &vbo);
glGenBuffers(2, &ibo);
std::vector<unsigned int> indic;
indic.push_back(0);
indic.push_back(1);
indic.push_back(2);
size = indic.size();
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * 4, &vertecies, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(Vertex) * 3,0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, &indic[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void bind(){
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
}
void unbind(){
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void drawWithBindings(){
bind();
glDrawElements(GL_TRIANGLES, size, GL_UNSIGNED_INT, 0);
unbind();
}
virtual ~Mesh();
protected:
private:
GLuint vbo, ibo;
unsigned int size;
};
#endif // MESH_H
Mesh.cpp
#include "mesh.h"
Mesh::Mesh()
{
//ctor
}
Mesh::~Mesh()
{
std::cout << "mesh" << std::endl;
}
renderingEngine.h
#ifndef RENDERINGENGINE_H
#define RENDERINGENGINE_H
#include "game.h"
#include "mesh.h"
#include "shader.h"
class RenderingEngine : public Object
{
public:
RenderingEngine();
void render();
virtual ~RenderingEngine();
protected:
private:
Mesh* mesh;
Shader* shader;
Vertex vertex[3];
};
#endif // RENDERINGENGINE_H
renderingEngine.cpp
#include "renderingengine.h"
RenderingEngine::RenderingEngine()
{
vertex[0].position[0] = -1;
vertex[0].position[1] = 0;
vertex[0].position[2] = 0;
vertex[1].position[0] = 1;
vertex[1].position[1] = -1;
vertex[1].position[2] = 0;
vertex[2].position[0] = 1;
vertex[2].position[1] = 1;
vertex[2].position[2] = 0;
int indices[3] = {0, 1, 2};
shader = new Shader("vertex.vs", "fragment.fs");
mesh = new Mesh(vertex, indices);
}
RenderingEngine::~RenderingEngine()
{
std::cout << "renderingEngine" << std::endl;
}
void RenderingEngine::render(){
Game::getWindow().clear();
shader->bind();
mesh->drawWithBindings();
Game::getWindow().display();
}
Shader.h
#ifndef SHADER_H
#define SHADER_H
#include "object.h"
#include <GL/glew.h>
#include <cstdlib>
#include <fstream>
class Shader : public Object
{
public:
Shader();
Shader(const std::string& vertexShader, const std::string& fragmentShader){
program = glCreateProgram();
if(program == 0){
std::cerr << "Cannot create program" << std::endl;
exit(EXIT_FAILURE);
}
glBindAttribLocation(program, 0, "pos");
addVertexShader(vertexShader);
addFragmentShader(fragmentShader);
compileProgram();
}
void addVertexShader(const std::string& filename){
addShader(loadShader(filename), GL_VERTEX_SHADER);
}
void addFragmentShader(const std::string& filename){
addShader(loadShader(filename), GL_FRAGMENT_SHADER);
}
void compileProgram(){
glLinkProgram(program);
GLint errorprogram = 0;
char array[2048];
int i = 0;
glGetProgramiv(program, GL_LINK_STATUS, &errorprogram);
if(errorprogram == 0){
glGetProgramInfoLog(program, 2048, &i, array);
std::cout << array << std::endl;
}
glValidateProgram(program);
glGetProgramiv(program, GL_VALIDATE_STATUS, &errorprogram);
if(errorprogram == 0){
glGetProgramInfoLog(program, 2048, &i, array);
std::cout << array << std::endl;
}
}
void bind(){
glUseProgram(program);
}
virtual ~Shader();
protected:
private:
void addShader(const std::string& source, GLuint type){
GLint shader = glCreateShader(type);
if(shader == 0){
std::cerr << "Cannot shader program" << std::endl;
exit(EXIT_FAILURE);
}
const GLchar* h[1];
h[0] = source.c_str();
const GLint s[1] = {(GLint)source.size()};
glShaderSource(shader, 1, h, s);
glCompileShader(shader);
GLint errorShader = 0;
char array[2048];
int i = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &errorShader);
if(errorShader == 0){
glGetShaderInfoLog(shader, 2048, &i, array);
std::cout << array << std::endl;
}
glAttachShader(program, shader);
}
std::string loadShader(const std::string& filename) const{
std::string source = "";
std::ifstream shader(filename);
if(shader.is_open()){
std::string line;
while(!shader.eof()){
while(getline(shader, line)){
source += line;
source += '\n';
}
}
}
return source;
}
GLuint program = 0;
};
#endif // SHADER_H
Shader.cpp
#include "shader.h"
Shader::Shader()
{
//ctor
}
Shader::~Shader()
{
std::cout << "shader" << std::endl;
}
不确定我是否发现了所有问题,但这里有一些跳出的问题:
glGenBuffers()
的参数错误:
glGenBuffers(1, &vbo);
glGenBuffers(2, &ibo);
第二次调用,你也想只生成一个缓冲区名,所以应该是:
glGenBuffers(1, &vbo);
glGenBuffers(1, &ibo);
顶点缓冲区大小错误:
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * 4, &vertecies, GL_STATIC_DRAW);
在发布的示例中,您只有 3 个顶点。所以这应该是:
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * 3, &vertecies, GL_STATIC_DRAW);
实际上,您可能希望将顶点数作为参数传递,或者传递 vector
而不是数组。
错误的步幅值:
glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(Vertex) * 3,0);
第5个参数是步幅,它是顶点之间的偏移量,以字节为单位。在这种情况下,这是一个顶点的大小:
glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(Vertex), 0);
索引缓冲区大小错误:
size = indic.size();
...
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, &indic[0], GL_STATIC_DRAW);
size
变量包含索引的数量。 glBufferData()
的参数是以字节为单位的大小:
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size * sizeof(GLuint), &indic[0], GL_STATIC_DRAW);
大家好,我开始将 opengl 与 C++ 一起使用,我发现这些函数与 java lwjgl.So 中的函数不一样,让我直说吧。 我想绘制指数 我看了一个关于绘制 ibo 的教程,但它不会 work.I 只得到黑屏。
Window.cpp
#include "garbagecollector.h"
#include "window.h"
Window::Window(int width, int height, const std::string& title)
{
if(!glfwInit())
{
GarbageCollector::collectGarbage();
exit(EXIT_FAILURE);
}
/*glfwWindowHint(GLFW_SAMPLES, 32);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL
*/
window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL);
if(!window){
std::cerr << "Cannot create window!" << std::endl;
GarbageCollector::collectGarbage();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
if(glewInit() != GLEW_OK)
{
std::cerr << "Failed to init GLEW!" << std::endl;
GarbageCollector::collectGarbage();
exit(EXIT_FAILURE);
}
}
void Window::clear(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void Window::display(){
glfwSwapBuffers(window);
glfwPollEvents();
}
Window::~Window()
{
std::cout << "window" << std::endl;
glfwTerminate();
}
Mesh.h
#ifndef MESH_H
#define MESH_H
#include "object.h"
#include <GL/glew.h>
#include <vector>
struct Vertex{
float position[3];
};
class Mesh : public Object
{
public:
Mesh();
Mesh(Vertex vertecies[], int indices[]){
glGenBuffers(1, &vbo);
glGenBuffers(2, &ibo);
std::vector<unsigned int> indic;
indic.push_back(0);
indic.push_back(1);
indic.push_back(2);
size = indic.size();
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * 4, &vertecies, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(Vertex) * 3,0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, &indic[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void bind(){
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
}
void unbind(){
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void drawWithBindings(){
bind();
glDrawElements(GL_TRIANGLES, size, GL_UNSIGNED_INT, 0);
unbind();
}
virtual ~Mesh();
protected:
private:
GLuint vbo, ibo;
unsigned int size;
};
#endif // MESH_H
Mesh.cpp
#include "mesh.h"
Mesh::Mesh()
{
//ctor
}
Mesh::~Mesh()
{
std::cout << "mesh" << std::endl;
}
renderingEngine.h
#ifndef RENDERINGENGINE_H
#define RENDERINGENGINE_H
#include "game.h"
#include "mesh.h"
#include "shader.h"
class RenderingEngine : public Object
{
public:
RenderingEngine();
void render();
virtual ~RenderingEngine();
protected:
private:
Mesh* mesh;
Shader* shader;
Vertex vertex[3];
};
#endif // RENDERINGENGINE_H
renderingEngine.cpp
#include "renderingengine.h"
RenderingEngine::RenderingEngine()
{
vertex[0].position[0] = -1;
vertex[0].position[1] = 0;
vertex[0].position[2] = 0;
vertex[1].position[0] = 1;
vertex[1].position[1] = -1;
vertex[1].position[2] = 0;
vertex[2].position[0] = 1;
vertex[2].position[1] = 1;
vertex[2].position[2] = 0;
int indices[3] = {0, 1, 2};
shader = new Shader("vertex.vs", "fragment.fs");
mesh = new Mesh(vertex, indices);
}
RenderingEngine::~RenderingEngine()
{
std::cout << "renderingEngine" << std::endl;
}
void RenderingEngine::render(){
Game::getWindow().clear();
shader->bind();
mesh->drawWithBindings();
Game::getWindow().display();
}
Shader.h
#ifndef SHADER_H
#define SHADER_H
#include "object.h"
#include <GL/glew.h>
#include <cstdlib>
#include <fstream>
class Shader : public Object
{
public:
Shader();
Shader(const std::string& vertexShader, const std::string& fragmentShader){
program = glCreateProgram();
if(program == 0){
std::cerr << "Cannot create program" << std::endl;
exit(EXIT_FAILURE);
}
glBindAttribLocation(program, 0, "pos");
addVertexShader(vertexShader);
addFragmentShader(fragmentShader);
compileProgram();
}
void addVertexShader(const std::string& filename){
addShader(loadShader(filename), GL_VERTEX_SHADER);
}
void addFragmentShader(const std::string& filename){
addShader(loadShader(filename), GL_FRAGMENT_SHADER);
}
void compileProgram(){
glLinkProgram(program);
GLint errorprogram = 0;
char array[2048];
int i = 0;
glGetProgramiv(program, GL_LINK_STATUS, &errorprogram);
if(errorprogram == 0){
glGetProgramInfoLog(program, 2048, &i, array);
std::cout << array << std::endl;
}
glValidateProgram(program);
glGetProgramiv(program, GL_VALIDATE_STATUS, &errorprogram);
if(errorprogram == 0){
glGetProgramInfoLog(program, 2048, &i, array);
std::cout << array << std::endl;
}
}
void bind(){
glUseProgram(program);
}
virtual ~Shader();
protected:
private:
void addShader(const std::string& source, GLuint type){
GLint shader = glCreateShader(type);
if(shader == 0){
std::cerr << "Cannot shader program" << std::endl;
exit(EXIT_FAILURE);
}
const GLchar* h[1];
h[0] = source.c_str();
const GLint s[1] = {(GLint)source.size()};
glShaderSource(shader, 1, h, s);
glCompileShader(shader);
GLint errorShader = 0;
char array[2048];
int i = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &errorShader);
if(errorShader == 0){
glGetShaderInfoLog(shader, 2048, &i, array);
std::cout << array << std::endl;
}
glAttachShader(program, shader);
}
std::string loadShader(const std::string& filename) const{
std::string source = "";
std::ifstream shader(filename);
if(shader.is_open()){
std::string line;
while(!shader.eof()){
while(getline(shader, line)){
source += line;
source += '\n';
}
}
}
return source;
}
GLuint program = 0;
};
#endif // SHADER_H
Shader.cpp
#include "shader.h"
Shader::Shader()
{
//ctor
}
Shader::~Shader()
{
std::cout << "shader" << std::endl;
}
不确定我是否发现了所有问题,但这里有一些跳出的问题:
glGenBuffers()
的参数错误:glGenBuffers(1, &vbo); glGenBuffers(2, &ibo);
第二次调用,你也想只生成一个缓冲区名,所以应该是:
glGenBuffers(1, &vbo); glGenBuffers(1, &ibo);
顶点缓冲区大小错误:
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * 4, &vertecies, GL_STATIC_DRAW);
在发布的示例中,您只有 3 个顶点。所以这应该是:
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * 3, &vertecies, GL_STATIC_DRAW);
实际上,您可能希望将顶点数作为参数传递,或者传递
vector
而不是数组。错误的步幅值:
glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(Vertex) * 3,0);
第5个参数是步幅,它是顶点之间的偏移量,以字节为单位。在这种情况下,这是一个顶点的大小:
glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(Vertex), 0);
索引缓冲区大小错误:
size = indic.size(); ... glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, &indic[0], GL_STATIC_DRAW);
size
变量包含索引的数量。glBufferData()
的参数是以字节为单位的大小:glBufferData(GL_ELEMENT_ARRAY_BUFFER, size * sizeof(GLuint), &indic[0], GL_STATIC_DRAW);