1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
| #include <iostream>
#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
const int WIN_X = 800;
const int WIN_Y = 600;
const int BUFSIZE = 1024;
int sTime;
int cTime;
int eTime;
bool go = true;
int mx = 0;
int my = 0;
bool click;
SDL_Surface *screen;
SDL_Event event;
void initGL()
{
SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetCaption("OpenGL Rox", NULL);
screen = SDL_SetVideoMode(WIN_X, WIN_Y, 32, SDL_OPENGL);
glEnable(GL_DEPTH_TEST);
SDL_WarpMouse(screen->w / 2, screen->h / 2);
}
void taketime(bool cas)
{
if (!cas)
sTime = SDL_GetTicks();
else
{
cTime = SDL_GetTicks();
eTime = cTime - sTime;
if (eTime < 10)
SDL_Delay(10 - eTime);
}
}
void drawsquare(bool cas)
{
if (cas)
glLoadName(1);
glColor3ub(255, 255, 255);
glBegin(GL_QUADS);
glVertex3d(0, 0, 0);
glVertex3d(0, 10, 0);
glVertex3d(10, 10, 0);
glVertex3d(10, 0, 0);
glEnd();
if (cas)
glLoadName(2);
glColor3ub(255, 0, 0);
glBegin(GL_QUADS);
glVertex3d(3, 3, 0.2);
glVertex3d(3, 7, 0.2);
glVertex3d(7, 7, 0.2);
glVertex3d(7, 3, 0.2);
glEnd();
}
void processHits(GLint hits, GLuint *buffer)
{
unsigned int i;
unsigned int j;
GLuint names;
GLuint *ptr;
std::cout << "hits = " << hits << std::endl;
ptr = (GLuint *)buffer;
for (i = 0; i < hits; i++)
{
names = *ptr;
std::cout << " number of names for this hit = " << names << std::endl;
ptr++;
std::cout << " z1 is " << (float) *ptr / 0x7fffffff;
ptr++;
std::cout << " z2 is " << (float) *ptr / 0x7fffffff << std::endl;
ptr++;
std::cout << " NAMES ";
for (j = 0; j < names; j++)
{
std::cout << *ptr << std::endl;
ptr++;
}
std::cout << std::endl;
}
std::cout << "\n-----------------------------------------\n" << std::endl;;
}
void picking()
{
glPushAttrib(GL_ALL_ATTRIB_BITS);
GLuint selectBuf[BUFSIZE];
GLint hits = 0;
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
glSelectBuffer(BUFSIZE, selectBuf);
(void) glRenderMode(GL_SELECT);
glInitNames();
glPushName(0);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPickMatrix((GLdouble) mx, (GLdouble) (viewport[3] - my), 2.0, 2.0, viewport);
gluPerspective(70, float(WIN_X / WIN_Y), 0.1, 1000);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
drawsquare(true);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix ();
hits = glRenderMode(GL_RENDER);
processHits(hits, selectBuf);
glPopAttrib();
}
void draw()
{
glViewport(0, 0, WIN_X, WIN_Y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70, float(WIN_X / WIN_Y), 0.1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
gluLookAt(-10, -10, 10, 0, 0, 0, 0, 0, 1);
drawsquare(false);
if (click)
picking();
glFlush();
SDL_GL_SwapBuffers();
}
void checkevents()
{
if (event.type == SDL_QUIT)
go = false;
else if (event.type == SDL_KEYDOWN)
if (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_DELETE)
go = false;
if (event.type == SDL_MOUSEMOTION)
{
mx = event.motion.x;
my = event.motion.y;
}
//MOUSE
if (event.type == SDL_MOUSEBUTTONDOWN)
{
if (event.button.button == SDL_BUTTON_LEFT)
click = true;
}
else if (event.type == SDL_MOUSEBUTTONUP)
{
if (event.button.button == SDL_BUTTON_LEFT)
click = false;
}
if (click)
{
std::cout << "x : " << mx << std::endl;
std::cout << "y : " << my << std::endl << std::endl;
}
}
int main(int argc, char **argv)
{
#ifdef WIN32
freopen("CON","w", stderr);
freopen("CON","w", stdout);
#endif
initGL();
while (go)
{
taketime(false);
while (SDL_PollEvent(&event))
checkevents();
draw();
taketime(true);
}
SDL_Quit();
return (0);
} |
Partager