Results 1 to 4 of 4

Thread: Struggling to color each vertex of Hexagon

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2017
    Posts
    20

    Struggling to color each vertex of Hexagon

    Hello everyone, I am a new registered user.

    I just recently started learning OpenGL and I come from a WIN32 API background. To cut straight to the chase, I am stuck and I am here to ask for assistance. Basically, I have a Hexagon drawn like this:

    Code :
     				// Position			 Color
    GLfloat vertices[] = { -0.25f, -0.5f, 0.0f,  1.0f, 0.0f, 0.0f,   // Red
    				 0.25f, -0.5f, 0.0f,  0.0f, 1.0f, 0.0f,   // Green
    				 0.5f, 0.0f, 0.0f,	   0.0f, 0.0f, 1.0f ,   // Blue
     
    				 0.25f, 0.5f, 0.0f,  1.0f, 0.0f, 1.0f,    // Purple
    				-0.25f, 0.5f, 0.0f,  1.0f, 1.0f, 1.0f,   // White
    				-0.5f, 0.0f, 0.0f,	1.0f, 1.0f, 0.0f     // Yellow
    						};

    Here is how my vertex and fragment shaders currently look like:
    Vertex
    Code :
    #version 430 core
     
    layout (location = 0) in vec3 position; 
    layout (location = 1) in vec3 color;
    layout (location = 2) in vec3 newColor;
     
    uniform mat4 resultantMatrix;
     
    out vec3 output;
     
    void main() 
    { 
    	vec4 currentPos = vec4(position, 1.0f);
    	gl_Position = resultantMatrix * currentPos ;  
    	output = color; //... maybe + newColor;?
     // Err.... How to output 2 things in the fragment shader...?
    };

    Fragment
    Code :
    #version 430 core
     
    in vec3 o; 
    out vec4 Color; 
     
    uniform float Timer;
     
    void main()
    {
       vec3 tempColor = output * abs(sin(Timer));
       tempColor.x = (sin(Timer) / 2) + 0.5f;
    //   tempColor.y = (sin(Timer) / 2) + 0.5f; // Not doing what I expect it to do
       Color = vec4(tempColor, 1.0f); 
    };

    What I am trying to accomplish is I want to make the color of each vertex change as time goes on. The issue I am having is here:

    Code :
    glGenBuffers(1, &vao);
    	glBindVertexArray(vao);
     
    	glGenBuffers(1, &vbo);
    	glBindBuffer(GL_ARRAY_BUFFER, vbo);
     
    	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);  
    	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));  
    //	glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(21 * sizeof(GLfloat))); It stores the colors in the 2nd layout but it still wouldn't work
     
    	glEnableVertexAttribArray(0);
    	glEnableVertexAttribArray(1);
    //	glEnableVertexAttribArray(2);

    Currently my hexagon only colors 2 or 3 vertices differently, the others are all the same. I am trying to separate the data into another layout but either I'm doing it wrong or the result is not what I want. I would greatly appreciate any help you could give me because I am absolutely stuck. I prefer not to generate another VBO because that may seem a bit expensive for such a simple thing, I am sure there's a trick I can pull here to make all vertices glow differently, but my knowledge of OpenGL is not decent yet, hence why I'm here seeking your help.

    Thank you very much.

  2. #2
    Newbie Newbie
    Join Date
    Feb 2018
    Posts
    12
    Hello !

    i'm a beginner myself but I am able to answer some of your question:

    1)Using multiple outputs of the Vertex shader as inputs to the Fragment shader:

    Either you just put the outputs right after another and let OpenGL shader program linke figure the location itself:

    Vertex shader:
    out vec4 color1;
    out vec4 color2;

    Fragment Shader:
    in vec4 color1;
    in vec4 color2;

    Or use GLSL "Interface Blocks":

    Vertex Shader:
    out VS_OUT{
    vec4 color1;
    vec4 color2
    }vs_out;

    Fragment Shader:
    in VS_OUT{
    vec4 color1;
    vec4 color2
    }fs_in;

    Note that you will have to refer color1 as:
    vs_out.color1 in the vertex shader
    fs_in.color1 in the fragment shader

    2)Attributes Pointer and location

    I recommend you read this post on stackoverflow:
    stackoverflow.com/questions/16380005/opengl-3-4-glvertexattribpointer-stride-and-offset-miscalculation


    I see you have chosen the "Interleaved" way of packing your data in the buffer

    I don't really understand the purpose of the second color, knowing you didn't even load some of it in the buffer.

    3)General Debugging
    First I recommend you to check shader compilation errors, here is an example implementation:

    Code :
    void checkcompile(GLuint shader) {
    		GLint isCompiled = 0;
    		glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
    		if (isCompiled == GL_FALSE)
    		{
    			GLint maxLength = 0;
    			glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
     
    			// The maxLength includes the NULL character
    			std::vector<GLchar> errorLog(maxLength);
    			glGetShaderInfoLog(shader, maxLength, &maxLength,&errorLog[0]);
     
    			string msg(errorLog.begin(), errorLog.end());
    			//the string msg is now holding the error log, print it as you want
    		}
    	}

    Secondly, on windows platform you could use some OpenGL Tracing tool like GLIntercept (Google it) to track more general OpenGL errors.

    Good luck

  3. #3
    Senior Member Regular Contributor
    Join Date
    Jul 2012
    Posts
    459
    Well, first I would try something simple:

    Draw your polygon, without any color animation, without your secondary color.

    When this works fine, then add progressively what you want.

    From what I see, and except for your second color, the VBO and pointers are set correctly. This latter looks weird, but you'll tell more once you have something working. You'll have to rename o in your fragment shader into output in order it to match the one from your vertex shader.

  4. #4
    Junior Member Newbie
    Join Date
    Mar 2017
    Posts
    20

    Reply

    Quote Originally Posted by Silence View Post
    Well, first I would try something simple:

    Draw your polygon, without any color animation, without your secondary color.

    When this works fine, then add progressively what you want.

    From what I see, and except for your second color, the VBO and pointers are set correctly. This latter looks weird, but you'll tell more once you have something working. You'll have to rename o in your fragment shader into output in order it to match the one from your vertex shader.
    Hello, thank you for your reply. I managed to get it to change colors. What I did was, I made it switch between one color output to another per time using if-else clause in my fragment shader. However, I seem to be facing another challenge. I want to be able to print out some text near my hexagon, but the text will not show. That's how I'm trying to display text:
    Code :
                   text = "Hello";
    	       glRasterPos2i(20, 20); // location to start printing text
    	       for (int i = 0; i < text.size(); ++i)
    	       glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, text[i]);

    The above code lies in my update() function which looks something like this:

    Code :
    timeSinceStart = glutGet(GLUT_ELAPSED_TIME);
    	TimeLocation = glGetUniformLocation(program, "Timer ");
    	timeSinceStart /= 1000; // Convert to MS
    	glProgramUniform1f(program, TimeLocation, timeSinceStart);
     
    	text = "Hello";
    	glRasterPos2i(20, 20); // location to start printing text
    	for (int i = 0; i < text.size(); ++i)
    		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, text[i]); // Print a character on the screen
     
    	fRotationAngle += 0.01f
    	if (fRotationAngle > 360)
    	{
    		fRotationAngle -= 360;
    	}
     
    	glutPostRedisplay(); // Tell Glut to re-draw the updated scene

    It's worth mentioning that my hexagon is rotating, and I am taking advantage of the glm library that OpenGL offers so I wouldn't have to create and define my own matrices and other mathematical-related concepts. Do you think this is a camera issue? Because if I do glLoadIdentity() all the sudden my hexagon disappears and my text appears. How can I make my text follow my hexagon and not just sit on the origin?

    Thank you for your help.

Similar Threads

  1. Struggling with ArcBall
    By wotnot in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 04-04-2016, 03:18 AM
  2. Struggling trying to use ES 2.0 and GLKView on iOS
    By Duncan Champney in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 11-11-2011, 08:50 AM
  3. drawing hexagon
    By ajbiv in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 01-31-2009, 11:54 AM
  4. Hexagon mesh
    By FurnaceMan in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 02-25-2003, 05:10 AM
  5. Hexagon sphere
    By luc_pop in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 11-04-2001, 06:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean