Dextron
Dextron

Reputation: 628

Drawing a line in Opengl not displaying C++

I am trying to draw a line straight across my window The screen colour is working but the line doesn't seem to draw. I am fairly certain this is because I might of set the position wrong and the line is being clipped from the window but I'm not sure how to fix this.

my full code

#include <GL\glew.h>
#include <GLFW/glfw3.h>
#include <GL\glut.h>
#include <glm.hpp>
#include <GL\freeglut.h>
#include <GL\GL.h>
#include <IL/il.h>

using namespace std;

 int main() {
    int windowWidth = 1024, windowHeight = 1024;
    if (!glfwInit())
        return -1;


    GLFWwindow* window;
    window = glfwCreateWindow(windowWidth, windowHeight, "electroCraft", NULL, NULL);
    glfwMakeContextCurrent(window); // stes the specified window as active INACTIVE SCREEN IF WINDOW NOT CURRENT!!!!
    if (!window) {
        glfwTerminate();
        printf("Screen failed to start. ABORTING...\n");
        return -1;
    }
    glViewport(0, 0, windowWidth, windowHeight);
    glOrtho(0, windowWidth, 0, windowHeight, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glEnable(GL_DEPTH_TEST);
    while (!glfwWindowShouldClose(window)) {
        glClearColor(62.0f / 255.0f, 85.9f / 255.0f, 255.0 / 255.0, 0.0);
        glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

        //begin drawing
        glBegin(GL_LINE);
        glVertex2f(20, 100);
        glVertex2f(600, 100);
        glEnd();

        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwTerminate();
    return 0;
}

Upvotes: 1

Views: 2125

Answers (1)

Rabbid76
Rabbid76

Reputation: 210877

As mentioned in the comment you've to use GL_LINES rather than GL_LINE, because GL_LINE is not a valid Primitive type.

glBegin(GL_LINES); // <----
glVertex2f(20, 100);
glVertex2f(600, 100);
glEnd();

But there is another issue. The default matrix mode is GL_MODELVIEW (see glMatrixMode), so the orthographic projection is set to the model view matrix and is overwritten by the identity matrix (glLoadIdentity). You've to set the matrix mode GL_PROJECTION, before glOrtho:

glMatrixMode(GL_PROJECTION); // <----
glOrtho(0, windowWidth, 0, windowHeight, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

Upvotes: 2

Related Questions