Hey all! Been trying for hours to make a fucking ground using two triangles and I can’t even get this example of the simplest kind to work. What am I doing wrong? Thanks.
Code in C:
#include <math.h>
#include "GL_utilities.h"
#include "loadobj.h"
#include "VectorUtils2.h"
#include "LoadTGA.h"
#define near 1.0
#define far 30.0
#define right 0.5
#define left -0.5
#define top 0.5
#define bottom -0.5
GLfloat projectionMatrix[] = { 2.0f*near/(right-left), 0.0f, (right+left)/(right-left), 0.0f,
0.0f, 2.0f*near/(top-bottom), (top+bottom)/(top-bottom), 0.0f,
0.0f, 0.0f, -(far + near)/(far - near), -2*far*near/(far - near),
0.0f, 0.0f, -1.0f, 0.0f };
GLfloat vertices[] = { -50,0,-50,
50,0,-50,
-50,0,50,
50,0,-50,
-50,0,50,
50,0,50 };
// Just an identity matrix, for now..
GLfloat total[16] = { 1.0, 0, 0, 0,
0, 1.0, 0, 0,
0, 0, 1.0, 0,
0, 0, 0, 1.0
};
GLfloat cam[16];
unsigned int groundVertexArrayObjID;
GLuint program;
void OnTimer(int value)
{
glutPostRedisplay();
}
void init(void)
{
// ground
unsigned int groundVertexBufferObjID;
dumpInfo();
// GL inits
glClearColor(1.0, 1.0, 1.0, 1.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
printError("GL inits");
// Load and compile shader
program = loadShaders("lab2-1.vert", "lab2-1.frag");
printError("init shader");
// Ground
glGenVertexArrays(1, &groundVertexArrayObjID);
glBindVertexArray(groundVertexArrayObjID);
// Allocate Vertex Buffer Objects
glGenBuffers(1, &groundVertexBufferObjID);
// Ground vertex data
glBindBuffer(GL_ARRAY_BUFFER, groundVertexBufferObjID);
glBufferData(GL_ARRAY_BUFFER, 6*3*sizeof(GLfloat), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(glGetAttribLocation(program, "in_Position"), 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(glGetAttribLocation(program, "in_Position"));
glUniformMatrix4fv(glGetUniformLocation(program, "projectionMatrix"), 1, GL_TRUE, projectionMatrix);
// Cam settings
Point3D p = {0,0,5};
Point3D l = {0,0,-15};
lookAt(&p, &l, 0, 1, 0, cam);
glUniformMatrix4fv(glGetUniformLocation(program, "cam"), 1, GL_TRUE, cam);
printError("init arrays");
}
void display(void)
{
printError("pre display");
glClear(GL_DEPTH_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT);
glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, total);
glBindVertexArray(groundVertexArrayObjID); // Select VAO
glDrawArrays(GL_TRIANGLES, 0, 6);
printError("display");
glFlush();
glutTimerFunc(20, &OnTimer, 0);
}
int main(int argc, const char *argv[])
{
glutInit(&argc, argv);
glutCreateWindow ("GL3 white triangle example");
glutDisplayFunc(display);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
init ();
glutMainLoop();
}
Vertex shader:
#version 150
uniform mat4 projectionMatrix;
uniform mat4 cam;
uniform mat4 mdlMatrix;
in vec3 in_Position;
void main(void)
{
gl_Position = projectionMatrix * cam * mdlMatrix * vec4(in_Position, 1.0);
}
Fragment shader:
#version 150
uniform sampler2D texUnit;
in vec3 outColor;
in vec2 outTexCoord;
out vec4 out_Color;
void main(void)
{
out_Color = vec4(1.0,0.0,0.0,0.5);
}