24,855
社区成员
发帖
与我相关
我的任务
分享
void Reshape(int w1, int h1) {
WinWidth = w1;
WinHeight = h1;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w1, h1);
gluPerspective(70, 1.0*w1 / h1, 1, 1000);//透视
glOrtho(-30.0f, 30.0f, -30.0f, 30.0f, -60.0f, 60.0f);//正交
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
#include "stdafx.h"
#include <GL/glut.h>
#include <math.h>
#include <time.h>
#include <stdio.h>
int WinWidth, WinHeight;
double posX, posY, posZ, viewX, viewY, viewZ, upX, upY, upZ;
const GLfloat PI = 3.141515926;
void setAll(double px, double py, double pz,
double vx, double vy, double vz,
double ux, double uy, double uz)
{
posX = px;
posY = py;
posZ = pz;
viewX = vx;
viewY = vy;
viewZ = vz;
upX = ux;
upY = uy;
upZ = uz;
}
void setLookAt() {
gluLookAt(posX, posY, posZ,
viewX, viewY, viewZ,
upX, upY, upZ);
}
void setPos(double x, double y, double z) {
posX = x;
posY = y;
posZ = z;
}
void DisplayBox() {
GLfloat x, y, angle;
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0f, 7.5f, 0.0f);
int ipvot = 0;
for (angle = 0.0f; angle <= (4.0f*PI); angle += (PI / 8.0f)) {
x = 5.0f*sin(angle);
y = 5.0f*cos(angle);
if (ipvot % 2 == 0) glColor3f(1.0f, 1.0f, 0.0f);
else glColor3f(0.0f, 1.0f, 1.0f);
ipvot++;
glVertex3f(x, 0.0f, y);
}
glEnd();
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0f, 0.0f, 0.0f);
for (angle = 0.0f; angle <= (4.0f*PI); angle += (PI / 8.0f)) {
x = 5.0f*sin(angle);
y = 5.0f*cos(angle);
if (ipvot % 2 == 0) glColor3f(0.5f, 0.0f, 0.5f);
else glColor3f(1.0f, 0.0f, 1.0f);
ipvot++;
glVertex3f(x, 0.0f, y);
}
glEnd();
glBegin(GL_TRIANGLE_FAN);
glVertex3f(10.0f, 7.5f, 0.0f);
int ipvot1 = 0;
for (angle = 0.0f; angle <= (4.0f*PI); angle += (PI / 8.0f)) {
x = 5.0f*sin(angle);
y = 5.0f*cos(angle);
if (ipvot1 % 2 == 0) glColor3f(1.0f, 1.0f, 0.0f);
else glColor3f(0.0f, 1.0f, 1.0f);
ipvot1++;
glVertex3f(10.0f+x, 0.0f, y);
}
glEnd();
glBegin(GL_TRIANGLE_FAN);
glVertex3f(10.0f, 0.0f, 0.0f);
for (angle = 0.0f; angle <= (4.0f*PI); angle += (PI / 8.0f)) {
x = 5.0f*sin(angle);
y = 5.0f*cos(angle);
if (ipvot % 2 == 0) glColor3f(0.5f, 0.0f, 0.5f);
else glColor3f(1.0f, 0.0f, 1.0f);
ipvot++;
glVertex3f(10.0f+x, 0.0f, y);
}
glEnd();
glBegin(GL_TRIANGLE_FAN);
glVertex3f(-10.0f, 7.5f, 0.0f);
for (angle = 0.0f; angle <= (4.0f*PI); angle += (PI / 8.0f)) {
x = 5.0f*sin(angle);
y = 5.0f*cos(angle);
if (ipvot1 % 2 == 0) glColor3f(1.0f, 1.0f, 0.0f);
else glColor3f(0.0f, 1.0f, 1.0f);
ipvot1++;
glVertex3f(-10.0f + x, 0.0f, y);
}
glEnd();
glBegin(GL_TRIANGLE_FAN);
glVertex3f(-10.0f, 0.0f, 0.0f);
for (angle = 0.0f; angle <= (4.0f*PI); angle += (PI / 8.0f)) {
x = 5.0f*sin(angle);
y = 5.0f*cos(angle);
if (ipvot % 2 == 0) glColor3f(0.5f, 0.0f, 0.5f);
else glColor3f(1.0f, 0.0f, 1.0f);
ipvot++;
glVertex3f(-10.0f + x, 0.0f, y);
}
glEnd();
}
void DrawGrid() {
glLineWidth(2);
glColor3f(0.0, 0.0, 1.0);
for (int i = -100; i <= 100; i += 10) {
glBegin(GL_LINES);
glVertex3f(i, 0.0f, -100.0f);
glVertex3f(i, 0.0f, 100.f);
glEnd();
glBegin(GL_LINES);
glVertex3f(-100.0f, 0.0f, i);
glVertex3f(100.0f, 0.0f, i);
glEnd();
}
}
void Display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
DrawGrid();
DisplayBox();
glutSwapBuffers();
}
void update() {
glLoadIdentity();
setLookAt();
glutPostRedisplay();
}
void Reshape(int w1, int h1) {
WinWidth = w1;
WinHeight = h1;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w1, h1);
gluPerspective(70, 1.0*w1 / h1, 1, 1000);
//glOrtho(-30.0f, 30.0f, -30.0f, 30.0f, -60.0f, 60.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
setAll(20, 1.75, 0, 0, 1.75, 0, 0, 1, 0);
}
void ProcessKeyBoard(int key, int x, int y) {
static double delta;
if (key == GLUT_KEY_LEFT) {
delta += 0.03;
setPos(20 * cos(delta), posY, 20 * sin(delta));
}
else if (key == GLUT_KEY_RIGHT) {
delta -= 0.03;
setPos(20 * cos(delta), posY, 20 * sin(delta));
}
else if (key == GLUT_KEY_UP) {
delta += 0.03;
setPos(20 * cos(delta), 40 * sin(delta), posZ);
}
else if (key == GLUT_KEY_DOWN) {
delta -= 0.03;
setPos(20 * cos(delta), 40 * sin(delta), posZ);
}
//setPos(20 * cos(delta), posY, 20 * sin(delta));
}
void myMouse(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
}
if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
{
glOrtho(-30.0f, 30.0f, -30.0f, 30.0f, -60.0f, 60.0f);
}
glutPostRedisplay();
}
int main(int argc, char *argv[]) {
WinWidth = 400;
WinHeight = 400;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(WinWidth, WinHeight);
glutCreateWindow("Rita's OpenGL");
glutReshapeFunc(&Reshape);
//glutMouseFunc(&myMouse);
glutIdleFunc(&update);
glutDisplayFunc(&Display);
glutSpecialFunc(ProcessKeyBoard);//写 char key和int key 的区别 glutKeyboardFunc &
init();
glutMainLoop();
return 0;
}
#include "stdafx.h"
#include <GL/glut.h>
#include <math.h>
#include <time.h>
#include <stdio.h>
int WinWidth, WinHeight;
double posX, posY, posZ, viewX, viewY, viewZ, upX, upY, upZ;
const GLfloat PI = 3.141515926;
void setAll(double px, double py, double pz,
double vx, double vy, double vz,
double ux, double uy, double uz)
{
posX = px;
posY = py;
posZ = pz;
viewX = vx;
viewY = vy;
viewZ = vz;
upX = ux;
upY = uy;
upZ = uz;
}
void setLookAt() {
gluLookAt(posX, posY, posZ,
viewX, viewY, viewZ,
upX, upY, upZ);
}
void setPos(double x, double y, double z) {
posX = x;
posY = y;
posZ = z;
}
void DisplayBox() {
GLfloat x, y, angle;
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0f, 7.5f, 0.0f);
int ipvot = 0;
for (angle = 0.0f; angle <= (4.0f*PI); angle += (PI / 8.0f)) {
x = 5.0f*sin(angle);
y = 5.0f*cos(angle);
if (ipvot % 2 == 0) glColor3f(1.0f, 1.0f, 0.0f);
else glColor3f(0.0f, 1.0f, 1.0f);
ipvot++;
glVertex3f(x, 0.0f, y);
}
glEnd();
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0f, 0.0f, 0.0f);
for (angle = 0.0f; angle <= (4.0f*PI); angle += (PI / 8.0f)) {
x = 5.0f*sin(angle);
y = 5.0f*cos(angle);
if (ipvot % 2 == 0) glColor3f(0.5f, 0.0f, 0.5f);
else glColor3f(1.0f, 0.0f, 1.0f);
ipvot++;
glVertex3f(x, 0.0f, y);
}
glEnd();
glBegin(GL_TRIANGLE_FAN);
glVertex3f(10.0f, 7.5f, 0.0f);
int ipvot1 = 0;
for (angle = 0.0f; angle <= (4.0f*PI); angle += (PI / 8.0f)) {
x = 5.0f*sin(angle);
y = 5.0f*cos(angle);
if (ipvot1 % 2 == 0) glColor3f(1.0f, 1.0f, 0.0f);
else glColor3f(0.0f, 1.0f, 1.0f);
ipvot1++;
glVertex3f(10.0f+x, 0.0f, y);
}
glEnd();
glBegin(GL_TRIANGLE_FAN);
glVertex3f(10.0f, 0.0f, 0.0f);
for (angle = 0.0f; angle <= (4.0f*PI); angle += (PI / 8.0f)) {
x = 5.0f*sin(angle);
y = 5.0f*cos(angle);
if (ipvot % 2 == 0) glColor3f(0.5f, 0.0f, 0.5f);
else glColor3f(1.0f, 0.0f, 1.0f);
ipvot++;
glVertex3f(10.0f+x, 0.0f, y);
}
glEnd();
glBegin(GL_TRIANGLE_FAN);
glVertex3f(-10.0f, 7.5f, 0.0f);
for (angle = 0.0f; angle <= (4.0f*PI); angle += (PI / 8.0f)) {
x = 5.0f*sin(angle);
y = 5.0f*cos(angle);
if (ipvot1 % 2 == 0) glColor3f(1.0f, 1.0f, 0.0f);
else glColor3f(0.0f, 1.0f, 1.0f);
ipvot1++;
glVertex3f(-10.0f + x, 0.0f, y);
}
glEnd();
glBegin(GL_TRIANGLE_FAN);
glVertex3f(-10.0f, 0.0f, 0.0f);
for (angle = 0.0f; angle <= (4.0f*PI); angle += (PI / 8.0f)) {
x = 5.0f*sin(angle);
y = 5.0f*cos(angle);
if (ipvot % 2 == 0) glColor3f(0.5f, 0.0f, 0.5f);
else glColor3f(1.0f, 0.0f, 1.0f);
ipvot++;
glVertex3f(-10.0f + x, 0.0f, y);
}
glEnd();
}
void DrawGrid() {
glLineWidth(2);
glColor3f(0.0, 0.0, 1.0);
for (int i = -100; i <= 100; i += 10) {
glBegin(GL_LINES);
glVertex3f(i, 0.0f, -100.0f);
glVertex3f(i, 0.0f, 100.f);
glEnd();
glBegin(GL_LINES);
glVertex3f(-100.0f, 0.0f, i);
glVertex3f(100.0f, 0.0f, i);
glEnd();
}
}
void Display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
DrawGrid();
DisplayBox();
glutSwapBuffers();
}
void update() {
glLoadIdentity();
setLookAt();
glutPostRedisplay();
}
void Reshape(int w1, int h1) {
WinWidth = w1;
WinHeight = h1;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w1, h1);
gluPerspective(70, 1.0*w1 / h1, 1, 1000);
//glOrtho(-30.0f, 30.0f, -30.0f, 30.0f, -60.0f, 60.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
setAll(20, 1.75, 0, 0, 1.75, 0, 0, 1, 0);
}
void ProcessKeyBoard(int key, int x, int y) {
static double delta;
if (key == GLUT_KEY_LEFT) {
delta += 0.03;
setPos(20 * cos(delta), posY, 20 * sin(delta));
}
else if (key == GLUT_KEY_RIGHT) {
delta -= 0.03;
setPos(20 * cos(delta), posY, 20 * sin(delta));
}
else if (key == GLUT_KEY_UP) {
delta += 0.03;
setPos(20 * cos(delta), 40 * sin(delta), posZ);
}
else if (key == GLUT_KEY_DOWN) {
delta -= 0.03;
setPos(20 * cos(delta), 40 * sin(delta), posZ);
}
//setPos(20 * cos(delta), posY, 20 * sin(delta));
}
void myMouse(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
}
if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
{
glOrtho(-30.0f, 30.0f, -30.0f, 30.0f, -60.0f, 60.0f);
}
glutPostRedisplay();
}
int main(int argc, char *argv[]) {
WinWidth = 400;
WinHeight = 400;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(WinWidth, WinHeight);
glutCreateWindow("Rita's OpenGL");
glutReshapeFunc(&Reshape);
//glutMouseFunc(&myMouse);
glutIdleFunc(&update);
glutDisplayFunc(&Display);
glutSpecialFunc(ProcessKeyBoard);//写 char key和int key 的区别 glutKeyboardFunc &
init();
glutMainLoop();
return 0;
}
int useP=0;//设置一个全局变量
useP=1-useP;//切换投影方式
if (useP) {//使用透视投影
//...
} else {//使用正交投影
//...
}