-
-
Save ACUVE/911477c5401841d2d8ed 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> | |
GLuint pid; | |
constexpr auto *const vs = R"|||( | |
void main( void ) | |
{ | |
// gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; | |
gl_Position = ftransform(); | |
gl_FrontColor = gl_Color; | |
gl_TexCoord[ 0 ] = gl_TextureMatrix[ 0 ] * gl_MultiTexCoord0; | |
} | |
)|||"; | |
constexpr auto *const fs = R"|||( | |
uniform sampler2D texture; | |
void main( void ) | |
{ | |
// gl_FragColor = vec4( 1.0, 0.0, 0.0, 0.0 ); | |
gl_FragColor = texture2DProj( texture, gl_TexCoord[ 0 ] ); | |
} | |
)|||"; | |
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 ); | |
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_TEXTURE_2D ); | |
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, 2, 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 ); | |
GLuint name{}; | |
glGenTextures( 1, &name ); | |
glBindTexture( GL_TEXTURE_2D, name ); | |
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); | |
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); | |
cv::Mat dest; | |
// cv::resize( frame, dest, cv::Size( 128, 128 ) ); | |
glTexImage2D( | |
GL_TEXTURE_2D, | |
0, | |
GL_RGB, | |
frame.cols, | |
frame.rows, | |
0, | |
GL_BGR, | |
GL_UNSIGNED_BYTE, | |
frame.data | |
); | |
// glUniform1i( glGetUniformLocation( pid, "texture"), name ); | |
{ | |
static unsigned long long int i = 0; | |
glRotatef( 1.0 * i, 0.5, 1.0, 0.25 ); | |
i++; | |
} | |
glNormal3d( 0.0, 0.0, 1.0 ); | |
glBegin( GL_QUADS ); | |
glTexCoord2f( 0.0, 0.0 ); | |
glVertex3d( -1.0, -1.0, 0.0 ); | |
glTexCoord2f( 0.0, 1.0 ); | |
glVertex3d( 1.0, -1.0, 0.0 ); | |
glTexCoord2f( 1.0, 1.0 ); | |
glVertex3d( 1.0, 1.0, 0.0 ); | |
glTexCoord2f( 1.0, 0.0 ); | |
glVertex3d( -1.0, 1.0, 0.0 ); | |
glEnd(); | |
glDeleteTextures( 1, &name ); | |
} | |
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