Created
June 23, 2013 09:50
-
-
Save roxlu/5844450 to your computer and use it in GitHub Desktop.
Setting color bit depth. The GLFW 3.0.0 release used a color bit depth of 5,6,5 by default. You should update to 3.0.2 to fix this or set the bit depth yourself. Download the latest from https://github.com/glfw/glfw. This source is a test case I used to figure out why my colors/texcoords were interpolating wrongly. Using this dir structure:
src/…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 2.8) | |
set(bd ${CMAKE_CURRENT_LIST_DIR}/) | |
set(src | |
${bd}/src/main.cpp | |
) | |
find_library(fr_co Cocoa) | |
find_library(fr_io IOKit) | |
find_library(fr_cf CoreFoundation) | |
find_library(fr_gl OpenGL) | |
set(libs | |
${bd}/glfw/lib/libglfw3.a | |
${fr_co} | |
${fr_io} | |
${fr_cf} | |
${fr_gl} | |
) | |
include_directories(${bd}/glfw/include | |
${fr_gl}/Headers/ | |
) | |
add_executable(test ${src}) | |
target_link_libraries(test ${libs}) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
d=${PWD} | |
if [ ! -d ${d}/build ] ; then | |
mkdir ${d}/build | |
fi | |
cd ${d}/build | |
cmake ../ | |
make -j2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <assert.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <vector> | |
//#define USE_565 | |
#define WIN_W 720 | |
#define WIN_H 1280 | |
#define GLFW_INCLUDE_GLCOREARB | |
#include <glfw3/glfw3.h> | |
# define eglGetShaderInfoLog( Shader ) \ | |
{ \ | |
GLint Status, Count; \ | |
GLchar *Error; \ | |
\ | |
glGetShaderiv( Shader, GL_COMPILE_STATUS, &Status ); \ | |
\ | |
if ( !Status ) \ | |
{ \ | |
glGetShaderiv( Shader, GL_INFO_LOG_LENGTH, &Count ); \ | |
\ | |
if ( Count > 0 ) \ | |
{ \ | |
Error = (GLchar *)malloc(Count); \ | |
glGetShaderInfoLog( Shader, Count, NULL, Error ); \ | |
printf( "%s\n", Error ); \ | |
free( Error ); \ | |
assert( 0 ); \ | |
} \ | |
} \ | |
} | |
# define eglGetShaderLinkLog(id) \ | |
{ \ | |
GLint ret = 0; \ | |
glGetProgramiv(id, GL_LINK_STATUS, &ret); \ | |
\ | |
if(ret == false) { \ | |
GLsizei chars_written = 0; \ | |
GLint log_length = 0; \ | |
glGetProgramiv(id, GL_INFO_LOG_LENGTH, &log_length); \ | |
if(log_length > 0) { \ | |
GLchar* buffer = new GLchar[log_length]; \ | |
glGetProgramInfoLog(id, 2048, &chars_written, buffer); \ | |
printf("\n%s\n\n", buffer); \ | |
delete[] buffer; \ | |
} \ | |
} \ | |
} | |
static const char* VS = "" | |
"#version 150\n" | |
"uniform mat4 u_pm;" | |
"in vec4 a_pos; " | |
"in vec2 a_tex; " | |
"out vec2 v_tex; " | |
"void main() {" | |
" gl_Position = u_pm * a_pos; " | |
" v_tex = a_tex;" | |
"}"; | |
static const char* FS = "" | |
"#version 150\n" | |
"in vec2 v_tex; " | |
"out vec4 fragcolor; " | |
"void main() {" | |
" fragcolor.r = v_tex.s; " | |
" fragcolor.g = v_tex.t; " | |
" fragcolor.b = 0.0;" | |
" fragcolor.a = 1.0; " | |
"}"; | |
GLuint prog; | |
GLuint vert; | |
GLuint frag; | |
GLuint vao; | |
GLuint vbo; | |
float ortho_matrix[16]; | |
struct Vec3 { | |
Vec3(float x, float y, float z):x(x),y(y),z(z){} | |
float x,y,z; | |
}; | |
struct Vec2 { | |
Vec2(float s, float t):s(s),t(t){} | |
float s,t; | |
}; | |
struct Vertex { | |
Vertex(Vec3 p, Vec2 t, float s):pos(p),tex(t),perc(s){} | |
Vec3 pos; | |
Vec2 tex; | |
float perc; | |
}; | |
static GLuint rx_create_shader(const char* vs, const char* fs, GLuint& vertID, GLuint& fragID) { | |
vertID = glCreateShader(GL_VERTEX_SHADER); | |
fragID = glCreateShader(GL_FRAGMENT_SHADER); | |
glShaderSource(vertID, 1, &vs, NULL); | |
glShaderSource(fragID, 1, &fs, NULL); | |
glCompileShader(vertID); eglGetShaderInfoLog(vertID); | |
glCompileShader(fragID); eglGetShaderInfoLog(fragID); | |
GLuint prog = glCreateProgram(); | |
glAttachShader(prog, vertID); | |
glAttachShader(prog, fragID); | |
return prog; | |
} | |
static void rx_ortho(float l, float r, float b, float t, float n, float f, float* dest) { | |
dest[0] = (2.0f / (r - l)); | |
dest[1] = 0.0f; | |
dest[2] = 0.0f; | |
dest[3] = 0.0f; | |
dest[4] = 0.0f; | |
dest[5] = (2.0f / (t - b)); | |
dest[6] = 0.0f; | |
dest[7] = 0.0f; | |
dest[8] = 0.0f; | |
dest[9] = 0.0f; | |
dest[10] = (-2.0f / (f - n)); | |
dest[11] = 0.0f; | |
dest[12] = - ((r + l) / (r - l)); | |
dest[13] = - ((t + b) / (t - b)); | |
dest[14] = - ((f + n) / (f - n)); | |
dest[15] = 1.0f; | |
} | |
static void draw(void) { | |
glClearColor(0.1, 0.2, 0.3, 1.0); | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glDrawArrays(GL_TRIANGLES, 0, 6); | |
} | |
int main() { | |
printf("GLFW texture coord interpolation test.\n"); | |
GLFWwindow* window; | |
int width, height; | |
if(!glfwInit()) { | |
fprintf( stderr, "Failed to initialize GLFW\n" ); | |
exit( EXIT_FAILURE ); | |
} | |
int major, minor, rev; | |
glfwGetVersion(&major, &minor, &rev); | |
printf("GLFW %d.%d.%d\n", major, minor, rev); | |
glfwWindowHint(GLFW_DEPTH_BITS, 32); | |
#if defined(USE_565) | |
glfwWindowHint(GLFW_RED_BITS, 5); | |
glfwWindowHint(GLFW_GREEN_BITS, 6); | |
glfwWindowHint(GLFW_BLUE_BITS, 5); | |
#else | |
glfwWindowHint(GLFW_RED_BITS, 8); | |
glfwWindowHint(GLFW_GREEN_BITS, 8); | |
glfwWindowHint(GLFW_BLUE_BITS, 8); | |
glfwWindowHint(GLFW_ALPHA_BITS, 8); | |
#endif | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); | |
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); | |
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, 1); | |
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); | |
window = glfwCreateWindow(WIN_W, WIN_H, "TEST", NULL, NULL ); | |
if(!window) { | |
fprintf( stderr, "Failed to open GLFW window\n" ); | |
glfwTerminate(); | |
exit( EXIT_FAILURE ); | |
} | |
glfwMakeContextCurrent(window); | |
glfwSwapInterval( 1 ); | |
glfwGetFramebufferSize(window, &width, &height); | |
// ------------------------------------------------------------------- | |
// a quad :) | |
float w = WIN_W; | |
float h = WIN_H; | |
std::vector<Vertex> verts; | |
Vertex a(Vec3(0, 0, 0), Vec2(0.0, 0.0), 0.0); | |
Vertex b(Vec3(w, 0, 0), Vec2(1.0, 0.0), 0.0); | |
Vertex c(Vec3(w, h, 0), Vec2(1.0, 1.0), 0.0); | |
Vertex d(Vec3(0, h, 0), Vec2(0.0, 1.0), 0.0); | |
verts.push_back(a); | |
verts.push_back(b); | |
verts.push_back(c); | |
verts.push_back(a); | |
verts.push_back(c); | |
verts.push_back(d); | |
// shader | |
prog = rx_create_shader(VS, FS, vert, frag); | |
glBindAttribLocation(prog, 0, "a_pos"); | |
glBindAttribLocation(prog, 1, "a_tex"); | |
glLinkProgram(prog); eglGetShaderLinkLog(prog); | |
glUseProgram(prog); | |
GLint u_pm = glGetUniformLocation(prog, "u_pm"); assert(u_pm >= 0); | |
rx_ortho(0, WIN_W, WIN_H, 0, -1.0, 1.0, ortho_matrix); | |
glUniformMatrix4fv(u_pm, 1, GL_FALSE, ortho_matrix); | |
// buffer | |
glGenVertexArrays(1, &vao); | |
glBindVertexArray(vao); | |
glGenBuffers(1, &vbo); | |
glBindBuffer(GL_ARRAY_BUFFER, vbo); | |
glBufferData(GL_ARRAY_BUFFER, verts.size() * sizeof(Vertex), &verts[0].pos.x, GL_STATIC_DRAW); | |
glEnableVertexAttribArray(0); | |
glEnableVertexAttribArray(1); | |
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)0); | |
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)12); | |
// ------------------------------------------------------------------- | |
while(!glfwWindowShouldClose(window)) { | |
draw(); | |
glfwSwapBuffers(window); | |
} | |
glfwTerminate(); | |
exit( EXIT_SUCCESS ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
d=${PWD} | |
if [ -f ${d}/build/test ] ; then | |
cd ${d}/build | |
./test | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
using 5,6,5 bit depth results in:
<img src="http://upload.roxlu.com/server/php/files/Screen%20Shot%202013-06-23%20at%2011.25.51%20AM.png" alt='" />