Draw a simple quad for now to make sure things are working
This commit is contained in:
parent
b6110e3c73
commit
e1878bc565
114
main.c
114
main.c
@ -54,39 +54,66 @@ int main(void) {
|
|||||||
|
|
||||||
SDL_GL_SetSwapInterval(1);
|
SDL_GL_SetSwapInterval(1);
|
||||||
|
|
||||||
int w, h, c;
|
// int w, h, c;
|
||||||
unsigned char *terrain_img = stbi_load("terrain.png", &w, &h, &c, 0);
|
// unsigned char *terrain_img = stbi_load("terrain.png", &w, &h, &c, 0);
|
||||||
|
int w = 20;
|
||||||
|
int h = 20;
|
||||||
int vcount = w * h * 3;
|
int vcount = w * h * 3;
|
||||||
float *verts = malloc(vcount * sizeof(float));
|
float *verts = malloc(vcount * sizeof(float));
|
||||||
float scale_y = 1.0f;
|
// float scale_y = 1.0f;
|
||||||
float shift_y = 16.0f;
|
// float shift_y = 16.0f;
|
||||||
for (int row = 0; row < h; row++) {
|
// for (int row = 0; row < h; row++) {
|
||||||
for (int col = 0; col < w; col++) {
|
// for (int col = 0; col < w; col++) {
|
||||||
unsigned char *texel = terrain_img + (row * w + col) * c;
|
// unsigned char *texel = terrain_img + (row * w + col) * c;
|
||||||
unsigned char y = texel[0];
|
// unsigned char y = texel[0];
|
||||||
|
|
||||||
|
// float *v = &verts[(row * w + col) * 3];
|
||||||
|
// *(v+0) = -w/2 + col;
|
||||||
|
// *(v+1) = y * scale_y + shift_y;
|
||||||
|
// *(v+2) = -h/2 + row;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// for (int row = 0; row < h; row++) {
|
||||||
|
// for (int col = 0; col < w; col++) {
|
||||||
|
// float *v = &verts[(row * w + col) * 3];
|
||||||
|
// *(v+0) = -w/2 + col;
|
||||||
|
// *(v+1) = 0.0f;
|
||||||
|
// *(v+2) = -h/2 + row;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
float quadVerts[] = {
|
||||||
|
// Position
|
||||||
|
0.5f, 0.5f, 0.0f,
|
||||||
|
0.5f, -0.5f, 0.0f,
|
||||||
|
-0.5f, -0.5f, 0.0f,
|
||||||
|
-0.5f, 0.5f, 0.0f,
|
||||||
|
};
|
||||||
|
|
||||||
|
unsigned int quadIndices[] = {
|
||||||
|
0, 1, 3, // first triangle
|
||||||
|
1, 2, 3 // second triangle
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
float *v = &verts[(row * w + col) * 3];
|
|
||||||
*(v+0) = -w/2 + col;
|
|
||||||
*(v+1) = y * scale_y + shift_y;
|
|
||||||
*(v+2) = -h/2 + row;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
printf("Loaded %d vertices\n", w * h);
|
printf("Loaded %d vertices\n", w * h);
|
||||||
stbi_image_free(terrain_img);
|
// stbi_image_free(terrain_img);
|
||||||
|
|
||||||
unsigned int icount = w * (h - 1) * 2;
|
// unsigned int icount = w * (h - 1) * 2;
|
||||||
unsigned int *indices = malloc(icount * sizeof(unsigned int));
|
// unsigned int *indices = malloc(icount * sizeof(unsigned int));
|
||||||
for (int row = 0; row < h - 1; row++) {
|
// for (int row = 0; row < h - 1; row++) {
|
||||||
for (int col = 0; col < w; col++) {
|
// for (int col = 0; col < w; col++) {
|
||||||
unsigned int idx1 = w * (row + 0) + col;
|
// unsigned int idx1 = w * (row + 0) + col;
|
||||||
unsigned int idx2 = w * (row + 1) + col;
|
// unsigned int idx2 = w * (row + 1) + col;
|
||||||
*(indices + idx1) = idx1;
|
// *(indices + idx1) = idx1;
|
||||||
*(indices + idx2) = idx2;
|
// *(indices + idx2) = idx2;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
const unsigned int NUM_STRIPS = h - 1;
|
// const unsigned int NUM_STRIPS = h - 1;
|
||||||
const unsigned int NUM_VERTS_PER_STRIP = w * 2;
|
// const unsigned int NUM_VERTS_PER_STRIP = w * 2;
|
||||||
|
|
||||||
unsigned int terrainVAO, terrainVBO, terrainIBO;
|
unsigned int terrainVAO, terrainVBO, terrainIBO;
|
||||||
glGenVertexArrays(1, &terrainVAO);
|
glGenVertexArrays(1, &terrainVAO);
|
||||||
@ -94,7 +121,8 @@ int main(void) {
|
|||||||
|
|
||||||
glGenBuffers(1, &terrainVBO);
|
glGenBuffers(1, &terrainVBO);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, terrainVBO);
|
glBindBuffer(GL_ARRAY_BUFFER, terrainVBO);
|
||||||
glBufferData(GL_ARRAY_BUFFER, vcount * sizeof(float), verts, GL_STATIC_DRAW);
|
// glBufferData(GL_ARRAY_BUFFER, vcount * sizeof(float), verts, GL_STATIC_DRAW);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVerts), quadVerts, GL_STATIC_DRAW);
|
||||||
|
|
||||||
// position attribute
|
// position attribute
|
||||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
||||||
@ -102,7 +130,7 @@ int main(void) {
|
|||||||
|
|
||||||
glGenBuffers(1, &terrainIBO);
|
glGenBuffers(1, &terrainIBO);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, terrainIBO);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, terrainIBO);
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, icount * sizeof(unsigned), indices, GL_STATIC_DRAW);
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(quadIndices), quadIndices, GL_STATIC_DRAW);
|
||||||
// Disable depth test and face culling.
|
// Disable depth test and face culling.
|
||||||
glDisable(GL_DEPTH_TEST);
|
glDisable(GL_DEPTH_TEST);
|
||||||
glDisable(GL_CULL_FACE);
|
glDisable(GL_CULL_FACE);
|
||||||
@ -118,22 +146,30 @@ int main(void) {
|
|||||||
glm_mat4_identity(projection);
|
glm_mat4_identity(projection);
|
||||||
|
|
||||||
float aspectRatio = (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT;
|
float aspectRatio = (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT;
|
||||||
glm_perspective(45.0f, aspectRatio, 0.1f, 100.0f, projection);
|
glm_perspective(30.0f, aspectRatio, 1.0f, 1000.0f, projection);
|
||||||
glUseProgram(shaderProgram);
|
glUseProgram(shaderProgram);
|
||||||
SDL_Event e;
|
SDL_Event e;
|
||||||
bool quit = false;
|
bool quit = false;
|
||||||
Uint64 now = SDL_GetPerformanceCounter();
|
Uint64 now = SDL_GetPerformanceCounter();
|
||||||
Uint64 last = 0;
|
Uint64 last = 0;
|
||||||
double dt = 0;
|
double dt = 0;
|
||||||
|
bool draw_wireframe = false;
|
||||||
float x = 0.0f, y = 0.0f;
|
float x = 0.0f, y = 0.0f;
|
||||||
while (quit == false) {
|
while (quit == false) {
|
||||||
last = now;
|
last = now;
|
||||||
now = SDL_GetPerformanceCounter();
|
now = SDL_GetPerformanceCounter();
|
||||||
dt = (double)((now - last)*1000 / (double)SDL_GetPerformanceFrequency());
|
dt = (double)((now - last)*1000 / (double)SDL_GetPerformanceFrequency());
|
||||||
|
(void)dt;
|
||||||
while (SDL_PollEvent( &e)) {
|
while (SDL_PollEvent( &e)) {
|
||||||
if (e.type == SDL_QUIT) {
|
if (e.type == SDL_QUIT) {
|
||||||
quit = true;
|
quit = true;
|
||||||
|
} else if (e.type == SDL_KEYDOWN) {
|
||||||
|
char key = e.key.keysym.sym;
|
||||||
|
if (key == '1') {
|
||||||
|
draw_wireframe = !draw_wireframe;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
char key = e.key.keysym.sym;
|
char key = e.key.keysym.sym;
|
||||||
float speed = 1.1f;
|
float speed = 1.1f;
|
||||||
if (key == 'w') {
|
if (key == 'w') {
|
||||||
@ -169,12 +205,20 @@ int main(void) {
|
|||||||
|
|
||||||
glViewport(0, 0, screen_width, screen_height);
|
glViewport(0, 0, screen_width, screen_height);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
for (unsigned strip = 0; strip < NUM_STRIPS; strip++) {
|
// for (unsigned strip = 0; strip < NUM_STRIPS; strip++) {
|
||||||
glDrawElements(GL_TRIANGLE_STRIP, // primitive type
|
// glDrawElements(GL_TRIANGLE_STRIP, // primitive type
|
||||||
NUM_VERTS_PER_STRIP, // number of indices to render
|
// NUM_VERTS_PER_STRIP, // number of indices to render
|
||||||
GL_UNSIGNED_INT, // index data type
|
// GL_UNSIGNED_INT, // index data type
|
||||||
(void*)(sizeof(unsigned) * (NUM_VERTS_PER_STRIP+2) * strip)); // offset to starting index
|
// (void*)(sizeof(unsigned) * (NUM_VERTS_PER_STRIP) * strip)); // offset to starting index
|
||||||
|
// }
|
||||||
|
|
||||||
|
if (draw_wireframe) {
|
||||||
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||||
|
} else {
|
||||||
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||||
}
|
}
|
||||||
|
glBindVertexArray(terrainVAO);
|
||||||
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
||||||
|
|
||||||
SDL_GL_SwapWindow(window);
|
SDL_GL_SwapWindow(window);
|
||||||
}
|
}
|
||||||
|
@ -4,5 +4,6 @@ out vec4 FragColor;
|
|||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
float h = (Height + 16)/32.0f; // shift and scale the height into a grayscale value
|
float h = (Height + 16)/32.0f; // shift and scale the height into a grayscale value
|
||||||
FragColor = vec4(h, h, h, 1.0);
|
// FragColor = vec4(h, h, h, 1.0);
|
||||||
|
FragColor = vec4(1, 1, 1, 1.0);
|
||||||
}
|
}
|
||||||
|
@ -7,5 +7,5 @@ uniform mat4 projection;
|
|||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
Height = pos.y;
|
Height = pos.y;
|
||||||
gl_Position = projection * view * model * vec4(pos, 1.0);
|
gl_Position = vec4(pos, 1.0);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user