openGL使用GLFW、GLEW库绘制点
作者:互联网
前言
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();
//缓冲区最后都会被存入顶点数组对象VAO(Vertex Array Object)
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;
//释放GLFW所有资源,直到调用glfwInit才能重新获取GLFW资源
glfwTerminate();
exit(EXIT_FAILURE);
}
//用于设置指定窗口的键回调,在按下、重复或释放键时调用该窗口
glfwSetKeyCallback(window, key_callback);
//调用线程上指定窗口的OpenGL或OpenGL ES上下文成为当前上下文。上下文一次只能在单个线程上成为当前上下文,并且每个线程一次只能有一个当前上下文。
glfwMakeContextCurrent(window);
int glewState = glewInit();
if (GLEW_OK != glewState)
{
cout << "GLEW initialize failed! Invoke glfwInit()." << std::endl;
exit(EXIT_FAILURE);
}
//由于这些原因,应用程序通常希望将交换间隔设置为1。可以将其设置为更高的值,但通常不建议这样做,因为这样会导致输入延迟。
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
程序运行正确
小结
以后要把着色器写在单独的着色器文档中,这样编译直接回报错,不然真的很难定位问题
程序源码下载
标签:openGL,int,void,window,GLFW,GLuint,GLEW,着色器 来源: https://blog.csdn.net/aoxuestudy/article/details/121565070