Compare commits
2 commits
57a6425691
...
da1fc7e72b
Author | SHA1 | Date | |
---|---|---|---|
|
da1fc7e72b | ||
|
0b9cb8cd27 |
8 changed files with 113 additions and 32 deletions
2
Makefile
2
Makefile
|
@ -14,7 +14,7 @@ EXEC_TEST=$(B_DIR)/test
|
||||||
# Build settings
|
# Build settings
|
||||||
CC=g++ -g
|
CC=g++ -g
|
||||||
# SDL options
|
# SDL options
|
||||||
CC_SDL=-lSDL2 `sdl2-config --cflags --libs` -lGLEW -lGL
|
CC_SDL=-lSDL2 `sdl2-config --cflags --libs` -lGLEW -lGL -lSOIL
|
||||||
#libraries
|
#libraries
|
||||||
LIBS=-ljsoncpp
|
LIBS=-ljsoncpp
|
||||||
|
|
||||||
|
|
30
engine/texture.cpp
Normal file
30
engine/texture.cpp
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
#include "texture.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
void texture::genTexureGpu(GLfloat pixels[]) {
|
||||||
|
//this->pixels = pixels;
|
||||||
|
glGenTextures(1, &ref);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, ref);
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 4, 3, 0, GL_RGB, GL_FLOAT, pixels);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
}
|
||||||
|
|
||||||
|
void texture::loadFromPath(std::string path) {
|
||||||
|
glGenTextures(1, &ref);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, ref);
|
||||||
|
|
||||||
|
int width, height;
|
||||||
|
unsigned char *image =
|
||||||
|
SOIL_load_image(path.c_str(), &width, &height, 0, SOIL_LOAD_RGB);
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
|
||||||
|
GL_UNSIGNED_BYTE, image);
|
||||||
|
SOIL_free_image_data(image);
|
||||||
|
//this->pixels = image;
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
}
|
20
engine/texture.hpp
Normal file
20
engine/texture.hpp
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
#ifndef __TEXTURE_H__
|
||||||
|
#define __TEXTURE_H__
|
||||||
|
|
||||||
|
#include <GL/glew.h>
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <SDL2/SDL_opengl.h>
|
||||||
|
#include <SDL2/SDL_video.h>
|
||||||
|
#include <string>
|
||||||
|
#include <SOIL/SOIL.h>
|
||||||
|
|
||||||
|
class texture{
|
||||||
|
public:
|
||||||
|
GLuint ref;
|
||||||
|
unsigned char *pixels;
|
||||||
|
|
||||||
|
void genTexureGpu(GLfloat pixels[]);
|
||||||
|
void loadFromPath(std::string path);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
BIN
sample.png
Normal file
BIN
sample.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 324 KiB |
11
shaders.json
11
shaders.json
|
@ -1,18 +1,23 @@
|
||||||
{
|
{
|
||||||
"vertex":[
|
"vertex":[
|
||||||
{"path":"shaders/vertex/simple.glsl"}
|
{"path":"shaders/vertex/simple.glsl"},
|
||||||
|
{"path":"shaders/vertex/texture.glsl"}
|
||||||
],
|
],
|
||||||
"fragment":[
|
"fragment":[
|
||||||
{
|
{
|
||||||
"path":"shaders/fragment/simple.glsl",
|
"path":"shaders/fragment/simple.glsl",
|
||||||
"framebuff_output":["outColor"]
|
"framebuff_output":["outColor"]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path":"shaders/fragment/texture.glsl",
|
||||||
|
"framebuff_output":["outColor"]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|
||||||
"combos":[
|
"combos":[
|
||||||
{
|
{
|
||||||
"vertex":0,
|
"vertex":1,
|
||||||
"fragment":0
|
"fragment":1
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
13
shaders/fragment/texture.glsl
Normal file
13
shaders/fragment/texture.glsl
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
#version 150 core
|
||||||
|
|
||||||
|
in vec3 Color;
|
||||||
|
in vec2 Texcoord;
|
||||||
|
|
||||||
|
out vec4 outColor;
|
||||||
|
|
||||||
|
uniform sampler2D tex;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
outColor = texture(tex, Texcoord) * vec4(Color, 1.0);
|
||||||
|
}
|
15
shaders/vertex/texture.glsl
Normal file
15
shaders/vertex/texture.glsl
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
#version 150 core
|
||||||
|
|
||||||
|
in vec2 position;
|
||||||
|
in vec3 color;
|
||||||
|
in vec2 texcoord;
|
||||||
|
|
||||||
|
out vec3 Color;
|
||||||
|
out vec2 Texcoord;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
Color = color;
|
||||||
|
Texcoord=texcoord;
|
||||||
|
gl_Position = vec4(position, 0.0, 1.0);
|
||||||
|
}
|
54
test.cpp
54
test.cpp
|
@ -1,6 +1,7 @@
|
||||||
#include "engine/object.hpp"
|
#include "engine/object.hpp"
|
||||||
#include "engine/rendering.hpp"
|
#include "engine/rendering.hpp"
|
||||||
#include "engine/shader.hpp"
|
#include "engine/shader.hpp"
|
||||||
|
#include "engine/texture.hpp"
|
||||||
#include <SDL2/SDL_video.h>
|
#include <SDL2/SDL_video.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
@ -14,36 +15,39 @@ int main(int argc, char *argv[]) {
|
||||||
renderer.Shaderinit();
|
renderer.Shaderinit();
|
||||||
|
|
||||||
GLfloat vertices[] = {
|
GLfloat vertices[] = {
|
||||||
-0.5f, 0.5f, 1.0f, 0.0f, 0.0f, // Top-left
|
//=========position======|===========color==============|=========texcoord
|
||||||
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, // Top-right
|
-0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Top-left
|
||||||
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, // Bottom-right
|
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Top-right
|
||||||
-0.5f, -0.5f, 1.0f, 1.0f, 1.0f // Bottom-left
|
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // Bottom-right
|
||||||
|
-0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, // Bottom-left
|
||||||
};
|
};
|
||||||
|
|
||||||
GLuint elements[] = {0, 2, 3, 1, 2, 3};
|
GLuint elements[] = {0, 1, 2, 2, 3, 0};
|
||||||
|
|
||||||
gameobject test(&renderer.shaders.at(0));
|
gameobject test(&renderer.shaders.at(0));
|
||||||
renderer.objects.push_back(test);
|
renderer.objects.push_back(test);
|
||||||
test.genVbo(vertices, 20);
|
test.genVbo(vertices, 20);
|
||||||
test.genEbo(elements, 2);
|
test.genEbo(elements, 6);
|
||||||
glBindVertexArray(test.vao);
|
glBindVertexArray(test.vao);
|
||||||
test.rend_shader->use();
|
test.rend_shader->use();
|
||||||
test.setVertexAtribPointer("position",2, false, 5, 0);
|
texture tex;
|
||||||
test.setVertexAtribPointer("color",3, false, 5, (void *)(2 * sizeof(GLfloat)));
|
|
||||||
|
|
||||||
gameobject test2(&renderer.shaders.at(0));
|
test.setVertexAtribPointer("position", 2, false, 7, 0);
|
||||||
renderer.objects.push_back(test2);
|
test.setVertexAtribPointer("color", 3, false, 7,
|
||||||
test2.genVbo(vertices, 20);
|
(void *)(2 * sizeof(GLfloat)));
|
||||||
test2.genEbo(elements, 2);
|
test.setVertexAtribPointer("texcoord", 2, false, 7,
|
||||||
glBindVertexArray(test2.vao);
|
(void *)(5 * sizeof(GLfloat)));
|
||||||
test2.rend_shader->use();
|
|
||||||
test2.setVertexAtribPointer("position",2, false, 5, 0);
|
tex.loadFromPath("sample.png");
|
||||||
test2.setVertexAtribPointer("color",2, false, 5, (void *)(2 * sizeof(GLfloat)));
|
|
||||||
|
|
||||||
SDL_Event windowEvent;
|
SDL_Event windowEvent;
|
||||||
|
GLenum err = glGetError();
|
||||||
|
if (err != GL_NO_ERROR) {
|
||||||
|
// std::cout << err << "\n" << posAttrib;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
//std::cout<<renderer.objects.at(0).get().element_number;
|
// std::cout<<renderer.objects.at(0).get().element_number;
|
||||||
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
if (SDL_PollEvent(&windowEvent)) {
|
if (SDL_PollEvent(&windowEvent)) {
|
||||||
|
@ -53,16 +57,10 @@ int main(int argc, char *argv[]) {
|
||||||
windowEvent.key.keysym.sym == SDLK_ESCAPE)
|
windowEvent.key.keysym.sym == SDLK_ESCAPE)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
// renderer.render();
|
||||||
GLenum err = glGetError();
|
test.render();
|
||||||
if (err != GL_NO_ERROR) {
|
// test2.render();
|
||||||
//std::cout << err << "\n" << posAttrib;
|
// glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, elements);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
renderer.render();
|
|
||||||
//test.render();
|
|
||||||
//test2.render();
|
|
||||||
//glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, elements);
|
|
||||||
|
|
||||||
SDL_GL_SwapWindow(window);
|
SDL_GL_SwapWindow(window);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue