#include <iostream>
// GLEW
#define GLEW_STATIC
#include <GL/glew.h>
// GLFW
#include <GLFW/glfw3.h>
// Window dimensions
const GLuint WIDTH = 600, HEIGHT = 600;
static void Initialize(void);
void drawPoint(int x, int y)
{
//float xv = float(x) / 600*2-1;
//float yv = float(y) / 600*2-1;
float xv = float(x) / 600*2-1;
float yv = float(y) / 600*2-1;
glBegin(GL_POINTS);
glVertex2f(xv,yv);
glEnd();
}
//lineDDA(600, 600, 0, 0);
void lineDDA(int x0, int y0, int xEnd, int yEnd)
{
int dx = xEnd - x0, dy = yEnd - y0, steps, k;
float xIncrement, yIncrement, x = x0, y = y0;
//dx = 600
//dy = 600
if (abs(dx) > fabs(dy))
steps = fabs(dx);
else
steps = fabs(dy);
xIncrement = float(dx) / float(steps);
yIncrement = float(dy) / float(steps);
drawPoint(round(x), (y));
for (k = 0; k < steps; k++)
{
x += xIncrement;
y += yIncrement;
drawPoint(round(x), (y));
}
}
void lineBres(int x0, int y0, int xEnd, int yEnd)
{
int dx = fabs(xEnd - x0), dy = fabs(yEnd - y0);
int p = 2 * dy - dx;
int twoDy = 2 * dy, twoDyMinusDx = 2 * (dy - dx);
int x, y;
if (x0 > xEnd)
{
x = xEnd;
y = yEnd;
xEnd = x0;
}
else
{
x = x0;
y = y0;
}
drawPoint(x,y);
while (x < xEnd)
{
x++;
if (p < 0)
p += twoDy;
else
{
y++;
p += twoDyMinusDx;
}
drawPoint(x,y);
}
}
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_ANY_PROFILE, GLFW_OPENGL_ANY_PROFILE);
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr);
glfwMakeContextCurrent(window);
while (!glfwWindowShouldClose(window))
{
Initialize();
float delta = glfwGetTime();
//RenderScene(window);
lineDDA(600,600,0,0);
lineBres(50,150,500,600);
// Swap the screen buffers
glfwSwapBuffers(window);
// Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
glfwPollEvents();
}
// Terminate GLFW, clearing any resources allocated by GLFW.
glfwTerminate();
return EXIT_SUCCESS;
}
static void Initialize(void) {
glClearColor(1.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
}