绘制4条线段
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h> #include "Shader.h"
#include <fstream>
#include <iostream>
using namespace std; void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window); // settings
const unsigned int SCR_WIDTH = ;
const unsigned int SCR_HEIGHT = ; // timing
float deltaTime = 0.0f;
float lastFrame = 0.0f; int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, );
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, );
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); #ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
#endif // glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
cout << "Failed to initialize GLEW!" << endl;
return -;
}
// configure global opengl state
// ----------------------------- Shader shader("E:\\C++\\High_level_GLSL\\2.1ver1.txt", "E:\\C++\\High_level_GLSL\\2.1frag1.txt",
"E:\\C++\\High_level_GLSL\\2.1geo1.txt"); float points[] = {
-0.5f, 0.5f,
0.5f, 0.5f,
0.5f, -0.5f,
-0.5f, -0.5f
}; /*float points[] = {
-0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 1.0f, 1.0f, 0.0f
};*/ unsigned int VAO, VBO;
glGenVertexArrays(, &VAO);
glGenBuffers(, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(points), &points, GL_STATIC_DRAW);
glEnableVertexAttribArray();
glVertexAttribPointer(, , GL_FLOAT, GL_FALSE, * sizeof(float), (void*));
//glEnableVertexAttribArray(1);
//glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(2 * sizeof(float)));
glBindVertexArray(); while (!glfwWindowShouldClose(window))
{
processInput(window); glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); shader.use();
glBindVertexArray(VAO);
glDrawArrays(GL_POINTS, , ); glfwSwapBuffers(window);
glfwPollEvents();
} glDeleteVertexArrays(, &VAO);
glDeleteBuffers(, &VBO); } void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ENTER) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
} // glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(, , width, height);
}
E:\\C++\\High_level_GLSL\\2.1ver1.txt
#version core
layout (location = ) in vec2 aPos; void main()
{
gl_Position = vec4(aPos, 0.0f, 1.0f);
}
E:\\C++\\High_level_GLSL\\2.1frag1.txt
#version core
out vec4 FragColor;
void main()
{
FragColor = vec4(0.0f, 1.0f, 0.0f, 1.0f);
}
E:\\C++\\High_level_GLSL\\2.1geo1.txt
#version core
layout (points) in;
layout (line_strip, max_vertices = ) out; void main()
{
gl_Position = gl_in[].gl_Position + vec4(-0.1f, 0.0f, 0.0f, 0.0f);
EmitVertex();
gl_Position = gl_in[].gl_Position + vec4(0.1f, 0.0f, 0.0f, 0.0f);
EmitVertex(); EndPrimitive();
}
Shader.h头文件,只需将链接里的#include <glad/glad.h>换成
#define GLEW_STATIC
#include <GL/glew.h>
就行