Created
September 20, 2014 09:41
-
-
Save saad749/e3211d3788e22f486018 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
#include <Windows.h> //Not Including results in error C3861: 'exit': identifier not found | |
#include <glut.h> //OpenGL Library | |
#include <iostream> //Not Including Results in error C2039: 'cout' : is not a member of 'std' | |
#include "RGBA.h" //Picture Loading Library --- Requires Windows.h && iostream to work. Better than pixMap.h | |
#define PI 3.1415926535898 | |
//Function Headers | |
void myInit(void); | |
void myDisplay(void); | |
void myKeys(unsigned char key, int x, int y); | |
void SpecialFunc(int key, int x, int y); | |
//Variables | |
RGBApixmap textures[3]; //Array to hold textures | |
//Window Variables | |
int windowWidth = 1360; | |
int windowHeight = 760; | |
//CSaad Position Variables | |
float CSaadX = 400.0; | |
float CSaadY = 600.0; | |
float CSaadWidth = 300.0; | |
float CSaadHeight = 160.0; | |
//CSaadSpeed | |
int CSaadSpeed = 10; | |
int main(int argc, char **argv) { | |
// init GLUT and create Window | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); | |
glutInitWindowPosition(100,100); | |
glutInitWindowSize(windowWidth, windowHeight); | |
glutCreateWindow("Lab 4: Texturing"); | |
//My Initializations | |
myInit(); | |
// register callbacks; Enable/Disable as Desired | |
glutDisplayFunc(myDisplay); //The Display Function | |
glutKeyboardFunc(myKeys); //Normal Keys Detection | |
glutSpecialFunc(SpecialFunc); //Arrow Keys Detection | |
//glutMouseFunc(myMouse); //Mouse Detection | |
//glutMotionFunc(myMove); | |
// enter GLUT event processing cycle | |
glutMainLoop(); | |
return 1; | |
} | |
void myInit(void) | |
{ | |
glClearColor(0.0, 1.0, 1.0, 0.0); //Set Background Color | |
glColor3f(1, 1, 1); //Set Foreground Color | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
gluOrtho2D(0.0, windowWidth, 0.0, windowHeight); | |
textures[0].readBMPFile("Images/PakGreen.bmp", 2001); | |
textures[0].setTexture(2001); | |
textures[1].readBMPFile("Images/Football.bmp", 2002); | |
textures[1].setTexture(2002); | |
textures[2].readBMPFile("Images/SaadGray.bmp", 2003); | |
textures[2].setTexture(2003); | |
} | |
void myDisplay(void) //User Defined Display Function | |
{ | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clears xx with bgColor | |
glViewport(0, 0, windowWidth, windowHeight); | |
glBindTexture(GL_TEXTURE_2D, 2001); | |
glEnable(GL_TEXTURE_2D); | |
glBegin(GL_QUADS); | |
glTexCoord2f(0.0, 0.0); | |
glVertex2f(0.0, 0.0); | |
glTexCoord2f(1.0, 0.0); | |
glVertex2f(1360.0, 0.0); | |
glTexCoord2f(1.0, 1.0); | |
glVertex2f(1360.0, 760.0); | |
glTexCoord2f(0.0, 1.0); | |
glVertex2f(0.0, 760.0); | |
glEnd(); | |
glBindTexture(GL_TEXTURE_2D, 2002); | |
glBegin(GL_QUADS); | |
glTexCoord2f(0.0, 1.0); | |
glVertex2f(CSaadX, CSaadY + CSaadHeight); | |
glTexCoord2f(1.0, 1.0); | |
glVertex2f(CSaadX + CSaadWidth, CSaadY + CSaadHeight); | |
glTexCoord2f(1.0, 0.0); | |
glVertex2f(CSaadX + CSaadWidth, CSaadY) status 0 int | |
; | |
glTexCoord2f(0.0, 0.0); | |
glVertex2f(CSaadX, CSaadY); | |
glEnd(); | |
glBindTexture(GL_TEXTURE_2D, 2003); | |
//Drawing a circle | |
glLineWidth(0.0); | |
/*float radius = 250; | |
int centerX = 800; | |
int centerY = 300; | |
float circleX = 0; | |
float circleY = 0; | |
float textureX = 0; | |
float textureY = 0;*/ | |
float delta_theta = (2*PI)/40; | |
float radius = 250; | |
int centerX = 800; | |
int centerY = 300; | |
float cosX = 0; | |
float sinY = 0; | |
float circleX = 0; | |
float circleY = 0; | |
float textureX = 0; | |
float textureY = 0; | |
glBegin( GL_POLYGON ); | |
for( float angle = 0; angle < 2 * PI; angle += delta_theta ) | |
{ | |
cosX = cos(angle); | |
sinY = sin(angle); | |
circleX = radius * cosX + centerX; | |
circleY = radius * sinY + centerY; | |
//textureX = cosX; | |
//textureY = sinY; | |
textureX = (1+cosX) *0.5; | |
textureY = ( 1+sinY)*0.5; | |
glTexCoord2f(textureX, textureY); | |
glVertex2f(circleX, circleY); | |
} | |
glEnd(); | |
////////////////////// perfect logic to draw & texture //////////////////// | |
/////////////////////////////////////////////////////////////////////////// | |
// | |
//int numPoints = 40; | |
//float startAngle =0.0; | |
//float endAngle = 360; | |
//// Get how many degrees we move our slice per vertex in radians | |
//const GLfloat angleDeltaRads = ((startAngle - endAngle) / numPoints) * PI / 180; | |
//glBegin(GL_TRIANGLE_FAN); | |
//// Draw the vertex at the centre of the circle | |
//glTexCoord2f(0.5,0.5); // when we store texcoord | |
//glVertex2f(centerX,centerY); | |
//// Draw the fan of vertexes around the central point | |
//for(int loop = 0; loop <= numPoints; ++loop) | |
//{ | |
// // Texture coordinates | |
// float s = ( cos(angleDeltaRads * loop) + 1.0 )*0.5 ; // X | |
// float t = ( sin(angleDeltaRads * loop) + 1.0) *0.5 ; // Y | |
// glTexCoord2f(s,t); | |
// // Vertex coordinates | |
// circleX = centerX + (cos(angleDeltaRads * loop ) * radius); // X | |
// circleY = centerY + (sin(angleDeltaRads * loop ) * radius); // Y | |
// glVertex2f(circleX,circleY); | |
//} | |
//glEnd(); | |
//////////////////// End perfect logic to draw & texture //////////////////// | |
///////////////////////////////////////////////////////////////////////// | |
glDisable(GL_TEXTURE_2D); | |
glutSwapBuffers(); //Swap Buffers | |
//glFlush(); | |
glutPostRedisplay(); //glFlush(); Sends pixels to display screen | |
} | |
void myKeys(unsigned char key, int x, int y) | |
{ | |
switch(key) | |
{ | |
case 'q': //Escape Key | |
exit(0); | |
} | |
glutPostRedisplay(); | |
} | |
void SpecialFunc(int key,int x, int y) | |
{ | |
switch(key) | |
{ | |
case GLUT_KEY_RIGHT: | |
CSaadX += CSaadSpeed; | |
if(CSaadX + CSaadWidth > windowWidth) | |
{ | |
CSaadX -= CSaadSpeed; | |
glViewport(0, 0, 900, 760); | |
} | |
break; | |
case GLUT_KEY_LEFT: | |
CSaadX -= CSaadSpeed; | |
if (CSaadX < 0) | |
{ | |
CSaadX += CSaadSpeed; | |
} | |
break; | |
case GLUT_KEY_UP: | |
CSaadY += CSaadSpeed; | |
if (CSaadY + CSaadHeight > windowHeight) | |
CSaadY -= CSaadSpeed; | |
break; | |
case GLUT_KEY_DOWN: | |
CSaadY -= CSaadSpeed; | |
if (CSaadY < 0) | |
CSaadY += CSaadSpeed; | |
break; | |
default: | |
break; | |
} | |
glutPostRedisplay(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment