-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmindVisionOpenGL.c
172 lines (157 loc) · 5.51 KB
/
mindVisionOpenGL.c
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <stdio.h>
#include <time.h>
#include <CameraApi.h>
#define CAMERA_MAX_COUNT 6
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdlib.h>
#include <string.h>
struct timespec last;
BYTE *pbyBuffer;
tSdkFrameHead sFrameInfo;
GLuint width = 1280, height = 1024;
GLuint vertexShader, fragmentShader, shaderProgram;
GLuint VBO, VAO;
GLfloat vertices[] = {
// 顶点坐标 纹理坐标
-1.0f, -1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 0.0f};
int vertexAttribLocation, texCoordAttribLocation, textureUniformLocation;
const char *vertexShaderSource =
"#version 330 core\n"
"layout (location = 0) in vec2 aPos;\n"
"layout (location = 1) in vec2 aTexCoord;\n"
"out vec2 TexCoord;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);\n"
" TexCoord = aTexCoord;\n"
"}\0";
const char *fragmentShaderSource =
"#version 330 core\n"
"out vec4 FragColor;\n"
"in vec2 TexCoord;\n"
"uniform sampler2D texture1;\n"
"void main()\n"
"{\n"
" FragColor = texture(texture1, TexCoord);\n"
"}\n\0";
void initShaderProgram()
{
// 编译顶点着色器
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
// 编译片段着色器
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
// 创建着色器程序
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// 获取着色器中的属性和uniform位置
vertexAttribLocation = glGetAttribLocation(shaderProgram, "aPos");
texCoordAttribLocation = glGetAttribLocation(shaderProgram, "aTexCoord");
textureUniformLocation = glGetUniformLocation(shaderProgram, "texture1");
}
void initBuffers()
{
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// 顶点位置属性
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (void *)0);
glEnableVertexAttribArray(0);
// 纹理坐标属性
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (void *)(2 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void display(GLFWwindow *window, GLuint textureID)
{
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
glBindTexture(GL_TEXTURE_2D, textureID);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glBindVertexArray(0);
glfwSwapBuffers(window);
glfwPollEvents();
}
int main()
{
if (!glfwInit())
{
exit(EXIT_FAILURE);
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
GLFWwindow *window = glfwCreateWindow(width, height, "Captured", NULL, NULL);
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK)
{
exit(EXIT_FAILURE);
}
glfwSwapInterval(1);
CameraSdkInit(0);
int iCameraCounts = CAMERA_MAX_COUNT;
tSdkCameraDevInfo tCameraEnumList[CAMERA_MAX_COUNT];
CameraEnumerateDevice(&tCameraEnumList[0], &iCameraCounts);
printf("Camera count: %d\n", iCameraCounts);
for (int i = 0; i < iCameraCounts; i++)
{
printf("Camera %d: %s\n", i, tCameraEnumList[i].acFriendlyName);
}
CameraHandle hCamera;
CameraInit(&tCameraEnumList[0], -1, -1, &hCamera);
clock_gettime(CLOCK_MONOTONIC, &last);
tSdkCameraCapbility tCapability;
CameraGetCapability(hCamera, &tCapability);
size_t len = tCapability.sResolutionRange.iHeightMax * tCapability.sResolutionRange.iWidthMax * 3;
BYTE *imgBuffer = (BYTE *)malloc(len);
CameraSetAeState(hCamera, 0);
CameraPlay(hCamera);
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
initShaderProgram();
initBuffers();
while (!glfwWindowShouldClose(window))
{
if (CameraGetImageBuffer(hCamera, &sFrameInfo, &pbyBuffer, 1000) == CAMERA_STATUS_SUCCESS)
{
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
long elapsed = (now.tv_sec - last.tv_sec) * 1e9 + (now.tv_nsec - last.tv_nsec);
last = now;
printf("fps:%lf, received%d %d %d %d\n", 1e9 / elapsed, sFrameInfo.uBytes, sFrameInfo.uiMediaType, sFrameInfo.iWidth, sFrameInfo.iHeight);
CameraImageProcess(hCamera, pbyBuffer, imgBuffer, &sFrameInfo);
CameraReleaseImageBuffer(hCamera, pbyBuffer);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, imgBuffer);
display(window, textureID);
}
else
{
printf("error no image fetched\n");
break;
}
}
free(imgBuffer);
printf("ending\n");
CameraUnInit(hCamera);
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}