draw triangle strip
This commit is contained in:
parent
4edb4a3449
commit
75d1a0db40
1
Makefile
1
Makefile
@ -131,6 +131,7 @@ MAIN_OBJS = \
|
||||
src/main.o \
|
||||
src/glad.o \
|
||||
src/opengl.o \
|
||||
$(patsubst %.glsl,%.glsl.o,$(wildcard src/shader/*.glsl)) \
|
||||
$(GLFW)
|
||||
|
||||
main: $(MAIN_OBJS)
|
||||
|
||||
@ -4,22 +4,26 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
unsigned int compile_shader(const void * vp,
|
||||
typedef unsigned int uint;
|
||||
|
||||
uint compile_shader(const void * vp,
|
||||
const int vp_length,
|
||||
const void * fp,
|
||||
const int fp_length);
|
||||
|
||||
int make_buffer(unsigned int target,
|
||||
uint make_buffer(unsigned int target,
|
||||
const void * data,
|
||||
size_t size);
|
||||
|
||||
int make_texture(const void * data,
|
||||
uint make_texture(const void * data,
|
||||
int internalformat,
|
||||
int width,
|
||||
int height,
|
||||
int format,
|
||||
int type);
|
||||
|
||||
uint make_framebuffer(uint * texture, int length);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
19
include/shader/test.fs.glsl.h
Normal file
19
include/shader/test.fs.glsl.h
Normal file
@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern uint32_t _binary_src_shader_test_fs_glsl_start __asm("_binary_src_shader_test_fs_glsl_start");
|
||||
extern uint32_t _binary_src_shader_test_fs_glsl_end __asm("_binary_src_shader_test_fs_glsl_end");
|
||||
extern uint32_t _binary_src_shader_test_fs_glsl_size __asm("_binary_src_shader_test_fs_glsl_size");
|
||||
|
||||
#define src_shader_test_fs_glsl_start ((const char *)&_binary_src_shader_test_fs_glsl_start)
|
||||
#define src_shader_test_fs_glsl_end ((const char *)&_binary_src_shader_test_fs_glsl_end)
|
||||
#define src_shader_test_fs_glsl_size (src_shader_test_fs_glsl_end - src_shader_test_fs_glsl_start)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
19
include/shader/test.vs.glsl.h
Normal file
19
include/shader/test.vs.glsl.h
Normal file
@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern uint32_t _binary_src_shader_test_vs_glsl_start __asm("_binary_src_shader_test_vs_glsl_start");
|
||||
extern uint32_t _binary_src_shader_test_vs_glsl_end __asm("_binary_src_shader_test_vs_glsl_end");
|
||||
extern uint32_t _binary_src_shader_test_vs_glsl_size __asm("_binary_src_shader_test_vs_glsl_size");
|
||||
|
||||
#define src_shader_test_vs_glsl_start ((const char *)&_binary_src_shader_test_vs_glsl_start)
|
||||
#define src_shader_test_vs_glsl_end ((const char *)&_binary_src_shader_test_vs_glsl_end)
|
||||
#define src_shader_test_vs_glsl_size (src_shader_test_vs_glsl_end - src_shader_test_vs_glsl_start)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
69
src/main.c
69
src/main.c
@ -8,7 +8,8 @@
|
||||
|
||||
#include "opengl.h"
|
||||
|
||||
typedef unsigned int uint;
|
||||
#include "shader/test.vs.glsl.h"
|
||||
#include "shader/test.fs.glsl.h"
|
||||
|
||||
int vp_width = 800;
|
||||
int vp_height = 600;
|
||||
@ -19,6 +20,13 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
||||
vp_height = height;
|
||||
}
|
||||
|
||||
const float triangle_array[] = {
|
||||
-1, -1,
|
||||
1, -1,
|
||||
-1, 1,
|
||||
1, 1
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_X11);
|
||||
@ -51,7 +59,7 @@ int main()
|
||||
|
||||
const char * data = "arst";
|
||||
|
||||
make_texture(data,
|
||||
uint texture_input = make_texture(data,
|
||||
GL_R8, // internalformat
|
||||
2, // width
|
||||
2, // height
|
||||
@ -65,30 +73,63 @@ int main()
|
||||
GL_RGBA,
|
||||
GL_UNSIGNED_BYTE);
|
||||
|
||||
uint fp_framebuffer;
|
||||
glGenFramebuffers(1, &fp_framebuffer);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, fp_framebuffer);
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture_framebuffer, 0);
|
||||
uint framebuffer = make_framebuffer(&texture_framebuffer, 1);
|
||||
|
||||
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
||||
assert(status == GL_FRAMEBUFFER_COMPLETE);
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// shaders
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
uint draw_buffers[1] = {GL_COLOR_ATTACHMENT0};
|
||||
glDrawBuffers(1, draw_buffers);
|
||||
uint program_test = compile_shader(src_shader_test_vs_glsl_start,
|
||||
src_shader_test_vs_glsl_size,
|
||||
src_shader_test_fs_glsl_start,
|
||||
src_shader_test_fs_glsl_size);
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// buffers
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
uint vertex_buffer = make_buffer(GL_ARRAY_BUFFER,
|
||||
triangle_array,
|
||||
(sizeof (triangle_array)));
|
||||
|
||||
uint vertex_array;
|
||||
glGenVertexArrays(1, &vertex_array);
|
||||
glBindVertexArray(vertex_array);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
|
||||
glVertexAttribPointer(0,
|
||||
2,
|
||||
GL_FLOAT,
|
||||
GL_FALSE,
|
||||
(sizeof (float)) * 2,
|
||||
(void*)0
|
||||
);
|
||||
glEnableVertexAttribArray(0);
|
||||
glBindVertexArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
while(!glfwWindowShouldClose(window)) {
|
||||
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, fp_framebuffer);
|
||||
/*
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
|
||||
uint draw_buffers[1] = {GL_COLOR_ATTACHMENT0};
|
||||
glDrawBuffers(1, draw_buffers);
|
||||
glViewport(0, 0, 2, 2);
|
||||
*/
|
||||
glViewport(0, 0, vp_width, vp_height);
|
||||
|
||||
glClearColor(0.1, 0.2, 0.3, 0.4);
|
||||
glClearColor(0.1, 0.2, 0.0, 0.4);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glUseProgram(program_test);
|
||||
glBindVertexArray(vertex_array);
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||
|
||||
//glFlush();
|
||||
//glFinish();
|
||||
|
||||
/*
|
||||
float out[4 * 2 * 2] = {0};
|
||||
glReadPixels(0, 0,
|
||||
2, 2,
|
||||
@ -99,8 +140,10 @@ int main()
|
||||
for (int i = 0; i < 4 * 2 * 2; i++) {
|
||||
printf("%f\n", out[i]);
|
||||
}
|
||||
*/
|
||||
//printf("swap\n");
|
||||
|
||||
break;
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
|
||||
28
src/opengl.c
28
src/opengl.c
@ -1,9 +1,11 @@
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "glad.h"
|
||||
#include "opengl.h"
|
||||
|
||||
unsigned int compile_shader(const void * vp,
|
||||
uint compile_shader(const void * vp,
|
||||
const int vp_length,
|
||||
const void * fp,
|
||||
const int fp_length)
|
||||
@ -59,7 +61,7 @@ unsigned int compile_shader(const void * vp,
|
||||
return shader_program;
|
||||
}
|
||||
|
||||
int make_buffer(unsigned int target,
|
||||
uint make_buffer(unsigned int target,
|
||||
const void * data,
|
||||
size_t size)
|
||||
{
|
||||
@ -67,10 +69,12 @@ int make_buffer(unsigned int target,
|
||||
glGenBuffers(1, &buffer);
|
||||
glBindBuffer(target, buffer);
|
||||
glBufferData(target, size, data, GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(target, 0);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
int make_texture(const void * data,
|
||||
uint make_texture(const void * data,
|
||||
int internalformat,
|
||||
int width,
|
||||
int height,
|
||||
@ -88,5 +92,23 @@ int make_texture(const void * data,
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, internalformat, width, height, 0, format, type, data);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
return texture;
|
||||
}
|
||||
|
||||
uint make_framebuffer(uint * texture, int length)
|
||||
{
|
||||
uint framebuffer;
|
||||
glGenFramebuffers(1, &framebuffer);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, texture[i], 0);
|
||||
}
|
||||
|
||||
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
||||
assert(status == GL_FRAMEBUFFER_COMPLETE);
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
return framebuffer;
|
||||
}
|
||||
|
||||
10
src/shader/test.fs.glsl
Normal file
10
src/shader/test.fs.glsl
Normal file
@ -0,0 +1,10 @@
|
||||
#version 330 core
|
||||
|
||||
out vec4 fragment_color;
|
||||
|
||||
in vec2 f_position;
|
||||
|
||||
void main()
|
||||
{
|
||||
fragment_color = vec4(f_position, 0, 0);
|
||||
}
|
||||
12
src/shader/test.vs.glsl
Normal file
12
src/shader/test.vs.glsl
Normal file
@ -0,0 +1,12 @@
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec2 position;
|
||||
|
||||
out vec2 f_position;
|
||||
|
||||
void main()
|
||||
{
|
||||
f_position = position;
|
||||
|
||||
gl_Position = vec4(position, 0, 1);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user