Texture loads but is drawn at right angle

This commit is contained in:
mmcwilliams 2024-04-01 17:45:50 -04:00
parent a5ceca86f6
commit baef478bba
1 changed files with 59 additions and 21 deletions

View File

@ -26,49 +26,87 @@ GLint screenWidth = 0;
GLint screenHeight = 0; GLint screenHeight = 0;
time_stamp ts; time_stamp ts;
void getScreenDimensions () { void loadTexture (GLuint& imageTexture1)
{
string image_path = samples::findFile("chart.png");
Mat image = imread(image_path, IMREAD_COLOR);
if (image.empty()) {
cout << "image empty" << endl;
} else {
cout << "loaded " << image_path << endl;
//flip(image, image, 1);
#if (CV_VERSION_MAJOR >= 4)
cvtColor(image, image, cv::COLOR_BGR2RGB);
#else
cvtColor(image, image, CV_BGR2RGB);
#endif
//glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glGenTextures(1, &imageTexture1);
glBindTexture(GL_TEXTURE_2D, imageTexture1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Set texture clamping method
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, // Type of texture
0, // Pyramid level (for mip-mapping) - 0 is the top level
GL_RGB, // Internal colour format to convert to
image.cols, // Image width i.e. 640 for Kinect in standard mode
image.rows, // Image height i.e. 480 for Kinect in standard mode
0, // Border width in pixels (can either be 1 or 0)
GL_RGB, // Input image format (i.e. GL_RGB, GL_RGBA, GL_BGR etc.)
GL_UNSIGNED_BYTE, // Image data type
image.ptr()); // The actual image data itself
}
}
void getScreenDimensions ()
{
if (screenWidth == 0 && screenHeight == 0) { if (screenWidth == 0 && screenHeight == 0) {
GLint dims[4] = {0}; GLint dims[4] = {0};
glGetIntegerv(GL_VIEWPORT, dims); glGetIntegerv(GL_VIEWPORT, dims);
screenWidth = dims[2]; screenWidth = dims[2];
screenHeight = dims[3]; screenHeight = dims[3];
cout << screenWidth << "," << screenHeight << endl; cout << screenWidth << "," << screenHeight << endl;
loadTexture(imageTexture);
} }
} }
void displayMe(void) void display(void)
{ {
getScreenDimensions(); getScreenDimensions();
ts = time_point_cast<microseconds>(system_clock::now());
string image_path = samples::findFile("chart.png");
Mat image = imread(image_path, IMREAD_COLOR);
#if (CV_VERSION_MAJOR >= 4)
cvtColor(image, image, cv::COLOR_BGR2RGB);
#else
cvtColor(image, image, CV_BGR2RGB);
#endif
// Set every pixel in the frame buffer to the current clear color.
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON); glEnable(GL_TEXTURE_2D);
glColor3f(1, 0, 0); glVertex3f(-1.0, -1.0, 0.5); //glViewport(0, 0, screenWidth, screenHeight);
glColor3f(0, 1, 0); glVertex3f(1.0, -1.0, 0); glMatrixMode(GL_PROJECTION);
glColor3f(0, 0, 1); glVertex3f(1.0, 1.0, 0); glLoadIdentity();
glColor3f(0, 0, 0); glVertex3f(-1.0, 1.0, 0);
glEnd(); glBindTexture(GL_TEXTURE_2D, imageTexture);
// Flush drawing command buffer to make drawing happen as soon as possible. glBegin(GL_QUADS); // front face
glFlush(); glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0f, -1.0f, 0.0f); //bottom right
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, 1.0f, 0.0f); //top right
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f); //top left
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, -1.0f, 0.0f); //bottom left
glEnd();
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
//glFlush();
} }
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
ts = time_point_cast<microseconds>(system_clock::now());
glutInit(&argc, argv); glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE); glutInitDisplayMode(GLUT_SINGLE);
glutCreateWindow("opengl_opencv_example"); glutCreateWindow("opengl_opencv_example");
glutFullScreen(); glutFullScreen();
glutDisplayFunc(displayMe); glutDisplayFunc(display);
glutMainLoop(); glutMainLoop();
return 0; return 0;
} }