Skip to content

Instantly share code, notes, and snippets.

@chebert
Created January 13, 2013 07:22
Show Gist options
  • Select an option

  • Save chebert/4522806 to your computer and use it in GitHub Desktop.

Select an option

Save chebert/4522806 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <GL/glew.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
char *file2string(const char *path)
{
FILE *fd;
long len,
r;
char *str;
if (!(fd = fopen(path, "r")))
{
fprintf(stderr, "Can't open file '%s' for reading\n", path);
return NULL;
}
fseek(fd, 0, SEEK_END);
len = ftell(fd);
printf("File '%s' is %ld long\n", path, len);
fseek(fd, 0, SEEK_SET);
if (!(str = (char*)malloc(len * sizeof(char))))
{
fprintf(stderr, "Can't malloc space for '%s'\n", path);
return NULL;
}
r = fread(str, sizeof(char), len, fd);
str[r - 1] = '\0'; /* Shader sources have to term with null */
fclose(fd);
return str;
}
GLuint uProjMatrix = 42, aPosition = 42;
void SetProjectionMatrix() {
glm::mat4 projection = glm::mat4(1.0f);
glUniformMatrix4fv(uProjMatrix, 1, false, glm::value_ptr(projection));
}
void Initialize() {
if (!SDL_SetVideoMode(400, 400, 0, SDL_OPENGL)) {
fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
glewInit();
}
GLuint InstallAndUseShaders() {
GLuint sp = glCreateProgram();
char *vsSource = file2string("wave.vert");
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, (const GLchar**)&vsSource, NULL);
glCompileShader(vs);
glAttachShader(sp, vs);
free(vsSource);
char *fsSource = file2string("wave.frag");
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, (const GLchar**)&fsSource, NULL);
glCompileShader(fs);
glAttachShader(sp, fs);
free(fsSource);
glLinkProgram(sp);
glUseProgram(sp);
std::cout << uProjMatrix << std::endl;
uProjMatrix = glGetUniformLocation(sp, "uProjMatrix");
aPosition = glGetAttribLocation(sp, "aPosition");
return sp;
}
int main(int argc, char **argv)
{
Initialize();
GLuint sp = InstallAndUseShaders();
SetProjectionMatrix();
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClearDepth(1.0f);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
GLuint h_vbo, h_ibo;
GLfloat vbo[] = {
-10, -10, 0,
0, -10, 0,
0, 0, 0,
};
unsigned short ibo[] = {
0, 1, 2,
};
glGenBuffers(1, &h_vbo);
glBindBuffer(GL_ARRAY_BUFFER, h_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vbo), vbo, GL_STATIC_DRAW);
glGenBuffers(1, &h_ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, h_ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(ibo), ibo, GL_STATIC_DRAW);
/* Main loop */
SDL_Event sdlEv;
while (true) {
while (SDL_PollEvent(&sdlEv))
switch (sdlEv.type) {
case SDL_QUIT: exit(0);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* draw */
glEnableVertexAttribArray(aPosition);
glBindBuffer(GL_ARRAY_BUFFER, h_vbo);
glVertexAttribPointer(aPosition, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, h_ibo);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, 0);
/* draw */
SDL_GL_SwapBuffers();
}
}
@chebert
Copy link
Copy Markdown
Author

chebert commented Jan 13, 2013

uniform mat4 uProjMatrix;
attribute vec3 aPosition;

void main(void)
{
gl_Position = uProjMatrix * gl_Vertex;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment