trying out gouraud shading, multiple enabled vertex attributes

hi there. im using ubuntu 14.04, GLEW and freeglut. i have the latest nvidia drivers. i’m wondering why this code i wrote isn’t working… its in c

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#include <GL/glew.h>
#include <GL/freeglut.h>

GLuint vao, buff;

void init();
void display();

int main (int argc, char **argv)
{
	glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutInitWindowSize(500, 500);
    glutInitContextVersion(4,3);
    glutInitContextProfile(GLUT_CORE_PROFILE);

    glutCreateWindow("Test Programs");
    
	if (glewInit() != GLEW_OK)
	{
		printf("glew could not load");
		exit(1);
	}
	
	init();
	
	glutDisplayFunc(display);
	
	glutMainLoop();
}

void init()
{
	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);
	
	
	GLfloat vertices[][2] = {
		{-0.90, -0.90},
		{0.85, -0.90},
		{-0.90, 0.85},
		{0.90, -0.85},
		{0.90, 0.90},
		{-0.85, 0.90}
	};
	
	GLubyte colors[][4] = {
		{255, 0, 0, 255},
		{0, 255, 0, 255},
		{0, 0, 255, 255},
		{10, 10, 10, 255},
		{100, 100, 100, 255},
		{255, 255, 255, 255}
	};
	
	glGenBuffers(1, &buff);
	glBindBuffer(GL_ARRAY_BUFFER, buff);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices) + sizeof(colors), NULL, GL_STATIC_DRAW);
	glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
	glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices), sizeof(colors), colors);
	
	const char *vshader =

		"#version 430 core
"
		"layout (location = 0) in vec2 vPosition;
"
		"layout (location = 1) in vec4 vColor;
"
		"out vec4 color;
"
	    "void main()
"
		"{
"
		"	color = vColor;
"
		"	gl_Position = vPosition;
"
		"}
";

	const char *fshader = 
		"#version 430 core
"
		"in vec4 color;
"
		"out vec4 fColor;
"
		"void main()
"
		"{
"
		"	fColor = color;
"
		"}
";

	GLuint prog;
	prog = glCreateProgram();

	GLuint vert = glCreateShader(GL_VERTEX_SHADER);
	GLuint frag = glCreateShader(GL_FRAGMENT_SHADER);

	GLint vlen = (GLint)strlen(vshader);
	GLint flen = (GLint)strlen(fshader);

	glShaderSource(vert, 1, &vshader, &vlen);
	glShaderSource(frag, 1, &fshader, &flen);
	glCompileShader(vert);
	glCompileShader(frag);


	glAttachShader(prog, vert);
	glAttachShader(prog, frag);
	glLinkProgram(prog);
	glUseProgram(prog);
	
	glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, (void *)sizeof(vertices));
	glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void *)0);
	
	glEnableVertexAttribArray(1);
	glEnableVertexAttribArray(0);
}

void display()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glBindVertexArray(vao);
	glDrawArrays(GL_TRIANGLES, 0 , 6);
	glutSwapBuffers();
}

};
its my first time trying to enable multiple vertex attributes at the same time so i can get the gouraud shading thing to work.
it compiles but i get the “Segmentation fault (core dumped)” message when i run it.
is it something wrong with my shaders?
array buffer?
vertex attributes?

thanks

[QUOTE=sandbucket;1260978]hi there. im using ubuntu 14.04, GLEW and freeglut. i have the latest nvidia drivers. i’m wondering why this code i wrote isn’t working… its in c

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#include <GL/glew.h>
#include <GL/freeglut.h>

GLuint vao, buff;

void init();
void display();

int main (int argc, char **argv)
{
	glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutInitWindowSize(500, 500);
    glutInitContextVersion(4,3);
    glutInitContextProfile(GLUT_CORE_PROFILE);

    glutCreateWindow("Test Programs");
    
	if (glewInit() != GLEW_OK)
	{
		printf("glew could not load");
		exit(1);
	}
	
	init();
	
	glutDisplayFunc(display);
	
	glutMainLoop();
}

void init()
{
	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);
	
	
	GLfloat vertices[][2] = {
		{-0.90, -0.90},
		{0.85, -0.90},
		{-0.90, 0.85},
		{0.90, -0.85},
		{0.90, 0.90},
		{-0.85, 0.90}
	};
	
	GLubyte colors[][4] = {
		{255, 0, 0, 255},
		{0, 255, 0, 255},
		{0, 0, 255, 255},
		{10, 10, 10, 255},
		{100, 100, 100, 255},
		{255, 255, 255, 255}
	};
	
	glGenBuffers(1, &buff);
	glBindBuffer(GL_ARRAY_BUFFER, buff);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices) + sizeof(colors), NULL, GL_STATIC_DRAW);
	glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
	glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices), sizeof(colors), colors);
	
	const char *vshader =

		"#version 430 core
"
		"layout (location = 0) in vec2 vPosition;
"
		"layout (location = 1) in vec4 vColor;
"
		"out vec4 color;
"
	    "void main()
"
		"{
"
		"	color = vColor;
"
		"	gl_Position = vPosition;
"
		"}
";

	const char *fshader = 
		"#version 430 core
"
		"in vec4 color;
"
		"out vec4 fColor;
"
		"void main()
"
		"{
"
		"	fColor = color;
"
		"}
";

	GLuint prog;
	prog = glCreateProgram();

	GLuint vert = glCreateShader(GL_VERTEX_SHADER);
	GLuint frag = glCreateShader(GL_FRAGMENT_SHADER);

	GLint vlen = (GLint)strlen(vshader);
	GLint flen = (GLint)strlen(fshader);

	glShaderSource(vert, 1, &vshader, &vlen);
	glShaderSource(frag, 1, &fshader, &flen);
	glCompileShader(vert);
	glCompileShader(frag);


	glAttachShader(prog, vert);
	glAttachShader(prog, frag);
	glLinkProgram(prog);
	glUseProgram(prog);
	
	glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, (void *)sizeof(vertices));
	glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void *)0);
	
	glEnableVertexAttribArray(1);
	glEnableVertexAttribArray(0);
}

void display()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glBindVertexArray(vao);
	glDrawArrays(GL_TRIANGLES, 0 , 6);
	glutSwapBuffers();
}

};
its my first time trying to enable multiple vertex attributes at the same time so i can get the gouraud shading thing to work.
it compiles but i get the “Segmentation fault (core dumped)” message when i run it.
is it something wrong with my shaders?
array buffer?
vertex attributes?

thanks[/QUOTE]

first of all gl_Position is a vec4 built-in variable so you are sending a vec2 to a vec4, you need to cast:


gl_Position = vec4(vPosition, 0.0, 1.0); // z value = 0.0 if you want a 2D mesh, for what i know is better to set w value = 1.0f every time

//all this because gl_Position is like write

out vec4 position;
void main()
{
//... blah blah

position = vec4(vPosition, 0.0, 1.0);

}

about the error you get it seems a core error, honestly i can only immagine what could be: it could be your glutInitContext function, probably your driver can’t handle opengl 4.3, or shader version. try to set shader version to 330 core, you don’t need 430 core for what you have to do, if the error remain try to set glutInitContext(3,3) too and see what happen

another thing: if nothing of this solutions works, try to replace your glutInitContextProfile() line with this two lines:


	glutInitContextFlags(GLUT_CORE_PROFILE | GLUT_DEBUG);
	glutInitContextProfile(GLUT_FORWARD_COMPATIBLE);