hi all.
ive been having trouble with this code. im 99% sure it has to do with my shaders, but i cant figure out the problem. its driving me nuts! i thought id ask here before setting up a mini program to check the shader compile logs and such, i will if you guys cant figure it out, or if you tell me to not be lazy and just check the logs.
im not sure if its a compiler problem either, so if you could try compiling this code in your compiler and letting me know if it works for you then thanks.
here is the code (written in c):
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum VAO_IDs {Triangles, NumVAOs};
enum Buffer_IDs {ArrayBuffer, NumBuffers};
enum Attrib_IDs { vPosition = 0};
GLuint VAOs[NumVAOs];
GLuint Buffers[NumBuffers];
const GLuint NumVertices = 6;
void init();
void display();
int main(int argc, char **argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(512,512);
glutInitContextVersion(4,3);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow("first opengl program");
if (glewInit()) {
exit(1);
}
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void init()
{
const char fshader[] =
"#version 430 core
"
"out vec4 fColor;
"
"void main()
"
"{
"
"fColor = vec4 (0.0, 0.0, 1.0, 1.0);
"
"}
";
const char vshader[] =
"#version 430 core
"
"layout (location = 0) in vec4 vPosition;
"
"void main()
"
"{
"
"gl_Position = vPosition;
"
"}
";
GLuint 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, (const GLchar**)&vshader, &vlen);
glShaderSource(frag, 1, (const GLchar**)&fshader, &flen);
glCompileShader(vert);
glCompileShader(frag);
glAttachShader(prog, vert);
glAttachShader(prog, frag);
glLinkProgram(prog);
glUseProgram(prog);
glGenVertexArrays(NumVAOs, VAOs);
glBindVertexArray(VAOs[Triangles]);
GLfloat vertices[6][4] = {
{-0.90, -0.90},
{0.85, -0.90},
{-0.90, 0.85},
{0.90, -0.85},
{0.90, 0.90},
{-0.85, 0.90}
};
glGenBuffers(NumBuffers, Buffers);
glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0,(void *)0);
glEnableVertexAttribArray(vPosition);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAOs[Triangles]);
glDrawArrays(GL_TRIANGLES, 0, NumVertices);
glFlush();
}
thanks.