-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelloShader.cpp
107 lines (83 loc) · 2.81 KB
/
HelloShader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*****************************************************************//**
* \file HelloShader.cpp
* \brief Introduction to OpenGL shader.
*
* \author Xuhua Huang
* \date October 23, 2022
*********************************************************************/
#define GLEW_STATIC
#include <GLFW\glfw3.h>
#include <GL\glew.h>
#include <iostream>
constexpr int numVAOs = 1;
GLuint renderingProgram;
GLuint vao[numVAOs];
GLuint createShaderProgram(void) {
/* Vertex Shader Source with GLSL syntax */
const char* vshaderSource = "#version 430 \n"
"void main(void) \n"
"{gl_Position = vec4(0.0, 0.0, 0.0, 1.0);}";
/* Fragement Shader Source with GLSL syntax */
const char* fshaderSource =
"#version 430 \n"
"out vec4 color; \n"
"void main(void) \n"
//"{color = vec4(0.0, 0.0, 1.0, 1.0);}";
"{if (gl_FragCoord.x < 200) color = vec4(1.0, 0.0, 0.0, 1.0); else color = vec4(0.0, 0.0, 1.0, 1.0);}";
/**
* 1. Create preliminary shaders.
* 2. Attach shader source from const char* variables.
* 3. Compile shders.
* \return
*/
GLuint vShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vShader, 1, &vshaderSource, NULL);
glShaderSource(fShader, 1, &fshaderSource, NULL);
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]);
return;
}
void display(GLFWwindow* window, double currentTime) {
glUseProgram(renderingProgram);
glPointSize(30.0f);
glDrawArrays(GL_POINTS, 0, 1);
return;
}
int main(void) {
// Check if the GLFW initialization is successful
if (!glfwInit()) {
exit(EXIT_FAILURE);
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// Create a new GLFWwindow pointer from the factory method
// glfwCreateWindow()
GLFWwindow* window = glfwCreateWindow(600, 600, "Hello Shader", NULL, NULL);
glfwMakeContextCurrent(window);
// Check if the GLEW initialization is successful
// unresolved external symbol __imp_glewInit referenced in function main
if (glewInit() != GLEW_OK) {
exit(EXIT_FAILURE);
}
glfwSwapInterval(1);
init(window);
while (!glfwWindowShouldClose(window)) {
display(window, glfwGetTime());
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}