Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shader textures #100

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
80e03d8
getters: fix glGetBoolean for GL_UNPACK_LSB_FIRST and GL_PACK_SWAP_BYTES
mardy Jan 16, 2025
5e4a4c8
Move draw_arrays_general() function to avoid forward declaration
mardy Jan 3, 2025
4008a94
Store drawing parameters into a struct
mardy Jan 3, 2025
599200a
glArrayElement() can also be called outside glBegin/glEnd
mardy Jan 8, 2025
1049d12
glArrayElement(): the glVertex command must be issued last
mardy Jan 8, 2025
a6c0595
Fix reading of vertex components
mardy Jan 8, 2025
cf093f7
arrays: build vertex attributes readers at drawing time
mardy Jan 9, 2025
639aa52
Implement getters for vertex attribute arrays
mardy Jan 9, 2025
78571e7
functions: get ready to expose OpenGL 2.0 functions
mardy Jan 6, 2025
153e480
getters: set GL_VERSION to 2.0 if glUseProgram is available
mardy Jan 6, 2025
a9b5052
lighting: move computation of color channels count to new function
mardy Jan 12, 2025
9fa885a
Add ogx_set_projection() function
mardy Jan 6, 2025
097be65
Refactor array readers API
mardy Jan 14, 2025
b4d0b5a
arrays: remove attribute readers from the global state
mardy Jan 18, 2025
8308d30
arrays: store readers into an array
mardy Jan 18, 2025
93259d1
Add murmurhash3 hashing functions
mardy Jan 20, 2025
40defed
include: update glext file with latest from Mesa
mardy Jan 23, 2025
ae8fded
Add support for OpenGL 2.0
mardy Jan 1, 2025
d08303c
examples: add OPenGL 2.0 example with SDL2
mardy Jan 21, 2025
44dc385
build: add src/ directory to library include directories
mardy Jan 23, 2025
a8ba386
examples: make the gl2gears example work with opengx
mardy Jan 23, 2025
25b8d89
examples: add joystick support to gl2gears
mardy Jan 23, 2025
6b3454e
arrays: store VBO at array definition time
mardy Jan 27, 2025
0cf188c
examples: add OpenGL 2.0 texturing example
mardy Jan 27, 2025
5b75eb2
arrays: support constant arrays with color data
mardy Jan 27, 2025
707d695
shader: support retrieving data from GL_SAMPLER_* uniforms
mardy Jan 27, 2025
d667dcb
shader: add a public API for retrieving the GXTexObj
mardy Jan 27, 2025
5f3205d
examples: make the cube_tex example work on the console
mardy Jan 27, 2025
defe5d1
shaders: rename public APIs for matrix management
mardy Feb 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ project(OpenGX VERSION 0.1)

option(BUILD_OPENGX "Build the opengx library" ON)
option(BUILD_DOCS "Build the documentation" OFF)
option(BUILD_EXAMPLES "Build the examples" OFF)

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
Expand Down Expand Up @@ -38,13 +39,21 @@ add_library(${TARGET} STATIC
src/gpu_resources.h
src/image_DXT.c
src/image_DXT.h
src/murmurhash3.cpp
src/murmurhash3.h
src/opengx.h
src/pixel_stream.cpp
src/pixel_stream.h
src/pixels.cpp
src/pixels.h
src/raster.cpp
src/selection.c
src/shader.c
src/shader.h
src/shader_api.c
src/shader_attribute.cpp
src/shader_functions.h
src/shader_uniform.c
src/state.h
src/stencil.c
src/stencil.h
Expand All @@ -64,6 +73,7 @@ set_target_properties(${TARGET} PROPERTIES
)

target_include_directories(${TARGET} PUBLIC
src/
${CMAKE_CURRENT_SOURCE_DIR}/include
)

Expand All @@ -75,7 +85,7 @@ install(TARGETS ${TARGET}
RUNTIME
PUBLIC_HEADER
)
install(DIRECTORY "include/GL" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
install(DIRECTORY "include/GL" "include/KHR" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/opengl.pc
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
install(FILES OpenGLConfig.cmake
Expand All @@ -87,3 +97,7 @@ if(BUILD_DOCS)
include(UseLATEX)
add_subdirectory(doc/src)
endif()

if(BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
9 changes: 9 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
find_package(PkgConfig REQUIRED)
pkg_check_modules(SDL2 IMPORTED_TARGET sdl2)
pkg_check_modules(OPENGL IMPORTED_TARGET opengl)

add_subdirectory(common)

if(SDL2_FOUND)
add_subdirectory(sdl2)
endif()
40 changes: 40 additions & 0 deletions examples/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
pkg_check_modules(PNG IMPORTED_TARGET libpng)

set(EXTRA_LIBS)
if(CMAKE_SYSTEM_NAME MATCHES "NintendoWii|NintendoGameCube")
list(APPEND EXTRA_LIBS opengx)
else()
list(APPEND EXTRA_LIBS PkgConfig::OPENGL)
endif()

function(add_resource out_var)
set(result)
foreach(in_f ${ARGN})
string(MAKE_C_IDENTIFIER ${in_f} input_identifier)
set(out_f "${CMAKE_CURRENT_BINARY_DIR}/${input_identifier}.o")

add_custom_command(
OUTPUT ${out_f}
COMMAND ${CMAKE_LINKER} -z noexecstack --relocatable --format binary --output ${out_f} ${in_f}
DEPENDS ${in_f}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Embedding ${in_f} as a resource"
)
list(APPEND result ${out_f})
endforeach()
set(${out_var} ${result} PARENT_SCOPE)
endfunction()

if(PNG_FOUND)
add_resource(TEXTURES
grid512.png
mix256.png
)
add_library(textures STATIC
${TEXTURES}
textures.c
textures.h
)
target_link_libraries(textures PUBLIC ${EXTRA_LIBS} PkgConfig::PNG)
target_include_directories(textures PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
endif()
Binary file added examples/common/grid512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/common/mix256.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions examples/common/textures.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*****************************************************************************
Copyright (c) 2024 Alberto Mardegan ([email protected])

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of copyright holders nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/

#include "textures.h"

#include <png.h>
#include <stdlib.h>
#include <string.h>

GLuint textures_load_range(const char *start, const char *end)
{
png_image png;
memset(&png, 0, sizeof(png));
png.version = PNG_IMAGE_VERSION;
png_image_begin_read_from_memory(&png, start, end - start);
png.format = PNG_FORMAT_RGBA;

char *buffer = malloc(PNG_IMAGE_SIZE(png));
png_image_finish_read(&png, NULL, buffer, 0, NULL);

GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, png.width, png.height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, buffer);
png_image_free(&png);
return texture;
}
53 changes: 53 additions & 0 deletions examples/common/textures.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*****************************************************************************
Copyright (c) 2024 Alberto Mardegan ([email protected])

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of copyright holders nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#ifndef TEXTURES_H
#define TEXTURES_H

#include <GL/gl.h>

#ifdef __cplusplus
extern "C" {
#endif

#define DECLARE_EMBEDDED_FILE(basename) \
extern char _binary_ ## basename ## _start[], _binary_ ## basename ## _end[]

DECLARE_EMBEDDED_FILE(grid512_png);
DECLARE_EMBEDDED_FILE(mix256_png);

#define textures_load(basename) \
textures_load_range(_binary_ ## basename ## _start, \
_binary_ ## basename ## _end)

GLuint textures_load_range(const char *start, const char *end);

#ifdef __cplusplus
} // extern C
#endif

#endif /* TEXTURES_H */
1 change: 1 addition & 0 deletions examples/sdl2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(opengl20)
26 changes: 26 additions & 0 deletions examples/sdl2/opengl20/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
set(EXTRA_FILES)
set(EXTRA_LIBS)
if(CMAKE_SYSTEM_NAME MATCHES "NintendoWii|NintendoGameCube")
list(APPEND EXTRA_FILES opengx_shaders.c)
list(APPEND EXTRA_LIBS opengx)
else()
list(APPEND EXTRA_LIBS PkgConfig::OPENGL)
endif()

macro(add_example name)
add_executable(${name} ${EXTRA_FILES} ${ARGN})
if (TARGET textures)
list(APPEND EXTRA_LIBS textures)
endif()
if (TARGET PkgConfig::GLM)
list(APPEND EXTRA_LIBS PkgConfig::GLM)
endif()
target_link_libraries(${name} ${EXTRA_LIBS} m PkgConfig::SDL2)
endmacro()

pkg_check_modules(GLM IMPORTED_TARGET glm)

if(GLM_FOUND)
add_example(cube_tex cube_tex.cpp)
endif()
add_example(gl2gears gl2gears.c)
Loading