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;
}