include windows include gl gl include gl glut include SDL SDL_image in

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <windows.h>
#include <gl\gl.h>
#include <gl\glut.h>
#include <SDL\SDL_image.h>
#include <iostream>
#include <string>
#include <map>
GLfloat xRot = 0.0f;
GLfloat yRot = 0.0f;
std::map<std::string, GLuint> textures;
void loadTexture(std::string file, std::string name){
GLuint texture;
int COLOR_FLAG;
SDL_Surface* img = IMG_Load(file.c_str());
if (!img)
std::cerr << "IMG_Load Error: " << IMG_GetError() << std::endl;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
if (img->format->BitsPerPixel == 24)
COLOR_FLAG = GL_RGB;
else
COLOR_FLAG = GL_RGBA;
glTexImage2D(GL_TEXTURE_2D, 0, COLOR_FLAG, img->w, img->h, 0, COLOR_FLAG, GL_UNSIGNED_BYTE, img->pixels);
textures[name] = texture;
SDL_FreeSurface(img);
}
void useTexture(std::string name){
if (textures.count(name))
glBindTexture(GL_TEXTURE_2D, textures[name]);
}
void destroyTexture(std::string name){
if (textures.count(name)){
glDeleteTextures(GL_TEXTURE_2D, &textures[name]);
textures.erase(name);
}
}
void DrawTetrahedron(){
glBegin(GL_TRIANGLE_FAN);
glTexCoord2f(0.5f, 1); glVertex3f(0.0f, 50.0f, 0.0f);
glTexCoord2f(1, 1); glVertex3f(0.0f, -50.0f, 50.0f);
glTexCoord2f(0, 0); glVertex3f(50.0f, -50.0f, -50.0f);
glTexCoord2f(1, 1); glVertex3f(-50.0f, -50.0f, -50.0f);
glTexCoord2f(0, 0); glVertex3f(0.0f, -50.0f, 50.0f);
glEnd();
glBegin(GL_TRIANGLES);
glTexCoord2f(0.5f, 1); glVertex3f(0.0f, -50.0f, 50.0f);
glTexCoord2f(1, 1); glVertex3f(50.0f, -50.0f, -50.0f);
glTexCoord2f(0, 0); glVertex3f(-50.0f, -50.0f, -50.0f);
glEnd();
}
void RenderScene(void){
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
glEnable(GL_TEXTURE_2D);
useTexture("wood");
DrawTetrahedron();
glPopMatrix();
glutSwapBuffers();
}
void SetupRC(){
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
void SpecialKeys(int key, int x, int y){
if (key == GLUT_KEY_UP)
xRot -= 5.0f;
if (key == GLUT_KEY_DOWN)
xRot += 5.0f;
if (key == GLUT_KEY_LEFT)
yRot -= 5.0f;
if (key == GLUT_KEY_RIGHT)
yRot += 5.0f;
if (key > 356.0f)
xRot = 0.0f;
if (key < -1.0f)
xRot = 355.0f;
if (key > 356.0f)
yRot = 0.0f;
if (key < -1.0f)
yRot = 355.0f;
glutPostRedisplay();
}
void ChangeSize(int w, int h){
GLfloat nRange = 100.0f;
if (h == 0) h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-nRange, nRange, -nRange*h / w, nRange*h / w, -nRange, nRange);
else
glOrtho(-nRange*w / h, nRange*w / h, -nRange, nRange, -nRange, nRange);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char* argv []){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("Wood Tetrahedron");
loadTexture("C:\\wood.jpg", "wood");
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
SetupRC();
glutMainLoop();
return 0;
}