?? test.cpp
字號:
//ArrayList OpenGL BALL Test
//By Justin Lee 2008-11
//OS Defines
#ifdef WIN32
#include <windows.h>
//OpenGL lib
#pragma comment(lib,"OpenGL32.lib")
#pragma comment(lib,"GlU32.lib")
#pragma comment(lib,"GlAux.lib")
#else
#include <glos.h>
#define __stdcall
#endif
//standard library
#include<stdio.h>
#include<stdlib.h>
#include<memory>
#include<math.h>
#include<time.h>
using namespace std;
//openGL interfaces
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>
#ifdef WIN32
#define auxInitWindow auxInitWindowA
#endif
//BALL Object would managed by Array
typedef struct _BALL_{
struct position{
GLfloat xyz[3];
} pos;
GLfloat r;
struct RGBA{
GLfloat rgba[4];
} color;
}BALL,*PBALL;
#include"ArrayList.h"
ArrayList<BALL> balllist;
//Add a BALL to ArrayList
void ArrayListTestAdd(GLfloat x,GLfloat y)
{
PBALL pb=(PBALL)malloc(sizeof(BALL));
memset(pb,0,sizeof(pb));
GLfloat f[6];
for(int i=0;i<6;i++){
f[i]=(GLfloat)((GLfloat)(rand()%1000)/1000.0f)*((rand()%2)?-1.0f:1.0f);
}
pb->pos.xyz[0]=x,pb->pos.xyz[1]=y,pb->pos.xyz[2]=-0.8f+f[0];
pb->r=fabsf(f[1]);
pb->color.rgba[0]=fabsf(f[2]),pb->color.rgba[1]=fabsf(f[3]),pb->color.rgba[2]=fabsf(f[4]);
pb->color.rgba[3]=fabsf(f[5])<0.1f?0.1f:fabsf(f[5]);
balllist.Insert(balllist.GetLength(),1,&pb,1);
printf("Add Ball COLOR:RGBA = ( %f ,%f, %f, %f )\nRADIUS:r = %f \nAt POSITION:XYZ = ( %f ,%f, %f )\n",
pb->color.rgba[0],pb->color.rgba[1],pb->color.rgba[2],pb->color.rgba[3],
pb->r,
pb->pos.xyz[0],pb->pos.xyz[1],pb->pos.xyz[2]
);
}
//Del a BALL to ArrayList
void ArrayListTestDel()
{
BALL* pb=(BALL*)malloc(sizeof(BALL));
unsigned long i,l;
l=balllist.GetLength();
if(l<=0){
free(pb);
return;
}
i=rand()%l;
*pb=balllist[i];
balllist.Remove(i,1);
printf("Del Ball COLOR:RGBA = ( %f ,%f, %f, %f )\nRADIUS:r = %f \nAt POSITION:XYZ = ( %f ,%f, %f )\n",
pb->color.rgba[0],pb->color.rgba[1],pb->color.rgba[2],pb->color.rgba[3],
pb->r,
pb->pos.xyz[0],pb->pos.xyz[1],pb->pos.xyz[2]
);
free(pb);
balllist.Refresh();
}
void myinit(void);
void __stdcall display(void);
void __stdcall myReshape(GLsizei w, GLsizei h);
void __stdcall addballFunc(AUX_EVENTREC *Event);
void __stdcall delballFunc(AUX_EVENTREC *Event);
void myinit(void)
{
GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat position[] = { 0.0, 3.0, 2.0, 0.0 };
GLfloat lmodel_ambient[] = { 0.4, 0.4, 0.4, 1.0 };
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glClearColor(0.0, 0.1, 0.1, 0.0);
}
void __stdcall display(void)
{
GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat low_shininess[] = { 5.0 };
GLfloat *pos = NULL;
GLfloat *mat_diffuse = NULL;
GLfloat r;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
unsigned long l,i;
l=balllist.GetLength();
for(i=0;i<l;i++){
pos=balllist[i].pos.xyz;
mat_diffuse=balllist[i].color.rgba;
r=balllist[i].r;
glPushMatrix();
glTranslatef (pos[0], pos[1], pos[2]);
glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
auxSolidSphere(r);
glPopMatrix();
}
glFlush();
}
#define WIDTH 640
#define HEIGHT 480
void __stdcall myReshape(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= (h * 2))
glOrtho (-6.0, 6.0, -3.0*((GLfloat)h*2)/(GLfloat)w,
3.0*((GLfloat)h*2)/(GLfloat)w, -10.0, 10.0);
else
glOrtho (-6.0*(GLfloat)w/((GLfloat)h*2),
6.0*(GLfloat)w/((GLfloat)h*2), -3.0, 3.0, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
}
void __stdcall addballFunc(AUX_EVENTREC *Event)
{
GLint x,y;
x=Event->data[AUX_MOUSEX];
y=Event->data[AUX_MOUSEY];
GLfloat fx,fy;
fx=(((GLfloat)x*10.0f)/(GLfloat)WIDTH)-5.0f;
fy=-((((GLfloat)y*6.0f)/(GLfloat)HEIGHT)-3.0f);
ArrayListTestAdd(fx,fy);
}
void __stdcall delballFunc(AUX_EVENTREC *Event)
{
ArrayListTestDel();
}
void main(void)
{
printf("-->Welcome to ArrayList Demo App!!!<--\nMouse(LEFT:ADD BALL/RIGHT:REMOVE BALL)\n");
time_t t;
srand((unsigned int)time(&t));
auxInitDisplayMode (AUX_SINGLE | AUX_RGBA);
auxInitPosition (0, 0, WIDTH, HEIGHT);
auxInitWindow ("ArrayList OpenGL Crash!");
myinit();
auxMouseFunc(AUX_LEFTBUTTON,AUX_MOUSEUP,addballFunc);
auxMouseFunc(AUX_RIGHTBUTTON,AUX_MOUSEUP,delballFunc);
auxReshapeFunc (myReshape);
auxMainLoop(display);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -