Line width in GLVersion(4, 1)

I s there any trick to use ofSetLineWidth() in GLVersion(4, 1)?

hey , you have to do this in the shaders as ofSetlineWidth does not work with openGL beyond #version 120

You can use glLineWidth(). Also note that line width is capped by graphics driver. See this discussion:

Cheers!

Thank you !!
Ok , i need use own shader.
But why default of shader dose not support linewidth , do you know?

Rendering variable width lines is surprisingly involved. I had the same discovery as you that glLineWidth() is no longer part of the GL spec (some OSes/drivers may still support it but you cant rely on it, OSX doesn’t.)

Here’s a few links I dug up when I was looking into it

GitHub - mattdesl/webgl-lines: some interactive content for a blog post (src for the above)
Simple and fast high quality antialiased lines with OpenGL – vitaliburkov
Wireframe Drawing

I think I went with something close to the last one just cause it suited the particular project I was doing.

i found this examples of lines on openGL4 you have to use geom shader
lines frag

#version 410 core

out vec4 fragColor;

void main(void)
{
	fragColor = vec4(1.0, 1.0, 0.0, 1.0);
}

lines vert

#version 410 core

uniform mat4 modelViewProjectionMatrix;

in vec4 position;

void main(void){
    
	gl_Position = modelViewProjectionMatrix * position;
    
}

lines geom

#version 410 core

layout(points) in;

layout(line_strip, max_vertices = 4) out;

void main(void)
{
    
    gl_Position = gl_in[0].gl_Position;
    EmitVertex();

    vec4 modPosition = gl_in[0].gl_Position;
    modPosition.x += 10.0;
    gl_Position = modPosition;
    EmitVertex();

    EndPrimitive();
}


.cpp

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
    
    
    printGLInfo();

    ofSetLogLevel(OF_LOG_VERBOSE);
    ofSetVerticalSync(true);
	ofSetFrameRate(60);
    ofBackground(40);
    
    shader.setGeometryInputType(GL_POINTS);
    shader.setGeometryOutputType(GL_LINE_STRIP);
    shader.setGeometryOutputCount(2);
    
    shader.setupShaderFromFile(GL_VERTEX_SHADER, "lines.vert");
    shader.setupShaderFromFile(GL_FRAGMENT_SHADER, "lines.frag");
    shader.setupShaderFromFile(GL_GEOMETRY_SHADER_EXT, "lines.geom");
    shader.linkProgram();
 
    
    ofBoxPrimitive box;
    box.set(300);
    ofMesh mesh = box.getMesh();
    verts = mesh.getVertices();
    
    GLint vertexLocation = shader.getAttributeLocation("position");
    
    //Generate & Bind Buffer:
    glGenBuffers(1, &verticesVboID);
    glBindBuffer(GL_ARRAY_BUFFER, verticesVboID);
    glBufferData(GL_ARRAY_BUFFER, verts.size() * sizeof(ofVec3f),  &verts[0], GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    //Generate & Bind Vertex Array:
    glGenVertexArrays(1, &vaoID);
    glBindVertexArray(vaoID);
    glBindBuffer(GL_ARRAY_BUFFER, verticesVboID);
    glEnableVertexAttribArray(vertexLocation);
    glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, sizeof(ofVec3f), 0);
    glBindVertexArray(0);
    
}



//--------------------------------------------------------------
void ofApp::update() {
    
    ofSetWindowTitle(ofToString(ofGetFrameRate()));
    
}

//--------------------------------------------------------------
void ofApp::draw(){
    
    ofEnableDepthTest();
    cam.begin();
    
    ofDrawAxis(1000);
    
   
    shader.begin();
    
    glBindVertexArray(vaoID);
    glDrawArrays(GL_POINTS, 0, verts.size());
    
    shader.end();

    cam.end();
    ofDisableDepthTest();
    
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){
 
}

//--------------------------------------------------------------

void ofApp::printGLInfo(){
    
    cout<<"IsGLProgrammableRederer: "<< ofIsGLProgrammableRenderer() <<endl;
    cout<<"GL-Version-Major: "<< ofGetGLRenderer()->getGLVersionMajor() <<endl;
    cout<<"GL-Version-Minor: "<< ofGetGLRenderer()->getGLVersionMinor() <<endl;
    cout<<"GLSL Version: "<<ofGLSLVersionFromGL(ofGetGLRenderer()->getGLVersionMajor(), ofGetGLRenderer()->getGLVersionMinor())<<endl;

}

.h

#pragma once

#include "ofMain.h"
#include "ofxGui.h"

class ofApp : public ofBaseApp{

public:
    void setup();
    void update();
    void draw();

    void keyPressed  (int key);

    
    
    void setupGui();
    
    void printGLInfo();
    
    ofEasyCam cam;
    
    ofShader shader;
    
    vector<glm::vec3> verts;
    
     GLuint verticesVboID;
     GLuint vaoID;
    
    
};

1 Like