-
-
Save ACUVE/c405033d0e81b9958a8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define GL_GLEXT_PROTOTYPES | |
#include <chrono> | |
#include <iostream> | |
#include <GL/gl.h> | |
#include <GL/glext.h> | |
#include <GL/glu.h> | |
#include <GLFW/glfw3.h> | |
#include <opencv2/core/core.hpp> | |
#include <opencv2/imgproc/imgproc.hpp> | |
#include <opencv2/highgui/highgui.hpp> | |
#include <opencv2/features2d/features2d.hpp> | |
constexpr auto *const vs = R"|||( | |
void main( void ) | |
{ | |
// gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; | |
gl_Position = ftransform(); | |
gl_FrontColor = gl_Color; | |
} | |
)|||"; | |
constexpr auto *const fs = R"|||( | |
void main( void ) | |
{ | |
gl_FragColor = gl_Color; | |
} | |
)|||"; | |
constexpr GLfloat lightpos[] = { 0.0, 0.0, 5.0, 1.0 }; | |
constexpr GLfloat lightcol[] = { 1.0, 1.0, 1.0, 1.0 }; | |
constexpr GLfloat lightamb[] = { 0.1, 0.1, 0.1, 1.0 }; | |
static | |
bool init( void ) | |
{ | |
auto const ver = glGetString( GL_VERSION ); | |
std::cout << ver << std::endl; | |
auto const vsid = glCreateShader( GL_VERTEX_SHADER ); | |
glShaderSource( vsid, 1, &vs, nullptr ); | |
glCompileShader( vsid ); | |
auto const fsid = glCreateShader( GL_FRAGMENT_SHADER ); | |
glShaderSource( fsid, 1, &fs, nullptr ); | |
glCompileShader( fsid ); | |
auto pid = glCreateProgram(); | |
glAttachShader( pid, vsid ); | |
glAttachShader( pid, fsid ); | |
glDeleteShader( vsid ); | |
glDeleteShader( fsid ); | |
GLint linked; | |
glLinkProgram( pid ); | |
glGetProgramiv( pid, GL_LINK_STATUS, &linked ); | |
if( linked == GL_FALSE ) | |
{ | |
std::cout << "test" << std::endl; | |
return false; | |
} | |
glUseProgram( pid ); | |
glEnable( GL_DEPTH_TEST ); | |
glDisable( GL_CULL_FACE ); | |
glEnable( GL_LIGHTING ); | |
glEnable( GL_LIGHT0 ); | |
glLightfv( GL_LIGHT0, GL_DIFFUSE, lightcol ); | |
glLightfv( GL_LIGHT0, GL_SPECULAR, lightcol ); | |
glLightfv( GL_LIGHT0, GL_AMBIENT, lightamb ); | |
glClearColor( 0.3, 0.3, 1.0, 0.0 ); | |
return true; | |
} | |
static | |
void draw( cv::Mat frame ) | |
{ | |
glMatrixMode( GL_PROJECTION ); | |
glLoadIdentity(); | |
gluPerspective( 60.0, 640.0 / 480.0, 1.0 , 100.0 ); | |
glMatrixMode( GL_MODELVIEW ); | |
glLoadIdentity(); | |
gluLookAt( 0, 0, 1, 0, 0, 0, 0, 1, 0 ); | |
constexpr GLfloat diffuse[] = { 0.6, 0.1, 0.1, 1.0 }; | |
constexpr GLfloat specular[] = { 0.3, 0.3, 0.3, 1.0 }; | |
glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffuse ); | |
glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, specular ); | |
glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, 100.0 ); | |
glNormal3d( 0.0, 0.0, 1.0 ); | |
glBegin( GL_QUADS ); | |
glVertex3d( -1.0, -1.0, -1.0 ); | |
glVertex3d( 1.0, -1.0, -1.0 ); | |
glVertex3d( 1.0, 1.0, -1.0 ); | |
glVertex3d( -1.0, 1.0, -1.0 ); | |
glEnd(); | |
// cv::flip( frame, frame, 0 ); | |
// glDrawPixels( frame.cols, frame.rows, GL_BGR, GL_UNSIGNED_BYTE, frame.data ); | |
} | |
int main( int argc, char **argv ) | |
{ | |
cv::VideoCapture cap( 0 ); | |
if( !cap.isOpened() ) | |
return 0; | |
if( !glfwInit() ) | |
return 0; | |
auto const window = glfwCreateWindow( 640, 480, "Hello World", nullptr, nullptr ); | |
glfwMakeContextCurrent( window ); | |
if( !init() ) | |
return 0; | |
cv::Mat frame; | |
while( true ) | |
{ | |
if( glfwWindowShouldClose( window ) ) | |
break; | |
cap >> frame; | |
if( frame.empty() ) | |
break; | |
{ | |
static auto last_time = std::chrono::high_resolution_clock::now(); | |
static auto last_print = 0; | |
auto const now = std::chrono::high_resolution_clock::now(); | |
auto const d = now - last_time; | |
auto const dd = std::chrono::duration_cast< std::chrono::duration< double > >( d ).count(); | |
auto print = std::printf( "\r%10.8fs", dd ); | |
if( d.count() != 0 ) | |
print += std::printf( " (%5.2ffps)", 1 / dd ); | |
for( auto i = print; i < last_print; ++i ) | |
std::putchar( ' ' ); | |
std::cout << std::flush; | |
last_print = print; | |
last_time = now; | |
} | |
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); | |
draw( std::move( frame ) ); | |
glfwSwapBuffers( window ); | |
glfwPollEvents(); | |
} | |
glfwTerminate(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment