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

Having issues loading shaders #46

Closed
carlosbispo opened this issue Sep 29, 2020 · 5 comments
Closed

Having issues loading shaders #46

carlosbispo opened this issue Sep 29, 2020 · 5 comments

Comments

@carlosbispo
Copy link

Hi! Great work!
Im trying to follow a simple case with OpenGL.
I´m trying to draw a triangle and paint it using shaders. However I´m doing something wrong and I don´t know what is the problem.
Here is the source code in C:

// Drawing a triangle on screen with vertex array

#include <stdlib.h>
#include <stdio.h>


#include <vitaGL.h>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>

// An array of 3 vectors which represents 3 vertices
static const float g_vertex_buffer_data[] = {
   -1.0f, -1.0f, 0.0f,
   1.0f, -1.0f, 0.0f,
   0.0f,  1.0f, 0.0f,
};

static const float colors[] = {1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0};

char *file_contents(const char *path);


char *file_contents(const char *path) {
	FILE *f = fopen(path, "r");
	// Read file size
	fseek(f, 0, SEEK_END);
	long fsize = ftell(f);
	fseek(f, 0, SEEK_SET);  /* same as rewind(f); */

	char *string = (char *)malloc(fsize + 1);
	fread(string, 1, fsize, f);
	
	fclose(f);
	
	return string;
}


GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path){

	// Create the shaders
	GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
	GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
	
	GLint Result = GL_FALSE;
	int InfoLogLength;

	// Compile Vertex Shader
	printf("Compiling shader : %s\n", vertex_file_path);
	//GLchar *vertex_code;
	char *vertex_code = file_contents(vertex_file_path);
	//vertex_code = ;
	//strcpy(vertex_code, VertexShaderCode.c_str());
	const int32_t vertexSourceSize = strlen(vertex_code);
	glShaderSource(VertexShaderID, 1, &vertex_code , NULL);
	glCompileShader(VertexShaderID);
	// Check Vertex Shader
	glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
	
	if (!Result) {
		printf("Cant compile vertex code: %s\n", vertex_code);
	}	
	
	if ( InfoLogLength > 0 ){
		std::vector<char> VertexShaderErrorMessage(InfoLogLength+1);
		glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
		printf("%s\n", &VertexShaderErrorMessage[0]);
	}

	// Compile Fragment Shader
	printf("Compiling shader : %s\n", fragment_file_path);
	//GLchar *fragment_code;
	char *fragment_code = file_contents(fragment_file_path);
	printf("Codigo fragment: %s", fragment_code);
	
	const int32_t fragmentSourceSize = strlen(fragment_code);
	glShaderSource(FragmentShaderID, 1, &fragment_code , &fragmentSourceSize);
	glCompileShader(FragmentShaderID);

	// Check Fragment Shader
	glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
	
	if (!Result) {
		printf("Cant compile fragment code: %s\n", vertex_code);
	}	
	
	glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
	if ( InfoLogLength > 0 ){
		std::vector<char> FragmentShaderErrorMessage(InfoLogLength+1);
		glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
		printf("%s\n", &FragmentShaderErrorMessage[0]);
	}

	// Link the program
	printf("Linking program\n");
	GLuint ProgramID = glCreateProgram();
	printf("Program:%d\n", ProgramID);
	glAttachShader(ProgramID, FragmentShaderID);
	glAttachShader(ProgramID, VertexShaderID);
	
	glLinkProgram(ProgramID);

	// Check the program
	/*
	glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
	glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
	if ( InfoLogLength > 0 ){
		std::vector<char> ProgramErrorMessage(InfoLogLength+1);
		glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
		printf("%s\n", &ProgramErrorMessage[0]);
	}
	*/
	
	
	//glDetachShader(ProgramID, VertexShaderID);
	//glDetachShader(ProgramID, FragmentShaderID);
	
	glDeleteShader(VertexShaderID);
	glDeleteShader(FragmentShaderID);

	return ProgramID;
}

void check_for_shader_compiler() {
    if(!vglHasRuntimeShaderCompiler()) {
        SceMsgDialogParam param;
        sceMsgDialogParamInit(&param);

        SceMsgDialogUserMessageParam user_msg;
        memset(&user_msg, 0, sizeof(SceMsgDialogUserMessageParam));
        user_msg.msg =  "You do not have the runtime shader compiler installed. It must be installed to run this program.\n\n"
                        "Please check the README.md in the repository for a link to instructions on installing it.";
        user_msg.buttonType = SCE_MSG_DIALOG_BUTTON_TYPE_OK;

        param.userMsgParam = &user_msg;
        param.mode = SCE_MSG_DIALOG_MODE_USER_MSG;
        sceMsgDialogInit(&param);

        while (sceMsgDialogGetStatus() != SCE_COMMON_DIALOG_STATUS_FINISHED) {
            vglStartRendering();
            glClear(GL_COLOR_BUFFER_BIT);
            vglStopRenderingInit();
            vglUpdateCommonDialog();
            vglStopRenderingTerm();
        }
        sceKernelExitProcess(0);
    }
}

int main(){
	
	vglEnableRuntimeShaderCompiler(GL_TRUE);
    vglUseVram(GL_TRUE);
    vglWaitVblankStart(GL_TRUE);
	
	
	// Initializing graphics device
	vglInit(0x800000);
	check_for_shader_compiler();
	
	glEnable(GL_DEPTH_TEST);
	
	glClearColor (0.0f, 1.0f, 0.0f, 1.0f);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-1, 1, -1, 1, -1, 1);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
	GLuint programID = LoadShaders( "app0:vertex.shader", "app0:fragment.shader" );
	
	
	
	for (;;){
		
		
		vglStartRendering();
		glClear(GL_COLOR_BUFFER_BIT);
		
		
		//vglEnableVertexAttribArray(0);
		glEnableClientState(GL_VERTEX_ARRAY);
		glUseProgram(programID);
		//glEnableClientState(GL_COLOR_ARRAY);
		
		glVertexPointer(3, GL_FLOAT, 0, g_vertex_buffer_data);
		//glColorPointer(3, GL_FLOAT, 0, colors);
		
		//glDrawObjects(GL_TRIANGLES, 0, 3);
		
		glDrawArrays(GL_TRIANGLES, 0, 3);
		
		glDisableClientState(GL_COLOR_ARRAY);
		
		vglStopRendering();
		glLoadIdentity();
	}
	
	vglEnd();
	
}

... And here is the shaders:
fragment.shader

float4 main(uniform float4 uClearColor) : COLOR
{
	return float4(0.0, 0.0, 1.0, 1.0);
}

vertex shader

void main(float3 aPosition,
	float4 aColor,
	uniform float4x4 wvp,
	float4 out vPosition : POSITION,
	float4 out vColor : COLOR)
{
	vPosition = float4(aPosition, 1.f);
}

This only gives a white triangle.
With Vita3K I can see they are compiling, but I don´t what is the problem!.

Thanks a lot!

@Rinnegatamante
Copy link
Owner

Vita3K has no full support for vitaGL at least last time i tried. The white triangle rendering is coming off of a real hw or the emulator?

@carlosbispo
Copy link
Author

Hello! Thanks for your reply! I have a white triangle in both vita3k and real hardware...

@Rinnegatamante
Copy link
Owner

What's the expected rendering? Vertex arrays passed are indeed for a triangle.

@carlosbispo
Copy link
Author

According to fragment shader it should appear red. But appears white... Is fragment code wrong?

@Rinnegatamante
Copy link
Owner

Don't delete the shaders after linking the program. It could produce unexpected behaviours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants