0
$\begingroup$

I'm learning OpenGL for a personal project. I need to draw a triangle mesh as a wireframe. From what I've read, I can do this using glPolygonMode. However, the program always draws my polygons filled. Is is a bad driver? I'm using the linux subsystem on a chromebook. It's basically debian in a container. How do I get an wireframe triangle?

Here is my program, with all the windowing functions and the shader compilation pulled into their own .cpp file.

#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <iostream>
#include "ah_graphics.h"
#include "ah_window.h"

const char *vertexShaderSource();

const char *fragmentShaderSource();


// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float positions[] = {
        -1.0f, 1.0f, 0.0f, // left
        -1.0f, 0.0f, 0.0f, // right
        0.0f, 1.0f, 0.0f // top
};


int main() {
    GLFWwindow *window = ahCreateWindow(800, 600);
    ahLoadGlFunctionPointers();
    GLuint program = ahCreateGlProgram(vertexShaderSource(), fragmentShaderSource());
    unsigned int VBO_pos, VAO;
    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);
    glGenBuffers(1, &VBO_pos);
    glBindBuffer(GL_ARRAY_BUFFER, VBO_pos);
    glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);
    int numComponents = 3; // (x,y,z) and (r,g,b)
    std::size_t stride = numComponents * sizeof(float);
    glVertexAttribPointer(0, numComponents, GL_FLOAT, GL_FALSE, stride, (void *) 0);
    glEnableVertexAttribArray(0);

    while (!glfwWindowShouldClose(window)) {
        // input
        ahProcessInput(window);
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glUseProgram(program);
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    return 0;
}

const char *vertexShaderSource() {
    return "#version 330 core\n"
           "layout (location = 0) in vec3 aPos;\n"
           "void main()\n"
           "{\n"
           "  gl_Position = vec4(aPos, 1.0);\n"
           "}\0";
}

const char *fragmentShaderSource() {
    return "#version 330 core\n"
           "void main()\n"
           "{\n"
           " gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
           "}\n\0";
}

And this is the scene it renders: enter image description here

$\endgroup$
4
  • 1
    $\begingroup$ I don't know the "ah" library, but please take a look at the shader compiling and linking section. Maybe there comes an error... in that case OpenGL can act very strange... The code you posted looks good, so I don't see the problem there $\endgroup$
    – Thomas
    Jan 25, 2022 at 13:34
  • $\begingroup$ What you can do is to use the "GLenum glGetError(void);" function to check for errors $\endgroup$
    – Thomas
    Jan 25, 2022 at 13:36
  • $\begingroup$ polygon mode is an OpenGL state, so once it is set it stays set until something else changes it. It would typically be set outside the main loop and/or changed by a something like a key press in the process keys function. Also, setting it after glUseProgram could have strange effects (but begs the question since this is a loop) I recommend moving the set polygon mode out of the main loop. Or at the very least put it before glUseProgram. $\endgroup$
    – pmw1234
    Jan 25, 2022 at 18:35
  • $\begingroup$ How about drawing lines instead as a workaround? $\endgroup$ Jan 28, 2022 at 10:40

1 Answer 1

-1
$\begingroup$

I've ran into the same problem in the same exact situation. How I fixed it was to draw using GL_LINE_STRIP inside the render loop and discard the glPolygonMode code.

glDrawArrays(GL_LINE_STRIP, 0, 3);

This workaround does work, but it would be very appreciated if someone smarter than me could figure out why glPolygonMode doesn't work in such cases. I'm aware that this post is a year old, but I hope this helps the people out there!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.