我正在学习OpenGl,我只是看到了如何编写着色器。但是第一个问题是,在本课程中,支持GLSL 3.30 (OpenGL 4),但是我的驱动程序(MESA)不允许我使用GLSL 1.30或ES 3.20的版本。因此,我更改了着色器中的#version
预处理器,然后继续。但是,不幸的是,当我尝试使用颜色时,它无效。
这是我的代码:
#include "colors.h"
#include <iostream>
#include <fstream>
#include <regex>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
unsigned int CompileShader(unsigned int type, std::string& source);
unsigned int CreateShader(std::string& vertexShader, std::string& fragmentShader);
int main(int argc, char* argv[]) {
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit()) {
std::cerr << "Couldn't initialize GLFW" << std::endl;
exit(1);
}
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", nullptr, nullptr);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
if(glewInit() != GLEW_OK) {
std::cerr << "Couldn't initialize GLEW" << std::endl;
exit(1);
}
float positions[6] = {
-0.5f, -0.5f,
0.0f, 0.5f,
0.5f, -0.5f
};
unsigned int buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), 0);
// Compiling Shader
std::string vertexShader = "#version 130n"
"n"
"in vec4 position;n"
"n"
"void main () {n"
" gl_Position = position;n"
"}";
std::string fragmentShader = "#version 130n"
"n"
"uniform vec4 color = vec4(1.0, 0.0, 0.0, 1.0);n"
"n"
"out vec4 outputF;n"
"n"
"void main () {n"
" outputF = color;n"
"}";
unsigned int shader = CreateShader(vertexShader, fragmentShader);
glUseProgram(shader);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
unsigned int CompileShader(unsigned int type, std::string& source) {
unsigned int id = glCreateShader(type);
const char* src = source.c_str();
glShaderSource(id, 1, &src, nullptr);
glCompileShader(id);
int result;
glGetShaderiv(id, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE) {
int length;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
char* message = (char*)alloca(length * sizeof(char));
glGetShaderInfoLog(id, length, &length, message);
std::cerr << "Failed to compile "
<< (type == GL_VERTEX_SHADER ? "vertex" : "fragment")
<< " shader. Error message:" << std::endl;
std::cerr << "t" << FG_ERROR << message << std::endl;
}
return id;
}
unsigned int CreateShader(std::string& vertexShader, std::string& fragmentShader) {
unsigned int program = glCreateProgram();
unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
glAttachShader(program, vs);
glAttachShader(program, vs);
glLinkProgram(program);
glValidateProgram(program);
glDeleteShader(vs);
glDeleteShader(fs);
return program;
}
我有一些时间来看看这个,发现您有一个小的复制糊错误:
unsigned int CreateShader(std::string& vertexShader, std::string& fragmentShader) {
unsigned int program = glCreateProgram();
unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
glAttachShader(program, vs);
glAttachShader(program, vs); //<--should be fs
您两次连接顶点着色器。这将设置一个OpenGL错误,实际上我发现它是使用我通常使用的OpenGL调试方法的方式。当我更深入地测试您的代码时,我将所有OpenGL调用用Define包围,如果OpenGL设置错误,则停止执行。我将其放在来源的顶部,并建议使用以下内容:
#include <cstdlib>
#include <iostream>
#include <iomanip>
void CheckOpenGLError(const char *stmt, const char *fname, int line);
#define GL_CHECK(stmt)
do
{
stmt;
CheckOpenGLError(#stmt, __FILE__, __LINE__);
} while (0)
void CheckOpenGLError(const char *stmt, const char *fname, int line)
{
GLenum err = glGetError();
if (err != GL_NO_ERROR)
{
std::cout << "OpenGL error 0x" << std::setfill('0') << std::setw(8) << std::hex << err << std::dec << ": " << gluErrorString(err) << ", at " << fname << ":" << line << " - for " << stmt << std::endl;
abort();
}
}
如果您希望以人类可读的形式打印出openGL错误,则需要在Glu库中链接到GluErrorString函数(linux上的glu32.lib或linux上的libglu.so),否则需要去查找错误代码。在这种情况下,我将您的电话包围着:
unsigned int CreateShader(std::string& vertexShader, std::string& fragmentShader) {
unsigned int program = glCreateProgram();
unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
GL_CHECK(glAttachShader(program, vs));
GL_CHECK(glAttachShader(program, vs)); //<--should be fs
当我运行代码时,它将此错误丢弃:
OpenGL error 0x00000502: invalid operation, at main.cpp:147 - for glAttachShader(program, vs)
也很少的音符,完成链接后,您可能应该分离着着色器:
https://gamedev.stackexchange.com/a/47912
unsigned int CreateShader(std::string& vertexShader, std::string& fragmentShader) {
unsigned int program = glCreateProgram();
unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glValidateProgram(program);
glDetachShader(program, vs);
glDetachShader(program, fs);
glDeleteShader(vs);
glDeleteShader(fs);
return program;
}