hi i’m still new with the 3D drawings but anyway i draw the cuboid 3D and made some rotations through key presses and now i want to add a texture but it only shows some unclear figure like lines or like when you are taking a photo to a screen … but generally this is the code i’ve written ( not all faces completed yet i was testing on only one face)
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <windows.h>
#include <gl/glut.h>
#include <fstream>
float angle = 0.0 , angley=0.0 , anglez =0.0;
const int IMG_WIDTH = 256;
const int IMG_HEIGHT = 256;
GLuint LoadTextureRAW(const char * filename, int wrap)
{
GLuint texture;
int width, height;
BYTE * data;
FILE * file;
file = fopen(filename, "r");
if (file == NULL)
{
return 0;
}
width = IMG_WIDTH;
height = IMG_HEIGHT;
data = (byte*)malloc(width * height * 3);
fread(data, width * height * 3, 1, file);
fclose(file);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap ? GL_REPEAT : GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap ? GL_REPEAT : GL_CLAMP);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, GL_BGR_EXT, GL_UNSIGNED_BYTE, data);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glEnable(GL_TEXTURE_2D);
return texture;
}
void drawCuboid()
{
GLuint texture1 = LoadTextureRAW("aa.bmp", 0);
glBindTexture(GL_TEXTURE_2D, texture1);
glColor3d(1, 1, 0);
glBegin(GL_QUADS);
glTexCoord2f(1.0, 1.0);
glVertex3f(-70, -70, 30);
glTexCoord2f(0.0, 1.0);
glVertex3f(70, -70, 30);
glTexCoord2f(0.0, 0.0);
glVertex3f(70, 70, 30);
glTexCoord2f(1.0, 0.0);
glVertex3f(-70, 70, 30);
glEnd();
glColor3d(1, 0, 1);
glBegin(GL_QUADS);
glVertex3f(-70, -70, 170);
glVertex3f(70, -70, 170);
glVertex3f(70, 70, 170);
glVertex3f(-70, 70, 170);
glEnd();
glColor3d(0, 1, 0);
glBegin(GL_QUADS);
glVertex3f(-70, -70, 30);
glVertex3f(-70, -70, 170);
glVertex3f(-70, 70, 170);
glVertex3f(-70, 70, 30);
glEnd();
glColor3d(1, 1, 1);
glBegin(GL_QUADS);
glVertex3f(70, -70, 30);
glVertex3f(70, -70, 170);
glVertex3f(70, 70, 170);
glVertex3f(70, 70, 30);
glEnd();
glColor3d(0, 1, 1);
glBegin(GL_QUADS);
glVertex3f(-70, 70, 30);
glVertex3f(-70, 70, 170);
glVertex3f(70, 70, 170);
glVertex3f(70, 70, 30);
glEnd();
glColor3d(1, 0, 0);
glBegin(GL_QUADS);
glVertex3f(-70, -70, 30);
glVertex3f(-70, -70, 170);
glVertex3f(70, -70, 170);
glVertex3f(70, -70, 30);
glEnd();
}
void draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glShadeModel(GL_SMOOTH);
glClearColor(0, 0, 0, 0);
glClearDepth(1);
glEnable(GL_DEPTH_TEST);
glPushMatrix();
glRotatef(angle, 1, 0, 0);
glRotatef(angley, 0, 1, 0);
glRotatef(anglez, 0, 0, 1);
drawCuboid();
glPopMatrix();
glutSwapBuffers();
glFlush();
glDisable(GL_TEXTURE_2D);
}
void keyboard(unsigned char keyPressed, int xMouse, int yMouse)
{
switch (keyPressed)
{
case 'w':
angle+=1;
break;
case 's':
angle -= 1;
break;
case 'd':
angley += 1;
break;
case 'a':
angley -= 1;
break;
case 'z':
anglez += 1;
break;
case 'x':
anglez -= 1;
break;
}
glutPostRedisplay();
}
void setTransformations()
{
glColor3f(0, 0, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50, 600 / 600, 1, 10000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
glViewport(0, 0, 600, 600);
gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
glOrtho(-100, 100, -100, 100, 1, 100);
}
void initialize(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA);
glutInitWindowPosition(0, 0);
glutInitWindowSize(800, 800);
glutCreateWindow("Computer Graphics Lab");
glutDisplayFunc(draw);
glutKeyboardFunc(keyboard);
setTransformations();
glutMainLoop();
}
void main(int argc, char *argv[])
{
initialize(argc, argv);
}