i have a problme in uplode my image in openGL i dont know what is wrong here but everytime the output is whaite windo this is my code and the image my ortho is 1,-1 and its 2D
//C:/Users/asus-pc/Documents/CG.bmp
#include <GL\glew.h> // A cross-platform open-source C/C++ extension loading library
#include <GL\freeglut.h>
#include<cmath>
#include<iostream>
using namespace std;
float w = 500, h = 500, newWidth, newHeight;
GLuint myTexture1;
char image1Path[] = "C:/Users/asus-pc/Pictures/CG.bmp";
GLuint LoadTexture(const char* filename, int width, int height)
{
GLuint texture;
unsigned char* data;
FILE* file;
//The following code will read in our RAW file
file = fopen(filename, "rb");
if (file == NULL)
{
cout << "Unable to open the image file" << endl << "Program will exit :(" << endl;
exit(0);
return 0;
}
data = (unsigned char*)malloc(width * height * 3);
fread(data, width * height * 3, 1, file);
fclose(file);
// reorder the image colors to RGB not BGR
for (int i = 0; i < width * height; ++i)
{
int index = i * 3;
unsigned char B, R;
B = data[index];
R = data[index + 2];
data[index] = R;
data[index + 2] = B;
}
/////////////////////////////////////////
// All Exercises TODO: load another texture image
//////
glGenTextures(1, &texture); //generate the texture with the loaded data
glBindTexture(GL_TEXTURE_2D, texture); //bind the texture to it's array
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); //set texture environment parameters
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
/////////////////////////////////////////
free(data); //free the texture array
if (glGetError() != GL_NO_ERROR)
printf("GLError in genTexture()\n");
return texture; //return whether it was successfull
}
void init() {
glClearColor(0.869, 0.880, 0.766, 1.0f);
//glClearColor(0.8f, 0.2f, 0.3f, 1.0f); //TOBIAS
//glClearColor(0.86f, 0.90f, 0.75f, 1.0f); // Set background color to orangeeee
glMatrixMode(GL_PROJECTION); // set the projection parameters
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(1.5, 1.5, -1.5, 0, 1, -1, 0, 1, 0);
myTexture1 = LoadTexture(image1Path, 1024, 1024);
if (myTexture1 == -1)
{
cout << "Error in loading the texture" << endl;
}
else
cout << "The texture value is: " << myTexture1 << endl;
}
void square() {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, myTexture1);
glColor3f(0, 0, 0);
glBegin(GL_QUADS);
glNormal3f(0, 0, 1);
glTexCoord2f(1, 0);
glVertex3f(1, -1, 0);
glTexCoord2f(1, 1);
glVertex3f(1, 1, 0);
glTexCoord2f(0, 1);
glVertex3f(-1, 1, 0);
glTexCoord2f(0, 0);
glVertex3f(-1, -1, 0);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void drawAll() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1, 1);
square();
glFlush();
glutSwapBuffers();
}
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); // Specify the display Mode – RGB, RGBA or color
// Index, single or double Buffer
glutCreateWindow("STUFF"); // Create a window with the given title
glutInitWindowSize(320, 320); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
init();
//glutReshapeFunc(reshape);
//glutIdleFunc(play);
glutDisplayFunc(drawAll);
glutMainLoop();
return 0;
}
i try to load some libary but it dosnt work and use different imgage to make sure the proplem not in my image this is my first use in openGL so i dont know much how to work with it