PDA

View Full Version : problems drawing with the mouse



pixarrin
05-03-2001, 12:13 AM
Hello friends
I want to program with windows98 and opengl
In my program, i draw some lines (first, i load this lines with differentes names), and after i prick with the mouse in my windows of opengl.
Then, my program, in "pulsaraton" procedure open a little window beneath the point where i prick (glupickmatrix) with my mouse. And my program should sense if there is any line in this little window. If there is any line, then varying hit=1, but my program donīt sense any line. Why?
I post a copy of my program.
Thankīs for helping.

/*
** Simple Color Index Example
*/

#define STRICT
#define BUFFER_LENGTH 64
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <math.h>
#include <stdio.h>
#define A 1
#define B 2
#define C 3
#define D 4
#define E 5
#define F 6


LRESULT APIENTRY WindProc(HWND,UINT,WPARAM,LPARAM);
void pulsaraton (float , float );
void prueba11(GLuint *pselectBuff);


GLfloat x1, y11;
GLfloat aspect;
bool sem, sem1=false, sem2=false, sem3=false, sem4=false, sem5=false, sem6=false;
char *className = "OpenGL";
char *windowName = "Simple Color Index";
int winX = 0, winY = 0;
int winWidth = 300, winHeight =300;
HDC hDC;
HGLRC hGLRC;
HPALETTE hPalette;
HINSTANCE hInst;

void (*idleFunc)(void);

/* Struct used to manage color ramps */
struct colorIndexState {
GLfloat amb[3]; /* ambient color / bottom of ramp */
//luz proveniente de todas las direcciones
GLfloat diff[3]; /* diffuse color / middle of ramp */
//luz proveniente de una direccion y apenas rebota.
//Es absorbida por la superficie
GLfloat spec[3]; /* specular color / top of ramp */
//luz proveniente de una direccion y rebota en
//una direccion preferentemente
GLfloat ratio; /* ratio of diffuse to specular in ramp */
GLint indexes[3]; /* where ramp was placed in palette */
};

/*
** Each entry in this array corresponds to a color ramp in the
** palette. The indexes member of each struct is updated to
** reflect the placement of the color ramp in the palette.
*/
#define NUM_COLORS (sizeof(colors) / sizeof(colors[0]))
struct colorIndexState colors[] = {
{
{ 1.0F, 1.0F, 0.0F },
{ 0.1F, 0.6F, 0.3F },
{ 1.0F, 1.0F, 1.0F },
0.00F, { 0, 0, 0 },
},
{
{ 0.0F, 0.0F, 0.0F },
{ 0.0F, 0.2F, 0.5F },
{ 1.0F, 1.0F, 1.0F },
0.75F, { 0, 0, 0 },
},
{
{ 0.0F, 0.05F, 0.05F },
{ 0.6F, 0.0F, 0.8F },
{ 1.0F, 1.0F, 1.0F },
0.75F, { 0, 0, 0 },
},
};

void
drawTorus(void)

{
int i;
if (i & 1) {
glMaterialiv(GL_FRONT, GL_COLOR_INDEXES, colors[0].indexes);
}
else {
glMaterialiv(GL_FRONT, GL_COLOR_INDEXES, colors[1].indexes);
}


glBegin(GL_LINES);
if (sem) {
glVertex2f(0, 0);//dibuja las lineas normales
glVertex2f(3, -1);
glVertex2f(0, 0);//dibuja las lineas normales
glVertex2f(1,-3);
}
glEnd();

glBegin(GL_LINES);
glVertex2f(0.0, 0.0);
glVertex2f(x1, y11);

glInitNames();
glPushName(0);

glLoadName(A);
glVertex2f(0.0, 0.0);
glVertex2f(2.0, -2.0);

glLoadName(B);
glVertex2f(2.0, -2.0);
glVertex2f(0.0, -2.0);

glLoadName(C);
glVertex2f(2.0, -2.0);
glVertex2f(2.0, 0.0);

glLoadName(D);
glVertex2f(2.0, 0.0);
glVertex2f(0.0, -0.0);

glLoadName(E);
glVertex2f(0.0, 0.0);
glVertex2f(0.0, -2.0);

glLoadName(F);
glVertex2f(0.0, -2.0);
glVertex2f(2.0, 0.0);
glEnd();

glBegin(GL_LINES);
if (sem1){
glVertex2f(0.0, 0.0);
glVertex2f(3.0, -0.0);
}

if (sem2){
glVertex2f(0.0, -1.0);
glVertex2f(3.0, -1.0);
}

if (sem3){
glVertex2f(0.0, -2.0);
glVertex2f(3.0, -2.0);
}

if (sem4){
glVertex2f(0.0, -3.0);
glVertex2f(3.0, -3.0);
}

if (sem5){
glVertex2f(0.0, -0.0);
glVertex2f(5.0, -9.0);
}

glEnd();

glPopMatrix();

return;
}

/************************************************** ***************/


void
pulsaraton (float x, float y)
{

GLuint selectBuff[BUFFER_LENGTH];
GLint hits, viewport[4];

glSelectBuffer(BUFFER_LENGTH, selectBuff);
glGetIntegerv(GL_VIEWPORT, viewport);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glRenderMode(GL_SELECT);
glLoadIdentity();
glTranslatef(-1.0, 1.0, 0.0);
gluPickMatrix(x, y, 0.5, 0.5, viewport);
gluPerspective(45.0f, aspect, 1.0, 3.0);
drawTorus();

hits = glRenderMode(GL_RENDER);

if (hits==1);
prueba11(selectBuff);

glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}


void
prueba11(GLuint *pselectBuff)
{

int id = pselectBuff[3];
switch(id)
{

case 1:
sem1=true;
break;

case 2:
sem2=true;
break;

case 3:
sem3=true;
break;

case 4:
sem4=true;
break;

case 5:
sem5=true;
break;

case 6:
sem6=true;
break;

}
sem=true;
drawTorus();
return;


}

void
setProjection(void)
{


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glTranslatef(-1.0, 1.0, 0.0);

if (winWidth > winHeight)
aspect = (GLfloat)winWidth / (GLfloat) winHeight;
else
aspect = (GLfloat)winHeight / (GLfloat) winWidth;

gluPerspective(45.0f, aspect, 1.0, 3.0);


glMatrixMode(GL_MODELVIEW);
}

void
init(void)
{
GLfloat matShine = 5.00F;
GLfloat light0Pos[4] = { 0.00F, 0.00F, 0.00F, 1.00F };

glClearIndex((GLfloat) colors[2].indexes[1]);//cambia el color del
//fondo
//elige el color del fondo en el mode color-index

setProjection();
glTranslatef(0.0F, 0.0F, -2.5F);//translada los objetos
//glTranslatef(TYPE x, TYPE y, TYPE z);
//translada un objeto x unidades en el eje x.....

glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, matShine);
//define las propiedades del material respecto a la luz
//glMaterialf(GLenum face, GLenum pname, TYPE param);
//face puede ser GL_FRONT, GL_BACK, o GL_FRONT_AND_BACK para
//indicar que cara es aplicada. En pname definimos que propiedad
//es aplicada, y en param definimos el valor de la propiedad
glLightfv(GL_LIGHT0, GL_POSITION, light0Pos);
//glLightfv(GL_enum light, GLenum pname, TYPE param);
//crea una luz, definida por GLenum que puede ser (GL_LIGHT0,
//GL_LIGHT1....o GL_LIGHT7).Las caracteristicas de la luz las
//define pname (puede ser entre otros GL_AMBIENT, GL_DIFUSSE...)
//y el valor de la caracteristica esta en param
glEnable(GL_LIGHT0);


glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
}

void
doRedraw(void)
{
static GLfloat x, y, z;


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glClear(mask) limpia los buffers especificados en mask

glPushMatrix();//mete todas las matrices en curso en el
//primer nivel de la pila


drawTorus();

glPopMatrix();//saca la matriz superior fuera de la pila,
//destruyendose el contenido de esta matriz. La siguiente
//matriz pasa a ser la nueva matriz superior

SwapBuffers(hDC);


}

void
redraw(void)
{
idleFunc = doRedraw;
}

void
resize(void)
{
setProjection();
glViewport(0, 0, winWidth, winHeight);
//define una ventana donde son dibujados los objetos
//glViewport(x, y, Width, Height);
//(x, y) el punto inferior-izquierdo
//Width, Height : la altura y anchura de la ventana
}

/************************************************** ***************/

void
setupPalette(HDC hDC)
{
PIXELFORMATDESCRIPTOR pfd;
LOGPALETTE* pPal;
int pixelFormat = GetPixelFormat(hDC);
int paletteSize;

DescribePixelFormat(hDC, pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);

/*
** Determine if a palette is needed and if so what size.
*/
if (pfd.dwFlags & PFD_NEED_PALETTE &#0124; &#0124;
pfd.iPixelType == PFD_TYPE_COLORINDEX) {
paletteSize = 1 << pfd.cColorBits;
if (paletteSize > 4096) {
paletteSize = 4096;
}
} else {
return;
}

pPal = (LOGPALETTE*)
malloc(sizeof(LOGPALETTE) + paletteSize * sizeof(PALETTEENTRY));
pPal->palVersion = 0x300;
pPal->palNumEntries = paletteSize;

/*
** Fill the logical palette with color ramps.
**
** Set up the logical palette so that it can be realized
** into the system palette as an identity palette.
**
** 1) The default static entries should be present and at the right
** location. The easiest way to do this is to grab them from
** the current system palette.
**
** 2) All non-static entries should be initialized to unique values.
** The easiest way to do this is to ensure that all of the non-static
** entries have the PC_NOCOLLAPSE flag bit set.
*/
{
int numRamps = NUM_COLORS;
int rampSize = (paletteSize - 20) / numRamps;
int extra = (paletteSize - 20) - (numRamps * rampSize);
int i, r;

/*
** Initialize static entries by copying them from the
** current system palette.
*/
GetSystemPaletteEntries(hDC, 0, paletteSize, &pPal->palPalEntry[0]);

/*
** Fill in non-static entries with desired colors.
*/
for (r=0; r<numRamps; ++r) {
int rampBase = r * rampSize + 10;
PALETTEENTRY *pe = &pPal->palPalEntry[rampBase];
int diffSize = (int) (rampSize * colors[r].ratio);
int specSize = rampSize - diffSize;

for (i=0; i<rampSize; ++i) {
GLfloat *c0, *c1;
GLint a;

if (i < diffSize) {
c0 = colors[r].amb;
c1 = colors[r].diff;
a = (i * 255) / (diffSize - 1);
} else {
c0 = colors[r].diff;
c1 = colors[r].spec;
a = ((i - diffSize) * 255) / (specSize - 1);
}

pe[i].peRed = (BYTE) (a * (c1[0] - c0[0]) + 255 * c0[0]);
pe[i].peGreen = (BYTE) (a * (c1[1] - c0[1]) + 255 * c0[1]);
pe[i].peBlue = (BYTE) (a * (c1[2] - c0[2]) + 255 * c0[2]);
pe[i].peFlags = PC_NOCOLLAPSE;
}

colors[r].indexes[0] = rampBase;
colors[r].indexes[1] = rampBase + (diffSize-1);
colors[r].indexes[2] = rampBase + (rampSize-1);
}

/*
** Initialize any remaining non-static entries.
*/
for (i=0; i<extra; ++i) {
int index = numRamps*rampSize+10+i;
PALETTEENTRY *pe = &pPal->palPalEntry[index];

pe->peRed = (BYTE) 0;
pe->peGreen = (BYTE) 0;
pe->peBlue = (BYTE) 0;
pe->peFlags = PC_NOCOLLAPSE;
}
}

hPalette = CreatePalette(pPal);
free(pPal);

if (hPalette) {
SelectPalette(hDC, hPalette, FALSE);
RealizePalette(hDC);
}
}

void
setupPixelFormat(HDC hDC)
{
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), /* size of this pfd */
1, /* version num */
PFD_DRAW_TO_WINDOW | /* support window */
PFD_SUPPORT_OPENGL | /* support OpenGL */
PFD_DOUBLEBUFFER, /* support double-buffering */
PFD_TYPE_COLORINDEX, /* color index mode */
8, /* 8-bit color depth */
0, 0, 0, 0, 0, 0, /* color bits (ignored) */
0, /* no alpha buffer */
0, /* alpha bits (ignored) */
0, /* no accumulation buffer */
0, 0, 0, 0, /* accum bits (ignored) */
16, /* depth buffer */
0, /* no stencil buffer */
0, /* no auxiliary buffers */
PFD_MAIN_PLANE, /* main layer */
0, /* reserved */
0, 0, 0, /* no layer, visible, damage masks */
};
int SelectedPixelFormat;
BOOL retVal;

SelectedPixelFormat = ChoosePixelFormat(hDC, &pfd);
if (SelectedPixelFormat == 0) {
MessageBox(WindowFromDC(hDC),
"ChoosePixelFormat failed\n"
"This application works best with an 8-bit\n"
"(256 color) display mode\n",
"Error",
MB_ICONERROR | MB_OK);
exit(1);
}

retVal = SetPixelFormat(hDC, SelectedPixelFormat, &pfd);
if (retVal != TRUE) {
MessageBox(WindowFromDC(hDC), "SetPixelFormat failed", "Error",
MB_ICONERROR | MB_OK);
exit(1);
}
}


//*****************************//


int APIENTRY
WinMain(
HINSTANCE hCurrentInst,//hThisInst,
HINSTANCE hPreviousInst,// hPrevInst,
LPSTR lpszCmdLine,// lpszArgs,
int nCmdShow)// nWinMode)
{

WNDCLASS wndClass;
HWND hWnd;
MSG msg;
//HANDLE hAccel;


/* Define and register a window class */
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WindProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hCurrentInst;
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.lpszMenuName = "MYMENU";
wndClass.lpszClassName = className;

if (!RegisterClass(&wndClass)) return 0;


/* Figure out a default size for the window */
winWidth = GetSystemMetrics(SM_CYSCREEN) / 3;
winHeight = GetSystemMetrics(SM_CYSCREEN) / 3;

/* Create a window of the previously defined class */
hWnd = CreateWindow(
className, /* Window class's name */
windowName, /* Title bar text */
WS_OVERLAPPEDWINDOW | /* The window's style */
WS_CLIPCHILDREN |
WS_CLIPSIBLINGS,
winX, winY, /* Position */
winWidth, winHeight, /* Size */
NULL, /* Parent window's handle */
NULL, /* Menu handle */
hCurrentInst, /* Instance handle */
NULL); /* No additional data */

/* Map the window to the screen */
ShowWindow(hWnd, nCmdShow);

/* Force the window to repaint itself */
UpdateWindow(hWnd);


/* Process Messages */
while (1) {
/* execute the idle function while there are no messages to process */
while (idleFunc &&
PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) == FALSE)
{
(*idleFunc)();
}
if (GetMessage(&msg, NULL, 0, 0) != TRUE) {
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return msg.wParam;
}


//*****************************//
LRESULT APIENTRY
WindProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{

switch (message) {



case WM_CREATE:
/*
** Set up for OpenGL rendering. Bind the rendering context to
** the same device context that the palette will be selected into.
*/
hDC = GetDC(hWnd);
setupPixelFormat(hDC);
setupPalette(hDC);
hGLRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hGLRC);
init();
idleFunc = doRedraw;
return 0;

case WM_DESTROY:
/*
** Finish OpenGL rendering.
*/
idleFunc = NULL;
if (hGLRC) {
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hGLRC);
}
ReleaseDC(hWnd, hDC);
PostQuitMessage(0);
return 0;
case WM_SIZE:
if (hGLRC) {
winWidth = (int) LOWORD(lParam);
winHeight = (int) HIWORD(lParam);

resize();
return 0;
}

case WM_PALETTECHANGED:
/*
** Update palette mapping if this *is not* the active window.
*/
if (hGLRC && hPalette && (HWND) wParam != hWnd) {
UnrealizeObject(hPalette);
SelectPalette(hDC, hPalette, FALSE);
RealizePalette(hDC);
redraw();
return 0;
}
break;

case WM_QUERYNEWPALETTE:
/*
** Update palette mapping if this *is* the active window.
*/
if (hGLRC && hPalette) {
UnrealizeObject(hPalette);
SelectPalette(hDC, hPalette, FALSE);
RealizePalette(hDC);
redraw();
return TRUE;
}

break;

case WM_PAINT:
/*
** Update the window. Don't use the device context returned by
** BeginPaint as it won't have the right palette selected into it.
*/
if (hGLRC) {
PAINTSTRUCT ps;

BeginPaint(hWnd, &ps);
redraw();
EndPaint(hWnd, &ps);
return 0;
}
break;


case WM_RBUTTONDOWN:
GLfloat d, e;


d = LOWORD(lParam);
d=d/83;
x1=d;

e = HIWORD(lParam);
e=e/83;

y11=-e;
pulsaraton(x1, y11);
break;


}

/* Deal with any unprocessed messages */
return DefWindowProc(hWnd, message, wParam, lParam);
}