我的最后一项工作包括在我已经制作的圆的中心添加一个带有纹理的 3D 立方体,但由于某种原因,屏幕只是显示为黑色,没有数字或任何东西;我知道这可能是我在错误的地方添加的东西或类似的东西,但我不知道究竟是哪个部分给出了错误。这个问题有什么解释吗?
#include"glut.h"
#include<cmath>
#include<iostream>
using namespace std;
GLfloat angle = 0.0f;
int refreshmill = 1;
float xr = 0, yr = 0; //to control the object's movement from left to right
// XZ position of the camera
float x = 0.0f, z = 5.0f; //Module 4
float angleX = 0.0f; //Module 4
//Shift + ArrowKey rotation
float transX = 0.0f;
float transY = 0.0f;
float rotY = 0.0f;
//end
//Mouse Commands
GLfloat theta3 = 0;
GLfloat phi = 0;
GLfloat rho = 5;
GLfloat camX = 0;
GLfloat camY = 0;
GLfloat camZ = 0;
GLfloat upX = 0;
GLfloat upY = 0;
GLfloat upZ = 0;
GLdouble zoom = 0.0f;
//end
//For cube usage
#define checkImageWidth 64
#define checkImageHeight 64
static GLubyte checkImage[checkImageHeight][checkImageWidth][4];
static GLuint texName;
void timer(int value) { //to control the rotation of the object
glutTimerFunc(refreshmill, timer, 0);
}
void myDisplay(void) {
//Circle One
float theta;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 0, 0);
glRotatef(rotY, 0.0, 1.0, 0.0); //Rotation with Shift + ArrowKey
glPushMatrix();
glBegin(GL_POLYGON);
for (int x = 0; x < 360; x++) {
theta = x * 3.142 / 180;
glVertex2f(150 * cos(theta) + xr, 150 * sin(theta) + yr);
}
glEnd();
glPopMatrix();
//Circle Two
float theta2;
glPushMatrix();
glTranslatef(0.5f, 0.0f, 0.0f); // rotation
glRotatef(angle, 0.0f, 0.0f, -0.5f); // rotation
glBegin(GL_POLYGON);
glColor3f(0, 0, 1);
for (int x = 0; x < 360; x++) {
theta2 = x * 3.142 / 180;
glVertex2f(150 + 15 * cos(theta2) + xr, 15 * sin(theta2) + yr);
}
angle += 0.2; // rotation
glEnd();
glPopMatrix();
//Draw Star
glColor3ub(119, 193, 15);
glPushMatrix();
glBegin(GL_POLYGON);
glVertex2d(15 + xr, 60 + yr);
glVertex2d(75 + xr, 75 + yr); //right peak
glVertex2d(15 + xr, 90 + yr);
glVertex2d(0 + xr, 150 + yr); //Up-peak Changed
glVertex2d(-15 + xr, 90 + yr);
glVertex2d(-75 + xr, 75 + yr);
glVertex2d(-15 + xr, 60 + yr);
glVertex2d(0 + xr, 0 + yr);
glEnd();
glPopMatrix();
//Draw cube
glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); //DECAL & MODULATE
glBindTexture(GL_TEXTURE_2D, texName);
glBegin(GL_QUADS);
// Front Face
glColor3f(1.0, 1.0, 0.0); //yellow
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.25f, -0.25f, 0.25f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(0.25f, -0.25f, 0.25f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.25f, 0.25f, 0.25f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.25f, 0.25f, 0.25f);
// Back Face
glColor3f(0.0, 1.0, 1.0); //cyn
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.25f, -0.25f, -0.25f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.25f, 0.25f, -0.25f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0.25f, 0.25f, -0.25f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(0.25f, -0.25f, -0.25f);
// Top Face
glColor3f(1.0, 0.0, 1.0); //mag side
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.25f, 0.25f, -0.25f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.25f, 0.25f, 0.25f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(0.25f, 0.25f, 0.25f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.25f, 0.25f, -0.25f);
// Bottom Face
glColor3f(1.0, 0.0, 0.0); //red
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.25f, -0.25f, -0.25f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0.25f, -0.25f, -0.25f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(0.25f, -0.25f, 0.25f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.25f, -0.25f, 0.25f);
// Right face
glColor3f(0.0, 0.0, 1.0); //bue
glTexCoord2f(1.0f, 0.0f); glVertex3f(0.25f, -0.25f, -0.25f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.25f, 0.25f, -0.25f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0.25f, 0.25f, 0.25f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(0.25f, -0.25f, 0.25f);
// Left Face
glColor3f(0.0, 1.0, 0.0); //green
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.25f, -0.25f, -0.25f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.25f, -0.25f, 0.25f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.25f, 0.25f, 0.25f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.25f, 0.25f, -0.25f);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
}
void makeCheckImage(void){
int i, j, c;
for (i = 0; i < checkImageHeight; i++) {
for (j = 0; j < checkImageWidth; j++) {
c = ((((i & 0x8) == 0) ^ ((j & 0x8)) == 0)) * 255;
checkImage[i][j][0] = (GLubyte)c;
checkImage[i][j][1] = (GLubyte)c;
checkImage[i][j][2] = (GLubyte)c;
checkImage[i][j][3] = (GLubyte)255;
}
}
}
//Close code
void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 27: //when the escape key is pressed the program will exit.
exit(0);
}
}
//Movement of drawing
void keyboard(int key, int x, int y) {
float fraction = 0.1f;
bool shift = false;
int mod = glutGetModifiers();
if (mod == GLUT_ACTIVE_SHIFT) {
shift = true;
}
if (!shift) {
switch (key) {
case GLUT_KEY_RIGHT: xr++; break;
case GLUT_KEY_LEFT: xr--; break;
case GLUT_KEY_UP: angleX -= 1.0f; break; //Module 4
case GLUT_KEY_DOWN: angleX += 1.0f; break; //Module 4
}
}
else {
switch (key) {
case GLUT_KEY_LEFT:// Rotación del dibujo hacia la izquierda en el eje de Y
rotY -= 1.0f;
break;
case GLUT_KEY_RIGHT:// Rotación del dibujo hacia la derecha en el eje de Y
rotY += 1.0f;
break;
}
}
}
//Mouse Function
void MouseFunc(int button, int state, int x, int y) {
GLdouble min_z = -100.0;
GLdouble max_z = 100.0;
if (button == 4 && zoom < max_z) {
zoom += 3.0;
}
else if (button == 3 && zoom > min_z) {
zoom -= 3.0;
}
}
void renderScene(void) {
// Clear Color and Depth Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLfloat camX = rho * cos(theta3*3.1415926f / 180)*sin(phi*3.1415926f / 180);
GLfloat camY = rho * sin(theta3*3.1415926f / 180);
GLfloat camZ = rho * cos(theta3*3.1415926f / 180)*cos(phi*3.1415926f / 180);
// Reduce theta slightly to obtain another point on the same longitude line on the sphere.
GLfloat dt = 1;
GLfloat eyeXtemp = -rho * cos((theta3 - dt)*3.1415926f / 180)*sin(phi*3.1415926f / 180);
GLfloat eyeYtemp = -rho * sin((theta3 - dt)*3.1415926f / 180);
GLfloat eyeZtemp = -rho * cos((theta3 - dt)*3.1415926f / 180)*cos(phi*3.1415926f / 180);
// Connect these two points to obtain the camera's up vector.
GLfloat upX = eyeXtemp - camX;
GLfloat upY = eyeYtemp - camY;
GLfloat upZ = eyeZtemp - camZ;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//Mouse Commands
GLdouble ortho = 250 + zoom; //Mouse wheel zoom
glOrtho(-ortho, ortho, -ortho, ortho, -250, 250); //Mouse wheel zoom
// Reset transformations
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(camX, camY, camZ, 0, 0, 0, upX, upY, upZ);
// Set the camera
gluLookAt(x, 0.0f, z, x, 0.0f, z - 1.0f, 0.0f, 1.0f, 0.0f); //Module 4
glRotatef(angleX, 1, 0, 0); //Module 4
myDisplay();
//DrawCube(0.0f, 0.0f, 0.85);
glFlush();
glutPostRedisplay();
glutSwapBuffers();
}
void init() {
glClearColor(0, 0, 0, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(0.0f, 0.1f, 0.1f, 100.0f);
glOrtho(-250, 250, -250, 250, -250, 250); //IMPORTANT- Define from negative to positive
glMatrixMode(GL_MODELVIEW);
//CUBE COMMANDS
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
makeCheckImage();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth,
checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,
checkImage);
}
int main(int argc, char** argv) {
// init GLUT and create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Homework: Circle");
// register callbacks
glutDisplayFunc(renderScene);
glutTimerFunc(0, timer, 0);
glutKeyboardFunc(handleKeypress);
glutSpecialFunc(keyboard);
glutMouseFunc(MouseFunc);
// OpenGL init
init();
// enter GLUT event processing cycle
glutMainLoop();
}