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/main.o \
|
||||||
src/glad.o \
|
src/glad.o \
|
||||||
src/opengl.o \
|
src/opengl.o \
|
||||||
|
$(patsubst %.glsl,%.glsl.o,$(wildcard src/shader/*.glsl)) \
|
||||||
$(GLFW)
|
$(GLFW)
|
||||||
|
|
||||||
main: $(MAIN_OBJS)
|
main: $(MAIN_OBJS)
|
||||||
|
|||||||
@ -4,21 +4,25 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
unsigned int compile_shader(const void * vp,
|
typedef unsigned int uint;
|
||||||
const int vp_length,
|
|
||||||
const void * fp,
|
|
||||||
const int fp_length);
|
|
||||||
|
|
||||||
int make_buffer(unsigned int target,
|
uint compile_shader(const void * vp,
|
||||||
const void * data,
|
const int vp_length,
|
||||||
size_t size);
|
const void * fp,
|
||||||
|
const int fp_length);
|
||||||
|
|
||||||
int make_texture(const void * data,
|
uint make_buffer(unsigned int target,
|
||||||
int internalformat,
|
const void * data,
|
||||||
int width,
|
size_t size);
|
||||||
int height,
|
|
||||||
int format,
|
uint make_texture(const void * data,
|
||||||
int type);
|
int internalformat,
|
||||||
|
int width,
|
||||||
|
int height,
|
||||||
|
int format,
|
||||||
|
int type);
|
||||||
|
|
||||||
|
uint make_framebuffer(uint * texture, int length);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
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
|
||||||
79
src/main.c
79
src/main.c
@ -8,7 +8,8 @@
|
|||||||
|
|
||||||
#include "opengl.h"
|
#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_width = 800;
|
||||||
int vp_height = 600;
|
int vp_height = 600;
|
||||||
@ -19,6 +20,13 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
|||||||
vp_height = height;
|
vp_height = height;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const float triangle_array[] = {
|
||||||
|
-1, -1,
|
||||||
|
1, -1,
|
||||||
|
-1, 1,
|
||||||
|
1, 1
|
||||||
|
};
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_X11);
|
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_X11);
|
||||||
@ -51,12 +59,12 @@ int main()
|
|||||||
|
|
||||||
const char * data = "arst";
|
const char * data = "arst";
|
||||||
|
|
||||||
make_texture(data,
|
uint texture_input = make_texture(data,
|
||||||
GL_R8, // internalformat
|
GL_R8, // internalformat
|
||||||
2, // width
|
2, // width
|
||||||
2, // height
|
2, // height
|
||||||
GL_RED,
|
GL_RED,
|
||||||
GL_UNSIGNED_BYTE);
|
GL_UNSIGNED_BYTE);
|
||||||
|
|
||||||
uint texture_framebuffer = make_texture(NULL,
|
uint texture_framebuffer = make_texture(NULL,
|
||||||
GL_RGBA32F,
|
GL_RGBA32F,
|
||||||
@ -65,30 +73,63 @@ int main()
|
|||||||
GL_RGBA,
|
GL_RGBA,
|
||||||
GL_UNSIGNED_BYTE);
|
GL_UNSIGNED_BYTE);
|
||||||
|
|
||||||
uint fp_framebuffer;
|
uint framebuffer = make_framebuffer(&texture_framebuffer, 1);
|
||||||
glGenFramebuffers(1, &fp_framebuffer);
|
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, fp_framebuffer);
|
|
||||||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture_framebuffer, 0);
|
|
||||||
|
|
||||||
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
//////////////////////////////////////////////////////////////////////
|
||||||
assert(status == GL_FRAMEBUFFER_COMPLETE);
|
// shaders
|
||||||
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
uint draw_buffers[1] = {GL_COLOR_ATTACHMENT0};
|
uint program_test = compile_shader(src_shader_test_vs_glsl_start,
|
||||||
glDrawBuffers(1, draw_buffers);
|
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)) {
|
while(!glfwWindowShouldClose(window)) {
|
||||||
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||||
glfwSetWindowShouldClose(window, true);
|
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, 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);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
glUseProgram(program_test);
|
||||||
|
glBindVertexArray(vertex_array);
|
||||||
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||||
|
|
||||||
//glFlush();
|
//glFlush();
|
||||||
//glFinish();
|
//glFinish();
|
||||||
|
|
||||||
|
/*
|
||||||
float out[4 * 2 * 2] = {0};
|
float out[4 * 2 * 2] = {0};
|
||||||
glReadPixels(0, 0,
|
glReadPixels(0, 0,
|
||||||
2, 2,
|
2, 2,
|
||||||
@ -99,8 +140,10 @@ int main()
|
|||||||
for (int i = 0; i < 4 * 2 * 2; i++) {
|
for (int i = 0; i < 4 * 2 * 2; i++) {
|
||||||
printf("%f\n", out[i]);
|
printf("%f\n", out[i]);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
//printf("swap\n");
|
||||||
|
|
||||||
break;
|
glfwSwapBuffers(window);
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
48
src/opengl.c
48
src/opengl.c
@ -1,12 +1,14 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
#include "glad.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 int vp_length,
|
||||||
const void * fp,
|
const void * fp,
|
||||||
const int fp_length)
|
const int fp_length)
|
||||||
{
|
{
|
||||||
unsigned int vertex_shader;
|
unsigned int vertex_shader;
|
||||||
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
|
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
|
||||||
@ -59,23 +61,25 @@ unsigned int compile_shader(const void * vp,
|
|||||||
return shader_program;
|
return shader_program;
|
||||||
}
|
}
|
||||||
|
|
||||||
int make_buffer(unsigned int target,
|
uint make_buffer(unsigned int target,
|
||||||
const void * data,
|
const void * data,
|
||||||
size_t size)
|
size_t size)
|
||||||
{
|
{
|
||||||
unsigned int buffer;
|
unsigned int buffer;
|
||||||
glGenBuffers(1, &buffer);
|
glGenBuffers(1, &buffer);
|
||||||
glBindBuffer(target, buffer);
|
glBindBuffer(target, buffer);
|
||||||
glBufferData(target, size, data, GL_STATIC_DRAW);
|
glBufferData(target, size, data, GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
glBindBuffer(target, 0);
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
int make_texture(const void * data,
|
uint make_texture(const void * data,
|
||||||
int internalformat,
|
int internalformat,
|
||||||
int width,
|
int width,
|
||||||
int height,
|
int height,
|
||||||
int format,
|
int format,
|
||||||
int type)
|
int type)
|
||||||
{
|
{
|
||||||
unsigned int texture;
|
unsigned int texture;
|
||||||
glGenTextures(1, &texture);
|
glGenTextures(1, &texture);
|
||||||
@ -88,5 +92,23 @@ int make_texture(const void * data,
|
|||||||
|
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, internalformat, width, height, 0, format, type, data);
|
glTexImage2D(GL_TEXTURE_2D, 0, internalformat, width, height, 0, format, type, data);
|
||||||
|
|
||||||
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
return texture;
|
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