Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6903707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:57:33+00:00 2026-05-27T07:57:33+00:00

I am having a hard time trying to draw simple lines through an OpenGL

  • 0

I am having a hard time trying to draw simple lines through an OpenGL ES 2.0 shader.
I am trying to use glDrawArrays with GL_LINES, but so far i’ve been unsuccessful.

My vertex shader is the most basic you can get:

attribute vec4 Position; 
attribute vec4 SourceColor; 

uniform mat4 Projection;
uniform mat4 ModelView;

varying vec4 DestinationColor; 

void main(void) { 
    DestinationColor = SourceColor; 
    gl_Position = Projection * ModelView * Position;
}

And the fragment shader:

varying lowp vec4 DestinationColor;

void main(void) {
    gl_FragColor = DestinationColor;
}

I have some doubts:

  1. How do you setup the Origin and Destination points coordinates?
  2. How to setup the Projection and ModelView matrices in this case? With a square, the ModelView matrix will hold the its Center coordinate, but what happens in the case of a Line?

Does anyone have an example?

EDIT:

Ok, i have the following code:

    glLineVertices[0].Position[0] = origin.x;
    glLineVertices[0].Position[1] = origin.y;
    glLineVertices[0].Position[2] = 0;
    glLineVertices[0].Color[0] = 1.0f;
    glLineVertices[0].Color[1] = 0.0f;
    glLineVertices[0].Color[2] = 0.0f;
    glLineVertices[0].Color[3] = 0.0f;
    glLineVertices[0].Normal[0] = 0.0f;
    glLineVertices[0].Normal[1] = 0.0f;
    glLineVertices[0].Normal[2] = 1.0f;

    glLineVertices[1].Position[0] = end.x;
    glLineVertices[1].Position[1] = end.y;
    glLineVertices[1].Position[2] = 0;
    glLineVertices[1].Color[0] = 1.0f;
    glLineVertices[1].Color[1] = 0.0f;
    glLineVertices[1].Color[2] = 0.0f;
    glLineVertices[1].Color[3] = 0.0f;
    glLineVertices[1].Normal[0] = 0.0f;
    glLineVertices[1].Normal[1] = 0.0f;
    glLineVertices[1].Normal[2] = 1.0f;

    glGenVertexArraysOES(1, &vertexArray);
    glBindVertexArrayOES(vertexArray);

    glGenBuffers(1, &vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, vertexStructureSize, glLineVertices, GL_STATIC_DRAW);

    glEnableVertexAttribArray(shaderManager.currentAttributeVertexPosition);
    glVertexAttribPointer(shaderManager.currentAttributeVertexPosition, 3, GL_FLOAT, GL_FALSE, sizeof(glLineVertex), 0);
    glEnableVertexAttribArray(shaderManager.currentAttributeSourceColor);
    glVertexAttribPointer(shaderManager.currentAttributeSourceColor, 4, GL_FLOAT, GL_FALSE,  sizeof(glLineVertex), (char *)12);
    glEnableVertexAttribArray(shaderManager.currentAttributeVertexNormal);
    glVertexAttribPointer(shaderManager.currentAttributeVertexNormal, 3, GL_FLOAT, GL_FALSE, sizeof(glLineVertex), (char *)28);

With a render method:

- (void)render
{
    //It might as well be the Identity Matrix, nothing happens either way.
    //modelViewMatrix = GLKMatrix4Identity;   
    modelViewMatrix = GLKMatrix4MakeTranslation(0, 0, 0.0f);    
    glUniformMatrix4fv(shaderManager.currentUniformModelViewMatrix, 1, 0, modelViewMatrix.m);

    glBindVertexArrayOES(vertexArray);
    glDrawArrays(GL_LINES, 0, size);  

}

And with this projection matrix:

_projectionMatrix = GLKMatrix4MakeOrtho(0, self.view.bounds.size.width, 0, self.view.bounds.size.height, -1.0f, 1.0f);

[shaderManager useShaderProgram:@"LineShader"];

glUniformMatrix4fv([shaderManager getUniform:@"Projection" ofShader:@"LineShader"], 1, 0, _projectionMatrix.m);

As soon as i use:

[lineDraw render];

Nothing happens.
Anyone knows why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:57:34+00:00Added an answer on May 27, 2026 at 7:57 am

    I have finally figured it out.

    These simple lines:

    glEnableVertexAttribArray(shaderManager.currentAttributeVertexNormal);
    glVertexAttribPointer(shaderManager.currentAttributeVertexNormal, 3, GL_FLOAT, GL_FALSE, sizeof(glLineVertex), (char *)28);
    

    were causing the issue, because i temporarily disabled the use of normals in the shader, commenting out the correspondent attribute. Didn’t know that just by doing that, no draw would occur, and neither getError() would throw any error out.

    I will consider the above answer as correct, though, because it helped me clarify the coordinates and correspondent matrices calculations. In this example, plain 2D lines, one doesn’t need any special modelViewMatrix, it may stay as “GLKMatrix4Identity”.

    Thanks again 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to bind to BugSense's lib but having a hard time trying to
I'm having a hard time trying to get a query that sounds simple. I'm
I'm having hard time trying to use teasers post in my wordpress theme (based
I'm trying to write a software in JavaScript, but I'm having hard time trying
Sorry for the vague question title, but I'm having a hard time trying to
I am having a hard time trying to add a simple clickable marker to
I am having a hard time trying to figure out when to use a
I'm having hard time trying to figure out how to auto-save user data in
I've been having a hard time trying to understand PyPy's translation. It looks like
I'm having a hard time trying to get my team comfortable with interface based

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.