The SDL forums have moved to
discourse.libsdl.org
.
This is just a read-only archive of the previous forums, to keep old links working.
SDL
Simple Directmedia Layer Forums
SDL2 iOS OpenGL es 3.0 simple triangle example not working
josh43
Joined: 14 Aug 2015
Posts: 4
Posted: Mon Aug 17, 2015 1:08 am
Hello guys, could anyone tell me why this simple example will not display anything?
Main
Code:
//
// main.cpp
// ___PROJECTNAME___
//
// Created on 8/14/15.
//
//
#include <OpenGLES/ES3/gl.h>
#include <OpenGLES/ES3/glext.h>
#include "SDL.h"
#include "Shader.h"
#define TRIANGLE_DEMO
static const GLfloat cube_vertices[] = {
// front
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
// back
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
-1.0, 1.0, -1.0,
};
static const GLushort cube_elements[] = {
// front
0, 1, 2,
2, 3, 0,
// top
3, 2, 6,
6, 7, 3,
// back
7, 6, 5,
5, 4, 7,
// bottom
4, 5, 1,
1, 0, 4,
// left
4, 0, 3,
3, 7, 4,
// right
1, 5, 6,
6, 2, 1,
};
static const GLfloat triangle[] = {
-1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
void update(void * window){
}
void cubeDrawer(SDL_Window * window){
SDL_Window * win = ((SDL_Window *)window);
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SDL_GL_SwapWindow(win);
}
void triangleDrawer(SDL_Window * window){
Shader tShader("triangleShader");
GLuint vao, vbo;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle),triangle,GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
//SDL_iPhoneSetAnimationCallback(window, 10, update, nullptr);
while(true){
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Oddly enough If I do not include these next 2 lines
// it will not even clear the color of the backscreen!
SDL_Event e;
SDL_PollEvent(&e);
tShader.bind(); // glUseProgram(my_shader_id);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
SDL_GL_SwapWindow(window);
}
}
int
main(int argc, char *argv[])
{
SDL_Window *window = nullptr;
if (SDL_Init(SDL_INIT_VIDEO) !=0) {
printf("Could not initialize SDL\n");
return 1;
}
if(SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES)!=0){
std::cout << "ERROR loading context profile mask\n";
}
if(SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3)!=0){
std::cout <<"ERROR setting context major version\n";
}
if(SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0)!=0){
std::cout <<"ERROR setting context minor version\n";
}
if(SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) != 0){
std::cout << " ERrror \n setting double buffer";
} // I have tried without the dubble buffer and nothing changes
if(SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE , 24) != 0){
std::cout << " ERrror \n setting depth buffer";
}
SDL_DisplayMode display;
SDL_GetDesktopDisplayMode(0, &display);
window = SDL_CreateWindow(NULL, 0, 0, display.w, display.h, SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN);
assert(window != nullptr);
SDL_GLContext glContext = SDL_GL_CreateContext(window);
assert(glContext != NULL);
#ifdef TRIANGLE_DEMO
triangleDrawer(window);
#else
cubeDrawer(window);
#endif
return 1;
}
Shader
Code:
#include "Shader.h"
static std::string LoadShader(const std::string &fileName);
Shader::Shader(const std::string & fileName)
{
m_program = glCreateProgram();
m_shaders[0] = CreateShader(LoadShader(fileName + ".vs"), GL_VERTEX_SHADER);
m_shaders[1] = CreateShader(LoadShader(fileName + ".fs"), GL_FRAGMENT_SHADER);
for (int i = 0; i < NUM_SHADERS; i++) {
glAttachShader(m_program, m_shaders[i]);
}
glBindAttribLocation(m_program, 0, "position");
//glBindAttribLocation(m_program, 1, "normal");
//glBindAttribLocation(m_program, 2, "textureCoord");
glLinkProgram(m_program);
CheckShaderError(m_program, GL_LINK_STATUS, true, "Error linking shader program");
glValidateProgram(m_program);
CheckShaderError(m_program, GL_LINK_STATUS, true, "Invalid shader program");
/*
uniform mat4 model;
uniform mat4 viewProjection;
*/
}
void Shader::CheckShaderError(GLuint shader, GLuint flag, bool isProgram, const std::string& errorMessage)
{
GLint success = 0;
GLchar error[1024] = { 0 };
if (isProgram)
glGetProgramiv(shader, flag, &success);
else
glGetShaderiv(shader, flag, &success);
if (success == GL_FALSE)
{
if (isProgram)
glGetProgramInfoLog(shader, sizeof(error), NULL, error);
else
glGetShaderInfoLog(shader, sizeof(error), NULL, error);
std::cerr << errorMessage << ": '" << error << "'" << std::endl;
}
}
void Shader::updateShadowMatrix(glm::mat4 &shadowM)
{
glUniform4fv(m_uniforms[SHADOW_MATRIX], 1, &shadowM[0][0]);
}
GLuint Shader::CreateShader(const std::string &fullText, GLenum typeShader) {
GLuint shader = glCreateShader(typeShader);
const GLchar * shaderSourceString[1];
GLint shaderSourceStringLength[1];
shaderSourceString[0] = fullText.c_str();
shaderSourceStringLength[0] = (GLint)fullText.length();
glShaderSource(shader, 1, shaderSourceString, shaderSourceStringLength);
glCompileShader(shader);
CheckShaderError(shader, GL_COMPILE_STATUS, false, "Error compiling shader!");
return shader;
}
static std::string LoadShader(const std::string &fileName) {
std::ifstream file;
file.open(fileName.c_str());
std::string output;
std::string line;
if (file.is_open()) {
while (file.good()) {
getline(file, line);
output.append(line + '\n');
}
}
else {
std::cerr << "ERror reading the file ";
}
return output;
}
void Shader::bind()
{
glUseProgram(m_program);
}
/*void Shader::update(Transform& trans, const Camera & cam)
{
glm::mat4 view = cam.getCameraMatrix();
glm::mat4 viewP = cam.getViewProjection();
glm::mat4 model = trans.getModelMatrix();
view = view * model;
glUniformMatrix4fv(m_uniforms[MODEL_U], 1, GL_FALSE, &model[0][0]);
glUniformMatrix4fv(m_uniforms[VIEWPROJECTION_U], 1, GL_FALSE, &viewP[0][0]);
glUniformMatrix4fv(m_uniforms[MODEL_VIEW], 1, GL_FALSE, &view[0][0]);
}
*/
Shader::~Shader()
{
for (int i = 0; i < NUM_SHADERS; i++) {
glDetachShader(m_program, m_shaders[i]);
glDeleteShader(m_shaders[i]);
}
glDeleteProgram(m_program);
}
//triangleShader.vs
Code:
#version 300 es
layout(location = 0) in highp vec3 position;
out highp vec3 colorPosition;
void main(){
colorPosition = position;
gl_Position = vec4(position,1.0f);
}
Code:
#version 300 es
in highp vec3 colorPosition;
out highp vec4 color;
void main(){
color = vec4(colorPosition,1.0f);
}
josh43
Joined: 14 Aug 2015
Posts: 4
Posted: Tue Aug 18, 2015 5:20 am
I needed to set glViewPort(.....)
Powered by
phpBB
© phpBB Group
Design by
Vjacheslav Trushkin
for
phpBBStyles.com
.
Content © SDL