有没有大神知道怎么在物体上增加纹理啊,求求了
visualstudio吧
全部回复
仅看楼主
level 1
#include<stdlib.h>
#include<gl/glut.h>
#include<math.h>
#include<windows.h>
#include <gl/GLAUX.H>
int a;
int k;
int z;
float mouseX, mouseY;
float cameraDistanceX;
float cameraDistanceY;
float cameraAngleX;
float cameraAngleY;
float times = 0.0;
int Rebuild = 1;
int Level = 7;
GLuint ttt[30];
GLfloat ambient[] = { 0.3f, 0.3f, 0.3f, 1.0f };
GLfloat diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat position[] = { 8.0f, 8.4f, 8.8f, 1.0f };
#define STEM 1
#define LEAF 2
#define LEAF_MAT 3
#define TREE_MAT 4
#define STEMANDLEAVES 5
#define TREE 6
GLfloat angle = 0.0f;
inline float randf()
{
return (rand() / (float)RAND_MAX);
}
void CreateTreeLists(void)
{
GLUquadricObj* cylquad = gluNewQuadric();
int i;
glNewList(STEM, GL_COMPILE);
glCallList(TREE_MAT);
glPushMatrix();
glColor3f(0.8, 0.2, 0.2);
glRotatef(-90, 1, 0, 0);
gluCylinder(cylquad, 0.1, 0.08, 1.3, 10, 2);
glPopMatrix();
glEndList();
glNewList(LEAF, GL_COMPILE);
glColor3f(0.1, 0.2,0.9);
glBegin(GL_TRIANGLES);
glNormal3f(-0.1, 0, 0.25);
glVertex3f(0, 0, 0);
glVertex3f(0.5, 0.5, 0.2);
glVertex3f(0, 1.0, 0);
glNormal3f(0.2, 0, 0.5);
glVertex3f(0, 0, 0);
glVertex3f(0, 1.0, 0);
glVertex3f(-0.5, 0.5, 0.2);
glEnd();
glEndList();
glNewList(STEMANDLEAVES, GL_COMPILE);
glPushMatrix();
glCallList(STEM);
glCallList(LEAF_MAT);
for (i = 0; i < 3; i++)
{
glTranslatef(0, 0.333, 0);
glRotatef(90, 0, 1, 0);
glPushMatrix();
glRotatef(0, 0, 1, 0);
glRotatef(50, 1, 0, 0);
glCallList(LEAF);
glPopMatrix();
glPushMatrix();
glRotatef(180, 0, 1, 0);
glRotatef(60, 1, 0, 0);
glCallList(LEAF);
glPopMatrix();
}
glPopAttrib();
glPopMatrix();
glEndList();
gluDeleteQuadric(cylquad);
}
void FractalTree(int level)
{
long savedseed;
if (level == 7)
{
glPushMatrix();
glRotatef(60 + rand() * 120, 0, 1, 0);
glCallList(STEMANDLEAVES);
glPopMatrix();
}
else
{
glCallList(STEM);
glPushMatrix();
glTranslatef(0, 1.3, 0);
glScalef(0.7, 0.7, 0.7);
savedseed = rand();
glPushMatrix();
glRotatef(0 + randf() * 90, 0, 1, 0);
glRotatef(30 + randf() * 30, 0, 0, 1);
FractalTree(level + 1);
glPopMatrix();
srand(savedseed);
savedseed = rand();
glPushMatrix();
glRotatef(180 + randf() * 90, 0, 1, 0);
glRotatef(30 + randf() * 30, 0, 0, 1);
FractalTree(level + 1);
glPopMatrix();
srand(savedseed);
savedseed = rand();
glPushMatrix();
glRotatef(90 + randf() * 90, 0, 1, 0);
glRotatef(30 + randf() * 30, 0, 0, 1);
FractalTree(level + 1);
glPopMatrix();
glPopMatrix();
}
}
#define imageWidth 64
#define imageHeight 64
GLubyte image[imageWidth][imageHeight][3];
void makeImage(void)
{
int i, j, r, g, b;
for (i = 0; i < imageWidth; i++)
{
for (j = 0; j < imageHeight; j++)
{
r = (i * j) % 255;
g = (4 * i) % 255;
b = (4 * j) % 255;
image[i][j][0] = (GLubyte)r;
image[i][j][1] = (GLubyte)g;
image[i][j][2] = (GLubyte)b;
}
}
}
int LoadGLTextures(GLuint& unTexture, const char* chFileName)
{
AUX_RGBImageRec* TextureImage;
TextureImage = auxDIBImageLoad(chFileName);
glGenTextures(10, &unTexture);
glBindTexture(GL_TEXTURE_2D, unTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TextureImage->sizeX, TextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if (TextureImage)
{
if (TextureImage->data)
{
free(TextureImage->data);
}
free(TextureImage);
}
return 1;
}
void init(void)
{
glClearColor(0.0, 0.6, 1.0, 0.0);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
makeImage();
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexImage2D(GL_TEXTURE_2D, 0, 3,imageWidth, imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, &image[0][0][0]);
glShadeModel(GL_SMOOTH);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHT0);
glEnable(GL_BLEND);
glEnable(GL_CW);
glEnable(GL_BACK);
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
LoadGLTextures(ttt[0], "1.bmp");
}
void display(void)
{
GLUquadricObj* g_Object = gluNewQuadric();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(75, 1, 1, 21);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 1,-10, 0,0,0 , 0, 0, 1);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glTranslatef(0-z, 0, 0.0);
glRotatef(cameraAngleX, 1.f, 0.0f, 0.0f);
glRotatef(cameraAngleY, 0, 1, 0);
glScalef(1.0+times, 1.0 + times, 1.0 + times);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3f(8.0, -5.4, -10.0);
glTexCoord2f(7.8, 0.0);
glVertex3f(8.0, -5.4, 10.0);
glTexCoord2f(0.0, 7.4);
glVertex3f(-8.0, -5.4, 10.0);
glTexCoord2f(7.4, 7.8);
glVertex3f(-8.0, -5.4, -10.0);
glEnd();
if (k == 1) { glRotatef(angle, 0.0f, 1.0f, 0.0f); }
glPushMatrix();
glColor3f(0.0, 1.0, 1.0);
glTranslatef(-0.2, 0, 0.0);
glBindTexture(GL_TEXTURE_2D, ttt[0]);
glutSolidTeapot(1.0);
glTranslatef(0.2, 0, 0.0);
glPopMatrix();
glPushMatrix(); //杯
glColor3f(1.0, 0.5, 0.7);
glTranslatef(1.0, -0.7, -0.7);
glRotatef(90.0f, -1, 0, 0);
gluCylinder(g_Object, 0.27, 0.32, 0.8, 20, 5);
glTranslatef(0, 0, 0.1);
gluDisk(g_Object, 0, 0.27, 20, 5);
glPopMatrix();
glTranslatef(0.0, -2.8, 0.0);
glColor3f(0.0, 0.0, 1.0);
if(k==1){ glRotatef(-angle, 0.0f, 1.0f, 0.0f); }
else if(k==2){ glRotatef(angle, 0.0f, 1.0f, 0.0f); }
glBindTexture(GL_TEXTURE_2D, ttt[1]);
glutSolidCube(4.0);
glTranslatef(0.0, 1.05, 0.0);
glRotatef(90.f, 1.0f, 0.0f, 0.0f);
glutSolidTorus(1, 2.5, 50, 50);
glRotatef(-90.f, 1.0f, 0.0f, 0.0f);
glTranslatef(0.0, -1.05, 0.0);
if (k == 2) { glRotatef(-angle, 0.0f, 1.0f, 0.0f); }
glTranslatef(-6.0, -1.0, -1.0);
glColor3f(0.0, 1.0, 0.0);
glutSolidCube(2.0);
glTranslatef(0.0, 0.0, 1.0);
glutSolidCube(2.0);
glTranslatef(0.0, 0.0, 1.0);
glutSolidCube(2.0);
glTranslatef(0.0, 0.0, 1.0);
glutSolidCube(2.0);
glTranslatef(12, 0.0, -3.0);
glutSolidCube(2.0);
glTranslatef(0.0, 0.0, 1.0);
glutSolidCube(2.0);
glTranslatef(0.0, 0.0, 1.0);
glutSolidCube(2.0);
glTranslatef(0.0, 0.0, 1.0);
glutSolidCube(2.0);
glScalef(3.5, 3.5, 3.5);
glTranslatef(-1.0,0.0,1.0);
if (Rebuild)
{
glNewList(TREE, GL_COMPILE);
FractalTree(0);
glEndList();
Rebuild = 0;
}
glCallList(TREE);
if (k == 3)
glRotatef(angle, 0.0f, 0.0f, 1.0f);
glTranslatef(-2.0, 3.2, 0.0);
glScalef(0.3, 0.3, 0.3);
glColor3f(1.0, 0.0, 0.0);
glutSolidSphere(1.0, 30, 30);
glTranslatef(14.0, -20, 0.0);
glColor3f(1.0, 1.0, 0.0);
glutSolidSphere(1.0, 30, 30);
glutSwapBuffers();
glFlush();
}
void myIdle(void)
{
angle += 0.5f;
if (angle >= 360.0f)
angle = 0.0f;
display();
}
void Keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case'q':
k=1;
break;
case'w':
k=2;
break;
case'e':
k=3;
break;
case'a':
times+=-0.1;
break;
case's':
times+=0.1;
break;
case'r':
k = 0;
z=0;
break;
case'z':
z += -1;
break;
case'x':
z += 1;
break;
}
}
void command_mouse(int button, int state, int x, int y)
{
mouseX = x;
mouseY = y;
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
a = 1;
}
}
void mouseMotionCB(int x, int y)
{
cameraAngleX = cameraAngleY = 0;
if (a==1)
{
cameraAngleY += (x - mouseX) * 0.1f;
cameraAngleX += (y - mouseY) * 0.1f;
}
glutPostRedisplay();
}
void Reshape(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 1, -10, 0, 0, 0, 0, 0, 1);
}
void command_menu(int value)
{
if (value == 1) {
Keyboard('q',0,0);
}
if (value == 2) {
Keyboard('w', 0, 0);
}
if (value == 3) {
Keyboard('e', 0, 0);
}
if (value == 4) {
Keyboard('a', 0, 0);
}
if (value == 5) {
Keyboard('s', 0, 0);
}
if (value == 6) {
Keyboard('r', 0, 0);
}
if (value == 7){
Keyboard('z', 0, 0);
}
if (value == 8) {
Keyboard('x', 0, 0);
}
if (value == 9) {
exit(0);
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(200, 200);
glutInitWindowSize(600, 600);
glutCreateWindow("scene");
glutKeyboardFunc(Keyboard);
glutMouseFunc(command_mouse);
glutMotionFunc(mouseMotionCB);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
init();
glutReshapeFunc(Reshape);
glutDisplayFunc(display);
glutCreateMenu(command_menu);
glutAddMenuEntry("teapot around", 1);
glutAddMenuEntry("table around", 2);
glutAddMenuEntry("sun around", 3);
glutAddMenuEntry("smaller", 4);
glutAddMenuEntry("bigger", 5);
glutAddMenuEntry("remake", 6);
glutAddMenuEntry("left", 7);
glutAddMenuEntry("right", 8);
glutAddMenuEntry("exit", 9);
CreateTreeLists();
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutDetachMenu(GLUT_LEFT_BUTTON);
glutIdleFunc(myIdle);
glutMainLoop();
return (0);
}
2020年05月30日 09点05分 1
1