首先,我是开放式的初学者,我正在从learnopengl.com学习opengl。所以我理解主要方法中的代码中发生了什么。代码在main方法中工作。但是当我试图为它创建一个类时,它不会呈现任何东西!我完全不知道什么是错的!
Sprite.h
#pragma once
#include"Shader.h"
#include<GL\glew.h>
class Sprite {
GLuint vao;
GLuint vbo;
int vertexCount;
Shader shader;
public:
Sprite(const GLfloat vertices[9], int noOfVertices, Shader s = Shader()) : vertexCount(noOfVertices) , shader(s)
{
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, this->vertexCount, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void draw() {
glBindVertexArray(this->vao);
glDrawArrays(GL_TRIANGLES, 0, this->vertexCount);
glBindVertexArray(0);
}
};
Shader.h
#pragma once
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>
#include<GL\glew.h>
class Shader {
GLuint programID;
GLuint vertID;
GLuint fragID;
public:
Shader(std::string vertex = "Shaders/Dvert.vert", std::string fragment = "Shaders/Dfrag.frag")
{
this->programID = glCreateProgram();
this->vertID = glCreateShader(GL_VERTEX_SHADER);
this->fragID = glCreateShader(GL_FRAGMENT_SHADER);
this->CompileShaders(vertex, fragment);
this->LinkProgram();
}
GLuint getProgramId() {
return this->programID;
}
private:
void CompileShaders(std::string vertexShaderPath, std::string FragShaderPath) {
std::string vCode = GetContentsFrom(vertexShaderPath).c_str();
const char * vertexCode = vCode.c_str();
glShaderSource(this->vertID, 1, &vertexCode, NULL);
glCompileShader(this->vertID);
this->HandleShaderErrors(this->vertID);
std::string fCode = GetContentsFrom(FragShaderPath).c_str();
const char * fragCode = fCode.c_str();
glShaderSource(this->fragID, 1, &fragCode, NULL);
glCompileShader(this->fragID);
std::cout << fragCode << std::endl;
this->HandleShaderErrors(this->fragID);
}
void LinkProgram() {
glAttachShader(this->programID, this->vertID);
glAttachShader(this->programID, this->fragID);
glLinkProgram(this->programID);
this->HandleLinkErrors();
}
std::string GetContentsFrom(std::string path) {
std::ifstream file(path);
std::ostringstream stream;
if (!file) {
std::cout << "Specified file path is invalid" << std::endl;
return "";
}
stream << file.rdbuf();
std::cout << stream.str() << std::endl << std::endl;
return stream.str();
}
void HandleLinkErrors() {
int result;
glGetProgramiv(this->programID, GL_LINK_STATUS, &result);
if (result == GL_FALSE) {
int length;
glGetProgramiv(this->programID, GL_INFO_LOG_LENGTH, &length);
std::vector<char> error(length);
glGetProgramInfoLog(this->programID, length , &length, &error[0]);
std::cout << &error[0] << std::endl;
glDetachShader(this->programID, this->vertID);
glDetachShader(this->programID, this->fragID);
}
}
void HandleShaderErrors(GLuint shader) {
int status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE) {
int length;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
std::vector<char> error(length);
glGetShaderInfoLog(shader, length, &length, &error[0]);
std::cout << &error[0] << std::endl;
glDeleteShader(shader);
}
}
};
Main.cpp的
#include<GL\glew.h>
#include<GLFW\glfw3.h>
#include"Console.h"
#include"Headers\Shader.h"
#include"Headers\Sprite.h"
#include<iostream>
static const GLfloat vertices[] = {
0.5f, -0.5f ,0.0f ,
-0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
int main() {
if (!glfwInit()) {
Console.Log("GLFW could not be initialised!!");
Console.Wait();
}
GLFWwindow *window = glfwCreateWindow(600, 400, "Open GL", nullptr, nullptr);
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK) {
Console.Log("GLEW could not be initialised!!");
Console.Wait();
}
Sprite s(vertices, 3);
glClearColor(0, 0, 0, 1);
glfwWindowHint(GLFW_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_VERSION_MINOR, 3);
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glfwPollEvents();
s.draw();
glfwSwapBuffers(window);
}
}
请帮帮我!!
注意: - main方法中的代码渲染没有的白色三角形 着色器,所以我的显卡没有着色器。
我创建的Console类也只是打印到控制台而与opengl没什么关系。 Console.Log isstd :: cout&lt;&lt;和Console.Wait是std :: cin.get();
答案 0 :(得分:0)
至少有两个问题。第一个是Sprite中的sizeof(vertices)
产生错误的结果,因为它给出了指针的大小,而不是数组的大小。要解决此问题,请将其替换为sizeof(GLfloat) * 9
。
第二个问题是,在创建窗口和上下文之后,您太晚指定了OpenGL上下文版本。要解决此问题,请在glfwCreateWindow
之前调用这些方法,而不是在之后:
glfwWindowHint(GLFW_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_VERSION_MINOR, 3);