无法在C++openGL中获取普通的2D着色器来绘制红色三角形(没有编译器错误)



我使用了openGL、GLEW和GLFW3。在openGl 3.0台面中,使用了默认着色器,并绘制了一个白色三角形。在4.2中,屏幕为黑色。未生成任何错误消息。

该问题并非由于将片段着色器错误标记为顶点着色器,反之亦然。

该程序包含3个功能:

(1( compileShader,它应该以std::string的形式接收着色器类型和源代码,并使用编译后的代码返回着色器ID。

(2( createProgram,接收顶点着色器和片段着色器的源代码,并返回已编译和附加着色器的程序ID。

(3( 和main,其中两个着色器源代码都定义为字符串。

对不起,谢谢。

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <string>
static unsigned int compileShader(unsigned int type, std::string const& sourceCode){
unsigned int shaderId = glCreateShader(type);
const char* source = sourceCode.c_str();
glShaderSource(shaderId, 1, &source, nullptr);
glCompileShader(shaderId);
int result;
glGetShaderiv(shaderId, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE){
if (type == GL_FRAGMENT_SHADER) std::cout<<"Fragmentn";
else std::cout<<"Vertex";
std::cout<<"SHADER COMPILATION FAILED!"<<std::endl;
int logLength;
glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &logLength);
char* infoLog = new char[logLength];
glGetShaderInfoLog(shaderId, logLength, NULL, infoLog);
std::cout<<infoLog<<std::endl;
delete[] infoLog;
glDeleteShader(shaderId);
return 0;
}
return shaderId;
}
static unsigned int createProgram(std::string const& vertexCode, std::string const& fragmentCode){
unsigned int vsId = compileShader(GL_VERTEX_SHADER, vertexCode);
unsigned int fsId = compileShader(GL_FRAGMENT_SHADER, fragmentCode);
unsigned int programId = glCreateProgram();
glAttachShader(programId, vsId);
glAttachShader(programId, fsId);
glLinkProgram(programId);
glValidateProgram(programId);
glDeleteShader(vsId);
glDeleteShader(fsId);
return programId;
}
int main()
{
GLFWwindow* window;
if (!glfwInit())
{
std::cout<<"GLFW initialization failed";
return -1;
}
///switches to opengl 4.2!
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
glewInit();
std::cout << "OpenGL " << glGetString(GL_VERSION) << "n" << std::endl;
float positions[6] = {
0.5f, 0.5f,
-0.5f, 0.5f,
0.0f, 0.0f
};
unsigned int buffer = 1;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);                                        //enables the xy attrib of position
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), 0); //specifies layout of xy attrib
std::string vertexSource =   R"(
#version 330 core
layout(location=0) in vec4 position;
void main(){
gl_Position = position;
}
)";
std::string fragmentSource = R"(
#version 330 core
layout(location=0) out vec4 color;
void main(){
color = vec4(1.0, 0.0, 0.0, 1.0);
}
)";
unsigned int programId = createProgram(vertexSource, fragmentSource);
glUseProgram(programId);
/* 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;
}

问题不在于着色器程序,而是使用了核心配置文件OpenGL上下文(GLFW_OPENGL_CORE_PROFILE(。因此,您必须创建一个命名的"顶点阵列对象"。默认VAO(0(在核心配置文件上下文中无效。

通过glGenVertexArrays生成顶点数组对象名称,通过glBindVertexArray:创建并绑定对象

unsigned int vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
unsigned int buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);                                        //enables the xy attrib of position
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), 0); //specifies layout of xy attrib

最新更新