[英]draw a line with open gl mouse function using dda/bresenham algorithm
So i have to draw a line using mouse function. 所以我必须使用鼠标功能画一条线。 When the mouse clicks two points, the program needs to draw a line connecting them.
当鼠标单击两个点时,程序需要画一条连接它们的线。
I need to make an imaginary 10x10 array and map the points on it and then connect the dots. 我需要制作一个假想的10x10数组并在其上映射点,然后连接点。 window size doesn't matter.
窗口大小无关紧要。
I have no clue about what to do about the imaginary array and how to save the mouse coordinates so i can us them. 我不知道如何处理虚数组以及如何保存鼠标坐标,以便我可以使用它们。 Help
救命
#include <glut.h>
#include<stdio.h>
void RenderScene(void) //점
{
glClear(GL_COLOR_BUFFER_BIT);
// Flush drawing commands
glFlush();
}
void SetupRC(void) //바탕
{
// setup clear color
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}
void ChangeSize(GLsizei w, GLsizei h) {
GLfloat aspectRatio;
// Prevent a divide by zero
if (h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
// Reset coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Establish clipping volume (left, right, bottom, top, near, far)
aspectRatio = (GLfloat)w / (GLfloat)h;
glOrtho(0, w, h, 0, 1.0, -1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void myRectf(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2){
glPointSize(10);
////ddaLine
int dx, dy;
int m = (y2 - y1) / (x2 - x1);
for (int i = x1; i <= x2; i++)
{
if (m <= 1)
{
dx = 1;
dy = m * dx;
}
else
{
dy = 1;
dx = dy / m;
}
x1 += dx;
y1 += dy;
glBegin(GL_POINTS);
glVertex3f(x1, y1, 0.f);
glEnd();
}
/////breline
while (x1 < x2)
{
int dx = x2 - x1;
int dy = y2 - y1;
int di = 2 * dy - dx;
int ds = 2 * dy;
int dt = 2 * (dy - dx);
x1++;
if (di < 0)
di += ds;
else
{
y1++;
di += dt;
}
glBegin(GL_POINTS);
glVertex3f(x1, y1, 0.f);
glEnd();
}
}
GLfloat g_x = -1000.f, g_y = -1000.f;
void MouseHandler(int button, int state, int x, int y){
if (button == GLUT_LEFT_BUTTON
&& state == GLUT_DOWN){
g_x = x; g_y = y;
printf("%d,%d \n", x, y);
}
glutPostRedisplay();
}
void main(int argc, char* argv[]){
int array[10][10];
glutInit(&argc, argv); // initialize GL context
// single or double buffering | RGBA color mode
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutCreateWindow("Lec01");
// callback function
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
SetupRC(); // initialize render context(RC)
glutMouseFunc(MouseHandler);
glutMainLoop(); // run GLUT framework
}
And I'm not allowed to use GL_LINES 而且我不允许使用GL_LINES
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.