OpenGL闪烁并且比C ++更多
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OpenGL闪烁并且比C ++更多相关的知识,希望对你有一定的参考价值。
我正在进行三角测量应用程序,我可以在窗口中的框架三角形内部单击以将其细分为3个三角形,依此类推。因此,每次我添加一个点时,我都会将两个三角形添加到定义为类Triangle的结构中(请参阅Triangle.h和.cpp)。对于每个对象三角形,我跟踪其ID,其3个顶点的索引以及其3个相邻三角形的索引,其中第一个顶点和第一个相邻的三角形相反。
然后我用GL_POINTS
和GL_LINE_LOOP
绘制每个点和每个三角形。
如果我使用glClear(GL_COLOR_BUFFER_BIT)
,线条会闪烁。如果我不使用它,则不会发生闪烁。
我的主要问题是三角图不一致。当我单击三角形内的新点时,应在此点与形成包含所述点的三角形的3个点之间绘制线条。有时会发生应有的情况,有时会出现额外的线条。我检查了我的结构,我的班级Triangle没有错误。我为每个添加的点和三角测量的每次更新单独验证了每个属性。
当我使用drawTriangle函数时,我调用3次glVertex2i(x1, y1)
来正确绘制三角形。我不明白为什么可以同时绘制4-5行。它也不明白为什么有时会发生,有时不会发生。
我认为与我使用openGL存在冲突...就像我会进行计算并更新我的三角测量结构而openGL会得到部分结果......我似乎没有完全控制绘图的重新显示glutPostRedisplay
...
双缓冲会解决我的问题吗?有没有办法我可以设法称自己更新绘图而不是依赖glutPostRedisplay
和glutMainLoop
以及那些东西。
/*-----------------------------------------
Name: main
Author: Michael Landry
Description: main program
Date: 2018-02-22
Version: 1.00
-------------------------------------------*/
#include <GL/glut.h>
#include <iostream>
#include <vector>
#include "functions.h"
#include "Point.h"
#include "Triangle.h"
GLfloat BLUE[3] = { 0,0,1 };
std::vector <Point> pointList;
std::vector <Triangle> triangleList;
int main(int argc, char **argv)
{
// Give directions to user
std::cout << "*** Welcome to the Delaunay Triangulation tool ***" << std::endl << std::endl;
std::cout << "Left click to insert a point to the triangulation or right click to exit." << std::endl << std::endl;
// Create the boundary triangle
Frame();
// Initialize GLUT display window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
glutInitWindowPosition(0, 0);
glutCreateWindow("Delaunay Triangulation");
// Call the display function for drawing
glutDisplayFunc(display);
glutReshapeFunc(reshape);
// Call the mouse callback function
glutMouseFunc(mouse);
glutIdleFunc(spindisplay);
glutMainLoop();
return 0;
}
/*-----------------------------------------
Name: functions
Author: Michael Landry
Description: functions for second lab
Date: 2018-02-22
Version: 1.00
-------------------------------------------*/
#ifndef FUNCTIONS_H_
#define FUNCTIONS_H_
#include "Point.h"
#include "Triangle.h"
const int SCREEN_WIDTH = 1366;
const int SCREEN_HEIGHT = 768;
void drawPoint(void);
void drawTriangle(void);
void display(void);
void reshape(int w, int h);
void spindisplay(void);
void mouse(int btn, int state, int x, int y);
void Frame(void);
bool inTriangle(const Point&);
void insert(const int);
int walk(const Point&);
#endif
/*-----------------------------------------
Name: functions
Author: Michael Landry
Description: functions for second lab
Date: 2018-02-22
Version: 1.00
-------------------------------------------*/
#include <GL/glut.h>
#include "Point.h"
#include "Triangle.h"
#include "functions.h"
#include <iostream>
#include <vector>
extern GLfloat BLUE[3];
extern std::vector <Point> pointList;
extern std::vector <Triangle> triangleList;
/*
Name: drawTriangle
Description: draw the triangulation
*/
void drawTriangle(void)
{
glColor3fv(BLUE);
glBegin(GL_LINE_LOOP);
for (size_t i = 0; i < triangleList.size(); i++)
{
// Extract the coordinates of the first point of the triangle
int x1 = pointList[triangleList[i].getS1() - 1].getX(); // -1 because the vector starts at 0
int y1 = pointList[triangleList[i].getS1() - 1].getY();
// Extract the coordinates of the second point of the triangle
int x2 = pointList[triangleList[i].getS2() - 1].getX();
int y2 = pointList[triangleList[i].getS2() - 1].getY();
// Extract the coordinates of the third point of the triangle
int x3 = pointList[triangleList[i].getS3() - 1].getX();
int y3 = pointList[triangleList[i].getS3() - 1].getY();
// Draw the triangle formed by the 3 points
glVertex2i(x1, y1);
glVertex2i(x2, y2);
glVertex2i(x3, y3);
}
glEnd();
glFlush();
}
/*
Name: drawPoint
Description: draw points in the window
*/
void drawPoint(void)
{
glColor3fv(BLUE);
glBegin(GL_POINTS);
for (size_t i = 0; i < pointList.size(); i++)
{
// Extract the coordinates of the point
int x = pointList[i].getX();
int y = pointList[i].getY();
// Draw the point
glVertex2i(x, y);
}
glEnd();
glFlush();
}
/*
Name: display
Description: prepare the window for display
*/
void display(void)
{
glClearColor(0.0, 0.0, 0.0, 1.0);
// glClear(GL_COLOR_BUFFER_BIT); // command is disabled to stop flickering
glLoadIdentity();
glPointSize(5);
drawPoint();
drawTriangle();
glFlush();
}
/*
Name: reshape
Description: reshape the window
*/
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/*
Name: spindisplay
Description: prepare for a redisplay
*/
void spindisplay(void)
{
glutPostRedisplay();
}
/*
Name: mouse
Description: detect left button click on mouse
*/
void mouse(int btn, int state, int x, int y)
{
if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
// Generate a new object point with the cursor coordinates on click event
Point newPoint(x, y, 1);
// Verify if the point is inside the boundary triangle and if it does not already exists in the list
if ((inTriangle(newPoint) == 1) && ((newPoint == pointList) == 0))
{
// Only add the point to the list if it is inside the boundary triangle
pointList.push_back(newPoint);
std::cout << "New point added" << std::endl;
std::cout << "x : " << x << " " << "y : " << y << std::endl;
std::cout << "Total number of points : " << pointList.size() << std::endl << std::endl;
// Find the triangle containing the new point
int triangleIndex = walk(newPoint);
std::cout << "Divide triangle number " << triangleIndex + 1 << std::endl << std::endl;
// Insert the new point in the triangulation
insert(triangleIndex);
std::cout << "ID S1 S2 S3 T1 T2 T3" << std::endl;
for (size_t i = 0; i < triangleList.size(); i++)
{
std::cout << triangleList[i].getID() << " " << triangleList[i].getS1() << " " << triangleList[i].getS2() << " " << triangleList[i].getS3() << " " << triangleList[i].getT1() << " " << triangleList[i].getT2() << " " << triangleList[i].getT3() << " " << std::endl;
}
std::cout << std::endl;
}
glutPostRedisplay();
}
if (btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
{
exit(1); // Exit the program
}
}
/*
Name: Frame
Description: create the initial boundary triangle
*/
void Frame(void)
{
// First point
Point myPoint1(SCREEN_WIDTH / 2, 100, 1);
pointList.push_back(myPoint1);
// Second point
Point myPoint2(100, SCREEN_HEIGHT - 100, 1);
pointList.push_back(myPoint2);
// Third point
Point myPoint3(SCREEN_WIDTH - 100, SCREEN_HEIGHT - 100, 1);
pointList.push_back(myPoint3);
// Create the first triangle of the list
Triangle triangleFrame(1, 1, 2, 3, 0, 0, 0);
triangleList.push_back(triangleFrame);
}
/*
Name: getDeterminant
Description: calculate the determinant of a 3 x 3 matrix
*/
int getDeterminant(const Point &point1, const Point &point2, const Point &point3)
{
// Form a 5 x 3 matrix containing the 3 triangle points
std::vector <Point> pointTriangle;
pointTriangle.push_back(point1);
pointTriangle.push_back(point2);
pointTriangle.push_back(point3);
pointTriangle.push_back(point1);
pointTriangle.push_back(point2);
// Calculate the determinant with the shoelace method
int detTriangle = 0;
int detTrianglePart;
for (int j = 0; j < 3; j++)
{
detTrianglePart = ((pointTriangle[j].getX() * pointTriangle[j + 1].getY() * pointTriangle[j + 2].getZ()) - (pointTriangle[j + 2].getX() * pointTriangle[j + 1].getY() * pointTriangle[j].getZ()));
detTriangle = detTriangle + detTrianglePart;
}
return detTriangle;
}
/*
Name: inTriangle
Description: test if a point is inside a triangle
*/
bool inTriangle(const Point& newPoint)
{
// Pre loop conditions
int k = 0; // counter
int detTriangle = -1;
int row = 3; // 3 points
int col = 3; // 3 coordinates
// Loop on all the points if the determinant stays negative
while ((k < row) && (detTriangle < 0))
{
// Initialize an object the 3 points
Point point1;
Point point2;
Point point3;
// Extract 2 points from the list of points
if (k == row - 1)
{
point1 = pointList[k];
point2 = pointList[k - row + 1];
point3 = newPoint;
}
else
{
point1 = pointList[k];
point2 = pointList[k + 1];
point3 = newPoint;
}
// Get the determinant of the triangle
detTriangle = getDeterminant(point1, point2, point3);
// Incrementation of the counter
k++;
}
// Output the position of the point
bool isInside = 0;
(detTriangle < 0) ? (isInside = 1) : (isInside = 0);
return isInside;
}
/*
Name: insert
Description: insert the new point and create 3 new triangles
*/
void insert(const int triangleIndex)
{
// Extract the Index of the new point
int newPointIndex = pointList.size();
// Create the line of the second new triangle
Triangle triangle2(triangleList.size() + 1, newPointIndex, triangleList[triangleIndex].getS3(), triangleList[triangleIndex].getS1(), triangleList[triangleIndex].getT2(), triangleList.size() + 2, triangleIndex + 1);
// Create the line of the third new triangle
Triangle triangle3(triangleList.size() + 2, newPointIndex, triangleList[triangleIndex].getS1(), triangleList[triangleIndex].getS2(), triangleList[triangleIndex].getT3(), triangleIndex + 1, triangleList.size() + 1);
// Get the adjacent triangles of the base triangle
int T1 = triangleList[triangleIndex].getT1();
int T2 = triangleList[triangleIndex].getT2();
int T3 = triangleList[triangleIndex].getT3();
// Update the line of first new triangle
triangleList[triangleIndex].setS1(newPointIndex);
triangleList[triangleIndex].setT2(triangleList.size() + 1);
triangleList[triangleIndex].setT3(triangleList.size() + 2);
// Update the adjacent triangles
if (T2 != 0 && T3 != 0)
{
triangleList[T2 - 1].setT3(triangleList.size() + 1); // T-1 because vector starts at 0
triangleList[T3 - 1].setT2(triangleList.size() + 2);
}
// Insert the new triangles in the triangulation
triangleList.push_back(triangle2);
triangleList.push_back(triangle3);
}
/*
Name: inCircle
Description: determine if a point is inside a circle
*/
bool inCircle(const Point& newPoint, const int triangleIndex)
{
// Get the coordinates of the new point
int x = newPoint.getX();
int y = newPoint.getY();
int z = newPoint.getZ();
// Get the coordinates of the first point
int x1 = pointList[triangleList[triangleIndex].getS1()].getX();
int y1 = pointList[triangleList[triangleIndex].getS1()].getY();
int z1 = pointList[triangleList[triangleIndex].getS1()].getZ();
// Get the coordinates of the second point
int x2 = pointList[triangleList[triangleIndex].getS2()].getX();
int y2 = pointList[triangleList[triangleIndex].getS2()].getY();
int z2 = pointList[triangleList[triangleIndex].getS2()].getZ();
// Get the coordinates of the third point
int x3 = pointList[triangleList[triangleIndex].getS3()].getX();
int y3 = pointList[triangleList[triangleIndex].getS3()].getY();
int z3 = pointList[triangleList[triangleIndex].getS3()].getZ();
// Create the 4 x 4 matrix
int pointArray[4][4] = { { x,y,z,1 }, {x1,y1,z1,1}, {x2,y2,z2,1}, {x3,y3,z3,1} };
// Create the 3 points of the first block A
Point pointA1(pointArray[1][1], pointArray[1][2], pointArray[1][3]);
Point pointA2(pointArray[2][1], pointArray[2][2], pointArray[2][3]);
Point pointA3(pointArray[3][1], pointArray[3][2], pointArray[3][3]);
// Create the 3 points of the second block B
Point pointB1(pointArray[1][0], pointArray[1][2], pointArray[1][3]);
Point pointB2(pointArray[2][0], pointArray[2][2], pointArray[2][3]);
Point pointB3(pointArray[3][0], pointArray[3][2], pointArray[3][3]);
// Create the 3 points of the third block C
Point pointC1(pointArray[1][0], pointArray[3][1], pointArray[1][3]);
Point pointC2(pointArray[2][0], pointArray[2][1], pointArray[2][3]);
Point pointC3(pointArray[3][0], pointArray[1][1], pointArray[3][3]);
// Create the 3 points of the fourth block D
Point pointD1(pointArray[1][0], pointArray[3][1], pointArray[1][2]);
Point pointD2(pointArray[2][0], pointArray[2][1], pointArray[2][2]);
Point pointD3(pointArray[3][0], pointArray[1][1], pointArray[3][2]);
// Find the determinant
int determinant = pointArray[0][0] * getDeterminant(pointA1, pointA2, pointA3) - pointArray[0][1] * getDeterminant(pointB1, pointB2, pointB3) + pointArray[0][2] * getDeterminant(pointC1, pointC2, pointC3) - pointArray[0][3] * getDeterminant(pointD1, pointD2, pointD3);
// Return the test value (1 if the point is inside the circle, 0 if not)
bool isInside;
(determinant < 0) ? (isInside = 1) : (isInside = 0);
return isInside;
}
/*
Name: walk
Description: find the triangle containing the new point
*/
int walk(const Point& newPoint)
{
// First triangle to test by default
int triangleToTest = 0;
int triangleIndex = -1;
do
{
// Define the points forming the first line to test
Point point1 = pointList[triangleList[triangleToTest].getS1() - 1];
Point point2 = pointList[triangleList[triangleToTest].getS2() - 1];
// Get the determinant
int determinant = getDeterminant(point1, point2, newPoint);
// Test if the point is on the left side of the line
if (determinant < 0)
{
// Define the points forming the second line to test
point1 = pointList[triangleList[triangleToTest].getS2() - 1];
point2 = pointList[triangleList[triangleToTest].getS3() - 1];
determinant = getDeterminant(point1, point2, newPoint);
// Test if the point is on the left side of the line
if (determinant < 0)
{
// Define the points forming the third line to test
point1 = pointList[triangleList[triangleToTest].getS3() - 1];
point2 = pointList[triangleList[triangleToTest].getS1() - 1];
determinant = getDeterminant(point1, point2, newPoint);
// Test if the point is on the left side of the line
if (determinant < 0)
{
triangleIndex = triangleToTest;
}
else
{
triangleToTest = triangleList[triangleToTest].getT2() - 1;
}
}
else
{
triangleToTest = triangleList[triangleToTest].getT1() - 1;
}
}
else
{
triangleToTest = triangleList[triangleToTest].getT3() - 1;
}
} while (triangleIndex == -1);
return triangleIndex;
}
/*-----------------------------------------
Name: Point
Author: Michael Landry
Description: Declaration of class Point
Date: 2018-02-15
Version: 1.00
-------------------------------------------*/
#ifndef POINT_H_
#define POINT_H_
#include <iostream>
#include <vector>
class Point
{
public:
// Default constructor
Point();
// Constructor with parameters
Point(int, int, int);
// Setters
void setPoint(int p_X, int p_Y, int p_Z);
void setX(int p_X);
void setY(int p_Y);
void setZ(int p_Z);
// Getters
int getX() const;
int getY() const;
int getZ() const;
// Overloaded operator
bool Point::operator==(const std::vector <Point> & p_pointList) const;
private:
// 3D coordinates as attributes
int m_X;
int m_Y;
int m_Z;
};
#endif /* POINT_H_ */
/*-----------------------------------------
Name: Point
Author: Michael Landry
Description: Implementation of class Point
Date: 2018-02-15
Version: 1.00
-------------------------------------------*/
#include "Point.h"
#include <iostream>
#include <vector>
// Default constructor initialises (0,0,0) as coordinates
Point::Point() : m_X(0), m_Y(0), m_Z(0)
{
}
// Constructor with parameters
Point::Point(int p_X, int p_Y, int p_Z) : m_X(p_X), m_Y(p_Y), m_Z(p_Z)
{
}
// Setter for the 3 coordinates
void Point::setPoint(int p_X, int p_Y, int p_Z)
{
m_X = p_X;
m_Y = p_Y;
m_Z = p_Z;
}
// Setter for each coordinate
void Point::setX(int p_X)
{
m_X = p_X;
}
void Point::setY(int p_Y)
{
m_Y = p_Y;
}
void Point::setZ(int p_Z)
{
m_Y = p_Z;
}
// Getter for each coordinate
int Point::getX() const
{
return m_X;
}
int Point::getY() const
{
return m_Y;
}
int Point::getZ() const
{
return m_Z;
}
// Overloaded operator
bool Point::operator==(const std::vector <Point> & p_pointList) const
{
// Initialize a counter
size_t i = 0;
bool isEqual = true;
do
{
isEqual = (m_X == p_pointList[i].getX()) && (m_Y == p_pointList[i].getY()) && (m_Z == p_pointList[i].getZ());
i++;
} while ((i < p_pointList.size()) && (isEqual == false));
return isEqual;
}
/*-----------------------------------------
Name: Triangle
Author: Michael Landry
Description: Declaration of class Triangle
Date: 2018-02-23
Version: 1.00
-------------------------------------------*/
#ifndef TRIANGLE_H_
#define TRIANGLE_H_
class Triangle
{
public:
// Constructor with no parameter
Triangle::Triangle();
// Constructor with parameters
Triangle::Triangle(int, int, int, int, int, int, int);
// Setters
void Triangle::setTriangle(int p_ID, int p_S1, int p_S2, int p_S3, int p_T1, int p_T2, int p_T3);
void Triangle::setID(int p_ID);
void Triangle::setS1(int p_S1);
void Triangle::setS2(int p_S2);
void Triangle::setS3(int p_S3);
void Triangle::setT1(int p_T1);
void Triangle::setT2(int p_T1);
void Triangle::setT3(int p_T1);
// Getters
int Triangle::getID() const;
int Triangle::getS1() const;
int Triangle::getS2() const;
int Triangle::getS3() const;
int Triangle::getT1() const;
int Triangle::getT2() const;
int Triangle::getT3() const;
private:
// The ID of the triangle
int m_ID;
// The 3 vertices forming the triangle
int m_S1;
int m_S2;
int m_S3;
// The 3 adjacent triangles
int m_T1;
int m_T2;
int m_T3;
};
#endif /* TRIANGLE_H_ */
/*-----------------------------------------
Name: Triangle
Author: Michael Landry
Description: Implementation of class Triangle
Date: 2018-02-23
Version: 1.00
-------------------------------------------*/
#include "Triangle.h"
// Constructor with no parameter
Triangle::Triangle() : m_ID(0), m_S1(0), m_S2(0), m_S3(0), m_T1(0), m_T2(0), m_T3(0)
{
}
// Constructor with parameters
Triangle::Triangle(int p_ID, int p_S1, int p_S2, int p_S3, int p_T1, int p_T2, int p_T3) : m_ID(p_ID), m_S1(p_S1), m_S2(p_S2), m_S3(p_S3), m_T1(p_T1), m_T2(p_T2), m_T3(p_T3)
{
}
// Global setter
void Triangle::setTriangle(int p_ID, int p_S1, int p_S2, int p_S3, int p_T1, int p_T2, int p_T3)
{
m_ID = p_ID;
m_S1 = p_S1;
m_S2 = p_S2;
m_S3 = p_S3;
m_T1 = p_T1;
m_T2 = p_T2;
m_T3 = p_T3;
}
// Setter for each individual parameter
void Triangle::setID(int p_ID)
{
m_ID = p_ID;
}
void Triangle::setS1(int p_S1)
{
m_S1 = p_S1;
}
void Triangle::setS2(int p_S2)
{
m_S2 = p_S2;
}
void Triangle::setS3(int p_S3)
{
m_S3 = p_S3;
}
void Triangle::setT1(int p_T1)
{
m_T1 = p_T1;
}
void Triangle::setT2(int p_T2)
{
m_T2 = p_T2;
}
void Triangle::setT3(int p_T3)
{
m_T3 = p_T3;
}
// Getter for each individual parameter
int Triangle::getID() const
{
return m_ID;
}
int Triangle::getS1() const
{
return m_S1;
}
int Triangle::getS2() const
{
return m_S2;
}
int Triangle::getS3() const
{
return m_S3;
}
int Triangle::getT1() const
{
return m_T1;
}
int Triangle::getT2() const
{
return m_T2;
}
int Triangle::getT3() const
{
return m_T3;
}
我建议从所有代码中删除所有glFlush
和glutPostRedisplay
调用。
但是,保持glutPostRedisplay
调用空闲函数spindisplay
:
void spindisplay(void)
{
glutPostRedisplay();
}
使用双缓冲:
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
并在主循环函数glutSwapBuffers
结束时放一个display
调用:
void display(void)
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glPointSize(5);
drawPoint();
drawTriangle();
glutSwapBuffers();
}
如果要保留单个缓冲区,
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
然后你必须在glFinish
函数结束时进行display
调用:
void display(void)
{
.....
glFinish();
}
首先,谢谢Rabbid76。我按照你的建议开始使用双缓冲而不是单缓冲。
这并没有解决绘制不需要的额外线的问题。我发现了问题:命令glBegin(GL_LINE_LOOP)和glEnd()都在我的循环之外(对于我......)。因此,openGL没有考虑在每次迭代后完成行循环。所以,我将命令glBegin(GL_LINE_LOOP)和glEnd()放在(for i ...)循环中,这样我就可以在每次迭代时改变行循环。在此更改之后,每当我在三角形内部单击时,我将其细分为另外三个三角形,依此类推,并且不会绘制额外的线条。
/*
Name: drawTriangle
Description: draw the triangulation
*/
void drawTriangle(void)
{
glColor3fv(BLUE);
for (size_t i = 0; i < triangleList.size(); i++)
{
glBegin(GL_LINE_LOOP);
// Extract the coordinates of the first point of the triangle
int x1 = pointList[triangleList[i].getS1() - 1].getX(); // -1 because the vector starts at 0
int y1 = pointList[triangleList[i].getS1() - 1].getY();
// Extract the coordinates of the second point of the triangle
int x2 = pointList[triangleList[i].getS2() - 1].getX();
int y2 = pointList[triangleList[i].getS2() - 1].getY();
// Extract the coordinates of the third point of the triangle
int x3 = pointList[triangleList[i].getS3() - 1].getX();
int y3 = pointList[triangleList[i].getS3() - 1].getY();
// Draw the triangle formed by the 3 points
glVertex2i(x1, y1);
glVertex2i(x2, y2);
glVertex2i(x3, y3);
glEnd();
}
}
以上是关于OpenGL闪烁并且比C ++更多的主要内容,如果未能解决你的问题,请参考以下文章