查看: 1172|回复: 10
|
graphic programming(OpenGL) in C++/C[已解决]
[复制链接]
|
|
不好意思,忘了交代。。
已决解,如要source code请索取
-----------------------------------------------------------------------------------------------------------------------------------
小弟的老师最近分了一个project给小弟,问题如下,但小弟学艺不精,看了很久都不会,请各位大大看看,教教小弟,或者给小弟有关的讯息/网站,感激不尽,谢谢!!
write an interactive application program that can perform the following :
--draw four non-overlapping polygon on the display window;with its position and size determined by the mouse cursor(ie. left button is used to input the vertices of polygon).keep all the vertices of each polygon in an arry.
--provide function to save all the drawn polygon in a data file
--provide function to read data file and display them
(HINT:to save, keep the vertices of each polygon in a data file;to display,read the vertices from the data file and draw them on the screen)
--allow user to select any one of the drawn polygon.(HINT :this can be done by, eg. right clicking the mouse inside the polygon region.).The selected polygon edges should change color or dispaly small visible dots at the vertices to show that the polygon has been selected.
--perform transformation on the selected polygon. allow user to translate,rotate and scale the polygon. the transformed polygon should be displayed on a subwindow (hint: use GLUT function glutCreateSubWindow(WindowID,xBottomLeft, width, height)).Users should be allowed to input parameters required for the transformation
[ 本帖最后由 心的太平洋 于 30-7-2008 09:23 AM 编辑 ] |
|
|
|
|
|
|
|

楼主 |
发表于 20-10-2007 02:11 AM
|
显示全部楼层
STEP 1:
小弟可以作到的是把5个不同的rectangle同时出现(以前的功课做到的,而且是auto-generate 的),但这次的是polygon, tat mean 他要的是不同的形状同时出现,还要user自己用老鼠determine的,以下小弟上次功课的coding,能改改吗?变成我要的吗?
#include <GL/glut.h>
#include <cstdlib>
#include <ctime>
GLsizei winWidth=600, winHeight=600;
int x[10];
int y[10];
void init (void)
{
glClearColor (1.0, 1.0, 1.0, 0.0);
glLoadIdentity();
glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0,GLdouble(winWidth),0.0,GLdouble(winHeight));
}
void drawRect (void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glRecti(x[0],y[0],x[1],y[1]);
glColor3f(0.0, 1.0, 0.0);
glRecti(x[2] + 310,y[2],x[3] + 310,y[3]);
glColor3f(0.0, 0.0, 1.0);
glRecti(x[4]+520, y[4], x[5]+520, y[5]);
glColor3f(1.0, 1.0, 0.0);
glRecti(x[6] ,y[6] + 400,x[7], y[7] +400);
glColor3f(1.0, 0.0, 1.0);
glRecti(x[8]+ 520, y[8]+ 200, x[9]+520, y[9]+200);
glFlush ( );
}
void generatorFunc (void)
{
srand (time(NULL));
x[0]=rand() % 300;
y[0]=rand() % 390;
x[1]=rand() % 300;
y[1]=rand() % 390;
x[2]=rand() % 200;
y[2]=rand() % 390;
x[3]=rand() % 200;
y[3]=rand() % 390;
x[4]=rand() % 80;
y[4]=rand() % 190;
x[5]=rand() % 80;
y[5]=rand() % 190;
x[6]=rand() % 510;
y[6]=rand() % 200;
x[7]=rand() % 510;
y[7]=rand() % 200;
x[8]=rand() % 80;
y[8]=rand() % 400;
x[9]=rand() % 80;
y[9]=rand() % 400;
}
void winReshapeFcn(GLint newWidth, GLint newHeight)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, GLdouble(newWidth), 0.0, GLdouble(newHeight));
glClear(GL_COLOR_BUFFER_BIT);
winWidth=newWidth;
winHeight=newHeight;
}
void main (int argc, char** argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition (200, 100);
glutInitWindowSize (winWidth, winHeight);
glutCreateWindow ("A110799");
init();
generatorFunc();
glutDisplayFunc (drawRect);
glutReshapeFunc(winReshapeFcn);
glutMainLoop ( );
} |
|
|
|
|
|
|
|
发表于 20-10-2007 11:59 AM
|
显示全部楼层
我以前有做过类似的
不是很记得,不过你的polygon要用detect overlapping,要用vector cross product
比如说你有2条线,分别由A,B,C,D组成,要怎样detect overlap? 当overlap时,C,D一定在A,B线条的不同方向
所以,你可以这样test:
(Vector A-B cross product Vector AC ) * (Vector A-B cross product Vector AD ) > 0
如果 > 0,就代表没有overlap
因为 (Vector AB cross product Vector AC ) 会得到一个 positive or negative 的value,这个value是depends on vector 的方向
Cross product A X B = |A||B|sin (theta) = (a1 i + a2 j ) x (b1 i + b2 j )
所以,你的vertice要有x,y coordinates |
|
|
|
|
|
|
|

楼主 |
发表于 20-10-2007 11:24 PM
|
显示全部楼层
谢谢tanhy,但我还做到那边,我的plan是先做出能画polygon先,以下是我从书本找到的,可以用老鼠画polyline,我想原则是没变的,但想不出如何能把它变成画polygon的,不懂怎样从GL_POLYLINES写成GL_POLYGON
#include <GL/glut.h>
GLsizei winWidth = 400, winHeight = 300; //initial display-window size
GLint ptCtr = 0;
class scrPt
{
public:
GLint x, y;
};
void init (void)
{
glClearColor (0.0, 0.0, 1.0, 1.0); //set display-window color to blue
glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0, 200.0, 0.0, 150.0);
}
void displayFcn (void)
{
glClear (GL_COLOR_BUFFER_BIT);
}
void winReshapeFcn (GLint newWidth, GLint newHeight)
{
/* reset viewport and projection parameter */
glViewport (0, 0, newWidth,newHeight);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluOrtho2D (0.0, GLdouble (newWidth), 0.0, GLdouble (newHeight));
/* Reset display-window size parameter. */
winWidth = newWidth;
winHeight = newHeight;
}
void drawLineSegment (scrPt endPt1, scrPt endPt2)
{
glBegin (GL_LINES);
glVertex2i (endPt1.x, endPt1.y);
glVertex2i (endPt2.x, endPt2.y);
glEnd();
}
void polyline (GLint button, GLint action, GLint xMouse, GLint yMouse)
{
static scrPt endPt1, endPt2;
if (ptCtr == 0)
{
if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)
{
endPt1.x = xMouse;
endPt1.y = winHeight - yMouse;
ptCtr = 1;
}
else
if (button == GLUT_RIGHT_BUTTON)
exit(0);
}
else
if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)
{
endPt2.x = xMouse;
endPt2.y = winHeight - yMouse;
drawLineSegment (endPt1, endPt2);
endPt1 = endPt2;
}
else
if (button == GLUT_RIGHT_BUTTON)
exit(0);
glFlush();
}
void main (int argc, char** argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition (100,100);
glutInitWindowSize (winWidth, winHeight);
glutCreateWindow ("Draw Interactive Polyline");
init();
glutDisplayFunc(displayFcn);
glutReshapeFunc(winReshapeFcn);
glutMouseFunc(polyline);
glutMainLoop ();
} |
|
|
|
|
|
|
|
发表于 20-10-2007 11:54 PM
|
显示全部楼层
|
|
|
|
|
|
|

楼主 |
发表于 21-10-2007 03:03 AM
|
显示全部楼层
原帖由 tanhy 于 20-10-2007 11:54 PM 发表 
找书太慢了,去Google吧
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=02
谢谢你,很好用!! |
|
|
|
|
|
|
|

楼主 |
发表于 26-10-2007 02:01 AM
|
显示全部楼层
急急急急急急!!!!
小弟还欠一个method罢了,那就是怎样select d polygon which i had draw
干嘛没人教小弟。。。。。
有人可以给我idea吗?急急急急急急急急急 |
|
|
|
|
|
|
|
发表于 26-10-2007 01:05 PM
|
显示全部楼层
|
|
|
|
|
|
|

楼主 |
发表于 26-10-2007 02:11 PM
|
显示全部楼层
明白一点点,但我有四个polygon噢。。
我的意思是
当user画了四个polygon,我要allow user to select one of d polygon..then do d transformation on d polygon |
|
|
|
|
|
|
|
发表于 26-10-2007 07:42 PM
|
显示全部楼层
create a link list of polygon objects
然后run through link list, 每一个polygon object call hit test
然后你就知道 user 选那一个 polygon 了
接下来要做什么看你自己了 |
|
|
|
|
|
|
|

楼主 |
发表于 27-10-2007 03:09 PM
|
显示全部楼层
谢谢你tanhy,我找到办法做了,大部分要完成了,哈哈。。 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|