帮我修改下面的代码,指点一下吧~
#include <stdlib.h>
#include <GL/glut.h>
#include <stdio.h>
static GLfloat spin = 36.0;
int singleb, doubleb;
void colorcube(void)
{
glRotated(30,0,0,1);
glBegin(GL_QUADS);
glColor3f(0.0, 1.0, 0.0);
glVertex3f( 1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f( 1.0, 1.0, 1.0);
glColor3f(1.0, 0.5, 0.0);
glVertex3f( 1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f( 1.0, -1.0, -1.0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f( 1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f( 1.0, -1.0, 1.0);
glColor3f(1.0, 1.0, 0.0);
glVertex3f( 1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f( 1.0, 1.0, -1.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, 1.0);
glColor3f(1.0, 1.0, 0.0);
glVertex3f( 1.0, 1.0, -1.0);
glVertex3f( 1.0, 1.0, 1.0);
glVertex3f( 1.0, -1.0, 1.0);
glVertex3f( 1.0, -1.0, -1.0);
glEnd();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(5.0,5.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);
colorcube();
glFlush();
glutSwapBuffers();
}
void spinDisplay (void)
{
spin = spin + 36.0;
if (spin > 360.0)
spin = spin - 360.0;
glutSetWindow(singleb);
glLoadIdentity();
glRotatef (spin, 0.0,1.0, 0.0);
glutPostRedisplay();
glutSetWindow(doubleb);
glLoadIdentity();
glRotatef(spin, 0.0,1.0, 0.0);
glutPostRedisplay();
glFlush();
}
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, float(w) / h, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void init(int width,int height)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float)width / (float)height, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
/* need both double buffering and z buffer */
int winWidth = 650;
int winHeight = 500;
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE |GLUT_DEPTH);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("3D Cube");
glutDisplayFunc(display);
init(winWidth,winHeight);
glutReshapeFunc(myReshape);
glutIdleFunc(spinDisplay);
glutMainLoop();
}
7 个解决方案
#1
自己顶一下~没人帮忙?
#2
这句话什么意思??
在空闲时绕顶点(1,1,1)X轴方向旋转
在空闲时绕顶点(1,1,1)X轴方向旋转
#3
没用过OpenGL,只用过D3D,所以代码没看。
简单说下思路,不知道OpenGL和D3D是不是一样。
首先定时的改变世界矩阵使物体沿x轴旋转,D3D中是有相关api的,直接可以生成需要的矩阵和世界矩阵相乘即可。
然后把物体平移到(1,1,1)的位置。就好了。
简单说下思路,不知道OpenGL和D3D是不是一样。
首先定时的改变世界矩阵使物体沿x轴旋转,D3D中是有相关api的,直接可以生成需要的矩阵和世界矩阵相乘即可。
然后把物体平移到(1,1,1)的位置。就好了。
#4
#include <stdlib.h>
#include <GL/glut.h>
#include <stdio.h>
static GLfloat spin = 36.0;
int singleb, doubleb;
void colorcube(void)
{
// glRotated(30,0,0,1);
glBegin(GL_QUADS);
glColor3f(0.0, 1.0, 0.0);
glVertex3f( 1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f( 1.0, 1.0, 1.0);
glColor3f(1.0, 0.5, 0.0);
glVertex3f( 1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f( 1.0, -1.0, -1.0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f( 1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f( 1.0, -1.0, 1.0);
glColor3f(1.0, 1.0, 0.0);
glVertex3f( 1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f( 1.0, 1.0, -1.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, 1.0);
glColor3f(1.0, 1.0, 0.0);
glVertex3f( 1.0, 1.0, -1.0);
glVertex3f( 1.0, 1.0, 1.0);
glVertex3f( 1.0, -1.0, 1.0);
glVertex3f( 1.0, -1.0, -1.0);
glEnd();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(1.f,1.f,1.f);
glRotatef(spin, 1.f,0.f,0.f);
colorcube();
glPopMatrix();
// glFlush();
glutSwapBuffers();
}
void spinDisplay (void)
{
spin = spin + 1.0;
if (spin > 360.0)
spin = spin - 360.0;
//glutSetWindow(singleb);
//glLoadIdentity();
//glRotatef (spin, 0.0,1.0, 0.0);
//glutPostRedisplay();
//glutSetWindow(doubleb);
//glLoadIdentity();
//glRotatef(spin, 0.0,1.0, 0.0);
//glutPostRedisplay();
// glFlush();
}
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, float(w) / h, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(5.0,5.0,10.0,1.0,1.0,1.0,0.0,1.0,0.0);
}
void init(int width,int height)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */
glShadeModel(GL_SMOOTH);
//glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
//gluPerspective(45.0, (float)width / (float)height, 0.1, 100.0);
//glMatrixMode(GL_MODELVIEW);
}
void TimerFunction(int value)
{
// Redraw the scene with new coordinates
glutPostRedisplay();
glutTimerFunc(3,TimerFunction, 1);
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
/* need both double buffering and z buffer */
int winWidth = 650;
int winHeight = 500;
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE |GLUT_DEPTH);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("3D Cube");
glutDisplayFunc(display);
glutReshapeFunc(myReshape);
glutIdleFunc(spinDisplay);
init(winWidth,winHeight);
glutTimerFunc(33, TimerFunction, 1);
glutMainLoop();
}
楼主,代码按照你的需求改了下, 不过不是很确定“在空闲时绕顶点(1,1,1)X轴方向旋转”这句话的的意思, 好友就是建议楼主在不太清楚某个函数的用法时, 不要乱用, 先查文档看一下究竟是什么意思。
#include <GL/glut.h>
#include <stdio.h>
static GLfloat spin = 36.0;
int singleb, doubleb;
void colorcube(void)
{
// glRotated(30,0,0,1);
glBegin(GL_QUADS);
glColor3f(0.0, 1.0, 0.0);
glVertex3f( 1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f( 1.0, 1.0, 1.0);
glColor3f(1.0, 0.5, 0.0);
glVertex3f( 1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f( 1.0, -1.0, -1.0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f( 1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f( 1.0, -1.0, 1.0);
glColor3f(1.0, 1.0, 0.0);
glVertex3f( 1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f( 1.0, 1.0, -1.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, 1.0);
glColor3f(1.0, 1.0, 0.0);
glVertex3f( 1.0, 1.0, -1.0);
glVertex3f( 1.0, 1.0, 1.0);
glVertex3f( 1.0, -1.0, 1.0);
glVertex3f( 1.0, -1.0, -1.0);
glEnd();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(1.f,1.f,1.f);
glRotatef(spin, 1.f,0.f,0.f);
colorcube();
glPopMatrix();
// glFlush();
glutSwapBuffers();
}
void spinDisplay (void)
{
spin = spin + 1.0;
if (spin > 360.0)
spin = spin - 360.0;
//glutSetWindow(singleb);
//glLoadIdentity();
//glRotatef (spin, 0.0,1.0, 0.0);
//glutPostRedisplay();
//glutSetWindow(doubleb);
//glLoadIdentity();
//glRotatef(spin, 0.0,1.0, 0.0);
//glutPostRedisplay();
// glFlush();
}
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, float(w) / h, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(5.0,5.0,10.0,1.0,1.0,1.0,0.0,1.0,0.0);
}
void init(int width,int height)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */
glShadeModel(GL_SMOOTH);
//glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
//gluPerspective(45.0, (float)width / (float)height, 0.1, 100.0);
//glMatrixMode(GL_MODELVIEW);
}
void TimerFunction(int value)
{
// Redraw the scene with new coordinates
glutPostRedisplay();
glutTimerFunc(3,TimerFunction, 1);
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
/* need both double buffering and z buffer */
int winWidth = 650;
int winHeight = 500;
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE |GLUT_DEPTH);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("3D Cube");
glutDisplayFunc(display);
glutReshapeFunc(myReshape);
glutIdleFunc(spinDisplay);
init(winWidth,winHeight);
glutTimerFunc(33, TimerFunction, 1);
glutMainLoop();
}
楼主,代码按照你的需求改了下, 不过不是很确定“在空闲时绕顶点(1,1,1)X轴方向旋转”这句话的的意思, 好友就是建议楼主在不太清楚某个函数的用法时, 不要乱用, 先查文档看一下究竟是什么意思。
#5
应该就是绕这个点旋转,呵呵...
#6
谢谢,我试试~ 我不熟悉C++,又刚学OPENGL,后来用java做了这个习题~
#7
谢谢您的帮助~
#1
自己顶一下~没人帮忙?
#2
这句话什么意思??
在空闲时绕顶点(1,1,1)X轴方向旋转
在空闲时绕顶点(1,1,1)X轴方向旋转
#3
没用过OpenGL,只用过D3D,所以代码没看。
简单说下思路,不知道OpenGL和D3D是不是一样。
首先定时的改变世界矩阵使物体沿x轴旋转,D3D中是有相关api的,直接可以生成需要的矩阵和世界矩阵相乘即可。
然后把物体平移到(1,1,1)的位置。就好了。
简单说下思路,不知道OpenGL和D3D是不是一样。
首先定时的改变世界矩阵使物体沿x轴旋转,D3D中是有相关api的,直接可以生成需要的矩阵和世界矩阵相乘即可。
然后把物体平移到(1,1,1)的位置。就好了。
#4
#include <stdlib.h>
#include <GL/glut.h>
#include <stdio.h>
static GLfloat spin = 36.0;
int singleb, doubleb;
void colorcube(void)
{
// glRotated(30,0,0,1);
glBegin(GL_QUADS);
glColor3f(0.0, 1.0, 0.0);
glVertex3f( 1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f( 1.0, 1.0, 1.0);
glColor3f(1.0, 0.5, 0.0);
glVertex3f( 1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f( 1.0, -1.0, -1.0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f( 1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f( 1.0, -1.0, 1.0);
glColor3f(1.0, 1.0, 0.0);
glVertex3f( 1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f( 1.0, 1.0, -1.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, 1.0);
glColor3f(1.0, 1.0, 0.0);
glVertex3f( 1.0, 1.0, -1.0);
glVertex3f( 1.0, 1.0, 1.0);
glVertex3f( 1.0, -1.0, 1.0);
glVertex3f( 1.0, -1.0, -1.0);
glEnd();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(1.f,1.f,1.f);
glRotatef(spin, 1.f,0.f,0.f);
colorcube();
glPopMatrix();
// glFlush();
glutSwapBuffers();
}
void spinDisplay (void)
{
spin = spin + 1.0;
if (spin > 360.0)
spin = spin - 360.0;
//glutSetWindow(singleb);
//glLoadIdentity();
//glRotatef (spin, 0.0,1.0, 0.0);
//glutPostRedisplay();
//glutSetWindow(doubleb);
//glLoadIdentity();
//glRotatef(spin, 0.0,1.0, 0.0);
//glutPostRedisplay();
// glFlush();
}
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, float(w) / h, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(5.0,5.0,10.0,1.0,1.0,1.0,0.0,1.0,0.0);
}
void init(int width,int height)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */
glShadeModel(GL_SMOOTH);
//glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
//gluPerspective(45.0, (float)width / (float)height, 0.1, 100.0);
//glMatrixMode(GL_MODELVIEW);
}
void TimerFunction(int value)
{
// Redraw the scene with new coordinates
glutPostRedisplay();
glutTimerFunc(3,TimerFunction, 1);
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
/* need both double buffering and z buffer */
int winWidth = 650;
int winHeight = 500;
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE |GLUT_DEPTH);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("3D Cube");
glutDisplayFunc(display);
glutReshapeFunc(myReshape);
glutIdleFunc(spinDisplay);
init(winWidth,winHeight);
glutTimerFunc(33, TimerFunction, 1);
glutMainLoop();
}
楼主,代码按照你的需求改了下, 不过不是很确定“在空闲时绕顶点(1,1,1)X轴方向旋转”这句话的的意思, 好友就是建议楼主在不太清楚某个函数的用法时, 不要乱用, 先查文档看一下究竟是什么意思。
#include <GL/glut.h>
#include <stdio.h>
static GLfloat spin = 36.0;
int singleb, doubleb;
void colorcube(void)
{
// glRotated(30,0,0,1);
glBegin(GL_QUADS);
glColor3f(0.0, 1.0, 0.0);
glVertex3f( 1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f( 1.0, 1.0, 1.0);
glColor3f(1.0, 0.5, 0.0);
glVertex3f( 1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f( 1.0, -1.0, -1.0);
glColor3f(1.0, 0.0, 0.0);
glVertex3f( 1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f( 1.0, -1.0, 1.0);
glColor3f(1.0, 1.0, 0.0);
glVertex3f( 1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f( 1.0, 1.0, -1.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, 1.0);
glColor3f(1.0, 1.0, 0.0);
glVertex3f( 1.0, 1.0, -1.0);
glVertex3f( 1.0, 1.0, 1.0);
glVertex3f( 1.0, -1.0, 1.0);
glVertex3f( 1.0, -1.0, -1.0);
glEnd();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(1.f,1.f,1.f);
glRotatef(spin, 1.f,0.f,0.f);
colorcube();
glPopMatrix();
// glFlush();
glutSwapBuffers();
}
void spinDisplay (void)
{
spin = spin + 1.0;
if (spin > 360.0)
spin = spin - 360.0;
//glutSetWindow(singleb);
//glLoadIdentity();
//glRotatef (spin, 0.0,1.0, 0.0);
//glutPostRedisplay();
//glutSetWindow(doubleb);
//glLoadIdentity();
//glRotatef(spin, 0.0,1.0, 0.0);
//glutPostRedisplay();
// glFlush();
}
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, float(w) / h, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(5.0,5.0,10.0,1.0,1.0,1.0,0.0,1.0,0.0);
}
void init(int width,int height)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */
glShadeModel(GL_SMOOTH);
//glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
//gluPerspective(45.0, (float)width / (float)height, 0.1, 100.0);
//glMatrixMode(GL_MODELVIEW);
}
void TimerFunction(int value)
{
// Redraw the scene with new coordinates
glutPostRedisplay();
glutTimerFunc(3,TimerFunction, 1);
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
/* need both double buffering and z buffer */
int winWidth = 650;
int winHeight = 500;
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE |GLUT_DEPTH);
glutInitWindowSize(winWidth, winHeight);
glutCreateWindow("3D Cube");
glutDisplayFunc(display);
glutReshapeFunc(myReshape);
glutIdleFunc(spinDisplay);
init(winWidth,winHeight);
glutTimerFunc(33, TimerFunction, 1);
glutMainLoop();
}
楼主,代码按照你的需求改了下, 不过不是很确定“在空闲时绕顶点(1,1,1)X轴方向旋转”这句话的的意思, 好友就是建议楼主在不太清楚某个函数的用法时, 不要乱用, 先查文档看一下究竟是什么意思。
#5
应该就是绕这个点旋转,呵呵...
#6
谢谢,我试试~ 我不熟悉C++,又刚学OPENGL,后来用java做了这个习题~
#7
谢谢您的帮助~