前言
openGL使用GLFW、GLEW库绘制点,下面这段代码:绘制蓝色背景,中心点是一个黄色的点,由于代码比较简单,所以我把顶点着色器和片元着色器硬编码到c++程序中。问题就在这!!!由于着色器都写在字符串中,造成着色器的错误很难排查。
#include "glew/glew.h"
#include "glfw/glfw3.h"
#include <iostream>
using namespace std;
static const unsigned int scr_width = 800;
static const unsigned int scr_height = 600;
static const int numVAOs = 1;
GLuint renderingProgram;
GLuint vao[numVAOs];
static void error_callback(int error, const char* description)
{
fprintf_s(stderr, "Error:%s\n", description);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
}
GLuint createShaderProgram()
{
const char* vShaderSource =
"#version core 430 \n"
"void main(void) \n"
"{ \n"
" gl_Position = vec4(0.f, 0.f, 0.f, 1.f); \n"
"} \n";
const char* fShaderSource =
"#version core 430 \n"
"out vec4 fragColor; \n"
"void main(void) \n"
"{ \n"
" fragColor = vec4(1.0f, 1.0f, 0.0f, 1.0f); \n"
"} \n";
GLuint vShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vShader, 1, &vShaderSource, nullptr);
glShaderSource(fShader, 1, &fShaderSource, nullptr);
glCompileShader(vShader);
glCompileShader(fShader);
GLuint vfProgram = glCreateProgram();
glAttachShader(vfProgram, vShader);
glAttachShader(vfProgram, fShader);
glLinkProgram(vfProgram);
return vfProgram;
}
void init(GLFWwindow* window)
{
renderingProgram = createShaderProgram();
glGenVertexArrays(numVAOs, vao);
glBindVertexArray(vao[0]);
}
void display(GLFWwindow* window, double currentTime)
{
glClearColor(0.f, 0.f, 1.f, 1.f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(renderingProgram);
glPointSize(30.0f);
glDrawArrays(GL_POINTS, 0, 1);
}
int main(int argc, char** argv)
{
int glfwState = glfwInit();
if (GLFW_FALSE == glfwState)
{
cout << "GLFW initialize failed. Invoke glfwInit()." << std::endl;
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSetErrorCallback(error_callback);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(scr_width, scr_height, "Draw point", nullptr, nullptr);
if (nullptr == window)
{
cout << "GLFW create window failed! Invoke glfwCreateWindow()." << std::endl;
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSetKeyCallback(window, key_callback);
glfwMakeContextCurrent(window);
int glewState = glewInit();
if (GLEW_OK != glewState)
{
cout << "GLEW initialize failed! Invoke glfwInit()." << std::endl;
exit(EXIT_FAILURE);
}
glfwSwapInterval(1);
init(window);
while (!glfwWindowShouldClose(window))
{
display(window, glfwGetTime());
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
return 0;
}
程序运行结果:
问题定位
1.首先背景颜色是对的,但中间的点颜色不对,而且程序没有报错。可以确定GLFW、GLEW、openGL库这些都 没问题。 2.程序着色器貌似没有问题
问题解决
问题就在着色器程序上: 把 #version core 430 改成: #version 430 core 或者 #version 430
程序运行正确
小结
以后要把着色器写在单独的着色器文档中,这样编译直接回报错,不然真的很难定位问题
程序源码下载
程序下载
|