PDA

View Full Version : this program output triangle without color,



lelieven
01-18-2014, 07:23 AM
this vertex shader
basic.vert

#version 330

layout(location= 0) in vec3 VertexPosition;
layout(location= 1) in vec3 VertexColor;

out vec3 Color;

void main()
{
Color=VertexColor;
gl_Position = vec4(VertexPosition,1.0);
}

this fragment shader
basic.frag

#version 330

in vec3 Color;
out vec4 FragColor;

void main()
{
FragColor =vec4(Color,1.0);
}



shader.c

#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>

#include "readtext.h"

#ifdef __APPLE__
#include <glut/glut.h>
#else
#define FREEGLUT_STATIC
#include <GL/glut.h>
#endif

#define BUFFER_OFFSET(i) ((char *)NULL + (i))

#pragma comment(lib,"../readtext.lib")
#pragma comment(lib,"../glew32.lib")
#pragma comment(lib,"freeglut_static.lib")


GLuint v, f; //着色器对象
GLuint p;

GLuint vaoHandle;
GLuint vboHandles[2], positionBufferHandle, colorBufferHandle;
//顶点位置数组
float positionData[9]={
-0.8f, -0.8f, 0.0f,
0.8f, -0.8f, 0.0f,
0.0f, 0.8f, 0.0f };

float colorData[9]={
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f };





void initShader()
{
char *vs,*fs;
vs=NULL;
fs=NULL;


v=glCreateShader(GL_VERTEX_SHADER);
f=glCreateShader(GL_FRAGMENT_SHADER);

vs=readText("../basic.vert");
fs=readText("../basic.frag");



glShaderSource(v,1,&vs,NULL);
glShaderSource(f,1,&fs,NULL);

free(vs);
free(fs);

glCompileShader(v);
glCompileShader(f);

p=glCreateProgram();


glAttachShader(p,v);
glAttachShader(p,f);
glLinkProgram(p);
glUseProgram(p);

}


//初始化VBO
initVBO()
{
/////////////////////////////////////////////////////////////////////////////////////////////

glGenBuffers(2,vboHandles);

positionBufferHandle = vboHandles[0];
colorBufferHandle = vboHandles[1];
//创建两个顶点缓冲对象

glBindBuffer(GL_ARRAY_BUFFER, positionBufferHandle);
glBindBuffer(GL_ARRAY_BUFFER, colorBufferHandle);


//绑定VBO以供使用并加载顶点数

glGenVertexArrays(1, &vaoHandle);
glBindVertexArray(vaoHandle);
//创建VAO对象

glEnableVertexAttribArray(0); //顶点坐标



//调用glVertexAttribPointer之前需要进行绑定操作

glBindBuffer(GL_ARRAY_BUFFER, positionBufferHandle);
glBufferData(GL_ARRAY_BUFFER, 9*sizeof(float), positionData, GL_STATIC_DRAW );
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));//顶点属性值传到着色器



glBindBuffer(GL_ARRAY_BUFFER, colorBufferHandle);
glBufferData(GL_ARRAY_BUFFER, 9*sizeof(float), colorData, GL_STATIC_DRAW);
glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 0,BUFFER_OFFSET(sizeof(positionData)));
glEnableVertexAttribArray(1); //颜色坐标
}


void init()
{
glewInit();

initShader();

initVBO();

glClearColor(0.0,0.0,0.0,0.0);
}




void display()
{
glClear(GL_COLOR_BUFFER_BIT);

glBindVertexArray(vaoHandle);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);

glutSwapBuffers();
}





int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(600,600);
glutInitWindowPosition(100,100);
glutCreateWindow("GLSL Test : Draw a triangle");
init();
glutDisplayFunc(display);


glutMainLoop();
return 0;
}