天天看點

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;
}      

程式運作結果:

openGL使用GLFW、GLEW庫繪制點

問題定位

1.首先背景顔色是對的,但中間的點顔色不對,而且程式沒有報錯。可以确定GLFW、GLEW、openGL庫這些都 沒問題。

2.程式着色器貌似沒有問題

問題解決

openGL使用GLFW、GLEW庫繪制點

問題就在着色器程式上:

openGL使用GLFW、GLEW庫繪制點

把 #version core 430

改成:

#version 430 core

或者

#version 430

openGL使用GLFW、GLEW庫繪制點

程式運作正确

openGL使用GLFW、GLEW庫繪制點

小結

程式源碼下載下傳