Hi,I try to draw a simple triangle without color attribute but nothing is rendered except
for a black screen…
#include"glew.h"
#include<gl\GL.h>
#include<gl\GLU.h>
#include<freeglut.h>
#include<iostream>
using namespace std;
GLuint positionID;
GLuint vbo = 0;
GLuint vao = 0;
GLuint ProgramID = 0;
void reshape(int x, int y)
{
glViewport(0, 0, x, y);
}
void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(ProgramID);
glDrawArrays(GL_TRIANGLES, 0, 3);
glutSwapBuffers();
}
void TestCompile(GLuint ShaderID)
{
GLint status;
glGetShaderiv(ShaderID, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE)
{
cout << “Compile Failed” << endl;
GLint infoLogLength;
glGetShaderiv(ShaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
cout << infoLogLength << endl;
GLchar* strInfoLog = new GLchar[infoLogLength + 1];
glGetShaderInfoLog(ShaderID, infoLogLength, NULL, strInfoLog);
cout<<strInfoLog;
}
else if (status == GL_TRUE)
{
cout << "Compiled Successfully" << endl;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(600, 400);
glutInitWindowPosition(300, 200);
glutCreateWindow(“My Window”);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glutReshapeFunc(reshape);
glewInit();
GLfloat vertex[] = { -0.5, -0.5, 0.0,
0.5, -0.5, 0.0,
0.0, 0.5, 0.0 };
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), vertex, GL_STATIC_DRAW);
ProgramID = glCreateProgram();
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
const GLchar* VertexSource = "#version 400
in vec3 position; void main(){ gl_Position = vec4(position,1.0); }";
const GLchar* FragmentSource = “#version 400
void main() { gl_FragColor = vec4(1.0,1.0,1.0,1.0); }”;
glShaderSource(VertexShaderID, 1, (const GLchar**)&VertexSource, 0);
glShaderSource(FragmentShaderID, 1, (const GLchar**)&FragmentSource, 0);
glCompileShader(VertexShaderID);
TestCompile(VertexShaderID);
glCompileShader(FragmentShaderID);
TestCompile(FragmentShaderID);
glAttachShader(ProgramID, VertexShaderID);
glAttachShader(ProgramID, FragmentShaderID);
glLinkProgram(ProgramID);
positionID = glGetAttribLocation(ProgramID, "position");
glVertexAttribPointer(positionID, 3*sizeof(GLfloat), GL_FLOAT, GL_FALSE, sizeof(vertex), 0);
glutDisplayFunc(render);
glutMainLoop();
return 0;
}