try to move a triangle in the scene,but in qualifier don't work

I create a triangle in the scene.And use a in variable to move the triangle’s position.But it doesn’t work.I don’t know why.
I have attached my code.Please help me

#include <stdio.h>
#include <string.h>
#include <math.h>

#include "GL\gl3w.h"
//#include "GLFW\glad.h"
#include "GLFW\glfw3.h"

GLuint          program;
GLuint          vao;
GLFWwindow		*window;

void startup()
{
	static const char * vs_source[] =
	{
		"#version 410 core                             
"
		"                                              
"
		"layout (location = 0) in vec4 offset;           
"
		"void main(void)                               
"
		"{                                             
"
		"    vec4 vertices[3] = vec4[](vec4( 0.25, -0.25, 0.5, 1.0), 
"
		"							   vec4(-0.25, -0.25, 0.5, 1.0),
"
		"							   vec4(0.25, 0.25, 0.5, 1.0)); 
"
		"    gl_Position = vertices[gl_VertexID] +offset ;       
"
		"}                                             
"
	};

	static const char * fs_source[] =
	{
		"#version 410 core                             
"
		"                                              
"
		"out vec4 color;                               
"
		"                                              
"
		"void main(void)                               
"
		"{                                             
"
		"    color = vec4(0.0, 0.8, 1.0, 1.0);         
"
		"}                                             
"
	};

	program = glCreateProgram();
	GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(fs, 1, fs_source, NULL);
	glCompileShader(fs);

	GLuint vs = glCreateShader(GL_VERTEX_SHADER);
	glShaderSource(vs, 1, vs_source, NULL);
	glCompileShader(vs);

	glAttachShader(program, vs);
	glAttachShader(program, fs);

	glLinkProgram(program);

	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);
}

void render(double currentTime) {
	GLfloat color[] = { sin(currentTime)*0.5+0.5, cos(currentTime)*0.5f+0.5f, 0.0f, 1.0f };
	glClearBufferfv(GL_COLOR, 0, color);

	glUseProgram(program);

	//glPointSize(40.0f);
	GLfloat attrib[] = { (float)sin(currentTime) * 0.5f,
		(float)cos(currentTime) * 0.6f,
		0.0f, 0.0f };
	
	glVertexAttrib4fv(0, attrib);

	glDrawArrays(GL_TRIANGLES, 0, 3);
}

void shutdown()
{
	glDeleteVertexArrays(1, &vao);
	glDeleteProgram(program);
}

int main(int argc, char *argv[])
{
	bool running = true;

	/*glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(400, 400);
	glutCreateWindow("OpenGL");*/

	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
	glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
	glfwWindowHint(GLFW_CONTEXT_ROBUSTNESS, GLFW_LOSE_CONTEXT_ON_RESET);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
	glfwWindowHint(GLFW_SAMPLES, 0);
	glfwWindowHint(GLFW_STEREO, GL_FALSE);
	window = glfwCreateWindow(800, 600, "OpenGL SuperBible - Single Point", NULL, NULL);
	if (!window)
	{
		printf("Failed to open window
");
		return 0;
	}
	glfwMakeContextCurrent(window);
	gl3wInit();

	startup();

	do
	{
		render(glfwGetTime());

		glfwSwapBuffers(window);
		glfwPollEvents();

		running &= (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_RELEASE);
		running &= (glfwWindowShouldClose(window) != GL_TRUE);
	} while (running);
	shutdown();
	glfwDestroyWindow(window);
	glfwTerminate();

	/*glutDisplayFunc(Show);
	glutMainLoop();*/

	return 0;
}

I try to move the triangle by passing offset to shader.But it seems the shader dones’t get the right value.

Does anyone know what is the problem?

[QUOTE=peng_can;1290924]I create a triangle in the scene.And use a in variable to move the triangle’s position.But it doesn’t work.I don’t know why.
I have attached my code.Please help me

#include <stdio.h>
#include <string.h>
#include <math.h>

#include "GL\gl3w.h"
//#include "GLFW\glad.h"
#include "GLFW\glfw3.h"

GLuint          program;
GLuint          vao;
GLFWwindow		*window;

void startup()
{
	static const char * vs_source[] =
	{
		"#version 410 core                             
"
		"                                              
"
		"layout (location = 0) in vec4 offset;           
"
		"void main(void)                               
"
		"{                                             
"
		"    vec4 vertices[3] = vec4[](vec4( 0.25, -0.25, 0.5, 1.0), 
"
		"							   vec4(-0.25, -0.25, 0.5, 1.0),
"
		"							   vec4(0.25, 0.25, 0.5, 1.0)); 
"
		"    gl_Position = vertices[gl_VertexID] +offset ;       
"
		"}                                             
"
	};

	static const char * fs_source[] =
	{
		"#version 410 core                             
"
		"                                              
"
		"out vec4 color;                               
"
		"                                              
"
		"void main(void)                               
"
		"{                                             
"
		"    color = vec4(0.0, 0.8, 1.0, 1.0);         
"
		"}                                             
"
	};

	program = glCreateProgram();
	GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(fs, 1, fs_source, NULL);
	glCompileShader(fs);

	GLuint vs = glCreateShader(GL_VERTEX_SHADER);
	glShaderSource(vs, 1, vs_source, NULL);
	glCompileShader(vs);

	glAttachShader(program, vs);
	glAttachShader(program, fs);

	glLinkProgram(program);

	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);
}

void render(double currentTime) {
	GLfloat color[] = { sin(currentTime)*0.5+0.5, cos(currentTime)*0.5f+0.5f, 0.0f, 1.0f };
	glClearBufferfv(GL_COLOR, 0, color);

	glUseProgram(program);

	//glPointSize(40.0f);
	GLfloat attrib[] = { (float)sin(currentTime) * 0.5f,
		(float)cos(currentTime) * 0.6f,
		0.0f, 0.0f };
	
	glVertexAttrib4fv(0, attrib);

	glDrawArrays(GL_TRIANGLES, 0, 3);
}

void shutdown()
{
	glDeleteVertexArrays(1, &vao);
	glDeleteProgram(program);
}

int main(int argc, char *argv[])
{
	bool running = true;

	/*glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(400, 400);
	glutCreateWindow("OpenGL");*/

	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
	glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
	glfwWindowHint(GLFW_CONTEXT_ROBUSTNESS, GLFW_LOSE_CONTEXT_ON_RESET);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
	glfwWindowHint(GLFW_SAMPLES, 0);
	glfwWindowHint(GLFW_STEREO, GL_FALSE);
	window = glfwCreateWindow(800, 600, "OpenGL SuperBible - Single Point", NULL, NULL);
	if (!window)
	{
		printf("Failed to open window
");
		return 0;
	}
	glfwMakeContextCurrent(window);
	gl3wInit();

	startup();

	do
	{
		render(glfwGetTime());

		glfwSwapBuffers(window);
		glfwPollEvents();

		running &= (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_RELEASE);
		running &= (glfwWindowShouldClose(window) != GL_TRUE);
	} while (running);
	shutdown();
	glfwDestroyWindow(window);
	glfwTerminate();

	/*glutDisplayFunc(Show);
	glutMainLoop();*/

	return 0;
}

[/QUOTE]

I am not an OpenGL expert, but the way you are doing things I haven’t seen so far in any tutorial for OGL >4. So all I can do for you is to make some suggestions.

I have never used glVertexAttrib4fv but this link (click me) here gives me the impression that you are using it wrong. This command seems to be intended for blocks of glBegin and glEnd but as I said: Not an expert here.

Then you are creating a VAO but basically doing nothing with it. I think you are mixing some commands (glVertexAttrib4fv) and objects (Vertex Array Object) that are not supposed to work together. You need to do some setup with a VAO before you can use it. First you need to create a buffer object that stores the values of your vertices. Then you need to activate the attributes that you want to use on your VAO and bind the buffer object to those attributes. Just google “vertex array object tutorial” and you will find enough to read about it.

Furthermore I think you are using vertex attributes / arrays for something they are not meant for. If you just want to add an offset every frame you should use a uniform variable or uniform buffer object for this purpose The latter one is more complicated, so google the first one first.

What you should also do (probably the first thing) is to call glGetError. If it does not return GL_NO_ERROR then you are doing something wrong with the OpenGL API (see my statement about glVertexAttrib4fv). In case of an error, you can query the error message. Have a look here: https://blog.nobel-joergensen.com/2013/01/29/debugging-opengl-using-glgeterror/

In general I would advise to get a good book that leads you through the whole learning process. Google: “ObenGL Cookbook second edition”. It is available for free as PDF (as far as I know - I bought it :stuck_out_tongue: ) and shows you a lot of stuff from setting up a program until advanced stuff like shadow mapping and deferred rendering. The things you want to do are already described in the first chapter.

[QUOTE=ProgrammerX;1290946]I am not an OpenGL expert, but the way you are doing things I haven’t seen so far in any tutorial for OGL >4. So all I can do for you is to make some suggestions.

I have never used glVertexAttrib4fv but this link (click me) here gives me the impression that you are using it wrong. This command seems to be intended for blocks of glBegin and glEnd but as I said: Not an expert here.

Then you are creating a VAO but basically doing nothing with it. I think you are mixing some commands (glVertexAttrib4fv) and objects (Vertex Array Object) that are not supposed to work together. You need to do some setup with a VAO before you can use it. First you need to create a buffer object that stores the values of your vertices. Then you need to activate the attributes that you want to use on your VAO and bind the buffer object to those attributes. Just google “vertex array object tutorial” and you will find enough to read about it.

Furthermore I think you are using vertex attributes / arrays for something they are not meant for. If you just want to add an offset every frame you should use a uniform variable or uniform buffer object for this purpose The latter one is more complicated, so google the first one first.

What you should also do (probably the first thing) is to call glGetError. If it does not return GL_NO_ERROR then you are doing something wrong with the OpenGL API (see my statement about glVertexAttrib4fv). In case of an error, you can query the error message. Have a look here: https://blog.nobel-joergensen.com/2013/01/29/debugging-opengl-using-glgeterror/

In general I would advise to get a good book that leads you through the whole learning process. Google: “ObenGL Cookbook second edition”. It is available for free as PDF (as far as I know - I bought it :stuck_out_tongue: ) and shows you a lot of stuff from setting up a program until advanced stuff like shadow mapping and deferred rendering. The things you want to do are already described in the first chapter.[/QUOTE]

Thanks for your advise.It’s very kind of you.
I read the book OpenGL.Superbible.7th.Edition.This book use the opengl 4.5.
Actually the code don’t have any problem.It seem’s to be my video card issue.After I update it’s driver version to the latest ,the code run properly.
Thanks all the same for your kind suggestion